/
main.go
229 lines (202 loc) · 8.81 KB
/
main.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
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
package main
import (
"flag"
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
"github.com/mischief/gopf"
)
var (
Namespace = "pf"
)
type PfExporter struct {
fw pf.Pf
metrics map[string]*prometheus.Desc
}
// Describe implements the prometheus.Collector interface.
func (e *PfExporter) Describe(ch chan<- *prometheus.Desc) {
for _, m := range e.metrics {
ch <- m
}
}
// Collect implements the prometheus.Collector interface.
func (e *PfExporter) Collect(ch chan<- prometheus.Metric) {
stats, err := e.fw.Stats()
if err != nil {
log.Errorf("failed to get pf stats: %v", err)
return
}
ch <- prometheus.MustNewConstMetric(e.metrics["state_total"], prometheus.GaugeValue, float64(stats.StateCount()))
ch <- prometheus.MustNewConstMetric(e.metrics["state_searches"], prometheus.CounterValue, float64(stats.StateSearches()))
ch <- prometheus.MustNewConstMetric(e.metrics["state_inserts"], prometheus.CounterValue, float64(stats.StateInserts()))
ch <- prometheus.MustNewConstMetric(e.metrics["state_removals"], prometheus.CounterValue, float64(stats.StateRemovals()))
ifstats := stats.IfStats()
if ifstats != nil {
ch <- prometheus.MustNewConstMetric(e.metrics["ipv4_bytes_in"], prometheus.CounterValue, float64(ifstats.IPv4.BytesIn))
ch <- prometheus.MustNewConstMetric(e.metrics["ipv4_bytes_out"], prometheus.CounterValue, float64(ifstats.IPv4.BytesOut))
ch <- prometheus.MustNewConstMetric(e.metrics["ipv4_packets_in_passed"], prometheus.CounterValue, float64(ifstats.IPv4.PacketsInPassed))
ch <- prometheus.MustNewConstMetric(e.metrics["ipv4_packets_in_blocked"], prometheus.CounterValue, float64(ifstats.IPv4.PacketsInBlocked))
ch <- prometheus.MustNewConstMetric(e.metrics["ipv4_packets_out_passed"], prometheus.CounterValue, float64(ifstats.IPv4.PacketsOutPassed))
ch <- prometheus.MustNewConstMetric(e.metrics["ipv4_packets_out_blocked"], prometheus.CounterValue, float64(ifstats.IPv4.PacketsOutBlocked))
ch <- prometheus.MustNewConstMetric(e.metrics["ipv6_bytes_in"], prometheus.CounterValue, float64(ifstats.IPv6.BytesIn))
ch <- prometheus.MustNewConstMetric(e.metrics["ipv6_bytes_out"], prometheus.CounterValue, float64(ifstats.IPv6.BytesOut))
ch <- prometheus.MustNewConstMetric(e.metrics["ipv6_packets_in_passed"], prometheus.CounterValue, float64(ifstats.IPv6.PacketsInPassed))
ch <- prometheus.MustNewConstMetric(e.metrics["ipv6_packets_in_blocked"], prometheus.CounterValue, float64(ifstats.IPv6.PacketsInBlocked))
ch <- prometheus.MustNewConstMetric(e.metrics["ipv6_packets_out_passed"], prometheus.CounterValue, float64(ifstats.IPv6.PacketsOutPassed))
ch <- prometheus.MustNewConstMetric(e.metrics["ipv6_packets_out_blocked"], prometheus.CounterValue, float64(ifstats.IPv6.PacketsOutBlocked))
}
queues, err := e.fw.Queues()
if err != nil {
log.Errorf("failed to get queue stats: %v", err)
return
}
for _, queue := range queues {
ch <- prometheus.MustNewConstMetric(e.metrics["queue_xmit_packets"], prometheus.CounterValue, float64(queue.Stats.TransmitPackets), queue.Name, queue.IfName)
ch <- prometheus.MustNewConstMetric(e.metrics["queue_xmit_bytes"], prometheus.CounterValue, float64(queue.Stats.TransmitBytes), queue.Name, queue.IfName)
ch <- prometheus.MustNewConstMetric(e.metrics["queue_dropped_packets"], prometheus.CounterValue, float64(queue.Stats.DroppedPackets), queue.Name, queue.IfName)
ch <- prometheus.MustNewConstMetric(e.metrics["queue_dropped_bytes"], prometheus.CounterValue, float64(queue.Stats.DroppedBytes), queue.Name, queue.IfName)
}
}
func NewPfExporter() (*PfExporter, error) {
var fw pf.Pf
var err error
if *fdno != -1 {
fw = pf.OpenFD(uintptr(*fdno))
} else {
fw, err = pf.Open()
if err != nil {
return nil, err
}
}
exp := &PfExporter{
fw: fw,
metrics: map[string]*prometheus.Desc{
"state_total": prometheus.NewDesc(
prometheus.BuildFQName(Namespace, "state", "total"),
"Number of pf states.",
nil,
nil),
"state_searches": prometheus.NewDesc(
prometheus.BuildFQName(Namespace, "state", "searches_total"),
"Number of pf state searches.",
nil, nil),
"state_inserts": prometheus.NewDesc(
prometheus.BuildFQName(Namespace, "state", "inserts_total"),
"Number of pf state inserts.",
nil,
nil),
"state_removals": prometheus.NewDesc(
prometheus.BuildFQName(Namespace, "state", "removals_total"),
"Number of pf state removals.",
nil,
nil),
"ipv4_bytes_in": prometheus.NewDesc(
prometheus.BuildFQName(Namespace, "ipv4", "bytes_in_total"),
"Number of bytes in on the pf loginterface over IPv4.",
nil,
nil),
"ipv4_bytes_out": prometheus.NewDesc(
prometheus.BuildFQName(Namespace, "ipv4", "bytes_out_total"),
"Number of bytes out on the pf loginterface over IPv4.",
nil,
nil),
"ipv4_packets_in_passed": prometheus.NewDesc(
prometheus.BuildFQName(Namespace, "ipv4", "packets_in_passed_total"),
"Number of packets passed in on the pf loginterface over IPv4.",
nil,
nil),
"ipv4_packets_in_blocked": prometheus.NewDesc(
prometheus.BuildFQName(Namespace, "ipv4", "packets_in_blocked_total"),
"Number of packets blocked in on the pf loginterface over IPv4.",
nil,
nil),
"ipv4_packets_out_passed": prometheus.NewDesc(
prometheus.BuildFQName(Namespace, "ipv4", "packets_out_passed_total"),
"Number of packets passed out on the pf loginterface over IPv4.",
nil,
nil),
"ipv4_packets_out_blocked": prometheus.NewDesc(
prometheus.BuildFQName(Namespace, "ipv4", "packets_out_blocked_total"),
"Number of packets blocked out on the pf loginterface over IPv4.",
nil,
nil),
"ipv6_bytes_in": prometheus.NewDesc(
prometheus.BuildFQName(Namespace, "ipv6", "bytes_in_total"),
"Number of bytes in on the pf loginterface over IPv6.",
nil,
nil),
"ipv6_bytes_out": prometheus.NewDesc(
prometheus.BuildFQName(Namespace, "ipv6", "bytes_out_total"),
"Number of bytes out on the pf loginterface over IPv6.",
nil,
nil),
"ipv6_packets_in_passed": prometheus.NewDesc(
prometheus.BuildFQName(Namespace, "ipv6", "packets_in_passed_total"),
"Number of packets passed in on the pf loginterface over IPv6.",
nil,
nil),
"ipv6_packets_in_blocked": prometheus.NewDesc(
prometheus.BuildFQName(Namespace, "ipv6", "packets_in_blocked_total"),
"Number of packets blocked in on the pf loginterface over IPv6.",
nil,
nil),
"ipv6_packets_out_passed": prometheus.NewDesc(
prometheus.BuildFQName(Namespace, "ipv6", "packets_out_passed_total"),
"Number of packets passed out on the pf loginterface over IPv6.",
nil,
nil),
"ipv6_packets_out_blocked": prometheus.NewDesc(
prometheus.BuildFQName(Namespace, "ipv6", "packets_out_blocked_total"),
"Number of packets blocked out on the pf loginterface over IPv6.",
nil,
nil),
"queue_xmit_packets": prometheus.NewDesc(
prometheus.BuildFQName(Namespace, "stats", "queue_transmitted_packets_total"),
"Number of transmitted packets in a queue partitioned by queue name and interface.",
[]string{"queue", "interface"},
nil),
"queue_xmit_bytes": prometheus.NewDesc(
prometheus.BuildFQName(Namespace, "stats", "queue_transmitted_bytes_total"),
"Number of transmitted bytes in a queue partitioned by queue name and interface.",
[]string{"queue", "interface"},
nil),
"queue_dropped_packets": prometheus.NewDesc(
prometheus.BuildFQName(Namespace, "stats", "queue_dropped_packets_total"),
"Number of dropped packets in a queue partitioned by queue name and interface.",
[]string{"queue", "interface"},
nil),
"queue_dropped_bytes": prometheus.NewDesc(
prometheus.BuildFQName(Namespace, "stats", "queue_dropped_bytes_total"),
"Number of dropped bytes in a queue partitioned by queue name and interface.",
[]string{"queue", "interface"},
nil),
},
}
return exp, nil
}
var (
listenAddress = flag.String("web.listen-address", ":9107", "Address to listen on for web interface and telemetry.")
metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
fdno = flag.Int("pf.fd", -1, "if set, use this fd for pf ioctls")
)
func main() {
flag.Parse()
exporter, err := NewPfExporter()
if err != nil {
log.Fatalf("Failed to create pf exporter: %v", err)
}
prometheus.MustRegister(exporter)
log.Infof("Starting Server: %s", *listenAddress)
http.Handle(*metricsPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>pf Exporter</title></head>
<body>
<h1>pf Exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
</body>
</html>`))
})
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}