Revision: 39890
Updated Code
at February 16, 2011 10:35 by mattsn0w
Updated Code
import sys, time from subprocess import Popen, PIPE, STDOUT startTime = time.time() br = '%BR%' HostsA = 'ahost1', 'ahost2', 'ahost3' Hostsb = 'bhost1', 'bhost2', 'bhost3' def stripSplitOut(cmdResult): cleanResult = [] for line in cmdResult: line = line.strip('\n') line = line.strip() line = line.split() cleanResult.append(line[0]) return cleanResult #End of Function def colorLoad(lval): lval_float = float(lval) result = None if lval_float < float('5'): result = ' %sGREEN%s %s %sENDCOLOR%s ' % ('%', '%', lval, '%', '%') elif lval_float > float('5') and lval_float < float('10'): result = ' %sBLUE%s %s %sENDCOLOR%s ' % ('%', '%', lval, '%', '%') elif lval_float > float('10') and lval_float < float('20'): result = ' %sPURPLE%s %s %sENDCOLOR%s ' % ('%', '%', lval, '%', '%') elif lval_float > float('20'): result = ' %sRED%s *%s* %sENDCOLOR%s ' % ('%', '%', lval, '%', '%') return result def getLoad(host): users = None one = None five = None fifteen = None h_line = None cmd = '''ssh -q %s "uptime" ''' % (host) proc = Popen([cmd], shell=True, stdout=PIPE, stderr=STDOUT) proc.wait() proc.returncode raw_result = proc.stdout if proc.returncode is 0: for line in raw_result: h_line = line.strip('\n') data_result = h_line.split(' ') clean_result = [] for item in data_result: if len(item) > 0: item = item.strip(',') clean_result.append(item) if len(clean_result) is 13: one = colorLoad(clean_result[10]) five = colorLoad(clean_result[11]) fifteen = colorLoad(clean_result[12]) elif len(clean_result) is 12: one = colorLoad(clean_result[9]) five = colorLoad(clean_result[10]) fifteen = colorLoad(clean_result[11]) elif len(clean_result) is 11: one = colorLoad(clean_result[8]) five = colorLoad(clean_result[9]) fifteen = colorLoad(clean_result[10]) elif len(clean_result) is 10: one = colorLoad(clean_result[7]) five = colorLoad(clean_result[8]) fifteen = colorLoad(clean_result[9]) final_result = [ host, one, five, fifteen ] elif proc.returncode is not 0: final_result = [ host, 'null', 'null', 'null' ] for line in raw_result: print line return final_result # End Of Function def getUserCount(host): h_line = None userList = [] cmd = '''ssh -q %s "who | awk '{ print \$1 }' |sort -u | grep -v msnow" ''' % (host) proc = Popen([cmd], shell=True, stdout=PIPE, stderr=STDOUT) proc.wait() proc.returncode lines = proc.stdout.readlines() if proc.returncode is 0: for line in lines: h_line = line.strip('\n') if h_line: userList.append(h_line) elif not h_line: pass userCountResult = [ len(userList) ] elif proc.returncode is not 0: userCountResult = [ 'NA' ] for line in lines: print line return userCountResult # End Of Function def getCores(host): clean_lines = [] cmd = '''ssh -q %s "grep proc /proc/cpuinfo"''' % (host) proc = Popen([cmd], shell=True, stdout=PIPE, stderr=STDOUT) proc.wait() proc.returncode if proc.returncode is 0: lines = proc.stdout.readlines() for line in lines: line = line.strip('\n') clean_lines.append(line) cores = [ len(clean_lines)/2 ] if cores[0] is 4: cores = [len(clean_lines) ] elif proc.returncode is not 0: cores = [0] return cores # End Of Function def getMemStats(host): cmd = '''ssh -q %s "vmstat -s| egrep -e'total.mem|free.mem|used.swap'" ''' % (host) proc = Popen([cmd], shell=True, stdout=PIPE, stderr=STDOUT) proc.wait() proc.returncode if proc.returncode is 0: lines = proc.stdout.readlines() memStatResult = stripSplitOut(lines) elif proc.returncode is not 0: memStatResult = [ 'mem:N/A' ] return memStatResult # End Of Function def getDiskStats(host): cmd = '''ssh -q %s "df -h /scratch 2>/dev/null | grep -v Filesystem; if [ \$? -ne 0 ]; then df -h / | grep -v Filesystem; fi "''' % (host) proc = Popen([cmd], shell=True, stdout=PIPE, stderr=STDOUT) proc.wait() proc.returncode if proc.returncode is 0: lines = proc.stdout.readlines() lines = lines[0].split() result = [ lines[1], lines[2] ] elif proc.returncode is not 0: result = [ 0, 0 ] return result # End Of Function def siteCheck(site, hosts): result = [] for host in hosts: userCount = getUserCount(host) hostLoad = getLoad(host) cores = getCores(host) mem = getMemStats(host) disk = getDiskStats(host) result.append([hostLoad, userCount, cores, mem, disk]) # print hostLoad, userCount, cores, mem, disk return result # Put it all together. hA = siteCheck('A Hosts', HostsA) hB = siteCheck('B Hosts', Hostsb) sites = {'Site A': hA, 'Site B': hB } # Put it alllllll together and print results. try: fd = open('/var/www/twiki/data/Main/WebTopicFile.txt', 'w') if fd: fd.writelines('''%META:TOPICINFO{author="Anonym0us" date="1292288113" format="1.1" version="1.1"}%\n%META:TOPICPARENT{name="Main"}%\n''') fd.writelines('''---+++ Page last refreshed at: %s \n''' % (time.ctime())) for site, siteResults in sites.items(): 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)) for item in siteResults: hostString = '| ' for stat in item: for object in stat: hostString += '%s | ' % object hostString += '\n' # print hostString fd.writelines(hostString) # Get finish time, elapsed, then write out to file. finishTime = time.time() elapsedTime = finishTime - startTime fd.writelines('''=This page was generated in %s seconds= %s\n=at %s= %s''' % (elapsedTime, br, time.ctime(), br, sys.argv[0])) fd.close() except: # print 'Something broke!' exit
Revision: 39889
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at January 22, 2011 06:06 by mattsn0w
Initial Code
import sys, time from subprocess import Popen, PIPE, STDOUT startTime = time.time() br = '%BR%' sjcDevHosts = 'sjchost1', 'sjchost2', 'sjchost3' dcoDevHosts = 'dcohost1', 'dcohost2', 'dcohost3' def stripSplitOut(cmdResult): cleanResult = [] for line in cmdResult: line = line.strip('\n') line = line.strip() line = line.split() cleanResult.append(line[0]) return cleanResult #End of Function def colorLoad(lval): lval_float = float(lval) result = None if lval_float < float('5'): result = ' %sGREEN%s %s %sENDCOLOR%s ' % ('%', '%', lval, '%', '%') elif lval_float > float('5') and lval_float < float('10'): result = ' %sBLUE%s %s %sENDCOLOR%s ' % ('%', '%', lval, '%', '%') elif lval_float > float('10') and lval_float < float('20'): result = ' %sPURPLE%s %s %sENDCOLOR%s ' % ('%', '%', lval, '%', '%') elif lval_float > float('20'): result = ' %sRED%s *%s* %sENDCOLOR%s ' % ('%', '%', lval, '%', '%') return result def getLoad(host): users = None one = None five = None fifteen = None h_line = None cmd = '''ssh -q %s "uptime" ''' % (host) proc = Popen([cmd], shell=True, stdout=PIPE, stderr=STDOUT) proc.wait() proc.returncode raw_result = proc.stdout if proc.returncode is 0: for line in raw_result: h_line = line.strip('\n') data_result = h_line.split(' ') clean_result = [] for item in data_result: if len(item) > 0: item = item.strip(',') clean_result.append(item) if len(clean_result) is 13: one = colorLoad(clean_result[10]) five = colorLoad(clean_result[11]) fifteen = colorLoad(clean_result[12]) elif len(clean_result) is 12: one = colorLoad(clean_result[9]) five = colorLoad(clean_result[10]) fifteen = colorLoad(clean_result[11]) elif len(clean_result) is 11: one = colorLoad(clean_result[8]) five = colorLoad(clean_result[9]) fifteen = colorLoad(clean_result[10]) elif len(clean_result) is 10: one = colorLoad(clean_result[7]) five = colorLoad(clean_result[8]) fifteen = colorLoad(clean_result[9]) final_result = [ host, one, five, fifteen ] elif proc.returncode is not 0: final_result = [ host, 'null', 'null', 'null' ] for line in raw_result: print line return final_result # End Of Function def getUserCount(host): h_line = None userList = [] cmd = '''ssh -q %s "who | awk '{ print \$1 }' |sort -u | grep -v msnow" ''' % (host) proc = Popen([cmd], shell=True, stdout=PIPE, stderr=STDOUT) proc.wait() proc.returncode lines = proc.stdout.readlines() if proc.returncode is 0: for line in lines: h_line = line.strip('\n') if h_line: userList.append(h_line) elif not h_line: pass userCountResult = [ len(userList) ] elif proc.returncode is not 0: userCountResult = [ 'NA' ] for line in lines: print line return userCountResult # End Of Function def getCores(host): clean_lines = [] cmd = '''ssh -q %s "grep proc /proc/cpuinfo"''' % (host) proc = Popen([cmd], shell=True, stdout=PIPE, stderr=STDOUT) proc.wait() proc.returncode if proc.returncode is 0: lines = proc.stdout.readlines() for line in lines: line = line.strip('\n') clean_lines.append(line) cores = [ len(clean_lines)/2 ] if cores[0] is 4: cores = [len(clean_lines) ] elif proc.returncode is not 0: cores = [0] return cores # End Of Function def getMemStats(host): cmd = '''ssh -q %s "vmstat -s| egrep -e'total.mem|free.mem|used.swap'" ''' % (host) proc = Popen([cmd], shell=True, stdout=PIPE, stderr=STDOUT) proc.wait() proc.returncode if proc.returncode is 0: lines = proc.stdout.readlines() memStatResult = stripSplitOut(lines) elif proc.returncode is not 0: memStatResult = [ 'mem:N/A' ] return memStatResult # End Of Function def getDiskStats(host): cmd = '''ssh -q %s "df -h /scratch 2>/dev/null | grep -v Filesystem; if [ \$? -ne 0 ]; then df -h / | grep -v Filesystem; fi "''' % (host) proc = Popen([cmd], shell=True, stdout=PIPE, stderr=STDOUT) proc.wait() proc.returncode if proc.returncode is 0: lines = proc.stdout.readlines() lines = lines[0].split() result = [ lines[1], lines[2] ] elif proc.returncode is not 0: result = [ 0, 0 ] return result # End Of Function def siteCheck(site, hosts): result = [] for host in hosts: userCount = getUserCount(host) hostLoad = getLoad(host) cores = getCores(host) mem = getMemStats(host) disk = getDiskStats(host) result.append([hostLoad, userCount, cores, mem, disk]) # print hostLoad, userCount, cores, mem, disk return result # Put it all together. sjc = siteCheck('San Jose', sjcDevHosts) dco = siteCheck('Denver', dcoDevHosts) sites = {'San Jose': sjc, 'Denver': dco } # Put it alllllll together and print results. try: fd = open('/var/www/twiki/data/Main/RodDevSysLoadV2.txt', 'w') if fd: fd.writelines('''%META:TOPICINFO{author="MattSnow" date="1292288113" format="1.1" version="1.1"}%\n%META:TOPICPARENT{name="MachinePage"}%\n''') fd.writelines('''---+++ Page last refreshed at: %s \n''' % (time.ctime())) for site, siteResults in sites.items(): 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(bytes)* | *Free RAM(bytes)* | *Total Swap Used(bytes)* | *Size of /scratch/ area* | *Amount of /scratch/ Used* | \n''' % (site)) for item in siteResults: hostString = '| ' for stat in item: for object in stat: hostString += '%s | ' % object hostString += '\n' # print hostString fd.writelines(hostString) # Get finish time, elapsed, then write out to file. finishTime = time.time() elapsedTime = finishTime - startTime fd.writelines('''=This page was generated in %s seconds= %s\n=at %s= %s''' % (elapsedTime, br, time.ctime(), br, sys.argv[0])) fd.close() except: # print 'Something broke!' exit
Initial URL
Initial Description
Initial Title
host load check output to TWiKi formatted Text
Initial Tags
Initial Language
Python