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", ]), )
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", ]), )
def _execute(self): # type: () -> RawSNMPData verify_ipaddress(self._ipaddress) check_plugin_names = self.get_check_plugin_names() snmp_config = self._snmp_config info = {} # type: RawSNMPData 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". section_name = check_utils.section_name_of(check_plugin_name) oid_info, has_snmp_info = SNMPDataSource._oid_info_from_section_name(section_name) 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 later 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 = [] # type: List[SNMPTable] for entry in oid_info: check_info_part = snmp.get_snmp_table(snmp_config, check_plugin_name, entry, self._use_snmpwalk_cache) check_info.append(check_info_part) info[section_name] = check_info else: info[section_name] = snmp.get_snmp_table(snmp_config, check_plugin_name, oid_info, self._use_snmpwalk_cache) return info
def test_get_data_types(snmp_config, type_name, oid, expected_response): response = snmp.get_single_oid(snmp_config, oid) assert response == expected_response assert isinstance(response, six.text_type) 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_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_simple_snmp_table_wrong_credentials(snmp_config): if snmp_config.is_usewalk_host: pytest.skip("Not relevant") snmp_config = snmp_config.update(credentials="dingdong") # 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): oid_info = ( ".1.3.6.1.2.1.1", ["1.0", "2.0", "5.0"], ) # type: OIDWithColumns snmp.get_snmp_table( snmp_config, check_plugin_name="", oid_info=oid_info, )
def test_get_simple_snmp_table_not_resolvable(snmp_config): if snmp_config.is_usewalk_host: pytest.skip("Not relevant") snmp_config = snmp_config.update(ipaddress="bla.local") # 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): oid_info = ( ".1.3.6.1.2.1.1", ["1.0", "2.0", "5.0"], ) # type: OIDWithColumns snmp.get_snmp_table( snmp_config, check_plugin_name="", oid_info=oid_info, )
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_oid_end_bin(snmp_config): oid_info = ( ".1.3.6.1.2.1.2.2.1", ["1", "2", "3", OID_END_BIN], ) # type: OIDWithColumns table = snmp.get_snmp_table( snmp_config, check_plugin_name="", oid_info=oid_info, ) assert table == [ [u'1', u'lo', u'24', u'\x01'], [u'2', u'eth0', u'6', u'\x02'], ]
def test_get_simple_snmp_table_oid_string(snmp_config): oid_info = ( ".1.3.6.1.2.1.2.2.1", ["1", "2", "3", OID_STRING], ) # type: OIDWithColumns table = snmp.get_snmp_table( snmp_config, check_plugin_name="", oid_info=oid_info, ) assert table == [ [u'1', u'lo', u'24', u'.1.3.6.1.2.1.2.2.1.1.1'], [u'2', u'eth0', u'6', u'.1.3.6.1.2.1.2.2.1.1.2'], ]
def test_get_simple_snmp_table_oid_string(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_STRING, ]), ) assert table == [ [u'1', u'lo', u'24', u'.1.3.6.1.2.1.2.2.1.1.1'], [u'2', u'eth0', u'6', u'.1.3.6.1.2.1.2.2.1.1.2'], ]
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(snmp_config): oid_info = ( ".1.3.6.1.2.1.1", ["1.0", "2.0", "5.0"], ) # type: OIDWithColumns table = snmp.get_snmp_table( snmp_config, check_plugin_name="", oid_info=oid_info, ) 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_with_hex_str(snmp_config): oid_info = ( ".1.3.6.1.2.1.2.2.1", [ "6", ], ) # type: OIDWithColumns table = snmp.get_snmp_table( snmp_config, check_plugin_name="", oid_info=oid_info, ) assert table == [ [u''], [ u'\x00\x12yb\xf9@', ], ]
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 get_all_snmp_tables(info): if not isinstance(info, list): return snmp.get_snmp_table(snmp_cfg, "unit-test", info, False) return [ snmp.get_snmp_table(snmp_cfg, "unit-test", i, False) for i in info ]
def _execute(self): import cmk.base.inventory_plugins self._verify_ipaddress() check_plugin_names = self.get_check_plugin_names() snmp_config = self._snmp_config 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
def _execute(self): # type: () -> RawSNMPData import cmk.base.inventory_plugins # pylint: disable=import-outside-toplevel self._verify_ipaddress() check_plugin_names = self.get_check_plugin_names() snmp_config = self._snmp_config info = {} # type: RawSNMPData oid_info = None # type: Optional[Union[OIDInfo, List[SNMPTree]]] 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) snmp_section_plugin = config.registered_snmp_sections.get( PluginName(section_name)) if snmp_section_plugin: oid_info = snmp_section_plugin.trees elif section_name in cmk.base.inventory_plugins.inv_info: # TODO: merge this into config.registered_snmp_sections! 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 = [] # type: List[SNMPTable] for entry in oid_info: check_info_part = snmp.get_snmp_table( snmp_config, check_plugin_name, entry, self._use_snmpwalk_cache) check_info.append(check_info_part) info[section_name] = check_info else: info[section_name] = snmp.get_snmp_table( snmp_config, check_plugin_name, oid_info, self._use_snmpwalk_cache) return info
def get_all_snmp_tables(info): if not isinstance(info, list): return snmp.get_snmp_table(SNMPConfig, "unit-test", info) return [snmp.get_snmp_table(SNMPConfig, "unit-test", i) for i in info]