Page MenuHomec4science

leds.py
No OneTemporary

File Metadata

Created
Thu, Jul 18, 01:28
#!/usr/bin/env python2
import collectd
import serial
import subprocess
from math import floor
DEBUG = 1
PORT = '/dev/ttyS0'
SPEED = 9600
CORES = int(subprocess.check_output('nproc'))
LEDS_NUMBER = 8 # number of leds in a column
LEDS_MAX = 100
LEDS_COLOR = '%d 0 0 '
LEDS_NOCOL = '0 0 0 '
hosts = set()
def debug(msg):
if DEBUG:
f = open('/tmp/debug', 'a+')
f.write(str(msg) + "\n")
f.close()
def write(vl, data=None):
global hosts
line = ''
hosts.add(vl.host)
index = list(hosts).index(vl.host)
if vl.type == 'if_octets':
pass
if vl.type == 'load':
debug(vl.values)
load1 = vl.values[0]
load1 = load1 if load1 <= CORES else CORES
LEDS_ON = LEDS_NUMBER / CORES * load1
LEDS_TR = 0 if LEDS_ON==0 else LEDS_ON - int(LEDS_ON)
LEDS_OFF = LEDS_NUMBER - LEDS_ON
line = "L%d %s" % \
(index, LEDS_COLOR % (LEDS_MAX) * int(floor(LEDS_ON)) \
+ LEDS_COLOR % (LEDS_MAX * LEDS_TR) \
+ LEDS_NOCOL * int(floor(LEDS_OFF)))
debug(line.strip())
if line != '':
ser = serial.Serial(PORT, SPEED)
ser.write(line.strip() + "\n")
ser.close()
collectd.register_write(write);

Event Timeline