#!/usr/bin/env python import sys, os, subprocess hosts = ["lirone", "kermit", "new-dracula", "algoza", "vilnius", "flute", "aard", "rapier", "theremin", "eigenharp", "qubit", "badger"] hosts += ["farm" + str(x) for x in range(1, 15)] PING_TRIES = 5 SSH_USER = "drkp" FIELDS = [("Name", "hostname"), ("OS", "os"), ("Kernel", "kernel"), ("CPU", "cpu"), ("RAM", "ram")] hosts.sort() data = {} def isAlive(host): for i in range(PING_TRIES): cmd = "ping -c 1 %s >/dev/null 2>&1" % (host) if (os.system(cmd) == 0): return True return False def firstLine(s): return s.split("\n")[0] def runOverSSH(host, cmd): proc = subprocess.Popen(["/usr/bin/ssh", "%s@%s" % (SSH_USER, host), cmd], stdout=subprocess.PIPE) ret = proc.wait() if (ret != 0): print "runOverSSH(%s, %s) failed: %d" % (host, cmd, ret) return None return proc.stdout.read() print "Pinging hosts..." for host in hosts: if isAlive(host): data[host] = {} else: print "warning: %s is not pingable, ignoring" % host print "Getting hostnames..." for host in data.keys(): data[host]["hostname"] = runOverSSH(host, "hostname").split(".")[0].strip() print "Getting OS versions..." for host in data.keys(): rel = runOverSSH(host, "lsb_release -irs") data[host]["os"] = rel.replace("\n", " ") print "Getting kernel versions..." for host in data.keys(): data[host]["kernel"] = runOverSSH(host, "uname -r").strip() print "Getting CPU info..." for host in data.keys(): procs = 0 cpuinfo = runOverSSH(host, "cat /proc/cpuinfo").strip() # Count hyperthreads/SMT cpu_thread_count = {} for line in cpuinfo.split("\n"): splitLine = [s.strip() for s in line.split(":")] if splitLine[0] == "processor": # Old kernels don't report "physical id" so we still need this procs += 1 last_physical_id = None last_core_id = None elif splitLine[0] == "model name": procType = splitLine[1] # Clean processor type up a little procType = procType.replace("(R)", "") procType = procType.replace("(TM)", "") procType = procType.replace("Intel ", "") procType = procType.replace("CPU ", "") procType = " ".join(procType.split()) elif splitLine[0] == "physical id": last_physical_id = splitLine[1] elif splitLine[0] == "core id": last_core_id = splitLine[1] elif splitLine[0] == "flags": # Count this (physical id, core id) pair if last_physical_id is not None: key = (last_physical_id, last_core_id) cpu_thread_count[key] = cpu_thread_count.get(key, 0) + 1 # If we found "physical id" fields, use that if len(cpu_thread_count) > 0: procs = len(cpu_thread_count) threads = sum(cpu_thread_count.itervalues()) data[host]["cpu"] = "%dx %s" % (procs, procType) if threads > 0 and procs != threads: data[host]["cpu"] += " (%d threads)" % (threads) print "Getting RAM info..." for host in data.keys(): meminfo = runOverSSH(host, "cat /proc/meminfo").strip() for line in meminfo.split("\n"): splitLine = line.split(":") if splitLine[0].strip() == "MemTotal": kb = int(splitLine[1].strip().split()[0]) mb = kb/1024 data[host]["ram"] = "%d MB" % (mb) print '{| border="1"' print "|+ PMG hosts (Linux)" print "!" + " !! ".join(x[0] for x in FIELDS) k = data.keys() k.sort() for host in k: print "|- " for field in FIELDS: print "| " + data[host][field[1]] print "|}"