forked from prometheus/snmp_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
145 lines (129 loc) · 3.95 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
139
140
141
142
143
144
145
package main
import (
"flag"
"fmt"
"net/http"
_ "net/http/pprof"
"os"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
"github.com/prometheus/common/version"
"github.com/prometheus/snmp_exporter/config"
)
var (
showVersion = flag.Bool("version", false, "Print version information.")
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 := config.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()
if *showVersion {
fmt.Fprintln(os.Stdout, version.Print("snmp_exporter"))
os.Exit(0)
}
log.Infoln("Starting snmp exporter", version.Info())
log.Infoln("Build context", version.BuildContext())
// Bail early if the config is bad.
c, err := config.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.
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head>
<title>SNMP Exporter</title>
<style>
label{
display:inline-block;
width:75px;
}
form label {
margin: 10px;
}
form input {
margin: 10px;
}
</style>
</head>
<body>
<h1>SNMP Exporter</h1>
<form action="/snmp">
<label>Target:</label> <input type="text" name="target" placeholder="X.X.X.X" value="1.2.3.4"><br>
<label>Module:</label> <input type="text" name="module" placeholder="module" value="default"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>`))
})
log.Infof("Listening on %s", *listenAddress)
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}