Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # GETNEXT Command Generator
- from pysnmp.entity.rfc3413.oneliner import cmdgen
- import pymssql, time
- from processing import Pool
- # max number of threads to run at one time
- THREAD_LIMIT = 2
- def fetchDevices():
- deviceList = {}
- numDevices = 0
- try:
- conn = pymssql.connect(host='SOMEHOST', user='SOMEUSER', password='OMEPASS', database='WhatsUp')
- except:
- print "MSSQL Server unavilable."
- exit()
- cur = conn.cursor()
- cur.execute('SELECT Device.sDisplayName, NetworkInterface.sNetworkAddress FROM Device INNER JOIN NetworkInterface on Device.nDefaultNetworkInterfaceID = NetworkInterface.nNetworkInterfaceID WHERE (Device.nDeviceTypeID=134)')
- row = cur.fetchone()
- while row:
- #print "ID=%s, Name=%s" % (row[0], row[1])
- deviceList[row[0]]= row[1]
- row = cur.fetchone()
- return deviceList
- def checkDevice(deviceName,deviceAddress,deviceNumber):
- print "Checking %s @ %s" % (deviceName, deviceAddress)
- errorIndication, errorStatus, errorIndex, varBindTable = cmdgen.CommandGenerator().nextCmd(
- cmdgen.CommunityData('test-agent', 'public', 0),
- cmdgen.UdpTransportTarget((deviceAddress, 161)),
- (1,3,6,1,4,1,1429,1,1,2,9,4,2,1,2)
- )
- if errorIndication:
- print errorIndication
- else:
- if errorStatus:
- print '%s at %s\n' % (errorStatus.prettyPrint(),varBindTable[-1][int(errorIndex)-1])
- else:
- for varBindTableRow in varBindTable:
- for name, val in varBindTableRow:
- if val == 62351:
- addoid = (1,3,6,1,4,1,1429,1,1,2,9,4,2,1,25)
- useOID = addoid + name[15:]
- errorIndication2, errorStatus2, errorIndex2, varBindTable2 = cmdgen.CommandGenerator().getCmd(
- cmdgen.CommunityData('test-agent', 'public', 0),
- cmdgen.UdpTransportTarget((deviceAddress, 161)),
- useOID)
- if errorIndication2:
- print errorIndication2
- else:
- if errorStatus2:
- print '%s at %s\n' % (errorStatus2.prettyPrint(),varBindTable2[-1][int(errorIndex2)-1])
- else:
- for varBindTableRow2 in varBindTable2:
- if varBindTableRow2[1]==2:
- addoid = (1,3,6,1,4,1,1429,1,1,2,9,4,2,1,1)
- macOID = addoid + name[15:]
- errorIndication3, errorStatus3, errorIndex3, varBindTable3 = cmdgen.CommandGenerator().getCmd(
- cmdgen.CommunityData('test-agent', 'public', 0),
- cmdgen.UdpTransportTarget((deviceAddress, 161)),
- macOID)
- if errorIndication3:
- print errorIndication3
- else:
- if errorStatus3:
- print "%s at %s\n" % (errorStatus3.prettyPrint(),varBindTable3[-1][int(errorIndex3)-1])
- else:
- for varBindTableRow3 in varBindTable3:
- print "[%s] ALARM: %s, %s, %s" % (deviceNumber,deviceName,deviceAddress,hexify(varBindTableRow3[1]))
- def hexify( octets ):
- return " ".join( [ '%x'%(ord(c)) for c in octets ] )
- # main program goes here
- #get device list from whatsup
- deviceList = fetchDevices()
- # KNOWN DEVICE WITH ALARMS
- #deviceList = {'NEOCL03GQM048': '172.30.52.48'}
- print "Devices to check: %s" % len(deviceList)
- deviceCount = 1
- tmpCount = 0
- tmpLimit = 10
- if __name__ == '__main__':
- pool = Pool(processes=THREAD_LIMIT) # start THREAD_LIMIT worker processes
- for deviceName,deviceAddress in deviceList.iteritems():
- pool.apply_async(checkDevice, (deviceName,deviceAddress,deviceCount))
- # not sure what these are for.. don't think you need them?
- tmpCount += 1
- deviceCount +=1
- if tmpCount == tmpLimit:
- break
- pool.close()
- pool.join()
Add Comment
Please, Sign In to add comment