1
1
from easysnmp import SNMPVariable
2
2
3
- from snmpcollector import _apply_expression_to_results
3
+ from snmpcollector import _apply_expression_to_results , _convert_counters_to_values
4
4
5
5
6
- def test_snmpget ():
6
+ def test_apply_expression_snmpget ():
7
7
results = [
8
- SNMPVariable (oid = '1.3.6.1.4.1.2021.13.16.2.1.3' , oid_index = '5' , value = 68000 , snmp_type = 'GAUGE' ),
8
+ SNMPVariable (oid = '1.3.6.1.4.1.2021.13.16.2.1.3' , oid_index = '5' , value = ' 68000' , snmp_type = 'GAUGE' ),
9
9
]
10
10
methods = ['walk' if isinstance (x , list ) else 'get' for x in results ]
11
11
expression = '$1'
@@ -15,10 +15,10 @@ def test_snmpget():
15
15
]
16
16
assert _apply_expression_to_results (results , methods , expression , output_path ) == expected_result
17
17
18
- def test_snmpget_add ():
18
+ def test_apply_expression_snmpget_add ():
19
19
results = [
20
- SNMPVariable (oid = '1.3.6.1.4.1.2021.13.16.2.1.3' , oid_index = '5' , value = 68000 , snmp_type = 'GAUGE' ),
21
- SNMPVariable (oid = '1.3.6.1.4.1.2021.13.16.2.1.3' , oid_index = '1' , value = 200 , snmp_type = 'GAUGE' ),
20
+ SNMPVariable (oid = '1.3.6.1.4.1.2021.13.16.2.1.3' , oid_index = '5' , value = ' 68000' , snmp_type = 'GAUGE' ),
21
+ SNMPVariable (oid = '1.3.6.1.4.1.2021.13.16.2.1.3' , oid_index = '1' , value = ' 200' , snmp_type = 'GAUGE' ),
22
22
]
23
23
methods = ['walk' if isinstance (x , list ) else 'get' for x in results ]
24
24
expression = '$1 + $2'
@@ -28,12 +28,12 @@ def test_snmpget_add():
28
28
]
29
29
assert _apply_expression_to_results (results , methods , expression , output_path ) == expected_result
30
30
31
- def test_snmpwalk ():
31
+ def test_apply_expression_snmpwalk ():
32
32
results = [
33
33
[
34
- SNMPVariable (oid = '1.3.6.1.4.1.2021.13.16.2.1.3' , oid_index = '1' , value = 60000 , snmp_type = 'GAUGE' ),
35
- SNMPVariable (oid = '1.3.6.1.4.1.2021.13.16.2.1.3' , oid_index = '2' , value = 61000 , snmp_type = 'GAUGE' ),
36
- SNMPVariable (oid = '1.3.6.1.4.1.2021.13.16.2.1.3' , oid_index = '3' , value = 62000 , snmp_type = 'GAUGE' ),
34
+ SNMPVariable (oid = '1.3.6.1.4.1.2021.13.16.2.1.3' , oid_index = '1' , value = ' 60000' , snmp_type = 'GAUGE' ),
35
+ SNMPVariable (oid = '1.3.6.1.4.1.2021.13.16.2.1.3' , oid_index = '2' , value = ' 61000' , snmp_type = 'GAUGE' ),
36
+ SNMPVariable (oid = '1.3.6.1.4.1.2021.13.16.2.1.3' , oid_index = '3' , value = ' 62000' , snmp_type = 'GAUGE' ),
37
37
],
38
38
]
39
39
methods = ['walk' if isinstance (x , list ) else 'get' for x in results ]
@@ -46,13 +46,13 @@ def test_snmpwalk():
46
46
]
47
47
assert _apply_expression_to_results (results , methods , expression , output_path ) == expected_result
48
48
49
- def test_expression_add ():
49
+ def test_apply_expression_expression_add ():
50
50
results = [
51
- SNMPVariable (oid = '1.3.6.1.4.1.2021.13.16.2.1.8' , oid_index = '23' , value = 500 , snmp_type = 'GAUGE' ),
51
+ SNMPVariable (oid = '1.3.6.1.4.1.2021.13.16.2.1.8' , oid_index = '23' , value = ' 500' , snmp_type = 'GAUGE' ),
52
52
[
53
- SNMPVariable (oid = '1.3.6.1.4.1.2021.13.16.2.1.3' , oid_index = '1' , value = 60000 , snmp_type = 'GAUGE' ),
54
- SNMPVariable (oid = '1.3.6.1.4.1.2021.13.16.2.1.3' , oid_index = '2' , value = 61000 , snmp_type = 'GAUGE' ),
55
- SNMPVariable (oid = '1.3.6.1.4.1.2021.13.16.2.1.3' , oid_index = '3' , value = 62000 , snmp_type = 'GAUGE' ),
53
+ SNMPVariable (oid = '1.3.6.1.4.1.2021.13.16.2.1.3' , oid_index = '1' , value = ' 60000' , snmp_type = 'GAUGE' ),
54
+ SNMPVariable (oid = '1.3.6.1.4.1.2021.13.16.2.1.3' , oid_index = '2' , value = ' 61000' , snmp_type = 'GAUGE' ),
55
+ SNMPVariable (oid = '1.3.6.1.4.1.2021.13.16.2.1.3' , oid_index = '3' , value = ' 62000' , snmp_type = 'GAUGE' ),
56
56
],
57
57
]
58
58
methods = ['walk' if isinstance (x , list ) else 'get' for x in results ]
@@ -65,16 +65,16 @@ def test_expression_add():
65
65
]
66
66
assert _apply_expression_to_results (results , methods , expression , output_path ) == expected_result
67
67
68
- def test_snmpwalk_missing_value ():
68
+ def test_apply_expression_snmpwalk_missing_value_walk ():
69
69
results = [
70
70
[
71
- SNMPVariable (oid = '1.3.6.1.4.1.2021.13.16.2.1.3' , oid_index = '1' , value = 60000 , snmp_type = 'GAUGE' ),
72
- SNMPVariable (oid = '1.3.6.1.4.1.2021.13.16.2.1.3' , oid_index = '2' , value = 61000 , snmp_type = 'GAUGE' ),
73
- SNMPVariable (oid = '1.3.6.1.4.1.2021.13.16.2.1.3' , oid_index = '3' , value = 62000 , snmp_type = 'GAUGE' ),
71
+ SNMPVariable (oid = '1.3.6.1.4.1.2021.13.16.2.1.3' , oid_index = '1' , value = ' 60000' , snmp_type = 'GAUGE' ),
72
+ SNMPVariable (oid = '1.3.6.1.4.1.2021.13.16.2.1.3' , oid_index = '2' , value = ' 61000' , snmp_type = 'GAUGE' ),
73
+ SNMPVariable (oid = '1.3.6.1.4.1.2021.13.16.2.1.3' , oid_index = '3' , value = ' 62000' , snmp_type = 'GAUGE' ),
74
74
],
75
75
[
76
- SNMPVariable (oid = '1.3.6.1.4.1.2021.13.16.2.2.2' , oid_index = '1' , value = 10 , snmp_type = 'GAUGE' ),
77
- SNMPVariable (oid = '1.3.6.1.4.1.2021.13.16.2.2.2' , oid_index = '2' , value = 10 , snmp_type = 'GAUGE' ),
76
+ SNMPVariable (oid = '1.3.6.1.4.1.2021.13.16.2.2.2' , oid_index = '1' , value = '10' , snmp_type = 'GAUGE' ),
77
+ SNMPVariable (oid = '1.3.6.1.4.1.2021.13.16.2.2.2' , oid_index = '2' , value = '10' , snmp_type = 'GAUGE' ),
78
78
],
79
79
]
80
80
methods = ['walk' if isinstance (x , list ) else 'get' for x in results ]
@@ -85,3 +85,66 @@ def test_snmpwalk_missing_value():
85
85
{ 'p' : 'snmp.test123.asdf.2' , 'v' : 6100.0 },
86
86
]
87
87
assert _apply_expression_to_results (results , methods , expression , output_path ) == expected_result
88
+
89
+ def test_apply_expression_snmpwalk_missing_value_get ():
90
+ results = [
91
+ SNMPVariable (oid = '1.3.6.1.4.1.2021.13.16.2.1.3' , oid_index = '1' , value = '60000' , snmp_type = 'GAUGE' ),
92
+ SNMPVariable (oid = '1.3.6.1.4.1.2021.13.16.2.2.2' , oid_index = '1' , value = None , snmp_type = 'GAUGE' ),
93
+ ]
94
+ methods = ['walk' if isinstance (x , list ) else 'get' for x in results ]
95
+ expression = '$1 / $2'
96
+ output_path = 'snmp.test123.asdf'
97
+ expected_result = []
98
+ assert _apply_expression_to_results (results , methods , expression , output_path ) == expected_result
99
+
100
+ def test_convert_counters_no_counters_no_change ():
101
+ """ If there are no counters, nothing should change """
102
+ now = 1234567890.123456
103
+ results = [
104
+ [
105
+ SNMPVariable (oid = '1.3.6.1.4.1.2021.13.16.2.1.3' , oid_index = '1' , value = '60000' , snmp_type = 'GAUGE' ),
106
+ SNMPVariable (oid = '1.3.6.1.4.1.2021.13.16.2.1.3' , oid_index = '2' , value = '61000' , snmp_type = 'GAUGE' ),
107
+ SNMPVariable (oid = '1.3.6.1.4.1.2021.13.16.2.1.3' , oid_index = '3' , value = '62000' , snmp_type = 'GAUGE' ),
108
+ ],
109
+ [
110
+ SNMPVariable (oid = '1.3.6.1.4.1.2021.13.16.2.2.2' , oid_index = '1' , value = '10' , snmp_type = 'GAUGE' ),
111
+ SNMPVariable (oid = '1.3.6.1.4.1.2021.13.16.2.2.2' , oid_index = '2' , value = '10' , snmp_type = 'GAUGE' ),
112
+ ],
113
+ ]
114
+ assert _convert_counters_to_values (results , now , "ASDF/1234" ) == results
115
+
116
+ def test_convert_counters_counter ():
117
+ """ First expression should be empty, next ones should work """
118
+ now = 1234567890.123456
119
+
120
+ results_0 = [SNMPVariable (oid = '.1.3.6.1.2.1.2.2.1.16' , oid_index = '1' , value = '1000' , snmp_type = 'COUNTER' )]
121
+ expected_0 = [SNMPVariable (oid = '.1.3.6.1.2.1.2.2.1.16' , oid_index = '1' , value = None , snmp_type = 'COUNTER_PER_S' )]
122
+ assert _convert_counters_to_values (results_0 , now , "ASDF/1234" ) == expected_0
123
+
124
+ results_1 = [SNMPVariable (oid = '.1.3.6.1.2.1.2.2.1.16' , oid_index = '1' , value = '2000.0' , snmp_type = 'COUNTER' )]
125
+ expected_1 = [SNMPVariable (oid = '.1.3.6.1.2.1.2.2.1.16' , oid_index = '1' , value = '1000.0' , snmp_type = 'COUNTER_PER_S' )]
126
+ assert _convert_counters_to_values (results_1 , now + 1.0 , "ASDF/1234" ) == expected_1
127
+
128
+ results_2 = [SNMPVariable (oid = '.1.3.6.1.2.1.2.2.1.16' , oid_index = '1' , value = '2300.0' , snmp_type = 'COUNTER' )]
129
+ expected_2 = [SNMPVariable (oid = '.1.3.6.1.2.1.2.2.1.16' , oid_index = '1' , value = '100.0' , snmp_type = 'COUNTER_PER_S' )]
130
+ assert _convert_counters_to_values (results_2 , now + 1.0 + 3.0 , "ASDF/1234" ) == expected_2
131
+
132
+ def test_convert_counters_overflow ():
133
+ """ First expression should be empty, next ones should work """
134
+ now = 1234567890.123456
135
+
136
+ results_0 = [SNMPVariable (oid = '.1.3.6.1.2.1.2.2.1.17' , oid_index = '1' , value = '123000.0' , snmp_type = 'COUNTER' )]
137
+ expected_0 = [SNMPVariable (oid = '.1.3.6.1.2.1.2.2.1.17' , oid_index = '1' , value = None , snmp_type = 'COUNTER_PER_S' )]
138
+ assert _convert_counters_to_values (results_0 , now , "ASDF/1234" ) == expected_0
139
+
140
+ results_1 = [SNMPVariable (oid = '.1.3.6.1.2.1.2.2.1.17' , oid_index = '1' , value = '234000.0' , snmp_type = 'COUNTER' )]
141
+ expected_1 = [SNMPVariable (oid = '.1.3.6.1.2.1.2.2.1.17' , oid_index = '1' , value = '111000.0' , snmp_type = 'COUNTER_PER_S' )]
142
+ assert _convert_counters_to_values (results_1 , now + 1.0 , "ASDF/1234" ) == expected_1
143
+
144
+ results_2 = [SNMPVariable (oid = '.1.3.6.1.2.1.2.2.1.17' , oid_index = '1' , value = '1000.0' , snmp_type = 'COUNTER' )]
145
+ expected_2 = [SNMPVariable (oid = '.1.3.6.1.2.1.2.2.1.17' , oid_index = '1' , value = None , snmp_type = 'COUNTER_PER_S' )]
146
+ assert _convert_counters_to_values (results_2 , now + 1.0 + 3.0 , "ASDF/1234" ) == expected_2
147
+
148
+ results_3 = [SNMPVariable (oid = '.1.3.6.1.2.1.2.2.1.17' , oid_index = '1' , value = '2000.0' , snmp_type = 'COUNTER' )]
149
+ expected_3 = [SNMPVariable (oid = '.1.3.6.1.2.1.2.2.1.17' , oid_index = '1' , value = '500.0' , snmp_type = 'COUNTER_PER_S' )]
150
+ assert _convert_counters_to_values (results_3 , now + 1.0 + 3.0 + 2.0 , "ASDF/1234" ) == expected_3
0 commit comments