-
Notifications
You must be signed in to change notification settings - Fork 13
/
kannel_exporter.py
executable file
·518 lines (430 loc) · 22.2 KB
/
kannel_exporter.py
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#!/usr/bin/env python3
"""Prometheus custom collector for Kannel gateway
https://github.com/apostvav/kannel_exporter"""
__version__ = '0.8.0'
import argparse
import logging
import os
import sys
import re
from urllib.request import urlopen
from urllib.error import URLError
from time import time
from collections import namedtuple
from wsgiref.simple_server import make_server
from typing import Any, Optional, Dict, List
from xml.parsers import expat
import xmltodict
from prometheus_client.registry import Collector
from prometheus_client.core import Metric
from prometheus_client.core import GaugeMetricFamily, CounterMetricFamily
from prometheus_client import REGISTRY, PLATFORM_COLLECTOR
from prometheus_client import GC_COLLECTOR, PROCESS_COLLECTOR
from prometheus_client import make_wsgi_app
# logger
logger = logging.getLogger('kannel_exporter') # pylint: disable=invalid-name
def uptime_to_secs(uptime: str) -> int:
days, hours, mins, secs = re.findall(r'\d+', uptime)
days = int(days) * 86400
hours = int(hours) * 3600
mins = int(mins) * 60
secs = int(secs)
return days + hours + mins + secs
def bearerbox_version(version: str) -> str:
try:
version = version.split('\n')[0]
version_position = version.find('version ')
# 'version ' is 8 chars long
if version_position != -1:
version = version[version_position + 8:].strip('`').rstrip('\'.')
else:
logger.warning(('Bearerbox version could not be found. '
'Version value set to empty string.'))
version = ""
except IndexError:
logger.error(('Failed to parse gateway version. '
'Version value set to empty string.'))
version = ""
return version
def _xmlpostproc(path, key, value): # pylint: disable=unused-argument
if value is None:
value = ""
return key, value
CollectorOpts = namedtuple('CollectorOpts', ['timeout',
'filter_smsc',
'collect_wdp',
'collect_box_uptime',
'collect_smsc_uptime',
'box_connections'])
CollectorOpts.__new__.__defaults__ = (15, False, False, False, False,
['wapbox', 'smsbox'])
class KannelCollector(Collector):
def __init__(self, target, password, opts=CollectorOpts()):
self.target = target
self.password = password
self.opts = opts
def parse_kannel_status(self) -> Optional[Dict[str, Any]]:
url = self.target + '/status.xml?password=' + self.password
status = None
xml = None
try:
with urlopen(url, timeout=self.opts.timeout) as response:
xml = response.read()
if xml is not None:
status = xmltodict.parse(xml, postprocessor=_xmlpostproc)
if status['gateway'] == 'Denied':
logger.error('Authentication failed.')
return None
except ValueError as err:
logger.error("Uknown URL type: %s. Error: %s", self.target, err)
except URLError as err:
logger.error("Failed to open target URL: %s. Error: %s", self.target, err)
except expat.ExpatError as err:
logger.error("Failed to parse status XML. Error: %s", err)
return status
def collect_msg_stats(self, gw_metrics: Dict[str, Any]) -> List[Metric]:
metrics = []
directions = ['received', 'sent']
states = ['total', 'queued']
# collect WDP metrics
if self.opts.collect_wdp:
for direction in directions:
for state in states:
metric_name = f"bearerbox_wdp_{direction}_{state}"
if state == 'total':
metric_help = f"Total number of WDP {direction}"
metric = CounterMetricFamily(metric_name, metric_help)
else:
metric_help = f"Number of {direction} WDP in queue"
metric = GaugeMetricFamily(metric_name, metric_help)
metric_value = int(gw_metrics['wdp'][direction][state])
metric.add_sample(metric_name, value=metric_value,
labels={})
metrics.append(metric)
# collect SMS metrics
for direction in directions:
for state in states:
metric_name = f"bearerbox_sms_{direction}_{state}"
if state == 'total':
metric_help = f"Total number of SMS {direction}"
metric = CounterMetricFamily(metric_name, metric_help)
else:
metric_help = f"Number of {direction} SMS in queue"
metric = GaugeMetricFamily(metric_name, metric_help)
metric_value = int(gw_metrics['sms'][direction][state])
metric.add_sample(metric_name, value=metric_value, labels={})
metrics.append(metric)
metric_name = 'bearerbox_sms_storesize'
metric_value = int(gw_metrics['sms']['storesize'])
metric = GaugeMetricFamily(metric_name, 'Number of SMS in storesize')
metric.add_sample('bearerbox_sms_storesize', value=metric_value, labels={})
metrics.append(metric)
# collect DLR metrics
for direction in directions:
metric_name = f"bearerbox_dlr_{direction}_total"
metric_help = f"Total number of DLR {direction}"
metric_value = int(gw_metrics['dlr'][direction]['total'])
metric = CounterMetricFamily(metric_name, metric_help)
metric.add_sample(metric_name, value=metric_value, labels={})
metrics.append(metric)
metric_name = 'bearerbox_dlr_queued'
metric_value = int(gw_metrics['dlr']['queued'])
metric = GaugeMetricFamily(metric_name, "Number of DLRs in queue")
metric.add_sample(metric_name, value=metric_value, labels={})
metrics.append(metric)
metric_name = 'bearerbox_dlr_storage'
metric = GaugeMetricFamily(metric_name, 'DLR storage type info')
metric_labels = {'storage': gw_metrics['dlr']['storage']}
metric.add_sample(metric_name, value=1, labels=metric_labels)
metrics.append(metric)
return metrics
@staticmethod
def _collect_box_uptime(box_details, box, tuplkey):
# Helper method to collect box uptime metrics.
# In case of multiple boxes with same type, id and host,
# only the lowest uptime value will be exposed in order to avoid duplicates.
uptime = uptime_to_secs(box['status'])
if tuplkey in box_details:
if ('uptime' not in box_details[tuplkey] or
uptime < box_details[tuplkey]['uptime']):
box_details[tuplkey]['uptime'] = uptime
else:
box_details[tuplkey] = {}
box_details[tuplkey]['uptime'] = uptime_to_secs(box['status'])
def collect_box_stats(self, box_metrics: Dict[str, Any]) -> List[Metric]:
metrics = []
box_connections = {b: 0 for b in self.opts.box_connections}
box_details = {}
box_conn_mtr = GaugeMetricFamily('bearerbox_box_connections',
'Number of box connections')
box_queue_mtr = GaugeMetricFamily('bearerbox_box_queue',
'Number of messages in box queue')
box_uptime_mtr = GaugeMetricFamily('bearerbox_box_uptime_seconds',
'Box uptime in seconds (*)')
if box_metrics:
# when there's only one box connected on the gateway
# xmltodict returns an OrderedDict instead of a list of OrderedDicts
if not isinstance(box_metrics['box'], list):
box_metrics['box'] = [box_metrics['box']]
for box in box_metrics['box']:
box_connections[box['type']] = box_connections.get(box['type'], 0) + 1
# some type of boxes (e.g wapbox) don't have IDs.
box['id'] = box.get('id', '')
tuplkey = (box['type'], box['id'], box['IP'])
# some type of boxes (e.g wapbox) don't have queues.
if 'queue' in box:
if tuplkey not in box_details:
box_details[tuplkey] = {}
box_details[tuplkey]['queue'] = (box_details[tuplkey].get('queue', 0)
+ int(box['queue']))
# collect box uptime metrics
if self.opts.collect_box_uptime is True:
self._collect_box_uptime(box_details, box, tuplkey)
for key, value in box_connections.items():
box_conn_mtr.add_sample('bearerbox_box_connections',
value=value, labels={'type': key})
metrics.append(box_conn_mtr)
for key, value in box_details.items():
box_labels = {'type': key[0], 'id': key[1], 'ipaddr': key[2]}
if 'queue' in value:
box_queue_mtr.add_sample('bearerbox_box_queue',
value=value['queue'],
labels=box_labels)
if self.opts.collect_box_uptime is True:
box_uptime_mtr.add_sample('bearerbox_box_uptime_seconds',
value=value['uptime'],
labels=box_labels)
metrics.append(box_queue_mtr)
if self.opts.collect_box_uptime is True:
metrics.append(box_uptime_mtr)
return metrics
def _aggregate_smsc_stats(self, smsc_metrics:List[Dict]) -> Dict[str, Any]:
aggreg = {}
for smsc in smsc_metrics:
smscid = smsc['id']
if smscid not in aggreg:
aggreg[smscid] = {}
aggreg[smscid]['sms'] = {}
aggreg[smscid]['dlr'] = {}
aggreg[smscid]['online'] = 0
aggreg[smscid]['offline'] = 0
aggreg[smscid]['failed'] = (aggreg[smscid].get('failed', 0)
+ int(smsc['failed']))
aggreg[smscid]['queued'] = (aggreg[smscid].get('queued', 0)
+ int(smsc['queued']))
smsc_status = re.findall(r'\d+', smsc['status'])
if smsc_status:
aggreg[smscid]['online'] += 1
if self.opts.collect_smsc_uptime is True:
aggreg[smscid]['uptime'] = int(smsc_status[0])
else:
aggreg[smscid]['offline'] += 1
if self.opts.collect_smsc_uptime is True:
aggreg[smscid]['uptime'] = 0
# kannel 1.5 exposes metrics in a different format
if 'sms' not in smsc:
aggreg[smscid]['sms']['received'] = (aggreg[smscid]['sms'].get('received', 0)
+ int(smsc['received']['sms']))
aggreg[smscid]['sms']['sent'] = (aggreg[smscid]['sms'].get('sent', 0)
+ int(smsc['sent']['sms']))
aggreg[smscid]['dlr']['received'] = (aggreg[smscid]['dlr'].get('received', 0)
+ int(smsc['received']['dlr']))
aggreg[smscid]['dlr']['sent'] = (aggreg[smscid]['dlr'].get('sent', 0)
+ int(smsc['sent']['dlr']))
else:
aggreg[smscid]['sms']['received'] = (aggreg[smscid]['sms'].get('received', 0)
+ int(smsc['sms']['received']))
aggreg[smscid]['sms']['sent'] = (aggreg[smscid]['sms'].get('sent', 0)
+ int(smsc['sms']['sent']))
aggreg[smscid]['dlr']['received'] = (aggreg[smscid]['dlr'].get('received', 0)
+ int(smsc['dlr']['received']))
aggreg[smscid]['dlr']['sent'] = (aggreg[smscid]['dlr'].get('sent', 0)
+ int(smsc['dlr']['sent']))
return aggreg
def collect_smsc_stats(self, smsc_metrics: Dict[str, Any]) -> List[Metric]:
failed = CounterMetricFamily('bearerbox_smsc_failed_messages_total',
'Total number of SMSC failed messages',
labels=['smsc_id'])
queued = GaugeMetricFamily('bearerbox_smsc_queued_messages',
'Number of SMSC queued messages',
labels=['smsc_id'])
sms_received = CounterMetricFamily('bearerbox_smsc_received_sms_total',
'Total number of received SMS by SMSC',
labels=['smsc_id'])
sms_sent = CounterMetricFamily('bearerbox_smsc_sent_sms_total',
'Total number of SMS sent to SMSC',
labels=['smsc_id'])
dlr_received = CounterMetricFamily('bearerbox_smsc_received_dlr_total',
'Total number of DLRs received by SMSC',
labels=['smsc_id'])
dlr_sent = CounterMetricFamily('bearerbox_smsc_sent_dlr_total',
'Total number of DLRs sent to SMSC',
labels=['smsc_id'])
conn_online = GaugeMetricFamily('bearerbox_smsc_online_connections',
'Number of online SMSC connections',
labels=['smsc_id'])
conn_offline = GaugeMetricFamily('bearerbox_smsc_offline_connections',
'Number of offline SMSC connections',
labels=['smsc_id'])
uptime = GaugeMetricFamily('bearerbox_smsc_uptime_seconds',
'SMSC uptime in seconds (*)',
labels=['smsc_id'])
# when there's only one smsc connection on the gateway
# xmltodict returns a dict instead of a list of dicts
if not isinstance(smsc_metrics['smsc'], list):
smsc_metrics['smsc'] = [smsc_metrics['smsc']]
# Aggregate SMSC metrics by smsc-id
aggreg = self._aggregate_smsc_stats(smsc_metrics['smsc'])
for smsc in aggreg: # pylint: disable=consider-using-dict-items
failed.add_metric([smsc], aggreg[smsc]['failed'])
queued.add_metric([smsc], aggreg[smsc]['queued'])
sms_received.add_metric([smsc], aggreg[smsc]['sms']['received'])
sms_sent.add_metric([smsc], aggreg[smsc]['sms']['sent'])
dlr_received.add_metric([smsc], aggreg[smsc]['dlr']['received'])
dlr_sent.add_metric([smsc], aggreg[smsc]['dlr']['sent'])
conn_online.add_metric([smsc], aggreg[smsc]['online'])
conn_offline.add_metric([smsc], aggreg[smsc]['offline'])
if self.opts.collect_smsc_uptime is True:
uptime.add_metric([smsc], aggreg[smsc]['uptime'])
metrics = [
failed,
queued,
sms_received,
sms_sent,
dlr_received,
dlr_sent,
conn_online,
conn_offline,
]
if self.opts.collect_smsc_uptime:
metrics.append(uptime)
return metrics
def collect(self): # pylint: disable=inconsistent-return-statements
# bearerbox server status
metric = GaugeMetricFamily('bearerbox_up',
'Could the bearerbox server be reached')
start = time()
response = self.parse_kannel_status()
if response is None:
metric.add_sample('bearerbox_up', value=0, labels={})
yield metric
return []
metric.add_sample('bearerbox_up', value=1, labels={})
yield metric
# Version info
version = bearerbox_version(response['gateway']['version'])
metric = GaugeMetricFamily('bearerbox_build_info',
'Kannel bearerbox version info')
metric.add_sample('bearerbox_build_info', value=1,
labels={'version': version})
yield metric
# Gauge for the bearerbox uptime, in seconds
uptime = uptime_to_secs(response['gateway']['status'])
metric = GaugeMetricFamily('bearerbox_uptime_seconds',
'Current uptime in seconds (*)')
metric.add_sample('bearerbox_uptime_seconds', value=uptime, labels={})
yield metric
# WDP, SMS & DLR metrics
msg_metrics = self.collect_msg_stats(response['gateway'])
for metric in msg_metrics:
yield metric
# Box metrics
box_metrics = self.collect_box_stats(response['gateway']['boxes'])
for metric in box_metrics:
yield metric
# Number of smsc connections
metric = GaugeMetricFamily('bearerbox_smsc_connections',
'Number of SMSC connections')
metric.add_sample('bearerbox_smsc_connections',
value=int(response['gateway']['smscs']['count']),
labels={})
yield metric
# SMSC metrics
if not self.opts.filter_smsc:
smsc_metrics = self.collect_smsc_stats(response['gateway']['smscs'])
for metric in smsc_metrics:
yield metric
duration = time() - start
metric = GaugeMetricFamily('bearerbox_scrape_duration_seconds',
'Bearerbox metrics scrape duration in seconds (*)')
metric.add_sample('bearerbox_scrape_duration_seconds', value=duration, labels={})
yield metric
def read_password_file(path: str) -> str:
try:
with open(path) as pass_file:
password = pass_file.read().strip()
except OSError as err:
sys.exit(f"Failed to open file {path}.\n{err}")
except UnicodeError as err:
sys.exit(f"Failed to read file {path}.\n{err}")
return password
def cli() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description="Kannel exporter for Prometheus")
parser.add_argument('--target', dest='target',
help='Target kannel server, PROTO:HOST:PORT. ' +
'(default http://127.0.0.1:13000)',
default=os.environ.get('KANNEL_HOST', 'http://127.0.0.1:13000'))
parser.add_argument('--port', dest='port', type=int,
help='Exporter port. (default 9390)',
default=int(os.environ.get('KANNEL_EXPORTER_PORT', '9390')))
parser.add_argument('--timeout', dest='timeout', type=int,
help='Timeout for trying to get stats. (default 15)',
default=int(os.environ.get('KANNEL_EXPORTER_TIMEOUT', '15')))
parser.add_argument('--filter-smscs', dest='filter_smsc', action='store_true',
help='Filter out SMSC metrics')
parser.add_argument('--collect-wdp', dest='collect_wdp', action='store_true',
help='Collect WDP metrics.')
parser.add_argument('--collect-box-uptime', dest='collect_box_uptime',
action='store_true', help='Collect boxes uptime metrics')
parser.add_argument('--collect-smsc-uptime', dest='collect_smsc_uptime',
action='store_true', help='Collect smsc uptime metrics')
parser.add_argument('--box-connection-types', dest='box_connections',
nargs='+', default=['wapbox', 'smsbox'],
help='List of box connection types. (default wapbox, smsbox)')
parser.add_argument('--disable-exporter-metrics', dest='disable_exporter_metrics',
action='store_true', help='Disable exporter metrics')
parser.add_argument('--log-level', dest='log_level', default='WARNING',
choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
help='Define the logging level')
parser.add_argument('-v', '--version', dest='version', action='store_true',
help='Display version information and exit')
pass_group = parser.add_mutually_exclusive_group()
pass_group.add_argument('--password', dest='password',
help='Password of the kannel status page. Mandatory argument',
default=os.environ.get('KANNEL_STATUS_PASSWORD'))
pass_group.add_argument('--password-file', dest='password_file',
help='File contains the kannel status password')
return parser
def main():
# command line arguments
parser = cli()
args = parser.parse_args()
# display version and exit
if args.version is True:
print(f"Version is {__version__}")
sys.exit()
# logger configuration
logging.basicConfig(format="%(asctime)s %(name)s %(levelname)s: %(message)s",
datefmt="%Y-%m-%d %H:%M:%S")
logger.setLevel(args.log_level)
# check if password has been set
if args.password is None and args.password_file is None:
parser.error('Option --password or --password-file must be set.')
elif args.password_file:
status_password = read_password_file(args.password_file)
else:
status_password = args.password
# collector options
opts = CollectorOpts(args.timeout, args.filter_smsc, args.collect_wdp,
args.collect_box_uptime, args.collect_smsc_uptime,
args.box_connections)
if args.disable_exporter_metrics: # stop exposing exporter process metrics
REGISTRY.unregister(GC_COLLECTOR)
REGISTRY.unregister(PROCESS_COLLECTOR)
REGISTRY.unregister(PLATFORM_COLLECTOR)
REGISTRY.register(KannelCollector(args.target, status_password, opts))
app = make_wsgi_app()
httpd = make_server('', args.port, app)
httpd.serve_forever()
if __name__ == '__main__':
main()