check_computer_status.py

#!/usr/bin/env python
'''
check_computer_status.py
Eric Ayars
8/27/10

This program runs on the linux server for the network in my physics lab, 
and lets me know what machines are up or down or otherwise needing 
attention in the lab. It runs in conjunction with the Arduino-based LED
bank which is mounted on the server in my office.

Currently this runs every minute as a cron-job. I'd rather not run it more often that that, due to the load ping puts on a network. ;-)

'''

import os, stat, serial
debug = False;
Status = {}


ComputerList = ["physics", "Michael", "Douglas", "Enrico", "Lev", "James", "Maria", "Benjamin", "Albert", "Groucho", "Harpo", "Zeppo", "Richard"]

def bin(x,digits=0):
    oct2bin = ['000','001','010','011','100','101','110','111']
    binstring = [oct2bin[int(n)] for n in oct(x)]
    return ''.join(binstring).lstrip('0').zfill(digits)
    
# Check computers
for Computer in ComputerList:
    Status[Computer] = os.system("ping -q -c1 -W1 %s > /dev/null" % Computer)
    # change all "false" replies to 1, regardless of ping's return value.
    if Status[Computer]:
        Status[Computer] = 1
try:
    os.stat('/export/home/eayars')
except OSError:
    # home directories not mounted!
    Status["Richard"] = 1

if debug:
    for Computer, State in Status.iteritems():
        print "%s: %d" % (Computer, State)
    print "Enter 0 for up, 1 for down."
    for Computer in ComputerList:
        Status[Computer] = int(input("%s: " % Computer))

# build bytes to send to arduino
Bank1G = (1  * Status['Michael'] + 
          2  * Status['Douglas'] +
          4  * Status['Enrico']  +
          8  * Status['Lev']     +
          16 * Status['James']   +
          32 * Status['Maria']   +
          64 * Status['Richard'] )

Bank1R = ~Bank1G & 0b01111111
if Status['Richard']:
    Bank1R = Bank1R | 0b01000000

Bank2G = (1  * Status['Groucho'] +
          2  * Status['Harpo']   +
          4  * Status['Zeppo']   +
          8  * Status['Benjamin']+
          16 * Status['Albert']  +
          32 * Status['physics'] +
          64 * Status['Richard'] )

Bank2R = ~Bank2G & 0b01111111
if Status['Richard']:
    Bank2R = Bank2R | 0b01000000

if debug:
    print "Bank1G = %s" % bin(Bank1G,8)
    print "Bank1R = %s" % bin(Bank1R,8)
    print "Bank2G = %s" % bin(Bank2G,8)
    print "Bank2R = %s" % bin(Bank2R,8)

# Send stuff to Arduino
ser = serial.Serial()
ser.port = '/dev/tty.usbserial-A6004moQ'
ser.baudrate = '9600'
ser.open()

ser.write(chr(Bank1G))
ser.write(chr(Bank1R))
ser.write(chr(Bank2G))
ser.write(chr(Bank2R))



Generated by GNU enscript 1.6.4.