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.

195 lines
7.7 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):
  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. for line in txt.split("\n"):
  31. if '--CONTROLLER INFORMATION--' in line:
  32. controller = True
  33. if controller and 'Dynamic Storage Accelerator:' in line:
  34. controller = False
  35. elif '--ARRAY INFORMATION--' in line:
  36. array += 1
  37. volume = 0
  38. elif '--VOLUME INFORMATION--' in line:
  39. volume += 1
  40. elif '--END DEVICE INFORMATION--' in line:
  41. device += 1
  42. else:
  43. if controller and 'Name:' in line:
  44. data['data'].append({'#CONTROLLER': "controller-1"})
  45. if array and not volume and 'Name' in line:
  46. data['data'].append({'#ARRAY': 'ARRAY-' + str(array - 1)})
  47. if volume and 'Name' in line:
  48. data['data'].append({'#VOLUME': ('volume-' + str(array - 1) + '.' + str(volume - 1))})
  49. if device and 'ID:' in line:
  50. data['data'].append({'#DISK': 'disk-' + str(device - 1)})
  51. device += 1
  52. return data
  53. def sender(txt):
  54. if not txt:
  55. print(1)
  56. exit(1)
  57. array = 0
  58. arraysec = False
  59. volume = 0
  60. volsec = False
  61. packet = []
  62. controller = False
  63. device = 0
  64. devicesec = False
  65. for line in txt.split("\n"):
  66. if '--CONTROLLER INFORMATION--' in line:
  67. controller = True
  68. if controller and 'Dynamic Storage Accelerator:' in line:
  69. controller = False
  70. elif '--ARRAY INFORMATION--' in line:
  71. array += 1
  72. volume = 0
  73. arraysec = True
  74. volsec = False
  75. elif '--VOLUME INFORMATION--' in line:
  76. volume += 1
  77. arraysec = False
  78. volsec = True
  79. elif '--END DEVICE INFORMATION--' in line:
  80. device += 1
  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. packet.append(ZabbixMetric(hostname,
  132. 'id[{}]'.format('disk-' + str(device - 1)),
  133. line.split(":")[1].strip()))
  134. elif 'Disk Type:' in line:
  135. packet.append(ZabbixMetric(hostname,
  136. 'disktype[{}]'.format('disk-' + str(device - 1)),
  137. line.split(":")[1].strip()))
  138. elif 'State:' in line:
  139. packet.append(ZabbixMetric(hostname,
  140. 'state[{}]'.format('disk-' + str(device - 1)),
  141. line.split(":")[1].strip()))
  142. elif 'Size:' in line:
  143. packet.append(ZabbixMetric(hostname,
  144. 'size[{}]'.format('disk-' + str(device - 1)),
  145. line.split(":")[1].strip().split()[0]))
  146. elif 'Serial Number:' in line:
  147. packet.append(ZabbixMetric(hostname,
  148. 'serial[{}]'.format('disk-' + str(device - 1)),
  149. line.split(":")[1].strip()))
  150. elif 'Model:' in line:
  151. packet.append(ZabbixMetric(hostname,
  152. 'model[{}]'.format('disk-' + str(device - 1)),
  153. line.split(":")[1].strip()))
  154. zbx = ZabbixSender(server)
  155. try:
  156. zbx.send(packet)
  157. except Exception:
  158. print(1)
  159. else:
  160. print(0)
  161. def args():
  162. if len(sys.argv) > 2:
  163. print('too many args')
  164. return False
  165. elif len(sys.argv) == 1:
  166. return 'discovery'
  167. elif len(sys.argv) == 2 and sys.argv[1] == 'send':
  168. return 'sender'
  169. else:
  170. print('wrong args')
  171. return False
  172. def main():
  173. if not args():
  174. exit(0)
  175. elif args() == "discovery":
  176. print(json.dumps(discovery(exec(command)), indent=4, sort_keys=False))
  177. else:
  178. sender(exec(command))
  179. if __name__ == '__main__':
  180. main()

Powered by TurnKey Linux.