forked from prometheus/snmp_exporter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
107 lines (95 loc) · 2.88 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
package main
import (
"flag"
"fmt"
"net/http"
_ "net/http/pprof"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
"github.com/prometheus/common/version"
)
var (
configFile = flag.String(
"config.file", "snmp.yml",
"Path to configuration file.",
)
listenAddress = flag.String(
"web.listen-address", ":9116",
"Address to listen on for web interface and telemetry.",
)
// Metrics about the SNMP exporter itself.
snmpDuration = prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Name: "snmp_collection_duration_seconds",
Help: "Duration of collections by the SNMP exporter",
},
[]string{"module"},
)
snmpRequestErrors = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "snmp_request_errors_total",
Help: "Errors in requests to the SNMP exporter",
},
)
)
func init() {
prometheus.MustRegister(snmpDuration)
prometheus.MustRegister(snmpRequestErrors)
prometheus.MustRegister(version.NewCollector("snmp_exporter"))
}
func handler(w http.ResponseWriter, r *http.Request) {
cfg, err := LoadFile(*configFile)
if err != nil {
msg := fmt.Sprintf("Error parsing config file: %s", err)
http.Error(w, msg, 400)
log.Errorf(msg)
return
}
target := r.URL.Query().Get("target")
if target == "" {
http.Error(w, "'target' parameter must be specified", 400)
snmpRequestErrors.Inc()
return
}
moduleName := r.URL.Query().Get("module")
if moduleName == "" {
moduleName = "default"
}
module, ok := (*cfg)[moduleName]
if !ok {
http.Error(w, fmt.Sprintf("Unkown module '%s'", moduleName), 400)
snmpRequestErrors.Inc()
return
}
log.Debugf("Scraping target '%s' with module '%s'", target, moduleName)
start := time.Now()
registry := prometheus.NewRegistry()
collector := collector{target: target, module: module}
registry.MustRegister(collector)
// Delegate http serving to Promethues client library, which will call collector.Collect.
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
duration := float64(time.Since(start).Seconds())
snmpDuration.WithLabelValues(moduleName).Observe(duration)
log.Debugf("Scrape of target '%s' with module '%s' took %f seconds", target, moduleName, duration)
}
func main() {
flag.Parse()
log.Infoln("Starting snmp exporter", version.Info())
log.Infoln("Build context", version.BuildContext())
// Bail early if the config is bad.
c, err := LoadFile(*configFile)
if err != nil {
log.Fatalf("Error parsing config file: %s", err)
}
// Initilise metrics.
for module, _ := range *c {
snmpDuration.WithLabelValues(module)
}
http.Handle("/metrics", promhttp.Handler()) // Normal metrics endpoint for SNMP exporter itself.
http.HandleFunc("/snmp", handler) // Endpoint to do SNMP scrapes.
log.Infof("Listening on %s", *listenAddress)
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}