def test_get_simple_snmp_table_wrong_credentials(snmp_config): if snmp_config.is_usewalk_host: pytest.skip("Not relevant") cfg_dict = snmp_config._asdict() cfg_dict["credentials"] = "dingdong" snmp_config = snmp_utils.SNMPHostConfig(**cfg_dict) # TODO: Unify different error messages if snmp_config.is_inline_snmp_host: exc_match = "SNMP query timed out" elif not snmp_config.is_inline_snmp_host: exc_match = "Timeout: No Response from" else: raise NotImplementedError() with pytest.raises(MKSNMPError, match=exc_match): snmp.get_snmp_table(snmp_config, check_plugin_name=None, oid_info=(".1.3.6.1.2.1.1", [ "1.0", "2.0", "5.0", ]), use_snmpwalk_cache=False)
def test_get_simple_snmp_table_not_resolvable(snmp_config): if snmp_config.is_usewalk_host: pytest.skip("Not relevant") cfg_dict = snmp_config._asdict() cfg_dict["ipaddress"] = "bla.local" snmp_config = snmp_utils.SNMPHostConfig(**cfg_dict) # TODO: Unify different error messages if snmp_config.is_inline_snmp_host: exc_match = "Failed to initiate SNMP" elif not snmp_config.is_inline_snmp_host: exc_match = "Unknown host" else: raise NotImplementedError() with pytest.raises(MKSNMPError, match=exc_match): snmp.get_snmp_table(snmp_config, check_plugin_name=None, oid_info=(".1.3.6.1.2.1.1", [ "1.0", "2.0", "5.0", ]), use_snmpwalk_cache=False)
def test_get_simple_snmp_table_with_hex_str(snmp_config): table = snmp.get_snmp_table(snmp_config, check_plugin_name=None, oid_info=(".1.3.6.1.2.1.2.2.1", [ "6", ]), use_snmpwalk_cache=False) assert table == [ [u''], [ u'\x00\x12yb\xf9@', ], ]
def test_get_data_types(snmp_config, type_name, oid, expected_response): response = snmp.get_single_oid(snmp_config, oid) assert response == expected_response # TODO: Encoding is incosistent between single oids and walks assert isinstance(response, str) oid_start, oid_end = oid.rsplit(".", 1) table = snmp.get_snmp_table(snmp_config, check_plugin_name=None, oid_info=(oid_start, [oid_end]), use_snmpwalk_cache=False) assert table[0][0] == expected_response assert isinstance(table[0][0], six.text_type)
def test_get_simple_snmp_table_oid_end_bin(snmp_config): table = snmp.get_snmp_table(snmp_config, check_plugin_name=None, oid_info=(".1.3.6.1.2.1.2.2.1", [ "1", "2", "3", snmp_utils.OID_END_BIN, ]), use_snmpwalk_cache=False) assert table == [ [u'1', u'lo', u'24', u'\x01'], [u'2', u'eth0', u'6', u'\x02'], ]
def test_get_simple_snmp_table(snmp_config): table = snmp.get_snmp_table(snmp_config, check_plugin_name=None, oid_info=(".1.3.6.1.2.1.1", [ "1.0", "2.0", "5.0", ]), use_snmpwalk_cache=False) assert table == [ [ u'Linux zeus 4.8.6.5-smp #2 SMP Sun Nov 13 14:58:11 CDT 2016 i686', u'.1.3.6.1.4.1.8072.3.2.10', u'new system name', ], ] assert isinstance(table[0][0], six.text_type)
def test_get_simple_snmp_table_bulkwalk(snmp_config, bulk): cfg_dict = snmp_config._asdict() cfg_dict["is_bulkwalk_host"] = bulk snmp_config = snmp_utils.SNMPHostConfig(**cfg_dict) table = snmp.get_snmp_table(snmp_config, check_plugin_name=None, oid_info=(".1.3.6.1.2.1.1", [ "1.0", "2.0", "5.0", ]), use_snmpwalk_cache=False) assert table == [ [ u'Linux zeus 4.8.6.5-smp #2 SMP Sun Nov 13 14:58:11 CDT 2016 i686', u'.1.3.6.1.4.1.8072.3.2.10', u'new system name', ], ] assert isinstance(table[0][0], six.text_type)
def _execute(self): import cmk_base.inventory_plugins self._verify_ipaddress() check_plugin_names = self.get_check_plugin_names() snmp_config = self._host_config.snmp_config(self._ipaddress) info = {} for check_plugin_name in self._sort_check_plugin_names( check_plugin_names): # Is this an SNMP table check? Then snmp_info specifies the OID to fetch # Please note, that if the check_plugin_name is foo.bar then we lookup the # snmp info for "foo", not for "foo.bar". has_snmp_info = False section_name = cmk_base.check_utils.section_name_of( check_plugin_name) if section_name in config.snmp_info: oid_info = config.snmp_info[section_name] elif section_name in cmk_base.inventory_plugins.inv_info: oid_info = cmk_base.inventory_plugins.inv_info[ section_name].get("snmp_info") if oid_info: has_snmp_info = True else: oid_info = None if not has_snmp_info and check_plugin_name in self._fetched_check_plugin_names: continue if oid_info is None: continue # This checks data is configured to be persisted (snmp_check_interval) and recent enough. # Skip gathering new data here. The persisted data will be added latera if self._persisted_sections and section_name in self._persisted_sections: self._logger.debug( "%s: Skip fetching data (persisted info exists)" % (check_plugin_name)) continue # Prevent duplicate data fetching of identical section in case of SNMP sub checks if section_name in info: self._logger.debug( "%s: Skip fetching data (section already fetched)" % (check_plugin_name)) continue self._logger.debug("%s: Fetching data" % (check_plugin_name)) # oid_info can now be a list: Each element of that list is interpreted as one real oid_info # and fetches a separate snmp table. if isinstance(oid_info, list): check_info = [] for entry in oid_info: check_info_part = snmp.get_snmp_table( snmp_config, check_plugin_name, entry, self._use_snmpwalk_cache) # If at least one query fails, we discard the whole info table if check_info_part is None: check_info = None break else: check_info.append(check_info_part) else: check_info = snmp.get_snmp_table(snmp_config, check_plugin_name, oid_info, self._use_snmpwalk_cache) info[section_name] = check_info return info