def _rowstatus_action(self, session, value): if not session: raise ValueError, "no session specified" if self.rowstatus and self.indexoid: vbl = Basetypes.VarBindList() vbl.append(Basetypes.VarBind(self.rowstatus.OID+self.indexoid, self.rowstatus.syntaxobject(value))) session.set(vbl)
def set(self, session=None, **attribs): sess = session or self.session vbl = Basetypes.VarBindList() for colname, value in attribs.items(): col = self.columns[colname] value = col.syntaxobject(value) vbl.append(Basetypes.VarBind(col.OID + self.indexoid, value)) sess.set(vbl)
def createAndWait(self, session, *indexargs, **attribs): vbl = self._create_vbl(*indexargs, **attribs) vbl.append( Basetypes.VarBind( Basetypes.ObjectIdentifier(self.rowstatus.OID + self.indexoid), self.rowstatus.syntaxobject(4))) # create and go session.set(vbl) return self
def get_values(self, *args): sess = self.session if sess and self.indexoid: rv = [] vbl = Basetypes.VarBindList() cols = map(lambda n: self.columns.get(n), args) map(lambda o: vbl.append(Basetypes.VarBind(o.OID+self.indexoid)), cols) return_vbl = sess.get_varbindlist(vbl) return tuple(map(lambda vb: vb.value, return_vbl))
def _create_vbl(self, *indexargs, **attribs): if not self.create: raise ValueError, "Cannot create object of type "+self.__class__.__name__ self.__dict__["indexoid"] = self._make_index_oid(indexargs) vbl = Basetypes.VarBindList() for key, value in attribs.items(): colclass = self.columns[key] value = colclass.syntaxobject(value) vbl.append(Basetypes.VarBind(colclass.OID+self.indexoid, value)) return vbl
def _make_index_oid(self, indexargs): assert len(indexargs) == len(self.index) indexoid = Basetypes.ObjectIdentifier() i = 0 for idx in indexargs[:-1]: indexoid.extend(Basetypes.oid(self.index[i].syntaxobject(idx))) i += 1 end = self.index[i].syntaxobject(indexargs[-1]) end.implied = self.index.implied indexoid.extend(Basetypes.oid(end)) return indexoid
def _decode_a_varbindlist(vbl_tuple): vbl = Basetypes.VarBindList() for [oid, value] in vbl_tuple: obj = _find_object(oid) if obj is not None: if value is not None: if obj.syntaxobject: value = obj.syntaxobject(value) if obj.enumerations: value.enumerations = obj.enumerations vbl.append(Basetypes.VarBind(oid, value, obj)) else: # unknown varbind, deal with it later vbl.append(Basetypes.VarBind(oid, value)) return vbl
def genNotifications(self): self.fo.write("# notifications (traps) \n") for notif in self.smimodule.get_notifications(): initdict = {"OID": repr(Basetypes.ObjectIdentifier(notif.OID))} self.fo.write(genClass(notif, Objects.NotificationObject, initdict)) self._genOIDItem(notif.OID, notif.name)
def decode_oid(length, message): """decode ASN.1 object ID""" oid = [] # get the first subid subid = ord(message[0]) oid.append(subid // 40) oid.append(subid % 40) index = 1 # loop through the rest while index < length: # get a subid subid = ord(message[index]) if subid < 128: oid.append(subid) index = index + 1 else: # construct subid from a number of octets next = subid subid = 0 while next >= 128: # collect subid subid = (subid << 7) + (next & 0x7F) # take next octet index = index + 1 next = ord(message[index]) # just for sure if index > length: return bad_integer # append a subid to oid list subid = (subid << 7) + next oid.append(subid) index = index + 1 # return objid return Basetypes.OBJECT_IDENTIFIER(oid)
def refresh(self, session=None): if (session or self.session) and self.indexoid: sess = session or self.session vbl = Basetypes.VarBindList() map(lambda co: vbl.append(co.varbind.clear()), self.COLUMNS.values()) return_vbl = sess.get_varbindlist(vbl) for vb in return_vbl: self.COLUMNS[vb.Object.__name__].set_value(vb.value)
def genNodes(self): self.fo.write("# nodes\n") for node in self.smimodule.get_nodes(SMI.SMI_NODEKIND_NODE): if node.name: initdict = {} initdict["name"] = repr(node.name) initdict["OID"] = repr(Basetypes.ObjectIdentifier(node.OID)) self.fo.write(genClass(node, Objects.NodeObject, initdict)) self._genOIDItem(node.OID, node.name) self.fo.write("\n")
def genColumns(self): self.fo.write("# columns\n") for col in self.smimodule.get_columns(): initdict = {} initdict["syntaxobject"] = so = self._getSyntax(col) if so.find("Enumeration") >= 0: initdict["enumerations"] = col.syntax.enumerations initdict["OID"] = repr(Basetypes.ObjectIdentifier(col.OID)) self.fo.write(genClass(col, Objects.ColumnObject, initdict)) self.fo.write("\n") self._genOIDItem(col.OID, col.name)
def set(self, val, session=None): sess = session or self.session if self.access != SMI_ACCESS_READ_WRITE: raise RuntimeError("Scalar is not writeable") val = self.syntaxobject(val) try: sess.set_varbind(Basetypes.VarBind(self.OID, val)) except: self.value = None raise else: self.value = val
def get(self, session, indexoid=None): sess = session or self.session if indexoid: self.__dict__["indexoid"] = self._make_index_oid(indexoid) self.__dict__["COLUMNS"] = {} # clear cache with new dict vbl = Basetypes.VarBindList() VB = Basetypes.VarBind map(lambda o: vbl.append(VB(o.OID+self.indexoid)), self.columns.values()) return_vbl = sess.get_varbindlist(vbl) # insert in COLUMNS for vb in return_vbl: vbo = vb.Object colobj = vbo(vb.value, vb.name[len(vbo.OID)+1:]) self.add_column(colobj) return self
def genScalars(self): self.fo.write("# scalars \n") for scalar in self.smimodule.get_scalars(): if scalar.status not in \ (SMI.SMI_STATUS_DEPRECATED, SMI.SMI_STATUS_CURRENT, SMI.SMI_STATUS_MANDATORY): continue # do not expose optional or obsolete objects initdict = {} initdict["syntaxobject"] = so = self._getSyntax(scalar) if so.find("Enumeration") >= 0: initdict["enumerations"] = scalar.syntax.enumerations initdict["OID"] = repr(Basetypes.ObjectIdentifier(scalar.OID)) self.fo.write(genClass(scalar, Objects.ScalarObject, initdict)) self.fo.write("\n") self._genOIDItem(scalar.OID, scalar.name)
def genGroups(self): self.fo.write("# groups \n") for group in self.smimodule.get_groups(): if group.status not in (SMI.SMI_STATUS_CURRENT, SMI.SMI_STATUS_DEPRECATED, SMI.SMI_STATUS_MANDATORY): continue initdict = {} initdict["OID"] = repr(Basetypes.ObjectIdentifier(group.OID)) grouplist = [] for el in group.get_elements(): n = el.get_node() grouplist.append(n.name) initdict["group"] = "[%s]" % ", ".join(grouplist) self.fo.write(genClass(group, Objects.GroupObject, initdict)) self._genOIDItem(group.OID, group.name)
def genRows(self): self.fo.write("# rows \n") for row in self.smimodule.get_rows(): if row.status not in (SMI.SMI_STATUS_DEPRECATED, SMI.SMI_STATUS_CURRENT, SMI.SMI_STATUS_MANDATORY): continue initdict = {} columns = "{%s}" % ", ".join(["%r: %s" % (s, s) for s in self._get_colnames(row)]) initdict["columns"] = columns initdict["index"] = self._genIndexObjects(row) rowstatus = row.rowstatus if rowstatus: initdict["rowstatus"] = row.rowstatus.name initdict["OID"] = repr(Basetypes.ObjectIdentifier(row.OID)) self.fo.write(genClass(row, Objects.RowObject, initdict)) self.fo.write("\n")
def decode_timeticks(length, message): return Basetypes.TimeTicks(_decode_an_unsigned(length, message))
def decode_gauge32(length, message): return Basetypes.Gauge32(_decode_an_unsigned(length, message))
def decode_endofmibview(length, message): return Basetypes.endOfMibView()
def decode_nosuchinstance(length, message): return Basetypes.noSuchInstance()
def decode_unsigned32(length, message): return Basetypes.Unsigned32(_decode_an_unsigned(length, message))
def decode_v1traprequest(length, message): rawpdu = decode_sequence(length, message) assert len(rawpdu) == 6 return Basetypes.SNMPv1TrapPDU(rawpdu[0], rawpdu[1], rawpdu[2], rawpdu[3], rawpdu[4], _decode_a_varbindlist(rawpdu[5]))
def _oid_(self): new = Basetypes.ObjectIdentifier() self[-1].implied = self.implied for obj in self: new.extend(Basetypes.oid(obj)) return new
def get_scalars(self, *args): vbl = Basetypes.VarBindList() for o in [self.scalars[on] for on in args]: vbl.append(Basetypes.VarBind(o.OID+[0])) return_vbl = self.session.get_varbindlist(vbl) return tuple([vb.value for vb in return_vbl])
def decode_nosuchobject(length, message): return Basetypes.noSuchObject()
def varbind(self): return Basetypes.VarBind(self.OID, self.value, self.__class__)
def decode_ipv4(length, message): """decode ASN.1 IP address""" assert length == 4 address = (ord(message[0])<<24) + (ord(message[1])<<16) + \ (ord(message[2])<<8) + ord(message[3]) return Basetypes.IpAddress(address)
def decode_counter64(length, message): return Basetypes.Counter64(_decode_an_unsigned(length, message))
def set(self, value, session=None, indexoid=None): if indexoid: self.set_index(indexoid) sess = session or self.session vb = Basetypes.VarBind(self.OID, self.syntaxobject(value)) return sess.set_varbind(vb)
def decode_boolean(length, message): return Basetypes.Boolean(ord(message[0]))
def _add_vb(self, vb): prefixlen = self.prefixlen col = Basetypes.OID(vb.oid[prefixlen:prefixlen + 1]) row = Basetypes.OID(vb.oid[prefixlen + 1:]) #self.add_by_name(col, row, vb.value) self.set(str(col), str(row), vb.value) # XXX
def _add_vb(self, vb): prefixlen = len(self.OID) col = Basetypes.OID(vb.oid[prefixlen:prefixlen+1]) row = Basetypes.OID(vb.oid[prefixlen+1:]) self.add_by_name(col, row, vb.value)