-
Notifications
You must be signed in to change notification settings - Fork 485
/
snmp.go
107 lines (93 loc) · 2.77 KB
/
snmp.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 snmp_exporter
import (
"fmt"
"net/http"
"time"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/snmp_exporter/collector"
snmp_config "github.com/prometheus/snmp_exporter/config"
)
type snmpHandler struct {
cfg *Config
modules *snmp_config.Config
log log.Logger
}
func (sh *snmpHandler) handler(w http.ResponseWriter, r *http.Request) {
logger := sh.log
query := r.URL.Query()
snmpTargets := make(map[string]SNMPTarget)
for _, target := range sh.cfg.SnmpTargets {
snmpTargets[target.Name] = target
}
var target string
targetName := query.Get("target")
if len(query["target"]) != 1 || targetName == "" {
http.Error(w, "'target' parameter must be specified once", 400)
return
}
t, ok := snmpTargets[targetName]
if ok {
target = t.Target
} else {
target = targetName
}
moduleName := query.Get("module")
if len(query["module"]) > 1 {
http.Error(w, "'module' parameter must only be specified once", 400)
return
}
if moduleName == "" {
moduleName = "if_mib"
}
module, ok := (*sh.modules)[moduleName]
if !ok {
http.Error(w, fmt.Sprintf("Unknown module '%s'", moduleName), 400)
return
}
// override module connection details with custom walk params if provided
walkParams := query.Get("walk_params")
if len(query["walk_params"]) > 1 {
http.Error(w, "'walk_params' parameter must only be specified once", 400)
return
}
if walkParams != "" {
if wp, ok := sh.cfg.WalkParams[walkParams]; ok {
// module.WalkParams = wp
if wp.Version != 0 {
module.WalkParams.Version = wp.Version
}
if wp.MaxRepetitions != 0 {
module.WalkParams.MaxRepetitions = wp.MaxRepetitions
}
if wp.Retries != 0 {
module.WalkParams.Retries = wp.Retries
}
if wp.Timeout != 0 {
module.WalkParams.Timeout = wp.Timeout
}
module.WalkParams.Auth = wp.Auth
} else {
http.Error(w, fmt.Sprintf("Unknown walk_params '%s'", walkParams), 400)
return
}
logger = log.With(logger, "module", moduleName, "target", target, "walk_params", walkParams)
} else {
logger = log.With(logger, "module", moduleName, "target", target)
}
level.Debug(logger).Log("msg", "Starting scrape")
start := time.Now()
registry := prometheus.NewRegistry()
c := collector.New(r.Context(), target, module, logger)
registry.MustRegister(c)
// Delegate http serving to Prometheus client library, which will call collector.Collect.
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
duration := time.Since(start).Seconds()
level.Debug(logger).Log("msg", "Finished scrape", "duration_seconds", duration)
}
func (sh snmpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
sh.handler(w, r)
}