Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. from pysnmp import hlapi
  2. from influxdb import InfluxDBClient
  3.  
  4.  
  5. def get(target, oids, credentials, port=161, engine=hlapi.SnmpEngine(), context=hlapi.ContextData()):
  6. handler = hlapi.getCmd(
  7. engine,
  8. credentials,
  9. hlapi.UdpTransportTarget((target, port)),
  10. context,
  11. *construct_object_types(oids)
  12. )
  13. return fetch(handler, 1)[0]
  14.  
  15.  
  16. def construct_object_types(list_of_oids):
  17. object_types = []
  18. for oid in list_of_oids:
  19. object_types.append(hlapi.ObjectType(hlapi.ObjectIdentity(oid)))
  20. return object_types
  21.  
  22.  
  23. def fetch(handler, count):
  24. result = []
  25. for i in range(count):
  26. try:
  27. error_indication, error_status, error_index, var_binds = next(handler)
  28. if not error_indication and not error_status:
  29. items = {}
  30. for var_bind in var_binds:
  31. items[str(var_bind[0])] = cast(var_bind[1])
  32. result.append(items)
  33. else:
  34. raise RuntimeError('Got SNMP error: {0}'.format(error_indication))
  35. except StopIteration:
  36. break
  37. return result
  38.  
  39.  
  40. def cast(value):
  41. try:
  42. return int(value)
  43. except (ValueError, TypeError):
  44. try:
  45. return float(value)
  46. except (ValueError, TypeError):
  47. try:
  48. return str(value)
  49. except (ValueError, TypeError):
  50. pass
  51. return value
  52.  
  53.  
  54. KVW_TOTAL = '1.3.6.1.4.1.31034.12.1.1.2.6.1.1.2.0.1' # SCHLEIFENBAUER-DATABUS-MIB::sdbDevInKWhTotal.0.1
  55. KVW_SUBTOTAL = '1.3.6.1.4.1.31034.12.1.1.2.6.1.1.3.0.1' # SCHLEIFENBAUER-DATABUS-MIB::sdbDevInKWhSubtotal.0.1
  56. POWER_FACTOR = '1.3.6.1.4.1.31034.12.1.1.2.6.1.1.4.0.1' # SCHLEIFENBAUER-DATABUS-MIB::sdbDevInPowerFactor.0.1
  57. ACTUAL_CURRENT = '1.3.6.1.4.1.31034.12.1.1.2.6.1.1.5.0.1' # SCHLEIFENBAUER-DATABUS-MIB::sdbDevInActualCurrent.0.1
  58. PEAK_CURRENT = '1.3.6.1.4.1.31034.12.1.1.2.6.1.1.6.0.1' # SCHLEIFENBAUER-DATABUS-MIB::sdbDevInPeakCurrent.0.1
  59. ACTUAL_VOLTAGE = '1.3.6.1.4.1.31034.12.1.1.2.6.1.1.7.0.1' # SCHLEIFENBAUER-DATABUS-MIB::sdbDevInActualVoltage.0.1
  60. MIN_VOLTAGE = '1.3.6.1.4.1.31034.12.1.1.2.6.1.1.8.0.1' # SCHLEIFENBAUER-DATABUS-MIB::sdbDevInMinVoltage.0.1
  61. VOLT_AMPERE = '1.3.6.1.4.1.31034.12.1.1.2.6.1.1.9.0.1' # SCHLEIFENBAUER-DATABUS-MIB::sdbDevInPowerVoltAmpere.0.1
  62. POWER_WATT = '1.3.6.1.4.1.31034.12.1.1.2.6.1.1.10.0.1' # SCHLEIFENBAUER-DATABUS-MIB::sdbDevInPowerWatt.0.1
  63.  
  64. hosts = [
  65. {'host': 'pdu01', 'ip': '10.100.199.1'},
  66. {'host': 'pdu02', 'ip': '10.100.199.2'},
  67. {'host': 'pdu03', 'ip': '10.100.199.3'}
  68. ]
  69.  
  70. oids = [
  71. KVW_TOTAL,
  72. KVW_SUBTOTAL,
  73. POWER_FACTOR,
  74. ACTUAL_CURRENT,
  75. PEAK_CURRENT,
  76. ACTUAL_VOLTAGE,
  77. MIN_VOLTAGE,
  78. VOLT_AMPERE,
  79. POWER_WATT,
  80. ]
  81. community = '5up3r5ecr3t'
  82.  
  83. host = 'localhost'
  84. port = 8086
  85. user = 'root'
  86. password = 'root'
  87. dbname = 'mydb'
  88.  
  89. client = InfluxDBClient(host, port, user, password, dbname)
  90.  
  91. while True:
  92.  
  93. for host in hosts:
  94. try:
  95. data = get(host['ip'], oids, hlapi.CommunityData(community))
  96.  
  97. json_body = [
  98. {
  99. 'measurement': 'pdu',
  100. 'tags': {
  101. 'host': host.get('host'),
  102. },
  103. 'fields': {
  104. "kwh_total" : data.get(KVW_TOTAL),
  105. "kwh_subtotal": data.get(KVW_SUBTOTAL),
  106. "power_factor": int(data.get(POWER_FACTOR)) / 100,
  107. "peak_current": int(data.get(PEAK_CURRENT)) / 100,
  108. sleep(10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement