11from easysnmp import SNMPVariable
22
3- from snmpcollector import _apply_expression_to_results
3+ from snmpcollector import _apply_expression_to_results , _convert_counters_to_values
44
55
6- def test_snmpget ():
6+ def test_apply_expression_snmpget ():
77 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' ),
99 ]
1010 methods = ['walk' if isinstance (x , list ) else 'get' for x in results ]
1111 expression = '$1'
@@ -15,10 +15,10 @@ def test_snmpget():
1515 ]
1616 assert _apply_expression_to_results (results , methods , expression , output_path ) == expected_result
1717
18- def test_snmpget_add ():
18+ def test_apply_expression_snmpget_add ():
1919 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' ),
2222 ]
2323 methods = ['walk' if isinstance (x , list ) else 'get' for x in results ]
2424 expression = '$1 + $2'
@@ -28,12 +28,12 @@ def test_snmpget_add():
2828 ]
2929 assert _apply_expression_to_results (results , methods , expression , output_path ) == expected_result
3030
31- def test_snmpwalk ():
31+ def test_apply_expression_snmpwalk ():
3232 results = [
3333 [
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' ),
3737 ],
3838 ]
3939 methods = ['walk' if isinstance (x , list ) else 'get' for x in results ]
@@ -46,13 +46,13 @@ def test_snmpwalk():
4646 ]
4747 assert _apply_expression_to_results (results , methods , expression , output_path ) == expected_result
4848
49- def test_expression_add ():
49+ def test_apply_expression_expression_add ():
5050 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' ),
5252 [
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' ),
5656 ],
5757 ]
5858 methods = ['walk' if isinstance (x , list ) else 'get' for x in results ]
@@ -65,16 +65,16 @@ def test_expression_add():
6565 ]
6666 assert _apply_expression_to_results (results , methods , expression , output_path ) == expected_result
6767
68- def test_snmpwalk_missing_value ():
68+ def test_apply_expression_snmpwalk_missing_value_walk ():
6969 results = [
7070 [
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' ),
7474 ],
7575 [
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' ),
7878 ],
7979 ]
8080 methods = ['walk' if isinstance (x , list ) else 'get' for x in results ]
@@ -85,3 +85,66 @@ def test_snmpwalk_missing_value():
8585 { 'p' : 'snmp.test123.asdf.2' , 'v' : 6100.0 },
8686 ]
8787 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