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.

169 lines
4.6 KiB

2 years ago
  1. #! /usr/bin/env python
  2. import subprocess
  3. import sys
  4. import os
  5. import time
  6. ld_file = "/tmp/megacli-logical.out"
  7. pd_file = "/tmp/megacli-physical.out"
  8. ld_cmd = "cat /opt/py/megacli-logical.out"
  9. pd_cmd = "cat /opt/py/megacli-physical.out"
  10. pd_dict = dict(slot='Slot Number', port_stat='Port status', size='Raw Size', data='Inquiry Data',
  11. temp='Drive Temperature ', smart_err='Drive has flagged a S.M.A.R.T alert',
  12. manufacter='Inquiry Data', model='Inquiry Data', serial='Inquiry Data')
  13. ld_dict = dict(num='Virtual Drive:', raid_lvl='RAID Level ', size='Size', state='State', pd_count='Number Of Drives')
  14. def f_ld_discover(cli_out):
  15. drives = []
  16. linesplit = ''
  17. for line in cli_out:
  18. if 'Virtual Drive:' in line:
  19. linesplit = line.split(' ')
  20. drives.append(str(linesplit[2]))
  21. return drives
  22. def f_pd_discover(cli_out):
  23. drives = []
  24. linesplit = ''
  25. for line in cli_out:
  26. if 'Slot Number:' in line:
  27. linesplit = line.split(' ')
  28. drives.append(str(linesplit[2]))
  29. return drives
  30. def f_json_print(inp, name):
  31. first = True
  32. print("{")
  33. print("\t\"data\":[")
  34. for key in inp:
  35. if not first:
  36. sys.stdout.write(",")
  37. else:
  38. first = False
  39. sys.stdout.write("\n\t\t{\"{#" + name + "}\":\"" + key + "\"}")
  40. print("\n\t]")
  41. print("}")
  42. def f_read_file(path):
  43. cli_out = []
  44. try:
  45. f = open(path, 'r')
  46. except Exception:
  47. print(Exception)
  48. return []
  49. else:
  50. for line in f.readlines():
  51. cli_out.append(line.strip())
  52. f.close()
  53. return cli_out
  54. def f_read_cli(cmd, outfile):
  55. outdata, stderr = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True).communicate()
  56. try:
  57. f = open(outfile, 'w')
  58. except Exception:
  59. return 1
  60. else:
  61. f.write(outdata)
  62. f.close()
  63. return 0
  64. def f_get_pd_val(slot, val, source):
  65. data = f_read_file(source)
  66. k = 0
  67. out = ""
  68. for line in data:
  69. if pd_dict['slot'] in line:
  70. # print(line.strip().split(':')[1].strip())
  71. if line.strip().split(':')[1].strip() == slot:
  72. k = 1
  73. if k == 0:
  74. continue
  75. if k == 1:
  76. if pd_dict[val] in line:
  77. out = line.strip().split(':')[1].strip().split()
  78. if out != "":
  79. break
  80. if len(out) == 1 and out[0].isdigit():
  81. return out[0]
  82. elif val == 'smart_err':
  83. if out[0] == 'No':
  84. return '0'
  85. else:
  86. return '1'
  87. elif val == "manufacter":
  88. return out[0].strip()
  89. elif val == "model":
  90. return out[1].strip()
  91. elif val == "serial":
  92. return out[2].strip()
  93. elif val == 'size':
  94. return out[0]
  95. elif val == 'port_stat':
  96. if out[0] == 'Active':
  97. return '1'
  98. else:
  99. return '0'
  100. elif val == 'temp':
  101. return out[0][:-1]
  102. def f_get_ld_val(num, val, source):
  103. data = f_read_file(source)
  104. k = 0
  105. out = ""
  106. for line in data:
  107. if ld_dict['num'] in line:
  108. if line.split(':')[1].strip().split()[0].strip() == num:
  109. k = 1
  110. if k == 0:
  111. continue
  112. if k == 1:
  113. if ld_dict[val] in line:
  114. out = line.split(':')[1].strip()
  115. if out != "":
  116. break
  117. if val == 'raid_lvl':
  118. return out
  119. elif val == 'size':
  120. return out.split()[0]
  121. elif val == 'state':
  122. if out.strip() == 'Optimal':
  123. return '0'
  124. else:
  125. return '1'
  126. elif val == 'pd_count':
  127. return out.strip()
  128. return out
  129. if __name__ == '__main__':
  130. if len(sys.argv) == 1 or len(sys.argv) == 3 or len(sys.argv) > 4:
  131. print("Wrong args. Cache will be updated if necessary")
  132. stat_pd = os.stat(pd_file)
  133. stat_ld = os.stat(ld_file)
  134. if (float(time.time()) - float(stat_pd.st_ctime)) > 50:
  135. f_read_cli(pd_cmd, pd_file)
  136. if (float(time.time()) - float(stat_ld.st_ctime)) > 50:
  137. f_read_cli(ld_cmd, ld_file)
  138. if len(sys.argv) == 2:
  139. if sys.argv[1] == "ld":
  140. f_json_print(f_ld_discover(f_read_file(ld_file)), "LD")
  141. if sys.argv[1] == "pd":
  142. f_json_print(f_pd_discover(f_read_file(pd_file)), "PD")
  143. elif len(sys.argv) == 4:
  144. if sys.argv[1] == 'pd':
  145. if sys.argv[3] in pd_dict.keys():
  146. print(f_get_pd_val(sys.argv[2], sys.argv[3], pd_file))
  147. if sys.argv[1] == 'ld':
  148. if sys.argv[3] in ld_dict.keys():
  149. print(f_get_ld_val(sys.argv[2], sys.argv[3], ld_file))

Powered by TurnKey Linux.