Debian Codegraph @

main.go

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 package main
2
3 import (
4 "flag"
5 "fmt"
6 "net/http"
7 "os"
8
9 "github.com/czerwonk/bird_exporter/protocol"
10 "github.com/prometheus/client_golang/prometheus"
11 "github.com/prometheus/client_golang/prometheus/promhttp"
12 log "github.com/sirupsen/logrus"
13 )
14
15 const version string = "1.4.5"
16
17 var (
18 showVersion = flag.Bool("version", false, "Print version information.")
19 listenAddress = flag.String("web.listen-address", ":9324", "Address on which to expose metrics and web interface.")
20 metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
•21 birdSocket = flag.String("bird.socket", "/run/bird/bird.ctl", "Socket to communicate with bird routing daemon")
22 birdV2 = flag.Bool("bird.v2", false, "Bird major version >= 2.0 (multi channel protocols)")
23 tlsEnabled = flag.Bool("tls.enabled", false, "Enables TLS")
24 tlsCertChainPath = flag.String("tls.cert-file", "", "Path to TLS cert file")
25 tlsKeyPath = flag.String("tls.key-file", "", "Path to TLS key file")
26 newFormat = flag.Bool("format.new", true, "New metric format (more convenient / generic)")
27 enableBGP = flag.Bool("proto.bgp", true, "Enables metrics for protocol BGP")
28 enableOSPF = flag.Bool("proto.ospf", true, "Enables metrics for protocol OSPF")
29 enableKernel = flag.Bool("proto.kernel", true, "Enables metrics for protocol Kernel")
30 enableStatic = flag.Bool("proto.static", true, "Enables metrics for protocol Static")
31 enableDirect = flag.Bool("proto.direct", true, "Enables metrics for protocol Direct")
32 enableBabel = flag.Bool("proto.babel", true, "Enables metrics for protocol Babel")
33 enableRPKI = flag.Bool("proto.rpki", true, "Enables metrics for protocol RPKI")
34 enableBFD = flag.Bool("proto.bfd", true, "Enables metrics for protocol BFD")
35 // pre bird 2.0
•36 bird6Socket = flag.String("bird.socket6", "/run/bird/bird6.ctl", "Socket to communicate with bird6 routing daemon (not compatible with -bird.v2)")
37 birdEnabled = flag.Bool("bird.ipv4", true, "Get protocols from bird (not compatible with -bird.v2)")
38 bird6Enabled = flag.Bool("bird.ipv6", true, "Get protocols from bird6 (not compatible with -bird.v2)")
39 descriptionLabels = flag.Bool("format.description-labels", false, "Add labels from protocol descriptions.")
40 descriptionLabelsRegex = flag.String("format.description-labels-regex", "(\\w+)=(\\w+)", "Regex to extract labels from protocol description")
41 )
42
43 func init() {
44 flag.Usage = func() {
45 fmt.Println("Usage: bird_exporter [ ... ]\n\nParameters:")
46 fmt.Println()
47 flag.PrintDefaults()
48 }
49 }
50
51 func main() {
52 flag.Parse()
53
54 if *showVersion {
55 printVersion()
56 os.Exit(0)
57 }
58
59 startServer()
60 }
61
62 func printVersion() {
63 fmt.Println("bird_exporter")
64 fmt.Printf("Version: %s\n", version)
65 fmt.Println("Author(s): Daniel Czerwonk")
66 fmt.Println("Metric exporter for bird routing daemon")
67 }
68
69 func startServer() {
70 log.Infof("Starting bird exporter (Version: %s)", version)
71
72 if !*newFormat {
73 log.Info("INFO: You are using the old metric format. Please consider using the new (more convenient one) by setting -format.new=true.")
74 }
75
76 http.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
77 w.Write([]byte(`<html>
78 <head><title>Bird Routing Daemon Exporter (Version ` + version + `)</title></head>
79 <body>
80 <h1>Bird Routing Daemon Exporter</h1>
81 <p><a href="` + *metricsPath + `">Metrics</a></p>
82 <h2>More information:</h2>
83 <p><a href="https://github.com/czerwonk/bird_exporter">github.com/czerwonk/bird_exporter</a></p>
84 </body>
85 </html>`))
86 })
87 http.HandleFunc(*metricsPath, handleMetricsRequest)
88
89 log.Infof("Listening for %s on %s (TLS: %v)", *metricsPath, *listenAddress, *tlsEnabled)
90 if *tlsEnabled {
91 log.Fatal(http.ListenAndServeTLS(*listenAddress, *tlsCertChainPath, *tlsKeyPath, nil))
92 return
93 }
94
95 log.Fatal(http.ListenAndServe(*listenAddress, nil))
96 }
97
98 func handleMetricsRequest(w http.ResponseWriter, r *http.Request) {
99 reg := prometheus.NewRegistry()
100 p := enabledProtocols()
101 c := NewMetricCollector(*newFormat, p, *descriptionLabels, *birdSocket)
102 reg.MustRegister(c)
103
104 l := log.New()
105 l.Level = log.ErrorLevel
106 promhttp.HandlerFor(reg, promhttp.HandlerOpts{
107 ErrorLog: l,
108 ErrorHandling: promhttp.ContinueOnError,
109 }).ServeHTTP(w, r)
110 }
111
112 func enabledProtocols() protocol.Proto {
113 res := protocol.Proto(0)
114
115 if *enableBGP {
116 res |= protocol.BGP
117 }
118 if *enableOSPF {
119 res |= protocol.OSPF
120 }
121 if *enableKernel {
122 res |= protocol.Kernel
123 }
124 if *enableStatic {
125 res |= protocol.Static
126 }
127 if *enableDirect {
128 res |= protocol.Direct
129 }
130 if *enableBabel {
131 res |= protocol.Babel
132 }
133 if *enableRPKI {
134 res |= protocol.RPKI
135 }
136 if *enableBFD {
137 res |= protocol.BFD
138 }
139
140 return res
141 }