Guest User

Untitled

a guest
Jul 13th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. # GETNEXT Command Generator
  2. from pysnmp.entity.rfc3413.oneliner import cmdgen
  3. import pymssql, time
  4. from processing import Pool
  5.  
  6. # max number of threads to run at one time
  7. THREAD_LIMIT = 2
  8.  
  9.  
  10.  
  11. def fetchDevices():
  12. deviceList = {}
  13. numDevices = 0
  14. try:
  15. conn = pymssql.connect(host='SOMEHOST', user='SOMEUSER', password='OMEPASS', database='WhatsUp')
  16. except:
  17. print "MSSQL Server unavilable."
  18. exit()
  19. cur = conn.cursor()
  20. cur.execute('SELECT Device.sDisplayName, NetworkInterface.sNetworkAddress FROM Device INNER JOIN NetworkInterface on Device.nDefaultNetworkInterfaceID = NetworkInterface.nNetworkInterfaceID WHERE (Device.nDeviceTypeID=134)')
  21. row = cur.fetchone()
  22. while row:
  23. #print "ID=%s, Name=%s" % (row[0], row[1])
  24. deviceList[row[0]]= row[1]
  25. row = cur.fetchone()
  26.  
  27. return deviceList
  28.  
  29. def checkDevice(deviceName,deviceAddress,deviceNumber):
  30. print "Checking %s @ %s" % (deviceName, deviceAddress)
  31.  
  32. errorIndication, errorStatus, errorIndex, varBindTable = cmdgen.CommandGenerator().nextCmd(
  33. cmdgen.CommunityData('test-agent', 'public', 0),
  34. cmdgen.UdpTransportTarget((deviceAddress, 161)),
  35. (1,3,6,1,4,1,1429,1,1,2,9,4,2,1,2)
  36. )
  37.  
  38. if errorIndication:
  39. print errorIndication
  40. else:
  41. if errorStatus:
  42. print '%s at %s\n' % (errorStatus.prettyPrint(),varBindTable[-1][int(errorIndex)-1])
  43. else:
  44. for varBindTableRow in varBindTable:
  45. for name, val in varBindTableRow:
  46. if val == 62351:
  47. addoid = (1,3,6,1,4,1,1429,1,1,2,9,4,2,1,25)
  48. useOID = addoid + name[15:]
  49. errorIndication2, errorStatus2, errorIndex2, varBindTable2 = cmdgen.CommandGenerator().getCmd(
  50. cmdgen.CommunityData('test-agent', 'public', 0),
  51. cmdgen.UdpTransportTarget((deviceAddress, 161)),
  52. useOID)
  53. if errorIndication2:
  54. print errorIndication2
  55. else:
  56. if errorStatus2:
  57. print '%s at %s\n' % (errorStatus2.prettyPrint(),varBindTable2[-1][int(errorIndex2)-1])
  58. else:
  59. for varBindTableRow2 in varBindTable2:
  60. if varBindTableRow2[1]==2:
  61. addoid = (1,3,6,1,4,1,1429,1,1,2,9,4,2,1,1)
  62. macOID = addoid + name[15:]
  63. errorIndication3, errorStatus3, errorIndex3, varBindTable3 = cmdgen.CommandGenerator().getCmd(
  64. cmdgen.CommunityData('test-agent', 'public', 0),
  65. cmdgen.UdpTransportTarget((deviceAddress, 161)),
  66. macOID)
  67. if errorIndication3:
  68. print errorIndication3
  69. else:
  70. if errorStatus3:
  71. print "%s at %s\n" % (errorStatus3.prettyPrint(),varBindTable3[-1][int(errorIndex3)-1])
  72. else:
  73. for varBindTableRow3 in varBindTable3:
  74. print "[%s] ALARM: %s, %s, %s" % (deviceNumber,deviceName,deviceAddress,hexify(varBindTableRow3[1]))
  75.  
  76. def hexify( octets ):
  77. return " ".join( [ '%x'%(ord(c)) for c in octets ] )
  78.  
  79.  
  80. # main program goes here
  81.  
  82. #get device list from whatsup
  83. deviceList = fetchDevices()
  84.  
  85. # KNOWN DEVICE WITH ALARMS
  86. #deviceList = {'NEOCL03GQM048': '172.30.52.48'}
  87.  
  88. print "Devices to check: %s" % len(deviceList)
  89.  
  90. deviceCount = 1
  91. tmpCount = 0
  92. tmpLimit = 10
  93.  
  94. if __name__ == '__main__':
  95. pool = Pool(processes=THREAD_LIMIT) # start THREAD_LIMIT worker processes
  96.  
  97. for deviceName,deviceAddress in deviceList.iteritems():
  98. pool.apply_async(checkDevice, (deviceName,deviceAddress,deviceCount))
  99.  
  100. # not sure what these are for.. don't think you need them?
  101. tmpCount += 1
  102. deviceCount +=1
  103. if tmpCount == tmpLimit:
  104. break
  105.  
  106. pool.close()
  107. pool.join()
Add Comment
Please, Sign In to add comment