def test_device_versions(declared_hostinfo): dev = SnmpHandler(host='1.1.1.1', version='2c', community='public') hostinfo = HostInfo(dev) hostinfo.get_version() assert hostinfo.vendor == declared_hostinfo.vendor assert hostinfo.os == declared_hostinfo.os assert hostinfo.version == declared_hostinfo.version
def test_hostinfo_pysnmp_netsnmp(patch_pysnmp_getnext): handler = SnmpHandler(host='1.1.1.1', version='2c', community='public') hostinfo = HostInfo( handler, vendor='net-snmp', description='Cisco IOS Software, catalyst, Version 15.x, RELEASE') hostinfo.get_version() assert hostinfo.version == 'UNKNOWN'
def test_hostinfo_cisco_ios_version(): handler = SnmpHandler(host='1.1.1.1') hostinfo = HostInfo( handler, vendor='cisco', description='Cisco IOS Software, catalyst, Version 15.x, RELEASE') hostinfo.get_version() assert hostinfo.version == '15.x'
def _get_device_os(self, ip): dev = SnmpHandler(host=ip, version=self.snmp_version, community=self.snmp_community) hostinfo = HostInfo(dev) try: hostinfo.get_version() self.logger.info(f'{ip}: detected os "{hostinfo.os}"') except SnmpError as e: self.logger.error(f'{ip}: snmp-timeout') return False return hostinfo.os
def _get_device_os(self, ip): dev = SnmpHandler(host=ip, version=self.snmp_version, community=self.snmp_community) hostinfo = HostInfo(dev) try: hostinfo.get_version() self.logger.info(f'{ip}: detected os "{hostinfo.os}"') except SnmpError: self.logger.error(f'{ip}: snmp-timeout') return False if self.network_driver != '': self.logger.info(f'{ip}: custom configs overwrites os to: "{self.network_driver}"') return self.network_driver return hostinfo.os
def main(): argparser = SnmpArguments(description) argparser.parser.add_argument( '-d', help="Directory containing yaml version files") args = argparser.parser.parse_nelmon_args() snmp = NelmonSnmp(args) hostinfo = HostInfo(snmp) hostinfo.get_version() if hostinfo.version == 'UNKNOWN': exit_str = ['Unable to determine device version'] exit_str.append('Vendor: %s' % hostinfo.vendor) exit_str.append('OS: %s' % hostinfo.os) exit_str.append('Version: %s' % hostinfo.version) exit_str.append('*******************************') exit_str.append("Your version of Nelsnmp does't support this device") exit_str.append('please check:') exit_str.append('http://networklore.com/nelsnmp-hostinfo/') nelmon_exit(C.UNKNOWN, exit_str) if args.d: yaml_file = '%s/%s_%s.yml' % (args.d, hostinfo.vendor, hostinfo.os) else: nelmon_exit(C.OK, hostinfo.version) if not os.path.isfile(yaml_file): nelmon_exit(C.UNKNOWN, 'Unable to find file: %s' % yaml_file) data = None with open(yaml_file, 'r') as f: data = yaml.load(f.read()) if data is None: nelmon_exit(C.UNKNOWN, 'Unable to parse %s' % yaml_file) declared_versions = [] for policy in data: if data[policy] is not None: for version in data[policy]: declared_versions.append(version) if hostinfo.version == version: match_policy = policy hits = declared_versions.count(hostinfo.version) if hits == 0: nelmon_exit(C.UNKNOWN, '%s not declared in %s' % (hostinfo.version, yaml_file)) elif hits > 1: nelmon_exit( C.UNKNOWN, '%s is declared %s times in %s' % (hostinfo.version, hits, yaml_file)) else: if match_policy == 'approved': if data[match_policy][hostinfo.version]: nelmon_exit( C.OK, '%s - %s' % (hostinfo.version, data[match_policy][hostinfo.version])) else: nelmon_exit(C.OK, hostinfo.version) elif match_policy == 'critical': if data[match_policy][hostinfo.version]: nelmon_exit( C.CRITICAL, '%s - %s (critical)' % (hostinfo.version, data[match_policy][hostinfo.version])) else: nelmon_exit(C.CRITICAL, '%s - (critical)' % hostinfo.version) elif match_policy == 'vulnerable': if data[match_policy][hostinfo.version]: nelmon_exit( C.WARNING, '%s - %s (vulnerable)' % (hostinfo.version, data[match_policy][hostinfo.version])) else: nelmon_exit(C.WARNING, '%s - (warning)' % hostinfo.version) elif match_policy == 'obsolete': if data[match_policy][hostinfo.version]: nelmon_exit( C.WARNING, '%s - %s (obsolete)' % (hostinfo.version, data[match_policy][hostinfo.version])) else: nelmon_exit(C.WARNING, '%s - (obsolete)' % hostinfo.version) else: nelmon_exit( C.UNKNOWN, '%s is under unknown policy: %s' % (hostinfo.version, match_policy)) nelmon_exit(C.OK, hostinfo.version)
if m_args['level'] == "authPriv" and m_args['privacy'] is None: module.fail_json(msg='Privacy algorithm not set when using authPriv') nelsnmp_args = {} for key in m_args: if key in NELSNMP_PARAMETERS and m_args[key] is not None: nelsnmp_args[key] = m_args[key] try: dev = SnmpHandler(**nelsnmp_args) except Exception, err: module.fail_json(msg=str(err)) hostinfo = HostInfo(dev) try: hostinfo.get_version() except Exception, err: module.fail_json(msg=str(err)) results = {} results['ansible_device_vendor'] = hostinfo.vendor results['ansible_device_os'] = hostinfo.os results['ansible_device_version'] = hostinfo.version module.exit_json(ansible_facts=results) main()
if m_args['level'] == "authPriv" and m_args['privacy'] is None: module.fail_json( msg='Privacy algorithm not set when using authPriv') nelsnmp_args = {} for key in m_args: if key in NELSNMP_PARAMETERS and m_args[key] is not None: nelsnmp_args[key] = m_args[key] try: dev = SnmpHandler(**nelsnmp_args) except Exception, err: module.fail_json(msg=str(err)) hostinfo = HostInfo(dev) try: hostinfo.get_version() except Exception, err: module.fail_json(msg=str(err)) results = {} results['ansible_device_vendor'] = hostinfo.vendor results['ansible_device_os'] = hostinfo.os results['ansible_device_version'] = hostinfo.version module.exit_json(ansible_facts=results) main()
def test_hostinfo_get_version_known_vendor(declared_hostinfo): dev = SnmpHandler(host='1.1.1.1', version='2c', community='public') hostinfo = HostInfo(dev, vendor='cisco') hostinfo.get_version() assert hostinfo.version == '9.3(2)2'
def main(): """Plugin: check_version.""" argparser = SnmpArguments(description) argparser.parser.add_argument( '-d', help="Directory containing yaml version files") args = argparser.parser.parse_nelmon_args() snmp = NelmonSnmp(args) hostinfo = HostInfo(snmp) hostinfo.get_version() if hostinfo.version == 'UNKNOWN': exit_str = ['Unable to determine device version'] exit_str.append('Vendor: %s' % hostinfo.vendor) exit_str.append('OS: %s' % hostinfo.os) exit_str.append('Version: %s' % hostinfo.version) exit_str.append('*******************************') exit_str.append("Your version of Nelsnmp does't support this device") exit_str.append('please check:') exit_str.append('http://networklore.com/nelsnmp-hostinfo/') nelmon_exit(C.UNKNOWN, exit_str) if args.d: yaml_file = '%s/%s_%s.yml' % (args.d, hostinfo.vendor, hostinfo.os) else: nelmon_exit(C.OK, hostinfo.version) if not os.path.isfile(yaml_file): nelmon_exit(C.UNKNOWN, 'Unable to find file: %s' % yaml_file) data = None with open(yaml_file, 'r') as f: data = yaml.load(f.read()) if data is None: nelmon_exit(C.UNKNOWN, 'Unable to parse %s' % yaml_file) declared_versions = [] for policy in data: if data[policy] is not None: for version in data[policy]: declared_versions.append(version) if hostinfo.version == version: match_policy = policy hits = declared_versions.count(hostinfo.version) if hits == 0: nelmon_exit( C.UNKNOWN, '%s not declared in %s' % (hostinfo.version, yaml_file)) elif hits > 1: nelmon_exit( C.UNKNOWN, '%s is declared %s times in %s' % (hostinfo.version, hits, yaml_file)) else: if match_policy == 'approved': if data[match_policy][hostinfo.version]: nelmon_exit( C.OK, '%s - %s' % (hostinfo.version, data[match_policy][hostinfo.version]) ) else: nelmon_exit(C.OK, hostinfo.version) elif match_policy == 'critical': if data[match_policy][hostinfo.version]: nelmon_exit( C.CRITICAL, '%s - %s (critical)' % ( hostinfo.version, data[match_policy][hostinfo.version]) ) else: nelmon_exit(C.CRITICAL, '%s - (critical)' % hostinfo.version) elif match_policy == 'vulnerable': if data[match_policy][hostinfo.version]: nelmon_exit( C.WARNING, '%s - %s (vulnerable)' % ( hostinfo.version, data[match_policy][hostinfo.version]) ) else: nelmon_exit(C.WARNING, '%s - (warning)' % hostinfo.version) elif match_policy == 'obsolete': if data[match_policy][hostinfo.version]: nelmon_exit( C.WARNING, '%s - %s (obsolete)' % ( hostinfo.version, data[match_policy][hostinfo.version]) ) else: nelmon_exit(C.WARNING, '%s - (obsolete)' % hostinfo.version) else: nelmon_exit(C.UNKNOWN, '%s is under unknown policy: %s' % ( hostinfo.version, match_policy)) nelmon_exit(C.OK, hostinfo.version)
def test_hostinfo_invalid_parameter(): with pytest.raises(ValueError): dev = 'Not an SnmpHandler' hostinfo = HostInfo(dev) hostinfo.get_version()