-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
138 lines (111 loc) · 4.25 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"fmt"
"net/http"
"time"
"github.com/patrickmn/go-cache"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/robfig/cron"
"go.uber.org/zap"
"gopkg.in/alecthomas/kingpin.v2"
"github.com/topine/ibm-spectrum-exporter/collector"
"github.com/topine/ibm-spectrum-exporter/monitoring"
"github.com/topine/ibm-spectrum-exporter/spectrumservice"
)
//BUILDTIME contains the build time
var BUILDTIME string
//TAG
var TAG string
//COMMIT
var COMMIT string
//VERSION
var VERSION string
func main() {
var (
//port allocation done in the prometheus https://github.com/prometheus/prometheus/wiki/Default-port-allocations
addr = kingpin.Flag("listen-address", "Address on which to expose metrics and web interface.").Default(":9741").String()
metricsPath = kingpin.Flag("telemetry-path", "Path under which to expose metrics.").Default("/metrics").String()
metricConfigPath = kingpin.Flag("metric-config-path", "Metric configuration file absolute path").Default("metrics_conf.yaml").String()
baseURL = kingpin.Flag("base-url", "IBM Spectrum base url").Short('t').Required().String()
cacheMetrics = kingpin.Flag("cache-metrics", "Cache metrics to avoid multiple calls").Default("true").Bool()
collectionInterval = kingpin.Flag("collection-interval", "Metrics Collection interval").Default("@every 5m").String()
user = kingpin.Flag("user", "IBM Spectrum username").Short('u').Required().String()
password = kingpin.Flag("password", "IBM Spectrum username").Short('p').Required().String()
config monitoring.MetricsConfig
spectrumClient *spectrumservice.Client
localCache *cache.Cache
)
//kingpin.Version(version.Print("ibm-spectrum-exporter"))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
logger, err := zap.NewDevelopment()
//logger, err := zap.NewProduction()
if err != nil {
panic(err)
}
if *baseURL == "" {
logger.Sugar().Panic("IBM Spectrum base url missing")
}
err = config.GetConf(*metricConfigPath)
if err != nil {
logger.Sugar().Fatal("Error parsing the metrics configuration file: %v", err)
}
buildInfos()
//starting cache
localCache = cache.New(cache.NoExpiration, cache.NoExpiration)
//set all the metrics
spectrumClient = spectrumservice.NewClient(logger.Sugar(), config, localCache, *cacheMetrics,
*user, *password, *baseURL)
//Create a cron that will start a go routine to update the metrics
c := cron.New()
if *cacheMetrics {
collectMetrics(logger, spectrumClient)
//Create a cron that will start a go routine to update the metrics
err = c.AddFunc(*collectionInterval, func() {
collectMetrics(logger, spectrumClient)
})
if err != nil {
logger.Sugar().Fatalf("Cannot schedule collection %v", err)
} else {
logger.Sugar().Infof("Scheduler started with success with interval %s \n", *collectionInterval)
}
}
c.Start()
spectrumCollector, err := collector.NewIbmSpectrumCollector(config, logger, *spectrumClient)
if err != nil {
logger.Sugar().Fatal("Error creating collector: %v", err)
}
prometheus.MustRegister(spectrumCollector)
http.Handle(*metricsPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte(`<html>
<head><title>IBM Spectrum Exporter</title></head>
<body>
<h1>IBM Spectrum Exporter</h1>
<p><a href="` + *metricsPath + `">Metrics</a></p>
</body>
</html>`))
if err != nil {
logger.Sugar().Panicf("Writing output page: %v", err)
}
})
logger.Sugar().Infof("Exporter started with Success.")
logger.Sugar().Fatal(http.ListenAndServe(*addr, nil))
}
func collectMetrics(logger *zap.Logger, spectrumClient *spectrumservice.Client) {
logger.Sugar().Info("Starting to collect the metrics.")
err := spectrumClient.CollectAndCacheMetrics(collector.Filter, collector.State)
if err != nil {
logger.Sugar().Errorf("error Collecting metrics for cache %v", err)
}
logger.Sugar().Info("Finished collecting metrics")
}
// buildInfos returns builds information
func buildInfos() {
fmt.Println("Program started at: " + time.Now().String())
fmt.Println("BUILDTIME=" + BUILDTIME)
fmt.Println("TAG=" + TAG)
fmt.Println("COMMIT=" + COMMIT)
fmt.Println("VERSION=" + VERSION)
}