forked from zhyon404/prom2influx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
100 lines (90 loc) · 2.78 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"context"
"fmt"
"log"
"net/url"
"os"
"path/filepath"
"time"
"github.com/influxdata/influxdb1-client"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/api"
"github.com/prometheus/client_golang/api/prometheus/v1"
"gopkg.in/alecthomas/kingpin.v2"
"github.com/zhyon404/prom2influx/transfer"
)
type config struct {
influxdbURL string
prometheusURL string
monitorLabel string
influxdbDatabase string
start string
end string
step time.Duration
c int
retry int
}
func parseFlags() *config {
a := kingpin.New(filepath.Base(os.Args[0]), "Remote storage adapter")
a.HelpFlag.Short('h')
cfg := &config{}
a.Flag("influxdb-url", "The URL of the remote InfluxDB server to send samples to. None, if empty.").
Default("").StringVar(&cfg.influxdbURL)
a.Flag("prometheus-url", "The URL of the remote prometheus server to read samples to. None, if empty.").
Default("").StringVar(&cfg.prometheusURL)
a.Flag("monitor-label", "Prometheus Attach these labels to any time series or alerts when communicating with external systems. codelab-monitor, if empty.").
Default("codelab-monitor").StringVar(&cfg.monitorLabel)
a.Flag("influxdb.database", "The name of the database to use for storing samples in InfluxDB.").
Default("prometheus").StringVar(&cfg.influxdbDatabase)
a.Flag("start", "The time start.").
Default("").StringVar(&cfg.start)
a.Flag("end", "The time end").
Default("").StringVar(&cfg.end)
a.Flag("step", "The step").
Default("1m").DurationVar(&cfg.step)
a.Flag("c", "The connections").
Default("1").IntVar(&cfg.c)
a.Flag("retry", "The retry").
Default("3").IntVar(&cfg.retry)
_, err := a.Parse(os.Args[1:])
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, errors.Wrapf(err, "Error parsing commandline arguments"))
a.Usage(os.Args[1:])
os.Exit(2)
}
return cfg
}
func main() {
cfg := parseFlags()
host, err := url.Parse(cfg.influxdbURL)
if err != nil {
log.Fatal(err)
}
start, err := time.Parse(time.RFC3339, cfg.start)
if err != nil {
log.Println(err)
}
end, err := time.Parse(time.RFC3339, cfg.end)
if err != nil {
log.Println(err)
}
// NOTE: this assumes you've setup a user and have setup shell env variables,
// namely INFLUX_USER/INFLUX_PWD. If not just omit Username/Password below.
conf := client.Config{
URL: *host,
Username: os.Getenv("INFLUX_USER"),
Password: os.Getenv("INFLUX_PWD"),
}
con, err := client.NewClient(conf)
if err != nil {
log.Fatal(err)
}
log.Println("Connection", con)
c, _ := api.NewClient(api.Config{
Address: cfg.prometheusURL,
})
api := v1.NewAPI(c)
t := transfer.NewTrans(cfg.influxdbDatabase, start, end, cfg.step, api, con, cfg.c, cfg.retry, cfg.monitorLabel)
log.Fatalln(t.Run(context.Background()))
}