summaryrefslogtreecommitdiffstats
path: root/tests/topotests/lib/snmptest.py
blob: 814813f7f4589acebcfd8a63a7be328bc0c66b56 (plain)
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
# SPDX-License-Identifier: ISC
#
# topogen.py
# Library of helper functions for NetDEF Topology Tests
#
# Copyright (c) 2020 by Volta Networks
#
#

"""
SNMP library to test snmp walks and gets

Basic usage instructions:

* define an SnmpTester class giving a router, address, community and version
* use test_oid or test_walk to check values in MIBS
* see tests/topotest/simple-snmp-test/test_simple_snmp.py for example
"""

from lib.topolog import logger
import re


class SnmpTester(object):
    "A helper class for testing SNMP"

    def __init__(self, router, iface, community, version, options=""):
        self.community = community
        self.version = version
        self.router = router
        self.iface = iface
        self.options = options
        logger.info(
            "created SNMP tester: SNMPv{0} community:{1}".format(
                self.version, self.community
            )
        )

    def _snmp_config(self):
        """
        Helper function to build a string with SNMP
        configuration for commands.
        """
        return "-v {0} -c {1} {2} {3}".format(
            self.version, self.community, self.options, self.iface
        )

    @staticmethod
    def _get_snmp_value(snmp_output):
        tokens = snmp_output.strip().split()

        num_value_tokens = len(tokens) - 3

        # this copes with the emptys string return
        if num_value_tokens == 0:
            return tokens[2]

        if num_value_tokens > 1:
            output = ""
            index = 3
            while index < len(tokens) - 1:
                output += "{} ".format(tokens[index])
                index += 1
            output += "{}".format(tokens[index])
            return output
        # third token is the value of the object
        return tokens[3]

    @staticmethod
    def _get_snmp_oid(snmp_output):
        tokens = snmp_output.strip().split()

        # third token onwards is the value of the object
        return tokens[0].split(".", 1)[1]

    def _parse_multiline(self, snmp_output):
        results = snmp_output.strip().split("\n")

        out_dict = {}
        out_list = []
        for response in results:
            out_dict[self._get_snmp_oid(response)] = self._get_snmp_value(response)
            out_list.append(self._get_snmp_value(response))

        return out_dict, out_list

    def get(self, oid):
        cmd = "snmpget {0} {1}".format(self._snmp_config(), oid)

        result = self.router.cmd(cmd)
        if "not found" in result:
            return None
        return self._get_snmp_value(result)

    def get_next(self, oid):
        cmd = "snmpgetnext {0} {1}".format(self._snmp_config(), oid)

        result = self.router.cmd(cmd)
        print("get_next: {}".format(result))
        if "not found" in result:
            return None
        return self._get_snmp_value(result)

    def walk(self, oid):
        cmd = "snmpwalk {0} {1}".format(self._snmp_config(), oid)

        result = self.router.cmd(cmd)
        return self._parse_multiline(result)

    def parse_notif_ipv4(self, notif):
        # normalise values
        notif = re.sub(":", "", notif)
        notif = re.sub('"([0-9]{2}) ([0-9]{2}) "', r"\1\2", notif)
        notif = re.sub('"([0-9]{2}) "', r"\1", notif)
        elems = re.findall(r"([0-9,\.]+) = ([0-9,\.]+)", notif)

        # remove common part
        elems = elems[1:]
        return elems

    def is_notif_bgp4_valid(self, output_list, address):
        oid_notif_type = ".1.3.6.1.6.3.1.1.4.1.0"
        peer_notif_established = ".1.3.6.1.2.1.15.0.1"
        peer_notif_backward = ".1.3.6.1.2.1.15.0.2"
        oid_peer_last_error = ".1.3.6.1.2.1.15.3.1.14"
        oid_peer_remote_addr = ".1.3.6.1.2.1.15.3.1.7"
        oid_peer_state = ".1.3.6.1.2.1.15.3.1.2"

        nb_notif = len(output_list)
        for nb in range(0, nb_notif - 1):
            # identify type of notification
            # established or BackwardTransition

            if output_list[nb][0][0] != "{}".format(oid_notif_type):
                return False

            if output_list[nb][0][1] == "{}".format(peer_notif_established):
                logger.info("Established notification")
            elif output_list[nb][0][1] == "{}".format(peer_notif_backward):
                logger.info("Backward transition notification")
            else:
                return False

            # same behavior for 2 notification type in bgp4
            if output_list[nb][1][0] != "{}.{}".format(oid_peer_remote_addr, address):
                return False

            if output_list[nb][2][0] != "{}.{}".format(oid_peer_last_error, address):
                return False
            if output_list[nb][3][0] != "{}.{}".format(oid_peer_state, address):
                return False

        return True

    def is_notif_bgp4v2_valid(self, output_list, address, type_requested):
        oid_notif_type = ".1.3.6.1.6.3.1.1.4.1.0"
        peer_notif_established = ".1.3.6.1.3.5.1.0.1"
        peer_notif_backward = ".1.3.6.1.3.5.1.0.2"
        oid_peer_state = ".1.3.6.1.3.5.1.1.2.1.13"
        oid_peer_local_port = ".1.3.6.1.3.5.1.1.2.1.6"
        oid_peer_remote_port = ".1.3.6.1.3.5.1.1.2.1.9"
        oid_peer_err_code_recv = ".1.3.6.1.3.5.1.1.3.1.1"
        oid_peer_err_sub_code_recv = ".1.3.6.1.3.5.1.1.3.1.2"
        oid_peer_err_recv_text = ".1.3.6.1.3.5.1.1.3.1.4"

        nb_notif = len(output_list)
        for nb in range(nb_notif):
            if output_list[nb][0][0] != "{}".format(oid_notif_type):
                return False

            if output_list[nb][0][1] == "{}".format(peer_notif_established):
                logger.info("Established notification")
                notif_type = "Estab"

            elif output_list[nb][0][1] == "{}".format(peer_notif_backward):
                logger.info("Backward transition notification")
                notif_type = "Backward"
            else:
                return False

            if notif_type != type_requested:
                continue

            if output_list[nb][1][0] != "{}.1.{}".format(oid_peer_state, address):
                continue

            if output_list[nb][2][0] != "{}.1.{}".format(oid_peer_local_port, address):
                return False

            if output_list[nb][3][0] != "{}.1.{}".format(oid_peer_remote_port, address):
                return False

            if notif_type == "Estab":
                return True

            if output_list[nb][4][0] != "{}.1.{}".format(
                oid_peer_err_code_recv, address
            ):
                return False

            if output_list[nb][5][0] != "{}.1.{}".format(
                oid_peer_err_sub_code_recv, address
            ):
                return False

            if output_list[nb][6][0] != "{}.1.{}".format(
                oid_peer_err_recv_text, address
            ):
                return False

            return True

        return False

    def get_notif_bgp4(self, output_file):
        notifs = []
        notif_list = []
        whitecleanfile = re.sub("\t", " ", output_file)
        results = whitecleanfile.strip().split("\n")

        # don't consider additional SNMP or application messages
        for result in results:
            if re.search(r"(\.([0-9]+))+\s", result):
                notifs.append(result)

        oid_v4 = r"1\.3\.6\.1\.2\.1\.15"
        for one_notif in notifs:
            is_ipv4_notif = re.search(oid_v4, one_notif)
            if is_ipv4_notif != None:
                formated_notif = self.parse_notif_ipv4(one_notif)
                notif_list.append(formated_notif)

        return notif_list

    def get_notif_bgp4v2(self, output_file):
        notifs = []
        notif_list = []
        whitecleanfile = re.sub("\t", " ", output_file)
        results = whitecleanfile.strip().split("\n")

        # don't consider additional SNMP or application messages
        for result in results:
            if re.search(r"(\.([0-9]+))+\s", result):
                notifs.append(result)

        oid_v6 = r"1\.3\.6\.1\.3\.5\.1"
        for one_notif in notifs:
            is_ipv6_notif = re.search(oid_v6, one_notif)
            if is_ipv6_notif != None:
                formated_notif = self.parse_notif_ipv4(one_notif)
                notif_list.append(formated_notif)

        return notif_list

    def test_oid(self, oid, value):
        print("oid: {}".format(self.get_next(oid)))
        return self.get_next(oid) == value

    def test_oid_walk(self, oid, values, oids=None):
        results_dict, results_list = self.walk(oid)
        print("test_oid_walk: {} {}".format(oid, results_dict))
        if oids is not None:
            index = 0
            for oid in oids:
                # avoid key error for missing keys
                if not oid in results_dict.keys():
                    print("FAIL: missing oid key {}".format(oid))
                    return False
                if results_dict[oid] != values[index]:
                    print(
                        "FAIL{} {} |{}| == |{}|".format(
                            oid, index, results_dict[oid], values[index]
                        )
                    )
                    return False
                index += 1
            return True

        # Return true if 'values' is a subset of 'results_list'
        print("test {} == {}".format(results_list[: len(values)], values))
        return results_list[: len(values)] == values