-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
150 lines (130 loc) · 5.78 KB
/
main.go
File metadata and controls
150 lines (130 loc) · 5.78 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"flag"
"fmt"
"net/http"
"os"
"regexp"
"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.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")
includeInterfaces = flag.String("collectors.transceiver.include-interfaces", "", "Comma seperated list of interfaces to include from scrape")
excludeInterfacesRegex = flag.String("collectors.transceiver.exclude-interfaces-regex", "", "Regex Expression for interfaces to exclude from scrape")
includeInterfacesRegex = flag.String("collectors.transceiver.include-interfaces-regex", "", "Regex Expression for interfaces to include from scrape")
hwmonCollector = flag.Bool("collectors.hwmon", false, "Enable hwmon collector")
mstpdCollector = flag.Bool("collectors.mstpd", false, "Enable mstpd collector")
mstpctlPath = flag.String("collectors.mstpd.mstpctl-path", "/sbin/mstpctl", "mstpctl binary path")
logLevel = flag.String("log.level", "info", "The level the application logs at")
enabledCollectors []collector.Collector
)
func printVersion() {
fmt.Println("cumulus-exporter")
fmt.Printf("Version: %s\n", version)
fmt.Println("Author(s): @fluepke, @jwagner")
fmt.Println("Exposes varies metrics from devices running the Cumulus Linux operating system")
}
func setLogLevel() {
level, err := log.ParseLevel(*logLevel)
if err != nil {
fmt.Printf("log level %s is unknown: %v\n", level, err)
level = log.InfoLevel
}
log.SetLevel(level)
}
func main() {
flag.Parse()
setLogLevel()
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.TrimSpace(blacklistedIfaceName)
}
if len(blacklistedIfaceNames) == 1 && blacklistedIfaceNames[0] == "" {
blacklistedIfaceNames = []string{}
}
includedIfaceNames := strings.Split(*includeInterfaces, ",")
for index, includedIfaceName := range includedIfaceNames {
includedIfaceNames[index] = strings.TrimSpace(includedIfaceName)
}
if len(includedIfaceNames) == 1 && includedIfaceNames[0] == "" {
includedIfaceNames = []string{}
}
var includeIfaceRegex *regexp.Regexp
var includeErr error
if includeInterfacesRegex != nil && *includeInterfacesRegex != "" {
includeIfaceRegex, includeErr = regexp.Compile(*includeInterfacesRegex)
}
var excludeIfaceRegex *regexp.Regexp
var excludeErr error
if excludeInterfacesRegex != nil && *excludeInterfacesRegex != "" {
excludeIfaceRegex, excludeErr = regexp.Compile(*excludeInterfacesRegex)
}
if includeErr != nil {
log.Errorf("Could not compile include interface regex expression \"%s\". Disabling transceiver collector.", includeErr)
} else if excludeErr != nil {
log.Errorf("Could not compile exlude interface regex expression \"%s\". Disabling transceiver collector.", excludeErr)
} else if len(includedIfaceNames) > 0 && len(blacklistedIfaceNames) > 0 {
log.Errorf("Can't include and exclude interfaces at the same time. Disabling transceiver collector.")
} else {
enabledCollectors = append(enabledCollectors, transceivercollector.NewCollector(blacklistedIfaceNames, includedIfaceNames, excludeIfaceRegex, includeIfaceRegex, true, *collectInterfaceFeatures, false))
}
}
if *hwmonCollector {
log.Info("hwmon collector enabled")
enabledCollectors = append(enabledCollectors, hwmon.NewCollector())
}
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)
}