#! /usr/bin/python ## checkpc ## Check status of PCs in network ## ## By Julen Larrucea ## www.larrucea.eu version="2.1" import os,sys,math pssh_bin="" # pssh bin with full path (if not available, leave empty) me=os.popen('echo $USER').read().split()[0] mypc=os.popen('hostname').read().split()[0] pwho = "yes" ncheck=3 Dt= 3 # max seconds for ssh to wait pcs=[] pcs.append(['hmipc01','John',4]) pcs.append(['hmipc02','Tom',4]) pcs.append(['hmipc03','Mike',4]) pcs.append(['hmipc04','Peter',4]) pcs.append(['hmipc05','Jane',8]) pcs.append(['hmipc06','Joseph',8]) pcs.append(['hmipc07','Julen',8]) pcs.append(['hmipc08','Bob',8]) pcs.append(['hmipc09','Matt',8]) pcs.append(['hmipc10','Andy',8]) pcs.append(['hmipc11','Chris',64]) pcs.append(['hmipc12','Homer',8]) pcs.append(['hmipc13','Marge',8]) pcs.append(['hmipc14','Bart',8]) pcs.append(['hmipc15','Lisa',8]) pcs.append(['hmipc16','Maggy',8]) ''' You have to first copy the fillowing script into ~/.checkpc_local #! /bin/bash NCPU=$(( `cat /proc/cpuinfo | grep processor | wc -l` )) ICPU=$( ps axo pcpu | awk 'BEGIN { sum = 0 } NR > 1 { sum += $1 }; END { print sum }' ) IMEM=$( ps aux | awk 'BEGIN { sum = 0 } NR > 1 { sum += $4 }; END { print sum }' ) N=$( echo $HOSTNAME |awk -F "hmipc" '{print $2}' ) TOTCPUS=$( cat /proc/cpuinfo | grep processor | wc -l )# WHO=$( ps aux --sort=pcpu |tail -8 | awk '$3 > 40 {print $1}' | sort -k 2 |tail -1 ) ICPDNCPU=$( echo "$ICPU/$NCPU" |bc ) echo "CPU_MEM" $N $HOSTNAME $ICPDNCPU $IMEM $TOTCPUS $WHO exit 0 ''' raw=[] if pssh_bin == "": for pc in pcs: raw.append(os.system("ssh " + pc + "~/.checkpc_local")) elif pssh_bin != "": for line in os.popen(pssh_bin + "-h /home/larrucea/MyTools/hosts -i -P --timeout "+ str(Dt) +" /home/larrucea/MyTools/checkpc_local").readlines(): #print "a",line if line.split()[0]=="CPU_MEM": # No./pcname/cpu/mem/No.cpus/Who try: who=line.split()[6] except: who="" raw.append([line.split()[1],line.split()[2],line.split()[3],line.split()[4],line.split()[5],who]) if "[FAILURE]" in line: raw.append(["".join(list(line.split()[3])[-2:]),line.split()[3], "DOWN", "DOWN","DOWN","DOWN"]) raw=sorted(raw,key=lambda x:float(x[0])) def mean(numberList): #return the mean value of numbers if len(numberList) == 0: return float('nan') floatNums = [float(x) for x in numberList] return sum(floatNums) / len(numberList) def color(percen): #set the color for the shell output if float(percen)<15: return " %s " %('\033[42;30m'+str("%3.0f%% " %(percen))+'\033[0m') if float(percen)>=15 and float(percen)<=75: return " %s " %('\033[43;35m'+str("%3.0f%% " %(percen))+'\033[0m') if float(percen)>75: return " %s " %('\033[41;30m'+str("%3.0f%% " %(percen))+'\033[0m') print " ##checkpc.py## version " + version print "Checking the averaged CPU and Memory usage of PCs in our network..." print " PC Owner %T. CPU Used CPUs %Mem " for i in range(len(raw)): if raw[i][2] != "DOWN": cpui=float(raw[i][2]) totcpu=float(raw[i][4]) elif raw[i][2] == "DOWN": cpui,totcpu=-1,-1 if cpui > 100: cpui=100 if cpui*totcpu/100 < int(cpui*totcpu/100)+0.2: n_usedcpus=int(cpui*totcpu/100) elif cpui*totcpu/100 > int(cpui*totcpu/100)+0.2: n_usedcpus=int(cpui*totcpu/100)+1 # machine, user, averaged total load, av.tot. load/nproc, total memory used if pwho == "no": if raw[i][2] != "DOWN": print "%8s %7s %s %2i%1s%2s %s" %(raw[i][1], pcs[i][1],color(float(cpui)), n_usedcpus,"/", str(raw[i][4]), color(float(raw[i][3]))) elif raw[i][2] == "DOWN": print "%8s %7s %s " %(raw[i][1], pcs[i][1], "\033[41;30m <<<<< DOWN >>>>> \033[0m") elif pwho == "yes": usr=raw[i][5] if usr == "20076": usr="hildebrand" if usr == "20073": usr="dabrowski" if usr == "laube": usr="julen" if raw[i][2] != "DOWN": print "%8s %7s %s %2i%1s%2s %s %s" %(raw[i][1], pcs[i][1],color(float(cpui)), n_usedcpus,"/", str(raw[i][4]), color(float(raw[i][3])), usr) elif raw[i][2] == "DOWN": print "%8s %7s %s " %(raw[i][1], pcs[i][1], "\033[41;30m <<<<< DOWN >>>>> \033[0m") sys.exit()