2016-02-04 12:09:09 +00:00
|
|
|
from orchestra.utils.sys import run, sshrun, join
|
|
|
|
|
|
|
|
|
|
|
|
def retrieve_state(servers):
|
|
|
|
uptimes = []
|
|
|
|
pings = []
|
|
|
|
for server in servers:
|
|
|
|
address = server.get_address()
|
|
|
|
ping = run('ping -c 1 -w 1 %s' % address, async=True)
|
|
|
|
pings.append(ping)
|
|
|
|
uptime = sshrun(address, 'uptime', persist=True, async=True, options={'ConnectTimeout': 1})
|
|
|
|
uptimes.append(uptime)
|
|
|
|
|
|
|
|
state = {}
|
|
|
|
for server, ping, uptime in zip(servers, pings, uptimes):
|
|
|
|
ping = join(ping, silent=True)
|
|
|
|
ping = ping.stdout.splitlines()[-1].decode()
|
|
|
|
if ping.startswith('rtt'):
|
|
|
|
ping = '%s ms' % ping.split('/')[4]
|
|
|
|
else:
|
2016-02-04 12:13:39 +00:00
|
|
|
ping = '<span style="color:red"><b>Offline<b></span>'
|
2016-02-04 12:09:09 +00:00
|
|
|
|
|
|
|
uptime = join(uptime, silent=True)
|
2016-02-04 12:13:39 +00:00
|
|
|
uptime_stderr = uptime.stderr.decode()
|
2016-02-04 12:09:09 +00:00
|
|
|
uptime = uptime.stdout.decode().split()
|
|
|
|
if uptime:
|
|
|
|
uptime = 'Up %s %s load %s %s %s' % (uptime[2], uptime[3], uptime[-3], uptime[-2], uptime[-1])
|
|
|
|
else:
|
2016-02-04 12:13:39 +00:00
|
|
|
uptime = '<span style="color:red"><b>%s<b></span>' % uptime_stderr
|
2016-02-04 12:09:09 +00:00
|
|
|
state[server.pk] = (ping, uptime)
|
|
|
|
|
|
|
|
return state
|