import subprocess p = subprocess.Popen(['pbsnodes', '-a'], stdout=subprocess.PIPE,stderr=subprocess.PIPE, stdin=subprocess.PIPE) output, error = p.communicate() pbsinfo = output.split('cluster') totalcpus = 0 usedcpus = 0 freecpus = 0 DOWNOFF = [] DOWN = [] OFF = [] for NODE in pbsinfo: NODE = NODE.split('\n') useNode = True for OPT in NODE: if "node" in OPT and "status" not in OPT: thisNODE = OPT.split(".")[0] if "state = down,offline" in OPT: DOWNOFF.append(thisNODE) useNode = False break if "state = down" in OPT: DOWN.append(thisNODE) useNode = False break if "state = offline" in OPT: OFF.append(thisNODE) useNode = False break if "99" in OPT: useNode = False break if useNode: for OPT in NODE: if "np" in OPT: totalcpus += int(OPT.split(" = ")[1]) if "jobs =" in OPT: OPT = OPT.split(" = ")[1] OPT = OPT.split(",") usedcpus += len(OPT) print " =========================== " print " Total cpus :",totalcpus print " Used cpus :",usedcpus print " Free cpus :",totalcpus - usedcpus print " =========================== " print "Offline,down ", for i in DOWNOFF: print i, print "\n Down ", for i in DOWN: print i, print "\n Offline ", for i in OFF: print i, print "\n =========================== "