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.

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

Powered by TurnKey Linux.