def detect(self): ret = {} from zdcp.SettingsContainer import SC from netsnmp import VarList, Varbind, Session try: # .1.3.6.1.2.1.1.1.0 : Device info # .1.3.6.1.2.1.1.5.0 : Device name devobjs = VarList(Varbind('.1.3.6.1.2.1.1.1.0'), Varbind('.1.3.6.1.2.1.1.5.0')) session = Session(Version = 2, DestHost = self._ip, Community = SC['snmp']['read_community'], UseNumeric = 1, Timeout = 100000, Retries = 2) session.get(devobjs) ret['result'] = "OK" if (session.ErrorInd == 0) else "NOT_OK" except Exception as err: ret['snmp'] = "Not able to do SNMP lookup (check snmp -> read_community): %s"%str(err) else: ret['info'] = {'model':'unknown', 'type':'generic','snmp':devobjs[1].val.lower() if devobjs[1].val else 'unknown'} if devobjs[0].val: infolist = devobjs[0].val.split() if infolist[0] == "Juniper": if infolist[1] == "Networks,": ret['info']['model'] = infolist[3].lower() for tp in [ 'ex', 'srx', 'qfx', 'mx', 'wlc' ]: if tp in ret['info']['model']: ret['info']['type'] = tp break else: subinfolist = infolist[1].split(",") ret['info']['model'] = subinfolist[2] elif infolist[0] == "VMware": ret['info']['model'] = "esxi" ret['info']['type'] = "esxi" elif infolist[0] == "Linux": ret['info']['model'] = 'debian' if "Debian" in devobjs[0].val else 'generic' else: ret['info']['model'] = " ".join(infolist[0:4]) return ret
def get_inventory(self): from netsnmp import VarList, Varbind, Session result = [] try: outletobjs = VarList(Varbind('.1.3.6.1.4.1.10418.17.2.5.5.1.4')) stateobjs = VarList(Varbind('.1.3.6.1.4.1.10418.17.2.5.5.1.5')) slotobjs = VarList(Varbind('.1.3.6.1.4.1.10418.17.2.5.3.1.3')) session = Session(Version=2, DestHost=self._ip, Community=SC['snmp']['read_community'], UseNumeric=1, Timeout=100000, Retries=2) session.walk(outletobjs) session.walk(stateobjs) session.walk(slotobjs) slotdict = dict(map(lambda var: (var.iid, var.val), slotobjs)) for indx, outlet in enumerate(outletobjs, 0): result.append({ 'slot': outlet.tag[34:], 'unit': outlet.iid, 'name': outlet.val, 'state': Device.get_outlet_state(stateobjs[indx].val), 'slotname': slotdict.get(outlet.tag[34:], "unknown") }) except Exception as exception_error: self.log_msg("Avocent : error loading conf " + str(exception_error)) return result
def interfaces(self): from sdcp.SettingsContainer import SC from netsnmp import VarList, Varbind, Session interfaces = {} try: objs = VarList(Varbind('.1.3.6.1.2.1.2.2.1.2'), Varbind('.1.3.6.1.2.1.31.1.1.1.18')) session = Session(Version=2, DestHost=self._ip, Community=SC['snmp']['read_community'], UseNumeric=1, Timeout=100000, Retries=2) session.walk(objs) for entry in objs: intf = interfaces.get(int(entry.iid), { 'name': "None", 'description': "None" }) if entry.tag == '.1.3.6.1.2.1.2.2.1.2': intf['name'] = entry.val if entry.tag == '.1.3.6.1.2.1.31.1.1.1.18': intf[ 'description'] = entry.val if entry.val != "" else "None" interfaces[int(entry.iid)] = intf except Exception as exception_error: self.log_msg("Generic : error traversing interfaces: " + str(exception_error)) return interfaces
def get_vm_list(self, aSort=None): # aSort = 'id' or 'name' from netsnmp import VarList, Varbind, Session statelist = [] try: vmnameobjs = VarList(Varbind('.1.3.6.1.4.1.6876.2.1.1.2')) vmstateobjs = VarList(Varbind('.1.3.6.1.4.1.6876.2.1.1.6')) session = Session(Version=2, DestHost=self._ip, Community=SC['snmp']['read_community'], UseNumeric=1, Timeout=100000, Retries=2) session.walk(vmnameobjs) session.walk(vmstateobjs) for indx, result in enumerate(vmnameobjs): statetuple = { 'id': result.iid, 'name': result.val, 'state': vmstateobjs[indx].val, 'state_id': Device.get_state_str(vmstateobjs[indx].val) } statelist.append(statetuple) except: pass if aSort: statelist.sort(key=lambda x: x[aSort]) return statelist
def get_vm_state(self, aid): from netsnmp import VarList, Varbind, Session try: vmstateobj = VarList(Varbind(".1.3.6.1.4.1.6876.2.1.1.6." + str(aid))) session = Session(Version = 2, DestHost = self._ip, Community = SC['snmp']['read_community'], UseNumeric = 1, Timeout = 100000, Retries = 2) session.get(vmstateobj) return vmstateobj[0].val except: pass return "unknown"
def get_vm_id(self, aname): from netsnmp import VarList, Varbind, Session try: vmnameobjs = VarList(Varbind('.1.3.6.1.4.1.6876.2.1.1.2')) session = Session(Version = 2, DestHost = self._ip, Community = SC['snmp']['read_community'], UseNumeric = 1, Timeout = 100000, Retries = 2) session.walk(vmnameobjs) for result in vmnameobjs: if result.val == aname: return int(result.iid) except: pass return -1
def connect(self): #resolve hostname try: ip = gethostbyname(self.host) except: logging.debug('Could not resolve', self.host, 'connection failed') self.session = None return None self.session = Session( DestHost=ip, Community=self.community, Version=self.version, UseNumeric=1, Timeout=self.timeout, Retries=self.retries, )
def detect(aDict): """Function docstring for detect TBD. TODO -> make this a function of generic device instead !!! ZEB Args: - ip (required) Output: """ from os import system if system("ping -c 1 -w 1 {} > /dev/null 2>&1".format(aDict['ip'])) != 0: return {'result':'NOT_OK', 'info':'Not pingable' } ret = {'result':'OK'} from netsnmp import VarList, Varbind, Session try: # .1.3.6.1.2.1.1.1.0 : Device info # .1.3.6.1.2.1.1.5.0 : Device name devobjs = VarList(Varbind('.1.3.6.1.2.1.1.1.0'), Varbind('.1.3.6.1.2.1.1.5.0')) session = Session(Version = 2, DestHost = aDict['ip'], Community = SC['snmp']['read_community'], UseNumeric = 1, Timeout = 100000, Retries = 2) session.get(devobjs) except Exception as err: ret['snmp'] = "Not able to do SNMP lookup (check snmp -> read_community): %s"%str(err) ret['info'] = {'model':'unknown', 'type':'generic','snmp':devobjs[1].val.lower() if devobjs[1].val else 'unknown'} if devobjs[0].val: infolist = devobjs[0].val.split() if infolist[0] == "Juniper": if infolist[1] == "Networks,": ret['info']['model'] = infolist[3].lower() for tp in [ 'ex', 'srx', 'qfx', 'mx', 'wlc' ]: if tp in ret['info']['model']: ret['info']['type'] = tp break else: subinfolist = infolist[1].split(",") ret['info']['model'] = subinfolist[2] elif infolist[0] == "VMware": ret['info']['model'] = "esxi" ret['info']['type'] = "esxi" elif infolist[0] == "Linux": ret['info']['model'] = 'debian' if "Debian" in devobjs[0].val else 'generic' else: ret['info']['model'] = " ".join(infolist[0:4]) return ret
def get_slot_names(self): from netsnmp import VarList, Varbind, Session slots = [] try: slotobjs = VarList(Varbind('.1.3.6.1.4.1.10418.17.2.5.3.1.3')) session = Session(Version=2, DestHost=self._ip, Community=SC['snmp']['read_community'], UseNumeric=1, Timeout=100000, Retries=2) session.walk(slotobjs) for slot in slotobjs: slots.append([slot.iid, slot.val]) except Exception as exception_error: self.log_msg("Avocent : error loading pdu member names " + str(exception_error)) return slots
def get_state(self, slot, unit): from netsnmp import VarList, Varbind, Session try: stateobj = VarList( Varbind(".1.3.6.1.4.1.10418.17.2.5.5.1.5.1.{}.{}".format( slot, unit))) session = Session(Version=2, DestHost=self._ip, Community=SC['snmp']['read_community'], UseNumeric=1, Timeout=100000, Retries=2) session.get(stateobj) return { 'res': 'OK', 'state': Device.get_outlet_state(stateobj[0].val) } except Exception as e: self.log_msg("Avocent : error getting state:" + str(e)) return {'res': 'NOT_OK', 'info': str(e), 'state': 'unknown'}
def set_name(self, slot, unit, name): from netsnmp import VarList, Varbind, Session try: name = name[:16] session = Session(Version=2, DestHost=self._ip, Community=SC['snmp']['write_community'], UseNumeric=1, Timeout=100000, Retries=2) setobj = VarList( Varbind("enterprises", "10418.17.2.5.5.1.4.1.{}.{}".format(slot, unit), name, "OPAQUE")) session.set(setobj) return "{0}.{1}:'{2}'".format(slot, unit, name) except Exception as exception_error: self.log_msg("Avocent : error setting name " + str(exception_error)) return "Error setting name '%s'" % (name)
def set_state(self, slot, unit, state): from netsnmp import VarList, Varbind, Session try: session = Session(Version=2, DestHost=self._ip, Community=SC['snmp']['write_community'], UseNumeric=1, Timeout=100000, Retries=2) setobj = VarList( Varbind("enterprises", "10418.17.2.5.5.1.6.1.{}.{}".format(slot, unit), Device.set_outlet_state(state), "INTEGER")) session.set(setobj) self.log_msg("Avocent : {0} set state to {1} on {2}.{3}".format( self._ip, state, slot, unit)) return {'res': 'OK'} except Exception as exception_error: self.log_msg("Avocent : error setting state " + str(exception_error)) return {'res': 'NOT_OK', 'info': str(exception_error)}
def get_inventory(self): from netsnmp import VarList, Varbind, Session from sdcp.SettingsContainer import SC result = [] try: portobjs = VarList(Varbind('.1.3.6.1.4.1.25049.17.2.1.2')) session = Session(Version=2, DestHost=self._ip, Community=SC['snmp']['read_community'], UseNumeric=1, Timeout=100000, Retries=2) session.walk(portobjs) for obj in portobjs: result.append({ 'interface': obj.iid, 'name': obj.val, 'port': str(6000 + int(obj.iid)) }) except Exception as exception_error: self.log_msg("OpenGear : error loading conf " + str(exception_error)) return result
def switch_table(self): from socket import gethostbyaddr try: # Length of below is used to offset ip address (32) + 1 and mac base (33) + 1 cssidobjs = VarList(Varbind(".1.3.6.1.4.1.14525.4.4.1.1.1.1.15")) cipobjs = VarList(Varbind(".1.3.6.1.4.1.14525.4.4.1.1.1.1.4")) from sdcp.SettingsContainer import SC session = Session(Version=2, DestHost=self._ip, Community=SC['snmp']['read_community'], UseNumeric=1, Timeout=100000, Retries=2) session.walk(cssidobjs) session.walk(cipobjs) except: return ipdict = dict(map(lambda res: (res.tag[33:], res.val), cipobjs)) ret = [] for res in cssidobjs: macbase = res.tag[34:] mac = (macbase + "." + res.iid).split(".") mac = ":".join(map(lambda x: hex(int(x))[2:], mac)) try: clientname = gethostbyaddr(ipdict[macbase])[0] except: clientname = "unknown" ret.append({ 'Name': clientname, 'IP': ipdict.get(macbase), 'MAC': mac, 'SSID': res.val }) return ret