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_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 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_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
class SNMPSession: def __init__(self, host, community, version=2): self.session = None self.host = host self.community = community self.version = version def connect(self): self.session = Session( DestHost=self.host, Community=self.community, Version=self.version, UseNumeric=1, ) def walk(self, oid, stripoid=True): if not self.session: self.connect() vb = Varbind(oid) vl = VarList(vb) self.session.walk(vl) ret = {} for v in vl: if v.tag is None: continue full_oid = v.tag if v.iid or v.iid == 0: full_oid = v.tag + "." + v.iid if stripoid: full_oid = full_oid.replace(oid, '') full_oid = full_oid.replace(".", '') ret[full_oid] = v.val return ret def get(self, oid): if not self.session: self.connect() return self.session.get(oid)[0]
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
class SNMPSession: def __init__(self, host, community, version=2, timeout=2000000, retries=0, max_repetitions=49): self.session = None self.host = host self.community = community self.version = version self.timeout = timeout self.retries = retries self.max_rep = max_repetitions 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 walk(self, oid, stripoid=True, expect_str=False): vb = Varbind(oid) vl = VarList(vb) self.session.walk(vl) ret = {} for v in vl: if v.tag is None: continue full_oid = v.tag (full_oid, val) = handle_vb(v, expect_str) if stripoid: full_oid = full_oid.replace(oid + ".", '') ret[full_oid] = val return ret def bulkwalk(self, oid, stripoid=True, startidx=None, endidx=None, expect_str=False): ret = {} if oid[0] != ".": oid = "." + oid startindexpos = startidx runningtreename = oid stop = False while (runningtreename.startswith(oid) and stop is False): vrs = VarList(Varbind(runningtreename, startindexpos)) result = self.session.getbulk(0, self.max_rep, vrs) if self.session.ErrorInd: logging.warn("walk failed on: {0} ({1})".format( self.host, self.session.ErrorStr)) key = None if not result: logging.warn("got no result: %s %s", self.host, oid) break """ Print output from running getbulk""" for i in vrs: if endidx and int(i.iid) > int(endidx): stop = True break if not i.tag.startswith(oid): break (full_oid, val) = handle_vb(i, expect_str) key = full_oid.replace(oid + ".", "") if stripoid: ret[key] = val else: ret[full_oid] = val """ Set startindexpos for next getbulk """ if not vrs[-1].iid or not key or stop: break startindexpos = vrs[-1].iid """ Refresh runningtreename from last result""" runningtreename = vrs[-1].tag return ret def get(self, oid): return self.session.get(oid)[0]