-
Notifications
You must be signed in to change notification settings - Fork 13
/
test.py
executable file
·189 lines (171 loc) · 9.63 KB
/
test.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
#!/usr/bin/env python3
import unittest
import logging
import xmltodict
from kannel_exporter import KannelCollector, uptime_to_secs, bearerbox_version
from kannel_exporter import read_password_file, CollectorOpts
logging.basicConfig(level=logging.CRITICAL)
class KannelCollectorTestCase(unittest.TestCase):
@classmethod
def setUpClass(cls):
with open('test/v150.xml') as xml:
cls.status150 = xmltodict.parse(xml.read())
with open('test/v145.xml') as xml:
cls.status145 = xmltodict.parse(xml.read())
def test_uptime_to_secs(self):
uptime1 = uptime_to_secs("running, uptime 0d 1h 16m 31s")
uptime2 = uptime_to_secs("running, uptime 0d 0h 1m 38s")
uptime3 = uptime_to_secs("on-line 0d 0h 1m 53s")
self.assertEqual(uptime1, 4591)
self.assertEqual(uptime2, 98)
self.assertEqual(uptime3, 113)
def test_kannel_collector(self):
exporter = KannelCollector('', '')
self.assertEqual(exporter.parse_kannel_status(), None)
def test_collector_opts(self):
opts_def = CollectorOpts()
opts_nondef = CollectorOpts(5, True, True, False, False, ['smsbox'])
self.assertEqual(opts_def.timeout, 15)
self.assertEqual(opts_def.filter_smsc, False)
self.assertEqual(opts_def.collect_wdp, False)
self.assertEqual(opts_def.collect_box_uptime, False)
self.assertEqual(opts_def.box_connections, ['wapbox', 'smsbox'])
self.assertEqual(opts_nondef.timeout, 5)
self.assertEqual(opts_nondef.filter_smsc, True)
self.assertEqual(opts_nondef.collect_wdp, True)
self.assertEqual(opts_nondef.collect_box_uptime, False)
self.assertEqual(opts_nondef.box_connections, ['smsbox'])
def test_read_password(self):
password = read_password_file('test/secret')
self.assertEqual(password, 'supersecret')
def test_bearerbox_version(self):
version1 = """Kannel bearerbox version `1.4.5'.
Build `Jul 9 2018 21:06:42', compiler `4.8.5 20150623 (Red Hat 4.8.5-11)'.
System Linux, release 3.10.0-514.21.1.el7.x86_64, version #1 SMP Thu May 25 17:04:51 UTC 2017, machine x86_64.
Libxml version 2.9.1.
Using OpenSSL 1.0.1e-fips 11 Feb 2013.
Compiled with MySQL 5.5.52-MariaDB, using MySQL 5.5.52-MariaDB.
Using native malloc."""
version2 = """Kannel bearerbox version 1.4.4'.
Build `Jun 13 2018 04:21:44', compiler `4.8.5 20150623 (Red Hat 4.8.5-28)'.
System Linux, release 3.10.0-693.11.6.el7.x86_64, version #1 SMP Thu Jan 4 01:06:37 UTC 2018, machine x86_64.
Libxml version 2.9.1.
Using OpenSSL 1.0.2k-fips 26 Jan 2017.
Using native malloc."""
version3 = """Kannel bearerbox version `svn-r5150'.
Build `Sep 28 2015 02:39:14', compiler `4.8.3 20140911 (Red Hat 4.8.3-9)'.
System Linux, release 3.10.0-514.21.1.el7.x86_64, version #1 SMP Thu May 25 17:04:51 UTC 2017, machine x86_64.
Libxml version 2.9.1.
Using OpenSSL 1.0.1e-fips 11 Feb 2013.
Compiled with MySQL 5.5.44-MariaDB, using MySQL 5.5.52-MariaDB.
Using native malloc."""
version4 = """Kannel-HA bearerbox version `svn-r5150'.
Build `Sep 28 2015 02:39:14', compiler `4.8.3 20140911 (Red Hat 4.8.3-9)'.
System Linux, release 3.10.0-514.21.1.el7.x86_64, version #1 SMP Thu May 25 17:04:51 UTC 2017, machine x86_64.
Libxml version 2.9.1.
Using OpenSSL 1.0.1e-fips 11 Feb 2013.
Compiled with MySQL 5.5.44-MariaDB, using MySQL 5.5.52-MariaDB.
Using native malloc."""
self.assertEqual(bearerbox_version(version1), "1.4.5")
self.assertEqual(bearerbox_version(version2), "1.4.4")
self.assertEqual(bearerbox_version(version3), "svn-r5150")
self.assertEqual(bearerbox_version(version4), "svn-r5150")
self.assertEqual(bearerbox_version(""), "")
def test_msg_metrics(self):
exporter = KannelCollector('', '')
metrics = exporter.collect_msg_stats(self.status150['gateway'])
self.assertEqual(metrics[0].documentation, 'Total number of SMS received')
self.assertEqual(metrics[0].samples[0].value, 10)
self.assertEqual(metrics[1].documentation, 'Number of received SMS in queue')
self.assertEqual(metrics[1].samples[0].value, 5)
self.assertEqual(metrics[2].documentation, 'Total number of SMS sent')
self.assertEqual(metrics[2].samples[0].value, 30)
self.assertEqual(metrics[3].documentation, 'Number of sent SMS in queue')
self.assertEqual(metrics[3].samples[0].value, 3)
self.assertEqual(metrics[4].documentation, 'Number of SMS in storesize')
self.assertEqual(metrics[4].samples[0].value, 0)
self.assertEqual(metrics[5].documentation, 'Total number of DLR received')
self.assertEqual(metrics[5].samples[0].value, 60)
self.assertEqual(metrics[6].documentation, 'Total number of DLR sent')
self.assertEqual(metrics[6].samples[0].value, 24)
self.assertEqual(metrics[7].documentation, 'Number of DLRs in queue')
self.assertEqual(metrics[7].samples[0].value, 14)
self.assertEqual(metrics[8].documentation, 'DLR storage type info')
self.assertEqual(metrics[8].samples[0].value, 1)
self.assertEqual(metrics[8].samples[0].labels['storage'], 'mysql')
def test_box_metrics(self):
# bearerbox_box_connections Number of box connections
opts = CollectorOpts(collect_box_uptime=True)
exporter = KannelCollector('', '', opts)
metrics = exporter.collect_box_stats(self.status150['gateway']['boxes'])
self.assertEqual(metrics[0].documentation, 'Number of box connections')
self.assertEqual(metrics[0].samples[0].value, 0)
self.assertEqual(metrics[0].samples[0].labels['type'], 'wapbox')
self.assertEqual(metrics[0].samples[1].value, 1)
self.assertEqual(metrics[0].samples[1].labels['type'], 'smsbox')
self.assertEqual(metrics[0].samples[2].value, 1)
self.assertEqual(metrics[0].samples[2].labels['type'], 'sqlbox')
self.assertEqual(metrics[1].documentation, 'Number of messages in box queue')
self.assertEqual(metrics[1].samples[0].value, 7)
self.assertEqual(metrics[1].samples[1].value, 10)
self.assertEqual(metrics[2].documentation, 'Box uptime in seconds (*)')
self.assertEqual(metrics[2].samples[0].value, 3780)
self.assertEqual(metrics[2].samples[1].value, 3545)
def check_smsc_metrics(self, metrics):
self.assertEqual(metrics[0].documentation, 'Total number of SMSC failed messages')
self.assertEqual(metrics[0].samples[0].value, 3)
self.assertEqual(metrics[0].samples[1].value, 4)
self.assertEqual(metrics[0].samples[2].value, 8)
self.assertEqual(metrics[0].samples[3].value, 2)
self.assertEqual(metrics[1].documentation, 'Number of SMSC queued messages')
self.assertEqual(metrics[1].samples[0].value, 1)
self.assertEqual(metrics[1].samples[1].value, 3)
self.assertEqual(metrics[1].samples[2].value, 2)
self.assertEqual(metrics[1].samples[3].value, 5)
self.assertEqual(metrics[2].documentation, 'Total number of received SMS by SMSC')
self.assertEqual(metrics[2].samples[0].value, 0)
self.assertEqual(metrics[2].samples[1].value, 7)
self.assertEqual(metrics[2].samples[2].value, 2)
self.assertEqual(metrics[2].samples[3].value, 1)
self.assertEqual(metrics[3].documentation, 'Total number of SMS sent to SMSC')
self.assertEqual(metrics[3].samples[0].value, 15)
self.assertEqual(metrics[3].samples[1].value, 10)
self.assertEqual(metrics[3].samples[2].value, 3)
self.assertEqual(metrics[3].samples[3].value, 2)
self.assertEqual(metrics[4].documentation, 'Total number of DLRs received by SMSC')
self.assertEqual(metrics[4].samples[0].value, 30)
self.assertEqual(metrics[4].samples[1].value, 20)
self.assertEqual(metrics[4].samples[2].value, 6)
self.assertEqual(metrics[4].samples[3].value, 4)
self.assertEqual(metrics[5].documentation, 'Total number of DLRs sent to SMSC')
self.assertEqual(metrics[5].samples[0].value, 0)
self.assertEqual(metrics[5].samples[1].value, 16)
self.assertEqual(metrics[5].samples[2].value, 5)
self.assertEqual(metrics[5].samples[3].value, 3)
self.assertEqual(metrics[6].documentation, 'Number of online SMSC connections')
self.assertEqual(metrics[6].samples[0].value, 1)
self.assertEqual(metrics[6].samples[1].value, 1)
self.assertEqual(metrics[6].samples[2].value, 0)
self.assertEqual(metrics[6].samples[3].value, 0)
self.assertEqual(metrics[7].documentation, 'Number of offline SMSC connections')
self.assertEqual(metrics[7].samples[0].value, 0)
self.assertEqual(metrics[7].samples[1].value, 0)
self.assertEqual(metrics[7].samples[2].value, 1)
self.assertEqual(metrics[7].samples[3].value, 2)
self.assertEqual(metrics[8].documentation, 'SMSC uptime in seconds (*)')
self.assertEqual(metrics[8].samples[0].value, 178)
self.assertEqual(metrics[8].samples[1].value, 41)
self.assertEqual(metrics[8].samples[2].value, 0)
self.assertEqual(metrics[8].samples[3].value, 0)
def test_smsc_metrics_v150(self):
opts = CollectorOpts(collect_smsc_uptime=True)
exporter = KannelCollector('', '', opts)
metrics = exporter.collect_smsc_stats(self.status150['gateway']['smscs'])
self.check_smsc_metrics(metrics)
def test_smsc_metrics_v145(self):
opts = CollectorOpts(collect_smsc_uptime=True)
exporter = KannelCollector('', '', opts)
metrics = exporter.collect_smsc_stats(self.status145['gateway']['smscs'])
self.check_smsc_metrics(metrics)
if __name__ == "__main__":
unittest.main()