Posted By

mattsn0w on 01/22/11


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Sereniel


host load check output to TWiKi formatted Text


 / Published in: Python
 

  1. import sys, time
  2. from subprocess import Popen, PIPE, STDOUT
  3.  
  4. startTime = time.time()
  5. br = '%BR%'
  6.  
  7. HostsA = 'ahost1', 'ahost2', 'ahost3'
  8. Hostsb = 'bhost1', 'bhost2', 'bhost3'
  9.  
  10. def stripSplitOut(cmdResult):
  11. cleanResult = []
  12. for line in cmdResult:
  13. line = line.strip('\n')
  14. line = line.strip()
  15. line = line.split()
  16. cleanResult.append(line[0])
  17. return cleanResult
  18. #End of Function
  19.  
  20.  
  21. def colorLoad(lval):
  22. lval_float = float(lval)
  23. result = None
  24. if lval_float < float('5'):
  25. result = ' %sGREEN%s %s %sENDCOLOR%s ' % ('%', '%', lval, '%', '%')
  26. elif lval_float > float('5') and lval_float < float('10'):
  27. result = ' %sBLUE%s %s %sENDCOLOR%s ' % ('%', '%', lval, '%', '%')
  28. elif lval_float > float('10') and lval_float < float('20'):
  29. result = ' %sPURPLE%s %s %sENDCOLOR%s ' % ('%', '%', lval, '%', '%')
  30. elif lval_float > float('20'):
  31. result = ' %sRED%s *%s* %sENDCOLOR%s ' % ('%', '%', lval, '%', '%')
  32. return result
  33.  
  34.  
  35. def getLoad(host):
  36. users = None
  37. one = None
  38. five = None
  39. fifteen = None
  40. h_line = None
  41. cmd = '''ssh -q %s "uptime" ''' % (host)
  42. proc = Popen([cmd], shell=True, stdout=PIPE, stderr=STDOUT)
  43. proc.wait()
  44. proc.returncode
  45. raw_result = proc.stdout
  46.  
  47. if proc.returncode is 0:
  48. for line in raw_result:
  49. h_line = line.strip('\n')
  50.  
  51. data_result = h_line.split(' ')
  52. clean_result = []
  53.  
  54. for item in data_result:
  55. if len(item) > 0:
  56. item = item.strip(',')
  57. clean_result.append(item)
  58.  
  59. if len(clean_result) is 13:
  60. one = colorLoad(clean_result[10])
  61. five = colorLoad(clean_result[11])
  62. fifteen = colorLoad(clean_result[12])
  63. elif len(clean_result) is 12:
  64. one = colorLoad(clean_result[9])
  65. five = colorLoad(clean_result[10])
  66. fifteen = colorLoad(clean_result[11])
  67. elif len(clean_result) is 11:
  68. one = colorLoad(clean_result[8])
  69. five = colorLoad(clean_result[9])
  70. fifteen = colorLoad(clean_result[10])
  71. elif len(clean_result) is 10:
  72. one = colorLoad(clean_result[7])
  73. five = colorLoad(clean_result[8])
  74. fifteen = colorLoad(clean_result[9])
  75. final_result = [ host, one, five, fifteen ]
  76.  
  77. elif proc.returncode is not 0:
  78. final_result = [ host, 'null', 'null', 'null' ]
  79. for line in raw_result:
  80. print line
  81.  
  82. return final_result
  83. # End Of Function
  84.  
  85. def getUserCount(host):
  86. h_line = None
  87. userList = []
  88. cmd = '''ssh -q %s "who | awk '{ print \$1 }' |sort -u | grep -v msnow" ''' % (host)
  89. proc = Popen([cmd], shell=True, stdout=PIPE, stderr=STDOUT)
  90. proc.wait()
  91. proc.returncode
  92. lines = proc.stdout.readlines()
  93. if proc.returncode is 0:
  94. for line in lines:
  95. h_line = line.strip('\n')
  96. if h_line:
  97. userList.append(h_line)
  98. elif not h_line:
  99. pass
  100. userCountResult = [ len(userList) ]
  101. elif proc.returncode is not 0:
  102. userCountResult = [ 'NA' ]
  103. for line in lines:
  104. print line
  105. return userCountResult
  106. # End Of Function
  107.  
  108. def getCores(host):
  109. clean_lines = []
  110. cmd = '''ssh -q %s "grep proc /proc/cpuinfo"''' % (host)
  111. proc = Popen([cmd], shell=True, stdout=PIPE, stderr=STDOUT)
  112. proc.wait()
  113. proc.returncode
  114.  
  115. if proc.returncode is 0:
  116. lines = proc.stdout.readlines()
  117. for line in lines:
  118. line = line.strip('\n')
  119. clean_lines.append(line)
  120. cores = [ len(clean_lines)/2 ]
  121. if cores[0] is 4:
  122. cores = [len(clean_lines) ]
  123. elif proc.returncode is not 0:
  124. cores = [0]
  125. return cores
  126. # End Of Function
  127.  
  128. def getMemStats(host):
  129. cmd = '''ssh -q %s "vmstat -s| egrep -e'total.mem|free.mem|used.swap'" ''' % (host)
  130. proc = Popen([cmd], shell=True, stdout=PIPE, stderr=STDOUT)
  131. proc.wait()
  132. proc.returncode
  133.  
  134. if proc.returncode is 0:
  135. lines = proc.stdout.readlines()
  136. memStatResult = stripSplitOut(lines)
  137. elif proc.returncode is not 0:
  138. memStatResult = [ 'mem:N/A' ]
  139.  
  140. return memStatResult
  141. # End Of Function
  142.  
  143. def getDiskStats(host):
  144. cmd = '''ssh -q %s "df -h /scratch 2>/dev/null | grep -v Filesystem; if [ \$? -ne 0 ]; then df -h / | grep -v Filesystem; fi "''' % (host)
  145. proc = Popen([cmd], shell=True, stdout=PIPE, stderr=STDOUT)
  146. proc.wait()
  147. proc.returncode
  148.  
  149. if proc.returncode is 0:
  150. lines = proc.stdout.readlines()
  151. lines = lines[0].split()
  152. result = [ lines[1], lines[2] ]
  153. elif proc.returncode is not 0:
  154. result = [ 0, 0 ]
  155. return result
  156.  
  157. # End Of Function
  158.  
  159. def siteCheck(site, hosts):
  160. result = []
  161. for host in hosts:
  162. userCount = getUserCount(host)
  163. hostLoad = getLoad(host)
  164. cores = getCores(host)
  165. mem = getMemStats(host)
  166. disk = getDiskStats(host)
  167. result.append([hostLoad, userCount, cores, mem, disk])
  168. # print hostLoad, userCount, cores, mem, disk
  169. return result
  170.  
  171.  
  172. # Put it all together.
  173. hA = siteCheck('A Hosts', HostsA)
  174. hB = siteCheck('B Hosts', Hostsb)
  175.  
  176. sites = {'Site A': hA, 'Site B': hB }
  177.  
  178. # Put it alllllll together and print results.
  179. try:
  180. fd = open('/var/www/twiki/data/Main/WebTopicFile.txt', 'w')
  181.  
  182. if fd:
  183. fd.writelines('''%META:TOPICINFO{author="Anonym0us" date="1292288113" format="1.1" version="1.1"}%\n%META:TOPICPARENT{name="Main"}%\n''')
  184. fd.writelines('''---+++ Page last refreshed at: %s \n''' % (time.ctime()))
  185.  
  186. for site, siteResults in sites.items():
  187. fd.writelines('''---++ %s\n| *Hostname* | *1-min Load Avg.* | *5-min Load Avg.* | *15-min Load Avg.* | *Unique User Sessions* | *Number of Cores* | *Total RAM(kbytes)* | *Free RAM(kbytes)* | *Total Swap Used(kbytes)* | *Size of /scratch/ area* | *Amount of /scratch/ Used* | \n''' % (site))
  188. for item in siteResults:
  189. hostString = '| '
  190. for stat in item:
  191. for object in stat:
  192. hostString += '%s | ' % object
  193. hostString += '\n'
  194. # print hostString
  195. fd.writelines(hostString)
  196.  
  197.  
  198. # Get finish time, elapsed, then write out to file.
  199. finishTime = time.time()
  200. elapsedTime = finishTime - startTime
  201.  
  202. fd.writelines('''=This page was generated in %s seconds= %s\n=at %s= %s''' % (elapsedTime, br, time.ctime(), br, sys.argv[0]))
  203. fd.close()
  204.  
  205. except:
  206. # print 'Something broke!'
  207. exit

Report this snippet  

You need to login to post a comment.