forked from tynany/frr_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frr_exporter.go
134 lines (119 loc) · 3.92 KB
/
frr_exporter.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
package main
import (
"fmt"
"net/http"
"strconv"
"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/tynany/frr_exporter/collector"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
var (
listenAddress = kingpin.Flag("web.listen-address", "Address on which to expose metrics and web interface.").Default(":9342").String()
telemetryPath = kingpin.Flag("web.telemetry-path", "Path under which to expose metrics.").Default("/metrics").String()
frrVTYSHPath = kingpin.Flag("frr.vtysh.path", "Path of vtysh.").Default("/usr/bin/vtysh").String()
frrVTYSHTimeout = kingpin.Flag("frr.vtysh.timeout", "The timeout when running vtysh commends (default 20s).").Default("20s").String()
collectors = []*collector.Collector{}
)
func initCollectors() {
bgp := collector.NewBGPCollector()
collectors = append(collectors, &collector.Collector{
Name: bgp.Name(),
PromCollector: bgp,
Errors: bgp,
CLIHelper: bgp,
})
ospf := collector.NewOSPFCollector()
collectors = append(collectors, &collector.Collector{
Name: ospf.Name(),
PromCollector: ospf,
Errors: ospf,
CLIHelper: ospf,
})
bgp6 := collector.NewBGP6Collector()
collectors = append(collectors, &collector.Collector{
Name: bgp6.Name(),
PromCollector: bgp6,
Errors: bgp6,
CLIHelper: bgp6,
})
bgpl2vpn := collector.NewBGPL2VPNCollector()
collectors = append(collectors, &collector.Collector{
Name: bgpl2vpn.Name(),
PromCollector: bgpl2vpn,
Errors: bgpl2vpn,
CLIHelper: bgpl2vpn,
})
bfd := collector.NewBFDCollector()
collectors = append(collectors, &collector.Collector{
Name: bfd.Name(),
PromCollector: bfd,
Errors: bfd,
CLIHelper: bfd,
})
}
func handler(w http.ResponseWriter, r *http.Request) {
registry := prometheus.NewRegistry()
enabledCollectors := []*collector.Collector{}
for _, collector := range collectors {
if *collector.Enabled {
enabledCollectors = append(enabledCollectors, collector)
}
}
ne := collector.NewExporter(enabledCollectors)
ne.SetVTYSHPath(*frrVTYSHPath)
// error checking is done as part of parseCLI
frrTimeout, _ := time.ParseDuration(*frrVTYSHTimeout)
ne.SetVTYSHTimeout(frrTimeout)
registry.Register(ne)
gatheres := prometheus.Gatherers{
prometheus.DefaultGatherer,
registry,
}
handlerOpts := promhttp.HandlerOpts{
ErrorLog: log.NewErrorLogger(),
ErrorHandling: promhttp.ContinueOnError,
}
promhttp.HandlerFor(gatheres, handlerOpts).ServeHTTP(w, r)
}
func parseCLI() {
for _, collector := range collectors {
defaultState := "disabled"
enabledByDefault := collector.CLIHelper.EnabledByDefault()
if enabledByDefault == true {
defaultState = "enabled"
}
flagName := fmt.Sprintf("collector.%s", collector.CLIHelper.Name())
helpString := fmt.Sprintf("%s (default: %s).", collector.CLIHelper.Help(), defaultState)
collector.Enabled = kingpin.Flag(flagName, helpString).Default(strconv.FormatBool(enabledByDefault)).Bool()
}
log.AddFlags(kingpin.CommandLine)
kingpin.Version(version.Print("frr_exporter"))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
if _, err := time.ParseDuration(*frrVTYSHTimeout); err != nil {
log.Fatalf("invalid frr.vtysh.timeout flag %q: %s", *frrVTYSHTimeout, err)
}
}
func main() {
prometheus.MustRegister(version.NewCollector("frr_exporter"))
initCollectors()
parseCLI()
log.Infof("Starting frr_exporter %s on %s", version.Info(), *listenAddress)
http.HandleFunc(*telemetryPath, handler)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>FRR Exporter</title></head>
<body>
<h1>FRR Exporter</h1>
<p><a href="` + *telemetryPath + `">Metrics</a></p>
</body>
</html>`))
})
if err := http.ListenAndServe(*listenAddress, nil); err != nil {
log.Fatal(err)
}
}