You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
7.3 KiB

  1. #! /usr/bin/python3
  2. # coding=utf-8
  3. import subprocess
  4. import json
  5. import sys
  6. from pyzabbix import ZabbixMetric, ZabbixSender
  7. command = 'cat irst.txt'
  8. hostname = 'testhost'
  9. server = '192.168.1.56'
  10. def exec(exe):
  11. result = subprocess.Popen(exe, stdout=subprocess.PIPE, shell=True)
  12. return result.communicate()[0].decode('utf-8')
  13. def discovery(txt):
  14. array = 0
  15. volume = 0
  16. data = {'data': []}
  17. controller = False
  18. device = 0
  19. for line in txt.split("\n"):
  20. if '--CONTROLLER INFORMATION--' in line:
  21. controller = True
  22. if controller and 'Dynamic Storage Accelerator:' in line:
  23. controller = False
  24. elif '--ARRAY INFORMATION--' in line:
  25. array += 1
  26. volume = 0
  27. elif '--VOLUME INFORMATION--' in line:
  28. volume += 1
  29. elif '--END DEVICE INFORMATION--' in line:
  30. device += 1
  31. else:
  32. if controller and 'Name:' in line:
  33. data['data'].append({'#CONTROLLER': "controller-1"})
  34. if array and not volume and 'Name' in line:
  35. data['data'].append({'#ARRAY': 'ARRAY-' + str(array - 1)})
  36. if volume and 'Name' in line:
  37. data['data'].append({'#VOLUME': ('volume-' + str(array - 1) + '.' + str(volume - 1))})
  38. if device and 'ID:' in line:
  39. data['data'].append({'#DISK': 'disk-' + str(device - 1)})
  40. device += 1
  41. return data
  42. def sender(txt):
  43. array = 0
  44. arraysec = False
  45. volume = 0
  46. volsec = False
  47. packet = []
  48. controller = False
  49. device = 0
  50. devicesec = False
  51. for line in txt.split("\n"):
  52. if '--CONTROLLER INFORMATION--' in line:
  53. controller = True
  54. if controller and 'Dynamic Storage Accelerator:' in line:
  55. controller = False
  56. elif '--ARRAY INFORMATION--' in line:
  57. array += 1
  58. volume = 0
  59. arraysec = True
  60. volsec = False
  61. elif '--VOLUME INFORMATION--' in line:
  62. volume += 1
  63. arraysec = False
  64. volsec = True
  65. elif '--END DEVICE INFORMATION--' in line:
  66. device += 1
  67. devicesec = True
  68. arraysec = False
  69. volsec = False
  70. else:
  71. if controller:
  72. if 'Name:' in line:
  73. packet.append(ZabbixMetric(hostname, 'name[controller-1]', line.split(":")[1].strip()))
  74. elif 'Type:' in line:
  75. packet.append(ZabbixMetric(hostname, 'type[controller-1]', line.split(":")[1].strip()))
  76. if arraysec:
  77. if 'Name:' in line:
  78. packet.append(ZabbixMetric(hostname, 'name[{}]'.format('ARRAY-' + str(array - 1)),
  79. line.split(":")[1].strip()))
  80. elif 'Size:' in line:
  81. packet.append(ZabbixMetric(hostname, 'size[{}]'.format('ARRAY-' + str(array - 1)),
  82. line.split(":")[1].strip().split()[0]))
  83. elif 'Free:' in line:
  84. packet.append(ZabbixMetric(hostname, 'free[{}]'.format('ARRAY-' + str(array - 1)),
  85. line.split(":")[1].strip().split()[0]))
  86. elif 'Num Disks:' in line:
  87. packet.append(ZabbixMetric(hostname, 'numdisks[{}]'.format('ARRAY-' + str(array - 1)),
  88. line.split(":")[1].strip()))
  89. elif 'Num Vols:' in line:
  90. packet.append(ZabbixMetric(hostname, 'numvols[{}]'.format('ARRAY-' + str(array - 1)),
  91. line.split(":")[1].strip()))
  92. if volsec:
  93. if 'Name:' in line:
  94. packet.append(ZabbixMetric(hostname,
  95. 'name[{}]'.format('volume-' + str(array - 1) + '.' + str(volume - 1)),
  96. line.split(":")[1].strip()))
  97. elif 'Raid Level:' in line:
  98. packet.append(ZabbixMetric(hostname,
  99. 'raidlvl[{}]'.format('volume-' + str(array - 1) + '.' + str(volume - 1)),
  100. line.split(":")[1].strip()
  101. )
  102. )
  103. elif 'Size:' in line and not "StripeSize" in line:
  104. packet.append(ZabbixMetric(hostname,
  105. 'size[{}]'.format('volume-' + str(array - 1) + '.' + str(volume - 1)),
  106. line.split(":")[1].strip().split()[0]
  107. )
  108. )
  109. elif 'State:' in line:
  110. packet.append(ZabbixMetric(hostname,
  111. 'state[{}]'.format('volume-' + str(array - 1) + '.' + str(volume - 1)),
  112. line.split(":")[1].strip()
  113. )
  114. )
  115. if devicesec:
  116. if 'ID:' in line:
  117. packet.append(ZabbixMetric(hostname,
  118. 'id[{}]'.format('disk-' + str(device - 1)),
  119. line.split(":")[1].strip()))
  120. elif 'Disk Type:' in line:
  121. packet.append(ZabbixMetric(hostname,
  122. 'disktype[{}]'.format('disk-' + str(device - 1)),
  123. line.split(":")[1].strip()))
  124. elif 'State:' in line:
  125. packet.append(ZabbixMetric(hostname,
  126. 'state[{}]'.format('disk-' + str(device - 1)),
  127. line.split(":")[1].strip()))
  128. elif 'Size:' in line:
  129. packet.append(ZabbixMetric(hostname,
  130. 'size[{}]'.format('disk-' + str(device - 1)),
  131. line.split(":")[1].strip().split()[0]))
  132. elif 'Serial Number:' in line:
  133. packet.append(ZabbixMetric(hostname,
  134. 'serial[{}]'.format('disk-' + str(device - 1)),
  135. line.split(":")[1].strip()))
  136. elif 'Model:' in line:
  137. packet.append(ZabbixMetric(hostname,
  138. 'model[{}]'.format('disk-' + str(device - 1)),
  139. line.split(":")[1].strip()))
  140. for val in packet:
  141. print(val)
  142. zbx = ZabbixSender(server)
  143. zbx.send(packet)
  144. def args():
  145. if len(sys.argv) > 2:
  146. print('too many args')
  147. return False
  148. elif len(sys.argv) == 1:
  149. return 'discovery'
  150. elif len(sys.argv) == 2 and sys.argv[1] == 'send':
  151. return 'sender'
  152. else:
  153. print('wrong args')
  154. return False
  155. def main():
  156. if not args():
  157. exit(0)
  158. elif args() == "discovery":
  159. print(json.dumps(discovery(exec(command)), indent=4, sort_keys=False))
  160. else:
  161. sender(exec(command))
  162. if __name__ == '__main__':
  163. main()

Powered by TurnKey Linux.