Skip to content

Instantly share code, notes, and snippets.

@Cameron-D
Created November 29, 2016 11:54
Show Gist options
  • Save Cameron-D/b2c5c7c678c2019f5796f15ad3827cf2 to your computer and use it in GitHub Desktop.
Save Cameron-D/b2c5c7c678c2019f5796f15ad3827cf2 to your computer and use it in GitHub Desktop.
Set Turris Omnia user LED colours based on WAN usage
#!/bin/bash
# interface speeds in BYTES per second
WAN_DOWN=$((700 * 1024))
WAN_UP=$((45 * 1024))
# probably don't need to edit from here on
# read current values
RX=$(cat /sys/class/net/pppoe-wan/statistics/rx_bytes)
TX=$(cat /sys/class/net/pppoe-wan/statistics/tx_bytes)
TM=$(date +%s)
# read last run values
LRX=$(cat /tmp/traf.rx)
LTX=$(cat /tmp/traf.tx)
LTM=$(cat /tmp/traf.tm)
# store current run values
echo $RX > /tmp/traf.rx
echo $TX > /tmp/traf.tx
echo $TM > /tmp/traf.tm
# calcualte the differences for the time period
TRX=$(($RX - $LRX))
TTX=$(($TX - $LTX))
TTM=$(($TM - $LTM))
# and the average bytes per second
ARX=$(($TRX / $TTM))
ATX=$(($TTX / $TTM))
# and the total load as a percentage
# bash only supports intergers, which makes things... messier later.
PRX=$(($ARX * 100 / $WAN_DOWN))
PTX=$(($ATX * 100 / $WAN_UP))
# calcualte red/green colours
# https://github.com/ddrown/omnia-led-colors/blob/master/omnia-led-colors#L188
RED_RX=$(echo $PRX | awk '{print (255>int(($1/100)*255))?int(($1/100)*255):255}')
GRN_RX=$(echo $PRX | awk '{print int(((0>1-($1/100))?0:1-($1/100))*255)}')
RED_TX=$(echo $PTX | awk '{print (255>int(($1/100)*255))?int(($1/100)*255):255}')
GRN_TX=$(echo $PTX | awk '{print int(((0>1-($1/100))?0:1-($1/100))*255)}')
# turn user 1/2 leds on
echo -n 0 > /sys/class/leds/omnia-led:user1/autonomous
echo -n 255 > /sys/class/leds/omnia-led:user1/brightness
echo -n 0 > /sys/class/leds/omnia-led:user2/autonomous
echo -n 255 > /sys/class/leds/omnia-led:user2/brightness
# set led colours
echo -n $RED_RX $GRN_RX 0 > /sys/class/leds/omnia-led:user1/color
echo -n $RED_TX $GRN_TX 0 > /sys/class/leds/omnia-led:user2/color
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment