Exemplo n.º 1
0
    def test_is_mib_known_prefix_limit(self):
        poller = Poller.__new__(Poller)
        poller.mib_map = {"1.2.3.4.5": "test1"}
        found, mib = poller.is_mib_known("some ID", "1.2.3.4.5.6.7", "address")

        self.assertFalse(found)
        self.assertEqual(mib, "")
 def test_do_work_get(self, load_profiles, getCmd, bulkCmd):
     poller = Poller.__new__(Poller)
     poller.last_modified = 1609675634
     poller.snmpEngine = None
     poller.builder = MagicMock()
     poller.process_snmp_data = MagicMock()
     requested_profiles = ["profile1", "profile2"]
     load_profiles.return_value = {
         "profile1": {
             "frequency": 20,
             "varBinds": [["IF-MIB", "ifDescr", 1],
                          ["IF-MIB", "ifSpeed", 2]],
         },
         "profile2": {
             "frequency": 20,
             "varBinds": [["UDP-MIB", "udpOutDatagrams", 1]],
         },
     }
     getCmd.return_value = [
         (None, 0, 0, "Oid1"),
         (None, 0, 0, "Oid2"),
         (None, 0, 0, "Oid3"),
     ]
     poller.do_work(inventory_record, profiles=requested_profiles)
     self.assertEqual(poller.process_snmp_data.call_count, 3)
     self.assertEqual(getCmd.call_count, 1)
     self.assertEqual(bulkCmd.call_count, 0)
    def test_get_varbinds_for_walk_first_time(self):
        poller = Poller.__new__(Poller)

        profiles = {
            "test1": {
                "type": "walk"
            },
            "varBinds": [
                ["IF-MIB", "ifInDiscards", 1],
                ["IF-MIB", "ifOutErrors"],
                ["SNMPv2-MIB", "sysDescr", 0],
            ],
        }

        varbinds_get, get_mapping, varbinds_bulk, bulk_mapping = poller.get_var_binds(
            "192.168.0.1",
            walk=True,
            walked_first_time=False,
            profiles=profiles)

        self.assertEqual(0, len(varbinds_get))
        self.assertEqual(1, len(varbinds_bulk))
        self.assertEqual(0, len(get_mapping))
        self.assertEqual(0, len(bulk_mapping))

        walk_var_bind = next(iter(varbinds_bulk))

        self.assertEqual(
            "1.3.6",
            walk_var_bind._ObjectType__args[0]._ObjectIdentity__args[0])
Exemplo n.º 4
0
    def test_is_mib_known(self):
        poller = Poller.__new__(Poller)
        poller.mib_map = {"1.2.3.4.5.6": "test1"}
        found, mib = poller.is_mib_known("some ID", "1.2.3.4.5.6.7", "address")

        self.assertTrue(found)
        self.assertEqual("test1", mib)
Exemplo n.º 5
0
    def test_is_mib_known_when_mib_map_is_empty(self):
        poller = Poller.__new__(Poller)
        poller.mib_map = {}
        found, mib = poller.is_mib_known("some ID", "1.2.3.4.5.6", "address")

        self.assertFalse(found)
        self.assertEqual(mib, "")
Exemplo n.º 6
0
    def test_load_mib(self):
        poller = Poller.__new__(Poller)
        poller.builder = Mock()
        poller.load_mibs(["a", "b", "c"])
        calls = poller.builder.loadModules.call_args_list

        self.assertEqual("a", calls[0][0][0])
        self.assertEqual("b", calls[1][0][0])
        self.assertEqual("c", calls[2][0][0])
    def test_get_varbinds_for_poll_shadowed_by_family(self):
        poller = Poller.__new__(Poller)

        poller.profiles = {
            "profile1": {
                "frequency":
                20,
                "varBinds": [
                    ["UDP-MIB"],
                    ["UDP-MIB", "udpOutDatagrams"],
                    ["UDP-MIB", "udpOutDatagrams", 0],
                ],
            },
            "profile2": {
                "frequency":
                20,
                "varBinds": [
                    ["IF-MIB"],
                    ["IF-MIB", "ifDescr"],
                    ["IF-MIB", "ifDescr", 1],
                ],
            },
        }
        poller.load_mibs = Mock()
        profiles_requested = ["profile1", "profile2"]

        varbinds_get, get_mapping, varbinds_bulk, bulk_mapping = poller.get_var_binds(
            "192.168.0.1", profiles=profiles_requested)

        self.assertEqual(0, len(varbinds_get))
        self.assertEqual(2, len(varbinds_bulk))
        self.assertEqual(0, len(get_mapping))
        self.assertEqual(2, len(bulk_mapping))

        names = sorted(
            list(
                map(
                    lambda x:
                    (x._ObjectType__args[0]._ObjectIdentity__args[0]),
                    varbinds_bulk,
                )))

        self.assertEqual("IF-MIB", names[0])
        self.assertEqual("UDP-MIB", names[1])

        self.assertEqual({
            "IF-MIB": "profile2",
            "UDP-MIB": "profile1"
        }, bulk_mapping)
        poller.load_mibs.assert_called_with(["UDP-MIB", "IF-MIB"])
    def test_get_varbinds_for_walk(self):
        poller = Poller.__new__(Poller)
        varbinds_get, get_mapping, varbinds_bulk, bulk_mapping = poller.get_var_binds(
            "192.168.0.1", walk=True)

        self.assertEqual(0, len(varbinds_get))
        self.assertEqual(1, len(varbinds_bulk))
        self.assertEqual(0, len(get_mapping))
        self.assertEqual(0, len(bulk_mapping))

        walk_var_bind = next(iter(varbinds_bulk))

        self.assertEqual(
            "1.3.6",
            walk_var_bind._ObjectType__args[0]._ObjectIdentity__args[0])
    def test_do_work_no_work_to_do(self):
        poller = Poller.__new__(Poller)
        poller.last_modified = 1609675634
        poller.snmpEngine = None

        varbinds_bulk, varbinds_get = set(), set()
        get_mapping, bulk_mapping = {}, {}

        poller.get_var_binds = MagicMock()
        poller.get_var_binds.return_value = (
            varbinds_get,
            get_mapping,
            varbinds_bulk,
            bulk_mapping,
        )
        result = poller.do_work(inventory_record)
        self.assertEqual(result, (False, {}))
Exemplo n.º 10
0
 def test_do_work_errors(self, load_profiles, getCmd, bulkCmd):
     poller = Poller.__new__(Poller)
     poller.last_modified = 1609675634
     poller.snmpEngine = None
     poller.builder = MagicMock()
     poller.process_snmp_data = MagicMock()
     requested_profiles = ["profile1"]
     load_profiles.return_value = {
         "profile1": {
             "frequency": 20,
             "varBinds": [["IF-MIB", "ifDescr", 1]]
         }
     }
     getCmd.return_value = [(True, True, 2, [])]
     with self.assertRaises(SnmpActionError):
         poller.do_work(inventory_record, profiles=requested_profiles)
     self.assertEqual(poller.process_snmp_data.call_count, 0)
     self.assertEqual(getCmd.call_count, 1)
     self.assertEqual(bulkCmd.call_count, 0)
    def test_get_varbinds_for_walk_with_profiles(self):
        profiles = {
            "profile1": {
                "frequency": 20,
                "varBinds": [["IF-MIB"]]
            },
            "profile2": {
                "frequency": 20,
                "varBinds": [["UDP-MIB"]]
            },
        }

        poller = Poller.__new__(Poller)
        poller.profiles = profiles
        poller.load_mibs = Mock()

        varbinds_get, get_mapping, varbinds_bulk, bulk_mapping = poller.get_var_binds(
            "192.168.0.1",
            walk=True,
            walked_first_time=True,
            profiles=profiles)

        self.assertEqual(0, len(varbinds_get))
        self.assertEqual(2, len(varbinds_bulk))
        self.assertEqual(0, len(get_mapping))
        self.assertEqual(0, len(bulk_mapping))

        names = sorted(
            list(
                map(
                    lambda x: x._ObjectType__args[0]._ObjectIdentity__args[0],
                    varbinds_bulk,
                )))

        self.assertEqual("IF-MIB", names[0])
        self.assertEqual("UDP-MIB", names[1])
    def test_multiple_metrics_multiple_groups(
        self,
        m_time,
        m_extract_index_number,
        m_map_metric_type,
        m_get_group_key,
        m_resolved,
    ):
        poller = Poller.__new__(Poller)

        m_resolved.return_value = True
        m_get_group_key.side_effect = ["GROUP1", "GROUP2"]
        m_map_metric_type.side_effect = ["g", "g"]
        m_extract_index_number.return_value = 1

        m_time.return_value = 1640609779.473053

        var_bind_mock1_1 = Mock()
        var_bind_mock1_2 = Mock()
        var_bind_mock2_1 = Mock()
        var_bind_mock2_2 = Mock()

        var_bind_mock1_1.getMibSymbol.return_value = "IF-MIB", "some_metric", 1
        var_bind_mock1_1.prettyPrint.return_value = "some text"
        var_bind_mock1_1.getOid.return_value = "1.2.3.4.5.6.7"

        var_bind_mock1_2.prettyPrint.return_value = 65

        var_bind_mock2_1.getMibSymbol.return_value = "UDP-MIB", "next_metric", 1
        var_bind_mock2_1.prettyPrint.return_value = "some text2"
        var_bind_mock2_1.getOid.return_value = "9.8.7.6"

        var_bind_mock2_2.prettyPrint.return_value = 123

        varBindTable = [
            (var_bind_mock1_1, var_bind_mock1_2),
            (var_bind_mock2_1, var_bind_mock2_2),
        ]
        metrics = {}
        mapping = {}

        poller.process_snmp_data(varBindTable, metrics, mapping)

        self.assertEqual(
            {
                "GROUP1": {
                    "fields": {},
                    "metrics": {
                        "IF-MIB.some_metric": {
                            "oid": "1.2.3.4.5.6.7",
                            "time": 1640609779.473053,
                            "type": "g",
                            "value": 65.0,
                        }
                    },
                },
                "GROUP2": {
                    "fields": {},
                    "metrics": {
                        "UDP-MIB.next_metric": {
                            "oid": "9.8.7.6",
                            "time": 1640609779.473053,
                            "type": "g",
                            "value": 123.0,
                        }
                    },
                },
            },
            metrics,
        )
Exemplo n.º 13
0
 def test_exception_during_loading(self):
     poller = Poller.__new__(Poller)
     poller.builder = Mock()
     poller.builder.loadModules.side_effect = error.MibLoadError()
     poller.load_mibs(["a"])