#! /usr/bin/env python """A little module for mucking about with ruptime. Requires HP-UX ruptime. To adapt to other ruptimes, fiddle with the regular expressions at the top. (c) 2006 Tom Anderson Redistribution and use in source and binary forms, with or without modification, are permitted. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ # some example data for those without a HP-UX ruptime - load() is patched to use thisg bash example = """hpabcc up 1+17:40, 6 users, load 0.18, 0.13, 0.09 hpabcb down 1:13 hpabca down 14+08:34 hpabcd up 14+06:49, 3 users, load 0.10, 0.38, 0.49 """ import re import os lineRe = re.compile(r"(\S+)\s+(\S+)\s+([\d+:]+)(?:,\s+(\d+) users,\s+load ([\d., ]+))?") # hostname, status, uptime, users, load averages uptimeRe = re.compile(r"(?:(\d+)\+)?(\d*):(\d*)") # days, hours, minutes class Uptime(object): def __init__(self, hostname, status, uptime, users=None, loadavg=None): self.hostname = hostname self.status = status.lower() self.uptime = uptime self.users = users self.loadavg = loadavg def isUp(self): return self.status == "up" def prettyLoadavg(self): if (self.loadavg != None): return ", ".join(map("%.2f".__mod__, self.loadavg)) else: return "" def __str__(self): return "\t".join((self.hostname, self.status, str(self.uptime), _str(self.users), self.prettyLoadavg())) def __repr__(self): return "Uptime(" + ", ".join(map(repr, (self.hostname, self.status, str(self.uptime), str(self.users), str(self.loadavg)))) + ")" def _str(i, null=""): if (i != None): return str(i) else: return null def parse(line): m = lineRe.match(line) assert m, "bad line: " + line hostname, status, uptimeStr, usersStr, loadavgStr = m.groups() uptime = _parseUptime(uptimeStr) if (usersStr): users = int(usersStr) loadavg = tuple(map(float, loadavgStr.split(","))) return Uptime(hostname, status, uptime, users, loadavg) else: return Uptime(hostname, status, uptime) def _parseUptime(uptimeStr): m = uptimeRe.match(uptimeStr) d, h, m = map(_int, m.groups()) return (((d * 24) + h) * 60) + m def _int(s, null=0): if (s != None): return int(s) else: return null def load(): # return map(parse, os.popen("ruptime")) # uncomment this and delete the next line to use real ruptime return map(parse, example.splitlines()) def main(): for ut in load(): print ut def main_cgi(): print "Content-type: text/html" print print "" print "" print "" print "Uptimes" print "" print "" print "

Uptimes

" try: uptimes = load() print "" print "" for uptime in uptimes: print "" print "" if uptime.isUp(): print "" print "" if (uptime.users): print "" print "" print "" print "
HostStatusUptime (min)UsersLoad Average
", uptime.hostname, "", else: print "", print uptime.status, "", uptime.uptime, "", uptime.users, "", uptime.prettyLoadavg(), "
" except: print "

Ah. An error occurred:

" print "
"
		import sys
		print str(sys.exc_info()).replace("&", "&").replace("<", "<")
		print "
" print "" print "" if (__name__ == "__main__"): if ("GATEWAY_INTERFACE" in os.environ): main_cgi() else: main()