aboutsummaryrefslogtreecommitdiff
path: root/cmd/six_monitor/main.go
blob: cd41d8f84a66a5f05d9c1e73c56d556b0fed2e2f (plain)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// SPDX-License-Identifier: GPL-2.0-only
// Copyright (C) 2020 Michael Crute <mike@crute.us>. All rights reserved.
//
// Use of this source code is governed by a license that can be found in the
// LICENSE file.

package main

import (
	"flag"
	"fmt"
	"log"
	"net/http"
	_ "net/http/pprof"
	"os"
	"strconv"

	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promhttp"
	"github.com/prometheus/common/version"

	"code.crute.me/pomonaconsulting/six_monitoring/six"
)

const (
	namespace = "sixstatus"
	exporter  = "six_status_exporter"
)

var (
	up = prometheus.NewDesc(
		prometheus.BuildFQName(namespace, "", "up"),
		"Was the SIX query successful?",
		nil, nil,
	)
	rsPrefixCount = prometheus.NewDesc(
		prometheus.BuildFQName(namespace, "route_server", "prefix_count"),
		"Number of prefixes advertised to a route server.",
		[]string{"ipversion", "mtu", "server"}, nil,
	)
	rsErrorCount = prometheus.NewDesc(
		prometheus.BuildFQName(namespace, "route_server", "error_count"),
		"Number of errors reported by a route server.",
		[]string{"ipversion", "mtu", "server"}, nil,
	)
	rsTransitErrorCount = prometheus.NewDesc(
		prometheus.BuildFQName(namespace, "route_server", "transit_error_count"),
		"Number of transit errors reported by a route server.",
		[]string{"ipversion", "mtu", "server"}, nil,
	)
	irrPrefixCount = prometheus.NewDesc(
		prometheus.BuildFQName(namespace, "irr", "prefix_count"),
		"Number of prefixes resolved from IRR records.",
		[]string{"ipversion"}, nil,
	)
	irrASNCount = prometheus.NewDesc(
		prometheus.BuildFQName(namespace, "irr", "asn_count"),
		"Number of ASNs resolved from IRR records.",
		[]string{"ipversion"}, nil,
	)
	irrAsSetPrefixCount = prometheus.NewDesc(
		prometheus.BuildFQName(namespace, "irr", "as_set_prefix_count"),
		"Number of prefixes in the AS-SET resolved from IRR records.",
		[]string{"ipversion"}, nil,
	)
	pdbPrefixCount = prometheus.NewDesc(
		prometheus.BuildFQName(namespace, "peeringdb", "prefix_count"),
		"Number of prefixes configured in PeeringDB.",
		[]string{"ipversion"}, nil,
	)
	roaCount = prometheus.NewDesc(
		prometheus.BuildFQName(namespace, "", "roa_count"),
		"Number of resolved ROA records.",
		nil, nil,
	)
)

type SIXCollector struct {
	ASN int
}

var _ prometheus.Collector = (*SIXCollector)(nil)

func (c *SIXCollector) Describe(ch chan<- *prometheus.Desc) {
	ch <- up
	ch <- rsPrefixCount
	ch <- rsErrorCount
	ch <- rsTransitErrorCount
	ch <- irrPrefixCount
	ch <- irrASNCount
	ch <- irrAsSetPrefixCount
	ch <- pdbPrefixCount
	ch <- roaCount
}

func routeServerMetrics(rs *six.RouteServer, ch chan<- prometheus.Metric) {
	ch <- prometheus.MustNewConstMetric(
		rsPrefixCount, prometheus.GaugeValue, float64(rs.IPv4.Prefixes),
		"4", "1500", string(strconv.Itoa(rs.Number)),
	)
	ch <- prometheus.MustNewConstMetric(
		rsErrorCount, prometheus.GaugeValue, float64(rs.IPv4.Errors),
		"4", "1500", string(strconv.Itoa(rs.Number)),
	)
	ch <- prometheus.MustNewConstMetric(
		rsTransitErrorCount, prometheus.GaugeValue, float64(rs.IPv4.TransitErrors),
		"4", "1500", string(strconv.Itoa(rs.Number)),
	)
	ch <- prometheus.MustNewConstMetric(
		rsPrefixCount, prometheus.GaugeValue, float64(rs.IPv6.Prefixes),
		"6", "1500", string(strconv.Itoa(rs.Number)),
	)
	ch <- prometheus.MustNewConstMetric(
		rsErrorCount, prometheus.GaugeValue, float64(rs.IPv6.Errors),
		"6", "1500", string(strconv.Itoa(rs.Number)),
	)
	ch <- prometheus.MustNewConstMetric(
		rsTransitErrorCount, prometheus.GaugeValue, float64(rs.IPv6.TransitErrors),
		"6", "1500", string(strconv.Itoa(rs.Number)),
	)
	ch <- prometheus.MustNewConstMetric(
		rsPrefixCount, prometheus.GaugeValue, float64(rs.IPv4Jumbo.Prefixes),
		"4", "9000", string(strconv.Itoa(rs.Number)),
	)
	ch <- prometheus.MustNewConstMetric(
		rsErrorCount, prometheus.GaugeValue, float64(rs.IPv4Jumbo.Errors),
		"4", "9000", string(strconv.Itoa(rs.Number)),
	)
	ch <- prometheus.MustNewConstMetric(
		rsTransitErrorCount, prometheus.GaugeValue, float64(rs.IPv4Jumbo.TransitErrors),
		"4", "9000", string(strconv.Itoa(rs.Number)),
	)
	ch <- prometheus.MustNewConstMetric(
		rsPrefixCount, prometheus.GaugeValue, float64(rs.IPv6Jumbo.Prefixes),
		"6", "9000", string(strconv.Itoa(rs.Number)),
	)
	ch <- prometheus.MustNewConstMetric(
		rsErrorCount, prometheus.GaugeValue, float64(rs.IPv6Jumbo.Errors),
		"6", "9000", string(strconv.Itoa(rs.Number)),
	)
	ch <- prometheus.MustNewConstMetric(
		rsTransitErrorCount, prometheus.GaugeValue, float64(rs.IPv6Jumbo.TransitErrors),
		"6", "9000", string(strconv.Itoa(rs.Number)),
	)
}

func (c *SIXCollector) Collect(ch chan<- prometheus.Metric) {
	sp, err := six.FetchParseSIXCSV()
	if err != nil {
		log.Printf("error: Fetching and parsing CSV: %s", err)
		ch <- prometheus.MustNewConstMetric(up, prometheus.GaugeValue, 0)
		return
	}

	ps, ok := sp.GetParticipantByASN(c.ASN)
	if !ok {
		log.Printf("error: No participant for ASN: %d", c.ASN)
		ch <- prometheus.MustNewConstMetric(up, prometheus.GaugeValue, 0)
		return
	}

	p := ps[0]

	ch <- prometheus.MustNewConstMetric(up, prometheus.GaugeValue, 1)

	// ipversion, mtu, server

	if p.RouteServer2 != nil {
		routeServerMetrics(p.RouteServer2, ch)
	}

	if p.RouteServer3 != nil {
		routeServerMetrics(p.RouteServer3, ch)
	}

	ch <- prometheus.MustNewConstMetric(
		irrPrefixCount, prometheus.GaugeValue, float64(p.IRRv4.PrefixCount),
		"4",
	)
	ch <- prometheus.MustNewConstMetric(
		irrASNCount, prometheus.GaugeValue, float64(p.IRRv4.ASNCount),
		"4",
	)
	ch <- prometheus.MustNewConstMetric(
		irrAsSetPrefixCount, prometheus.GaugeValue, float64(p.IRRv4.ASSetCount),
		"4",
	)
	ch <- prometheus.MustNewConstMetric(
		pdbPrefixCount, prometheus.GaugeValue, float64(p.PeeringDBPrefixCountv4),
		"4",
	)
	ch <- prometheus.MustNewConstMetric(
		irrPrefixCount, prometheus.GaugeValue, float64(p.IRRv6.PrefixCount),
		"6",
	)
	ch <- prometheus.MustNewConstMetric(
		irrASNCount, prometheus.GaugeValue, float64(p.IRRv6.ASNCount),
		"6",
	)
	ch <- prometheus.MustNewConstMetric(
		irrAsSetPrefixCount, prometheus.GaugeValue, float64(p.IRRv6.ASSetCount),
		"6",
	)
	ch <- prometheus.MustNewConstMetric(
		pdbPrefixCount, prometheus.GaugeValue, float64(p.PeeringDBPrefixCountv6),
		"6",
	)

	ch <- prometheus.MustNewConstMetric(roaCount, prometheus.GaugeValue, float64(p.ROACount))
}

func main() {
	var (
		showVersion   = flag.Bool("version", false, "Print version information.")
		listenAddress = flag.String("web.listen-address", ":9119", "Address to listen on for web interface and telemetry.")
	)
	flag.Parse()

	if len(os.Args) != 2 {
		fmt.Fprintf(os.Stdout, "usage: %s <ASN>\n", os.Args[0])
		os.Exit(1)
	}

	asn, err := strconv.Atoi(os.Args[1])
	if err != nil {
		fmt.Fprintln(os.Stdout, "invalid ASN, must be numeric")
		fmt.Fprintf(os.Stdout, "usage: %s <ASN>\n", os.Args[0])
		os.Exit(1)
	}

	if *showVersion {
		fmt.Fprintln(os.Stdout, version.Print(exporter))
		os.Exit(0)
	}
	log.Println("Starting", exporter, version.Info())
	log.Println("Build context", version.BuildContext())

	prometheus.MustRegister(version.NewCollector(exporter), &SIXCollector{asn})

	log.Println("Starting Server: ", *listenAddress)
	http.Handle("/metrics", promhttp.Handler())
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte(`<html>
             <head><title>SIX Status Exporter</title></head><body>
             <h1>SIX Status Exporter</h1>
             <p><a href="/metrics">Metrics</a></p>
             </body></html>`))
	})
	log.Fatal(http.ListenAndServe(*listenAddress, nil))
}