-
-
Save vookimedlo/441ab36772c8c6bc4f0c3ec153407488 to your computer and use it in GitHub Desktop.
Set Turris Omnia user LED colours based on WAN usage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# interface speeds in BYTES per second | |
IFC="eth1" | |
WAN_DOWN=$((60 * 1024 * 128)) | |
WAN_UP=$((60 * 1024 * 128)) | |
# probably don't need to edit from here on | |
# read current values | |
RX=$(cat /sys/class/net/$IFC/statistics/rx_bytes) | |
TX=$(cat /sys/class/net/$IFC/statistics/tx_bytes) | |
TM=$(date +%s) | |
if [ ! -f /tmp/traf.rx ]; then | |
echo "0" >/tmp/traf.rx | |
echo "0" >/tmp/traf.tx | |
echo "0" >/tmp/traf.tm | |
fi | |
# 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:user2/color | |
#echo -n $RED_TX $GRN_TX 0 > /sys/class/leds/omnia-led:user1/color |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment