-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
71 lines (62 loc) · 1.86 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
package main
import (
"net/http"
"strconv"
"time"
"github.com/alecthomas/kingpin/v2"
"github.com/orange-cloudfoundry/nsxt_exporter/api"
"github.com/orange-cloudfoundry/nsxt_exporter/config"
"github.com/orange-cloudfoundry/nsxt_exporter/metrics"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/version"
"github.com/sirupsen/logrus"
"github.com/vmware/vsphere-automation-sdk-go/runtime/log"
)
var (
configFile = kingpin.Flag("config", "Configuration file path").Default("config.yml").File()
)
func main() {
kingpin.Version(version.Print("nsxt-exporter"))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
object := config.NewConfig(*configFile)
namespace := "nsxt"
if object.Exporter.Namespace != "" {
namespace = object.Exporter.Namespace
}
manager, err := api.NewNSXApi(object.Nsxt)
if err != nil {
logrus.Fatal(err)
}
lvl, err := logrus.ParseLevel(object.Log.Level)
if err != nil {
logrus.Warnf("invalid log.level value '%s'", object.Log.Level)
lvl = logrus.InfoLevel
}
logrus.SetLevel(lvl)
log.SetLogger(logrus.StandardLogger())
recorder := metrics.NewRecorder(manager, namespace)
go func() {
for {
for {
err := recorder.RecordMetrics()
if err != nil {
logrus.WithError(err).Error("could not fetch metrics data")
break
}
logrus.Debugf("sleeping %.0fs...", object.Exporter.IntervalDuration.Seconds())
time.Sleep(object.Exporter.IntervalDuration)
}
logrus.Debugf("sleeping %.0fs after error...", object.Exporter.ErrorIntervalDuration.Seconds())
time.Sleep(object.Exporter.ErrorIntervalDuration)
}
}()
http.Handle(object.Exporter.Path, promhttp.Handler())
listen := ":" + strconv.Itoa(object.Exporter.Port)
logrus.Infof("listening on %s", listen)
// nolint: gosec
err = http.ListenAndServe(listen, nil)
if err != nil {
logrus.Fatal("Error when serving: " + err.Error())
}
}