Debian Codegraph @

prometheus/prometheus-foomuuri-exporter

raw
1 #!/usr/bin/python3
2 # pylint: disable=invalid-name
3 # pylint: enable=invalid-name
4
5 """Prometheus exporter for Foomuuri metrics."""
6
7 import argparse
8 import json
9 import pathlib
10 import re
11 import subprocess
12 import time
13 from collections import Counter
14
15 # pylint: disable=import-error
16 from prometheus_client import REGISTRY, start_http_server
17 from prometheus_client.core import CounterMetricFamily, GaugeMetricFamily
18 from prometheus_client.registry import Collector
19
20
21 class MonitorCollector(Collector):
22 """Collect Foomuuri Monitor metrics."""
23
24 # pylint: disable=too-few-public-methods
25
26 def __init__(self, args):
27 """Initialize class."""
28 super().__init__()
29 self._stat_file = pathlib.Path(args.statistics_file)
30
31 def collect(self):
32 """Return Foomuuri Monitor metrics."""
33 try:
34 stats = json.loads(self._stat_file.read_text(encoding='utf-8'))
35 except (OSError, ValueError):
36 return
37
38 # Target/group state
39 g = GaugeMetricFamily(
40 'foomuuri_monitor_up',
41 'Target connectivity status.',
42 labels=['type', 'name'],
43 )
44 for name, value in stats.items():
45 g.add_metric([value['type'], name], value['state'])
46 yield g
47
48 # Target packet loss
49 g = GaugeMetricFamily(
50 'foomuuri_monitor_packet_loss_ratio',
51 'Average ping packet loss.',
52 labels=['type', 'name'],
53 )
54 for name, value in stats.items():
55 if not value['time']:
56 continue
57 loss = sum(1 for item in value['time'] if item is None)
58 g.add_metric([value['type'], name], loss / len(value['time']))
59 yield g
60
61 # Target packet ping latency
62 g = GaugeMetricFamily(
63 'foomuuri_monitor_ping_seconds',
64 'Average network round trip time.',
65 labels=['type', 'name'],
66 )
67 for name, value in stats.items():
68 times = [item / 1000 for item in value['time'] if item is not None]
69 if not times:
70 continue
71 g.add_metric([value['type'], name], sum(times) / len(times))
72 yield g
73
74
75 class RulesetCollector(Collector):
76 """Collect ruleset metrics."""
77
78 # pylint: disable=too-few-public-methods
79
80 def __init__(self, args):
81 """Initialize class."""
82 super().__init__()
83 self._set_include = re.compile(args.set_include)
84 self._set_exclude = re.compile(args.set_exclude)
85 self._counter_include = re.compile(args.counter_include)
86 self._counter_exclude = re.compile(args.counter_exclude)
87
88 def collect(self):
89 """Return ruleset metrics."""
90 try:
91 nftdata = json.loads(
92 subprocess.run(
93 ['nft', '--json', 'list', 'table', 'inet', 'foomuuri'],
94 stdout=subprocess.PIPE,
95 check=False,
96 encoding='utf-8',
97 ).stdout
98 )
99 except (OSError, ValueError):
100 return
101
102 # Set size - merge IPv4 and IPv6 to single value
103 sets = Counter()
104 for item in nftdata.get('nftables', {}):
105 if 'set' in item:
106 name = item['set']['name'][:-2] # Without "_4"
107 if self._set_include.search(
108 name
109 ) and not self._set_exclude.search(name):
110 sets.update({name: len(item['set'].get('elem', []))})
111
112 g = GaugeMetricFamily(
113 'foomuuri_set_elements',
114 'Number of elements in set.',
115 labels=['name'],
116 )
117 for name, value in sets.items():
118 g.add_metric([name], value)
119 yield g
120
121 # Named counters
122 counters = {}
123 for item in nftdata.get('nftables', {}):
124 if 'counter' in item:
125 name = item['counter']['name']
126 if self._counter_include.search(
127 name
128 ) and not self._counter_exclude.search(name):
129 counters[name] = {
130 'bytes': item['counter']['bytes'],
131 'packets': item['counter']['packets'],
132 }
133
134 g = CounterMetricFamily(
135 'foomuuri_counter_bytes_total',
136 'Counter bytes value.',
137 labels=['name'],
138 )
139 for name, value in counters.items():
140 g.add_metric([name], value['bytes'])
141 yield g
142
143 g = CounterMetricFamily(
144 'foomuuri_counter_packets_total',
145 'Counter packets value.',
146 labels=['name'],
147 )
148 for name, value in counters.items():
149 g.add_metric([name], value['packets'])
150 yield g
151
152
153 def main():
154 """Parse arguments and run."""
155 # Command line parser
156 parser = argparse.ArgumentParser(
157 description='Export Foomuuri statistics to Prometheus.'
158 )
159 parser.add_argument(
160 '--address', default='::', help='listen address (default: ::)'
161 )
162 parser.add_argument(
163 '--port',
164 type=int,
165 default=11041,
166 help='listen port number (default: 11041)',
167 )
168 parser.add_argument(
169 '--tls-certificate',
170 metavar='FILENAME',
171 help='TLS certificate file name',
172 )
173 parser.add_argument(
174 '--tls-key', metavar='FILENAME', help='TLS key file name'
175 )
176 parser.add_argument(
177 '--no-monitor-statistics',
178 action='store_true',
179 help='do not export Foomuuri Monitor statistics',
180 )
181 parser.add_argument(
182 '--no-ruleset-statistics',
183 action='store_true',
184 help='do not export ruleset statistics',
185 )
186 parser.add_argument(
187 '--statistics-file',
188 metavar='FILENAME',
189 default='/var/lib/foomuuri/monitor.statistics',
190 help='Foomuuri Monitor statistics file name',
191 )
192 parser.add_argument(
193 '--set-include',
194 metavar='REGEXP',
195 default='.', # default all
196 help='set names to be included to ruleset size statistics',
197 )
198 parser.add_argument(
199 '--set-exclude',
200 metavar='REGEXP',
201 default='$^', # default nothing
202 help='set names to be excluded from ruleset size statistics',
203 )
204 parser.add_argument(
205 '--counter-include',
206 metavar='REGEXP',
207 default='.',
208 help='counter names to be included to ruleset traffic statistics',
209 )
210 parser.add_argument(
211 '--counter-exclude',
212 metavar='REGEXP',
213 default='$^',
214 help='counter names to be excluded from ruleset traffic statistics',
215 )
216 args = parser.parse_args()
217
218 # Register collectors
219 if not args.no_monitor_statistics:
220 REGISTRY.register(MonitorCollector(args))
221 if not args.no_ruleset_statistics:
222 REGISTRY.register(RulesetCollector(args))
223
224 # Run exporter
225 start_http_server(
226 port=args.port,
227 addr=args.address,
228 certfile=args.tls_certificate,
229 keyfile=args.tls_key,
230 )
231 while True:
232 time.sleep(1)
233
234
235 if __name__ == '__main__':
236 main()