import os, os.path, sys, commands, string, time ### Change the following lines to suit your hosts and interfaces hostIntf = { "192.168.1.11" : 19, "192.168.1.12" : 18415 } ### snmpBaseCmd = "snmpget -v 1 -c public" snmpIntfIn = "interfaces.ifTable.ifEntry.ifInOctets" snmpIntfOut = "interfaces.ifTable.ifEntry.ifOutOctets" cachefile = "bixsnmp_cache.txt" cachemap = { } hash = { } def loadCacheMap(): if not os.path.exists(cachefile): f = open(cachefile, "w") f.close() if not os.path.exists(cachefile): return False; f = open(cachefile, "r") for line in f.readlines(): columns = string.split(string.strip(line, "\r\n"), " : ") cachemap[columns[0]] = columns[1] # for element in cachemap: # print "[" + element + "]" + " [" + cachemap[element] + "]" f.close() return True; def writeCacheMap(): f = open(cachefile, "w+") for element in hash: f.write(element + " : " + hash[element] + "\r\n") f.close() return True; output = commands.getstatusoutput("snmpget --version") if output[0] == 0: loadCacheMap() try: elapsed = int(hash["Timestamp"]) - int(cachemap["Timestamp"]) except: elapsed = 0 if elapsed == 0: elapsed = 1 for host, intf in hostIntf.iteritems(): print "host : " + host print "interface : " + str(intf) key = host + "." + str(intf) cmd = snmpBaseCmd + " " + host + " " + snmpIntfIn + "." + str(intf) txtout = commands.getstatusoutput(cmd); # print cmd # print txtout[1] elements = string.split(txtout[1]) print "incounter : " + elements[3] value = elements[3] + "," ocin = int(elements[3]) cmd = snmpBaseCmd + " " + host + " " + snmpIntfOut + "." + str(intf) txtout = commands.getstatusoutput(cmd); elements = string.split(txtout[1]) print "outcounter : " + elements[3] value = value + elements[3] ocout = int(elements[3]) hash[key] = value try: columns = string.split(cachemap[key], ",") except: columns = { "0" : "0,0" } ocinold = 0 ocoutold = 0 if(len(columns) == 2): ocinold = int(columns[0]) ocoutold = int(columns[1]) else: ocinold = ocin ocoutold = ocout print "inrate : " + str(int((ocin - ocinold) / elapsed)) print "outrate : " + str(int((ocout - ocoutold) / elapsed)) print "" hash["Timestamp"] = str(time.time()) writeCacheMap() else: print "snmpget not installed"