diff --git a/deploy/roles/collectd/templates/leds.py b/deploy/roles/collectd/templates/leds.py index 5a2bc8c..99886f2 100644 --- a/deploy/roles/collectd/templates/leds.py +++ b/deploy/roles/collectd/templates/leds.py @@ -1,73 +1,74 @@ #!/usr/bin/env python2 import collectd import serial import subprocess from math import floor, ceil DEBUG = 1 PORT = '/dev/ttyS1' SPEED = 9600 CORES = int(subprocess.check_output('nproc')) LEDS_NUMBER = 8 # number of leds in a column LEDS_MAX = 100 LEDS_NOCOL = '0 0 0 ' FAN_MAX = 255 fan_last = 0 TEMP_LOW = 40 temp_last = 0 COLORS = [ '0 X X ' # cyan 'X 0 X ',# magenta 'X X 0 ',# yellow 'X 0 0 ', # red '0 X 0 ', # green '0 0 X ', # blue ] hosts = set() def debug(msg): if DEBUG: f = open('/tmp/debug', 'a+') f.write(str(msg) + "\n") f.close() def color(value, index): return COLORS[index].replace('X', str(int(value))) def write(vl, data=None): global hosts line = '' hosts.add(vl.host) index = list(hosts).index(vl.host) if vl.plugin == 'thermal': temp_last = max(temp_last, values[0]) if temp_last > TEMP_LOW: line += 'f 50 ' if vl.type == 'load': 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(floor(LEDS_ON)) - LEDS_OFF = LEDS_NUMBER - int(ceil(LEDS_TR)) - int(floor(LEDS_ON)) - line += "l%d %s " % \ - (index, color(LEDS_MAX, index) * int(floor(LEDS_ON)) \ - + color(LEDS_MAX * LEDS_TR, index) \ - + LEDS_NOCOL * LEDS_OFF) + leds_on = LEDS_NUMBER / CORES * load1 + + line += 'l%d ' % index + for i in xrange(LEDS_NUMBER): + if i < leds_on: + line += color(LEDS_MAX, index) + else: + line += LEDS_NOCOL if line != '': debug(line.strip()) ser = serial.Serial(PORT, SPEED) ser.write(line) ser.close() collectd.register_write(write);