#!/usr/bin/env python import sys, time, serial from daemon import Daemon from subprocess import Popen, PIPE def checksum(msg): c = 0 for ca in msg: c ^= ord(ca) return chr(c) def bcd(val): iu = int(val / 10) il = int(val % 10) return chr(iu * 16 + il) def disk_usage(): pr = Popen("df -h | grep \"/mnt/data\"", shell=True, stdout=PIPE) r = pr.wait() if r != 0: return "Unknown" d = pr.stdout.read() if len(d) == 0: return "Unknown" d = d.split() if len(d) < 3: return "Unknown" if len(d[-3]) > 5: return "Unknown" if len(d[-2]) > 4: return "Unknown" return "D: %4s (%5s Free)" % (d[-2], d[-3]) def mem_info(): f = file("/proc/meminfo", "r") d = f.read() f.close() d = d.splitlines() for l in d: if l.find("MemTotal:") > -1: mt = int(l.split()[1]) elif l.find("MemFree:") > -1: mf = int(l.split()[1]) return "RAM %4sGB: %4sGB Fr" % ("%1.2f" % (mt / (1024.0 * 1024.0)), "%1.2f" % (mf / (1024.0 * 1024.0))) def raid_status(): f = file("/proc/mdstat", "r") d = f.read() f.close() if d.find("recovery = ") > -1: d = d[d.find("recovery = ") + len("recovery = "):] d = d.split()[0] return "RAID: Rcvrng (%6s)" % d else: return "RAID: Unknown" def temp_status(): pr = Popen("sensors -A", shell=True, stdout=PIPE) pr.wait() d = pr.stdout.read() d = d.splitlines() t = [] for i in range(1, len(d), 3): t.append(float(d[i].split("+")[1].split("\xc2")[0])) t = sum(t) / len(t) return "Temp: %5s C" % ("%3.1f" % t) class MyDaemon(Daemon): def run(self): s = serial.Serial("/dev/ttyS0") # send the startup / CLS message msg = "\x0F" + "\x55" * 21 s.write("\xAA" + msg + checksum(msg)) time.sleep(0.2) while True: msg = "\x00" + time.strftime("Updated: %H:%M %d/%m") while len(msg) < 22: msg += " " s.write("\xAA" + msg + checksum(msg)) s.read() msg = "\x02" + mem_info() while len(msg) < 22: msg += " " s.write("\xAA" + msg + checksum(msg)) s.read() msg = "\x03" + disk_usage() while len(msg) < 22: msg += " " s.write("\xAA" + msg + checksum(msg)) s.read() msg = "\x04" + raid_status() while len(msg) < 22: msg += " " s.write("\xAA" + msg + checksum(msg)) s.read() msg = "\x05" + temp_status() while len(msg) < 22: msg += " " s.write("\xAA" + msg + checksum(msg)) s.read() # update the RTC on the display msg = "\x08" t = time.localtime() msg += bcd(t.tm_sec) msg += bcd(t.tm_min) msg += bcd(t.tm_hour) while len(msg) < 22: msg += " " s.write("\xAA" + msg + checksum(msg)) if self.test: break time.sleep(60) if __name__ == "__main__": daemon = MyDaemon('/tmp/monitor_daemon.pid') daemon.test = False if len(sys.argv) == 2: if 'start' == sys.argv[1]: daemon.start() elif 'stop' == sys.argv[1]: daemon.stop() elif 'restart' == sys.argv[1]: daemon.restart() elif 'test' == sys.argv[1]: daemon.test = True daemon.run() else: print "Unknown command" sys.exit(2) sys.exit(0) else: print "usage: %s start|stop|restart" % sys.argv[0] sys.exit(2)