|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + |
| 6 | + "github.com/tencentyun/tencentcloud-exporter/collector" |
| 7 | + "github.com/tencentyun/tencentcloud-exporter/config" |
| 8 | + "github.com/tencentyun/tencentcloud-exporter/instances" |
| 9 | + "github.com/tencentyun/tencentcloud-exporter/monitor" |
| 10 | + |
| 11 | + "github.com/prometheus/client_golang/prometheus" |
| 12 | + "github.com/prometheus/client_golang/prometheus/promhttp" |
| 13 | + "github.com/prometheus/common/log" |
| 14 | + "github.com/prometheus/common/version" |
| 15 | + kingpin "gopkg.in/alecthomas/kingpin.v2" |
| 16 | +) |
| 17 | + |
| 18 | +func main() { |
| 19 | + |
| 20 | + var ( |
| 21 | + listenAddress = kingpin.Flag("web.listen-address", "Address on which to expose metrics and web interface."). |
| 22 | + Default("localhost:8001").String() |
| 23 | + |
| 24 | + metricsPath = kingpin.Flag("web.telemetry-path", "Path under which to expose metrics."). |
| 25 | + Default("/metrics").String() |
| 26 | + |
| 27 | + configFile = kingpin.Flag("config.file", "Tencent qcloud exporter configuration file."). |
| 28 | + Default("qcloud.yml").String() |
| 29 | + ) |
| 30 | + log.AddFlags(kingpin.CommandLine) |
| 31 | + kingpin.Version(version.Print("tencent_qcloud_exporter")) |
| 32 | + kingpin.HelpFlag.Short('h') |
| 33 | + kingpin.Parse() |
| 34 | + |
| 35 | + log.Infoln("Starting tencent_qcloud_exporter", version.Info()) |
| 36 | + log.Infoln("Build context", version.BuildContext()) |
| 37 | + |
| 38 | + tencentConfig := config.NewConfig() |
| 39 | + if err := tencentConfig.LoadFile(*configFile); err != nil { |
| 40 | + log.Fatal(err.Error()) |
| 41 | + } |
| 42 | + |
| 43 | + credentialConfig := (*tencentConfig).Credential |
| 44 | + metricsConfig := (*tencentConfig).Metrics |
| 45 | + |
| 46 | + if err := monitor.InitClient(credentialConfig, (*tencentConfig).RateLimit); err != nil { |
| 47 | + log.Fatal(err.Error()) |
| 48 | + } |
| 49 | + |
| 50 | + if err := instances.InitClient(credentialConfig); err != nil { |
| 51 | + log.Fatal(err.Error()) |
| 52 | + } |
| 53 | + |
| 54 | + tencentCollector, err := collector.NewCollector(metricsConfig) |
| 55 | + if err != nil { |
| 56 | + log.Fatal(err.Error()) |
| 57 | + } |
| 58 | + |
| 59 | + registry := prometheus.NewRegistry() |
| 60 | + registry.MustRegister(tencentCollector) |
| 61 | + |
| 62 | + http.Handle(*metricsPath, promhttp.HandlerFor(registry, promhttp.HandlerOpts{})) |
| 63 | + |
| 64 | + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 65 | + w.Write([]byte(`<html> |
| 66 | + <head><title>TencentCloud Exporter</title></head> |
| 67 | + <body> |
| 68 | + <h1>TencentCloud Exporter</h1> |
| 69 | + <p><a href='` + *metricsPath + `'>Metrics</a></p> |
| 70 | + </body> |
| 71 | + </html>`)) |
| 72 | + }) |
| 73 | + |
| 74 | + log.Fatal(http.ListenAndServe(*listenAddress, nil)) |
| 75 | +} |
0 commit comments