View unanswered posts | View active topics It is currently Sat Feb 29, 2020 6:14 am



Reply to topic  [ 5 posts ] 
 Fitlet2 disk and network LED activity 
Author Message

Joined: Wed Feb 05, 2020 3:24 am
Posts: 6
Post Fitlet2 disk and network LED activity
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-answers/linux/start-service-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:
#!/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.



Wed Feb 05, 2020 4:02 pm
Profile
Site Admin

Joined: Mon Mar 16, 2009 2:11 pm
Posts: 2352
Post Re: Fitlet2 disk and network LED activity
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!

_________________
-------------------------
Irad Stavi
fit-PC forum admin

-------------------------


Wed Feb 05, 2020 5:26 pm
Profile

Joined: Wed Feb 05, 2020 3:24 am
Posts: 6
Post Re: Fitlet2 disk and network LED activity
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!


Wed Feb 05, 2020 5:34 pm
Profile

Joined: Sun Aug 19, 2018 9:53 am
Posts: 47
Location: North of Sweden
Post Re: Fitlet2 disk and network LED activity
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-answers/linux/start-service-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 Tricia (19.3) Cinnamon @ MBM2 Pro


Wed Feb 05, 2020 9:29 pm
Profile

Joined: Wed Feb 05, 2020 3:24 am
Posts: 6
Post Re: Fitlet2 disk and network LED activity
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.


Thu Feb 06, 2020 11:59 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 5 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.