| 1 |
""" |
| 2 |
sysinfo.py [host1] [host2] [options] |
| 3 |
|
| 4 |
obtain system info from remote machine. |
| 5 |
|
| 6 |
(c) Holger Krekel, MIT license |
| 7 |
""" |
| 8 |
|
| 9 |
import optparse |
| 10 |
import re |
| 11 |
import sys |
| 12 |
|
| 13 |
import execnet |
| 14 |
|
| 15 |
parser = optparse.OptionParser(usage=__doc__) |
| 16 |
parser.add_option( |
| 17 |
"-f", |
| 18 |
"--sshconfig", |
| 19 |
action="store", |
| 20 |
dest="ssh_config", |
| 21 |
default=None, |
| 22 |
help="use given ssh config file, and add info all contained hosts for getting info", |
| 23 |
) |
| 24 |
parser.add_option( |
| 25 |
"-i", |
| 26 |
"--ignore", |
| 27 |
action="store", |
| 28 |
dest="ignores", |
| 29 |
default=None, |
| 30 |
help="ignore hosts (useful if the list of hostnames come from a file list)", |
| 31 |
) |
| 32 |
|
| 33 |
|
| 34 |
def parsehosts(path): |
| 35 |
host_regex = re.compile(r"Host\s*(\S+)") |
| 36 |
l = [] |
| 37 |
|
| 38 |
with open(path) as fp: |
| 39 |
for line in fp: |
| 40 |
m = host_regex.match(line) |
| 41 |
if m is not None: |
| 42 |
(sshname,) = m.groups() |
| 43 |
l.append(sshname) |
| 44 |
return l |
| 45 |
|
| 46 |
|
| 47 |
class RemoteInfo: |
| 48 |
def __init__(self, gateway): |
| 49 |
self.gw = gateway |
| 50 |
self._cache = {} |
| 51 |
|
| 52 |
def exreceive(self, execstring): |
| 53 |
if execstring not in self._cache: |
| 54 |
channel = self.gw.remote_exec(execstring) |
| 55 |
self._cache[execstring] = channel.receive() |
| 56 |
return self._cache[execstring] |
| 57 |
|
| 58 |
def getmodattr(self, modpath): |
| 59 |
module = modpath.split(".")[0] |
| 60 |
return self.exreceive( |
| 61 |
""" |
| 62 |
import %s |
| 63 |
channel.send(%s) |
| 64 |
""" |
| 65 |
% (module, modpath) |
| 66 |
) |
| 67 |
|
| 68 |
def islinux(self): |
| 69 |
return self.getmodattr("sys.platform").find("linux") != -1 |
| 70 |
|
| 71 |
def getfqdn(self): |
| 72 |
return self.exreceive( |
| 73 |
""" |
| 74 |
import socket |
| 75 |
channel.send(socket.getfqdn()) |
| 76 |
""" |
| 77 |
) |
| 78 |
|
| 79 |
def getmemswap(self): |
| 80 |
if self.islinux(): |
| 81 |
return self.exreceive( |
| 82 |
r""" |
| 83 |
import commands, re |
| 84 |
out = commands.getoutput("free") |
| 85 |
mem = re.search(r"Mem:\s+(\S*)", out).group(1) |
| 86 |
swap = re.search(r"Swap:\s+(\S*)", out).group(1) |
| 87 |
channel.send((mem, swap)) |
| 88 |
""" |
| 89 |
) |
| 90 |
|
| 91 |
def getcpuinfo(self): |
| 92 |
if self.islinux(): |
| 93 |
return self.exreceive( |
| 94 |
""" |
| 95 |
# a hyperthreaded cpu core only counts as 1, although it |
| 96 |
# is present as 2 in /proc/cpuinfo. Counting it as 2 is |
| 97 |
# misleading because it is *by far* not as efficient as |
| 98 |
# two independent cores. |
| 99 |
cpus = {} |
| 100 |
cpuinfo = {} |
| 101 |
f = open("/proc/cpuinfo") |
| 102 |
lines = f.readlines() |
| 103 |
f.close() |
| 104 |
for line in lines + ['']: |
| 105 |
if line.strip(): |
| 106 |
key, value = line.split(":", 1) |
| 107 |
cpuinfo[key.strip()] = value.strip() |
| 108 |
else: |
| 109 |
corekey = (cpuinfo.get("physical id"), |
| 110 |
cpuinfo.get("core id")) |
| 111 |
cpus[corekey] = 1 |
| 112 |
numcpus = len(cpus) |
| 113 |
model = cpuinfo.get("model name") |
| 114 |
channel.send((numcpus, model)) |
| 115 |
""" |
| 116 |
) |
| 117 |
|
| 118 |
|
| 119 |
def debug(*args): |
| 120 |
print(" ".join(map(str, args)), file=sys.stderr) |
| 121 |
|
| 122 |
|
| 123 |
def error(*args): |
| 124 |
debug("ERROR", args[0] + ":", *args[1:]) |
| 125 |
|
| 126 |
|
| 127 |
def getinfo(sshname, ssh_config=None, loginfo=sys.stdout): |
| 128 |
if ssh_config: |
| 129 |
spec = f"ssh=-F {ssh_config} {sshname}" |
| 130 |
else: |
| 131 |
spec = "ssh=%s" % sshname |
| 132 |
debug("connecting to", repr(spec)) |
| 133 |
try: |
| 134 |
gw = execnet.makegateway(spec) |
| 135 |
except OSError: |
| 136 |
error("could not get sshgatway", sshname) |
| 137 |
else: |
| 138 |
ri = RemoteInfo(gw) |
| 139 |
|
| 140 |
prefix = sshname.upper() + " " |
| 141 |
print(prefix, "fqdn:", ri.getfqdn(), file=loginfo) |
| 142 |
for attr in ("sys.platform", "sys.version_info"): |
| 143 |
loginfo.write(f"{prefix} {attr}: ") |
| 144 |
loginfo.flush() |
| 145 |
value = ri.getmodattr(attr) |
| 146 |
loginfo.write(str(value)) |
| 147 |
loginfo.write("\n") |
| 148 |
loginfo.flush() |
| 149 |
memswap = ri.getmemswap() |
| 150 |
if memswap: |
| 151 |
mem, swap = memswap |
| 152 |
print(prefix, "Memory:", mem, "Swap:", swap, file=loginfo) |
| 153 |
cpuinfo = ri.getcpuinfo() |
| 154 |
if cpuinfo: |
| 155 |
numcpu, model = cpuinfo |
| 156 |
print(prefix, "number of cpus:", numcpu, file=loginfo) |
| 157 |
print(prefix, "cpu model", model, file=loginfo) |
| 158 |
return ri |
| 159 |
|
| 160 |
|
| 161 |
if __name__ == "__main__": |
| 162 |
options, args = parser.parse_args() |
| 163 |
hosts = list(args) |
| 164 |
ssh_config = options.ssh_config |
| 165 |
if ssh_config: |
| 166 |
hosts.extend(parsehosts(ssh_config)) |
| 167 |
ignores = options.ignores or () |
| 168 |
if ignores: |
| 169 |
ignores = ignores.split(",") |
| 170 |
for host in hosts: |
| 171 |
if host not in ignores: |
| 172 |
getinfo(host, ssh_config=ssh_config) |