Fitlet2 disk and network LED activity

Moderators: hassellbear, Andrey.Mazlin

Post Reply
TomT
Posts: 6
Joined: Wed Feb 05, 2020 3:24 am

Fitlet2 disk and network LED activity

Post by TomT »

I cooked up this simple bash shell script to flash the fitlet2 LEDs for network and disk activity under Linux. It's implemented entirely with bash builtins and runs as a background daemon.

It flashes the left LED "green" for disk activity, and the right LED "yellow" for network activity. It updates once per second. It cleans up, restoring the LEDs to green on exit. Run it at login, or configure as a service, for example: https://www.linode.com/docs/quick-answe ... e-at-boot/

It needs to run as root, in order to configure and write the gpios in /proc. But with a little system pre-configuration, that can be avoided. The script will check either way.

It's pretty simple and could be dressed up quite a lot. But, it works for my purposes. Enjoy!

[Updated to use only bash builtins, slightly more efficiently]

Code: Select all

#!/bin/bash
#
# Fitlet2 disk and network activity monitoring daemon
#    flashes left LED green for disk, right LED yellow for net
#

init () {
        cd /sys/class/gpio
        if [ ! -w gpio435/value ]
        then
                # initialize gpio nodes, if not already prepared
                if [ ! -w export ]
                then
                        echo "cannot initialize gpios" 1>&2
                        exit 1
                fi
                for G in 435 436 437 438
                do
                        echo $G > export
                        echo out > gpio$G/direction
                done
        fi

        # lights out
        for G in 435 436 437 438
        do
                echo 0 > gpio$G/value
        done

        # simple daemon mode
        trap '' 1
        trap 'fini' 2 3 15
        exec 0</dev/null 1>/dev/null 2>&1

        # fd 3 = left LED green (disk), fd 4 = right LED yellow (net)
        exec 3>gpio435/value 4>gpio438/value

        # fd 5 = fifo for sleeping via builtin read
        mkfifo -m 0600 /tmp/fitlet2leds-fifo
        exec 5<>/tmp/fitlet2leds-fifo
        rm -f /tmp/fitlet2leds-fifo

        INIT=1
}

fini () {
        # restore green lights
        echo 1 > gpio435/value
        echo 0 > gpio436/value
        echo 1 > gpio437/value
        echo 0 > gpio438/value
        exit 0
}

checkactive () {
        ODSK=$DSK ; DSK=0
        ONET=$NET ; NET=0

        # tally all read+write to non-loop base block devices
        while read MAJ MIN NAM F4 F5 RD F7 F8 F9 WR FOO
        do
                case $MIN in 0) ;; *) continue ;; esac
                case $NAM in loop*) continue ;; esac
                DSK=$((DSK+RD+WR))
        done < /proc/diskstats

        # tally all in+out IP octets to physical network devices
        while read HDR F1 F2 F3 F4 F5 F6 IN OUT FOO
        do
                case $HDR in IpExt*) ;; *) continue ;; esac
                case $F1 in [0-9]*) ;; *) continue ;; esac
                NET=$((NET+IN+OUT))
        done < /proc/net/netstat
}

while :
do
        case $INIT in "") init ;; esac
        case $((DSK - ODSK)) in 0) ;; *) echo 1 >&3 ;; esac
        case $((NET - ONET)) in 0) ;; *) echo 1 >&4 ;; esac
        read -t .25 -u 5
        echo 0 >&3
        echo 0 >&4
        read -t .75 -u 5
        checkactive
done &
Last edited by TomT on Thu Feb 06, 2020 11:56 pm, edited 2 times in total.

irads

Re: Fitlet2 disk and network LED activity

Post by irads »

This looks extremely useful.
I believe it can be used as a good reference for utilizing the LEDs to indicate application specific events as well.
Thanks for sharing!

TomT
Posts: 6
Joined: Wed Feb 05, 2020 3:24 am

Re: Fitlet2 disk and network LED activity

Post by TomT »

Actually, there is a slight bug in my "sleep" approach, it makes bash into a cpu hog by reading /dev/zero. Oops. I fixed it by simply calling sleep, but plan to find a better fix, using only bash builtins again. Fixed inline above.

Anyway, glad it helps!

Mikael63
Posts: 74
Joined: Sun Aug 19, 2018 9:53 am

Re: Fitlet2 disk and network LED activity

Post by Mikael63 »

TomT wrote:I cooked up this simple bash shell script to flash the fitlet2 LEDs for network and disk activity under Linux. It's implemented almost entirely with bash builtins and runs as a background daemon.

It flashes the left LED "green" for disk activity, and the right LED "yellow" for network activity. It updates once per second. It cleans up, restoring the LEDs to green on exit. Run it at login, or configure as a service, for example: https://www.linode.com/docs/quick-answe ... e-at-boot/

It needs to run as root, in order to configure and write the gpios in /proc. But with a little system pre-configuration, that can be avoided. The script will check either way.

It's pretty simple and could be dressed up quite a lot. But, it works for my purposes. Enjoy!
Thanks!
At last some useful with the LEDs :geek:

(But I've got my MBM2Pro in the back of my monitor so I cant see the LEDs an easy way)
Linux Mint Vera 21.1 Cinnamon @ MBM2 Pro

TomT
Posts: 6
Joined: Wed Feb 05, 2020 3:24 am

Re: Fitlet2 disk and network LED activity

Post by TomT »

I edited the original post with tweaked code in the sleep loop to use bash builtins. It should run with a bit less overhead, and won't fork any short-term processes.

Post Reply

Return to “Cool stuff with fitlet2”