Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/integrations/snmp_exporter/snmp.go
5391 views
1
package snmp_exporter
2
3
import (
4
"fmt"
5
"net/http"
6
"time"
7
8
"github.com/go-kit/log"
9
"github.com/go-kit/log/level"
10
"github.com/prometheus/client_golang/prometheus"
11
"github.com/prometheus/client_golang/prometheus/promhttp"
12
"github.com/prometheus/snmp_exporter/collector"
13
snmp_config "github.com/prometheus/snmp_exporter/config"
14
)
15
16
type snmpHandler struct {
17
cfg *Config
18
modules *snmp_config.Config
19
log log.Logger
20
}
21
22
func (sh *snmpHandler) handler(w http.ResponseWriter, r *http.Request) {
23
logger := sh.log
24
25
query := r.URL.Query()
26
27
snmpTargets := make(map[string]SNMPTarget)
28
for _, target := range sh.cfg.SnmpTargets {
29
snmpTargets[target.Name] = target
30
}
31
32
var target string
33
targetName := query.Get("target")
34
if len(query["target"]) != 1 || targetName == "" {
35
http.Error(w, "'target' parameter must be specified once", 400)
36
return
37
}
38
39
t, ok := snmpTargets[targetName]
40
if ok {
41
target = t.Target
42
} else {
43
target = targetName
44
}
45
46
moduleName := query.Get("module")
47
if len(query["module"]) > 1 {
48
http.Error(w, "'module' parameter must only be specified once", 400)
49
return
50
}
51
if moduleName == "" {
52
moduleName = "if_mib"
53
}
54
55
module, ok := (*sh.modules)[moduleName]
56
if !ok {
57
http.Error(w, fmt.Sprintf("Unknown module '%s'", moduleName), 400)
58
return
59
}
60
61
// override module connection details with custom walk params if provided
62
walkParams := query.Get("walk_params")
63
if len(query["walk_params"]) > 1 {
64
http.Error(w, "'walk_params' parameter must only be specified once", 400)
65
return
66
}
67
68
if walkParams != "" {
69
if wp, ok := sh.cfg.WalkParams[walkParams]; ok {
70
// module.WalkParams = wp
71
if wp.Version != 0 {
72
module.WalkParams.Version = wp.Version
73
}
74
if wp.MaxRepetitions != 0 {
75
module.WalkParams.MaxRepetitions = wp.MaxRepetitions
76
}
77
if wp.Retries != 0 {
78
module.WalkParams.Retries = wp.Retries
79
}
80
if wp.Timeout != 0 {
81
module.WalkParams.Timeout = wp.Timeout
82
}
83
module.WalkParams.Auth = wp.Auth
84
} else {
85
http.Error(w, fmt.Sprintf("Unknown walk_params '%s'", walkParams), 400)
86
return
87
}
88
logger = log.With(logger, "module", moduleName, "target", target, "walk_params", walkParams)
89
} else {
90
logger = log.With(logger, "module", moduleName, "target", target)
91
}
92
level.Debug(logger).Log("msg", "Starting scrape")
93
94
start := time.Now()
95
registry := prometheus.NewRegistry()
96
c := collector.New(r.Context(), target, module, logger)
97
registry.MustRegister(c)
98
// Delegate http serving to Prometheus client library, which will call collector.Collect.
99
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
100
h.ServeHTTP(w, r)
101
duration := time.Since(start).Seconds()
102
level.Debug(logger).Log("msg", "Finished scrape", "duration_seconds", duration)
103
}
104
105
func (sh snmpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
106
sh.handler(w, r)
107
}
108
109