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

Powered by TurnKey Linux.