-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
109 lines (94 loc) · 3.94 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
package main
import (
"flag"
"fmt"
"net/http"
"os"
"strings"
"github.com/wobcom/transceiver-exporter/transceiver-collector"
"gitlab.com/wobcom/cumulus-exporter/asic"
"gitlab.com/wobcom/cumulus-exporter/collector"
"gitlab.com/wobcom/cumulus-exporter/hwmon"
"gitlab.com/wobcom/cumulus-exporter/mstpd"
log "github.com/sirupsen/logrus"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const version string = "1.0"
var (
showVersion = flag.Bool("version", false, "Print version and exit")
listenAddress = flag.String("web.listen-address", "[::]:9457", "Address to listen on")
metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics")
asicCollector = flag.Bool("collectors.asic", false, "Enable ASIC collector")
transceiverCollector = flag.Bool("collectors.transceiver", false, "Enable transceiver collector (rx / tx power, temperatures, etc.)")
collectInterfaceFeatures = flag.Bool("collectors.transceiver.interface-features", false, "Collect interface features (results in many time series")
excludeInterfaces = flag.String("collectors.transceiver.exclude-interfaces", "", "Comma seperated list of interfaces to exclude from scrape")
hwmonCollector = flag.Bool("collectors.hwmon", false, "Enable hwmon collector")
hwmonCollectorConfig = flag.String("collectors.hwmon.config", "hwmon.yml", "hwmon collector config file")
mstpdCollector = flag.Bool("collectors.mstpd", false, "Enable mstpd collector")
mstpctlPath = flag.String("collectors.mstpd.mstpctl-path", "/sbin/mstpctl", "mstpctl binary path")
enabledCollectors []collector.Collector
)
func printVersion() {
fmt.Println("cumulus-exporter")
fmt.Printf("Version: %s\n", version)
fmt.Println("Author(s): @fluepke")
fmt.Println("Exposes varies metrics from devices running the Cumulus Linux operating system")
}
func main() {
flag.Parse()
if *showVersion {
printVersion()
os.Exit(0)
}
startServer()
}
func initialize() {
if *asicCollector {
log.Info("asic collector enabled")
enabledCollectors = append(enabledCollectors, asic.NewCollector())
}
if *transceiverCollector {
log.Info("transceiver collector enabled")
blacklistedIfaceNames := strings.Split(*excludeInterfaces, ",")
for index, blacklistedIfaceName := range blacklistedIfaceNames {
blacklistedIfaceNames[index] = strings.Trim(blacklistedIfaceName, " ")
}
enabledCollectors = append(enabledCollectors, transceivercollector.NewCollector(blacklistedIfaceNames, *collectInterfaceFeatures, false))
}
if *hwmonCollector {
log.Info("hwmon collector enabled")
hwmonCollectorConfig, err := hwmon.LoadConfiguration(*hwmonCollectorConfig)
if err != nil {
log.Errorf("Could not load hwmon collector config file: %v. Disabling hwmon collector.", err)
} else {
enabledCollectors = append(enabledCollectors, hwmon.NewCollector(hwmonCollectorConfig))
}
}
if *mstpdCollector {
enabledCollectors = append(enabledCollectors, mstpd.NewCollector(*mstpctlPath))
}
}
func startServer() {
log.Infof("Starting cumulus-exporter (version: %s)", version)
initialize()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`<html>
<head><title>cumulus-exporter (Version ` + version + `)</title></head>
<body>
<h1>cumulus-exporter</h1>
<p><a href="` + *metricsPath + `">Metrics</a></p>
</body>
</html>`))
})
http.HandleFunc(*metricsPath, handleMetricsRequest)
log.Infof("Listening on %s", *listenAddress)
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}
func handleMetricsRequest(w http.ResponseWriter, request *http.Request) {
registry := prometheus.NewRegistry()
registry.MustRegister(newCumulusCollector())
promhttp.HandlerFor(registry, promhttp.HandlerOpts{
ErrorHandling: promhttp.ContinueOnError,
}).ServeHTTP(w, request)
}