LED color based on bandwidth usage

I like your ideas… they are worth trying…

I’ve updated the program to add color customization to “dualbandwidth” mode.
And added a config compatible with Turris Omnia 2019+

3 Likes

Bug fix for times when omnia-led-colors stops working due to iw returning an STDERR.

@rll May I kindly make you aware of Monitoring (Realtime Graphs, LEDs, Collectd) for LAN Interfaces Connected to Switch-chip?

Before I go on: thank you for creating the Lua scripts for the LED colors.

My intention was to use the script for the LAN interfaces also. However, I observed that the LEDs do not react to local traffic (when using TurrisOS 5.x), e.g. from LAN1 to LAN2.

Therefore I extended your script (I still use an older version) by the following code at the beginning of function gather_iface_bandwidth():

        for i=0, 4 do
                local bytes_in, bytes_out

                local handle = io.popen("ethtool -S lan" .. i .. " | grep in_good_octets")
                local in_good_octets = handle:read("*a")
                local handle = io.popen("ethtool -S lan" .. i .. " | grep out_octets")
                local out_octets =  handle:read("*a")
                handle:close()

                if string.find(in_good_octets, "%d") ~= nil then
                        bytes_in  = string.sub(in_good_octets, string.find(in_good_octets, "%d+"))
                end

                if string.find(out_octets, "%d") ~= nil then
                        bytes_out = string.sub(out_octets, string.find(out_octets, "%d+"))
                end

                if bytes_in ~= nil then
                        local iface = "lan"..i
                        last_data = data.wan[iface]
                        local this_data = {bytes_in=bytes_in, bytes_out=bytes_out}
                        if last_data ~= nil then
                                this_data.bps_in = (bytes_in - last_data.bytes_in)*8
                                this_data.bps_out = (bytes_out - last_data.bytes_out)*8
                        end
                        data.wan[iface] = this_data
                end

        end

A few lines later I changed

if bytes_in ~= nil then

to

if bytes_in ~= nil and (iface ~= "lan0") and (iface ~= "lan1") and (iface ~= "lan2") and (iface ~= "lan3") and (iface ~= "lan4") then

… not too pretty, but there is no in in Lua.

The prerequisite is that ethtool is installed.

Feel free to merge this into your code.

Note 1: Actually my intention was to make this generic by first reading all available interfaces from cat /proc/net/dev and then iterating over that array to call ethtool -S for each. However, ethtool -S creates different outputs depending on the interface. E.g. in_good_octets and out_octets do not exist for eth2. For eth2 those are called good_octets_received and good_octets_sent. Therefore I used the simple solution above.

Note 2: ethtool -S does not provide statistics for all interfaces. For example # ethtool -S lan4.10 returns no stats available while /proc/net/dev actually provides data.

2 Likes

Thank you should go to @ dpdrown :slight_smile:

Cool, I’ll have a look at your modifications.

Hey, guys, does this still work with Omnia? I’ve commented out everything in /etc/config/rainbow and I the only colour I get is green so far so I guess that something doesn’t happen here :slight_smile:

Will try to dig in more…

I am still using it on TOS 5.x branche. No issues so far, working pretty well. All green means, you are still under threshold (very possibly), so it is matter of configuration and setting the right values for your setup/situation. Also Lan1-4 will show only defined color no matter what, so you have to use (in my case) wan(eth1), pci1(tun_turris)/2(wlan0)/3(wlan1) and setup them…:

my_config
onfig led wan
        option colorfunction 'dualbandwidth'
        option interface 'eth1'
        option limit_in 200000000
        option limit_out 15000000

config led pci1
        option colorfunction 'dualbandwidth'
        option interface 'tun_turris'
        option limit_in 20000000
        option limit_out 15000000

config led pci2
        option colorfunction 'ath10k'
        option phy 'phy0'

config led pci3
        option colorfunction 'ath9k'
        option interface 'wlan1'

Did you create this by hand or did you use LuCI? It seems like the usual way through LuCI would create a config like:

config led
	option sysfs 'omnia-led:pci1'
	option colorfunction 'dualbandwidth'
	option interface 'tun_turris'
	option limit_in 20000000
	option limit_out 15000000

There is template config …

EDIT: I apparently just needed to install luabitop package and now it appears to work fine.

I just installed this on the latest turris OS, it’s a fairly vanilla install with just a few extra packages installed (wireguard, etc). I can’t get it to actually work, I’ve tried turning off the rainbow service which DOES make all the LED’s come on and only the WAN blinks. After that, I tried restarting the omnia-led-colors service a few times, and it appears to do nothing. Am I missing any steps? I’ve completed all the install steps, and have not had “the old version” installed ever, so I don’t need to uninstall it.

TIA,
Chance

For TOS 6.x omnia-led-colors needs to be changed:

    --- /usr/sbin/omnia-led-colors.orig	2020-10-13 15:55:36.141319126 +0200
    +++ /usr/sbin/omnia-led-colors	2022-10-22 18:13:18.790449467 +0200
    @@ -124,7 +124,8 @@
     end
     
     function write_led(ledname, r, g, b)
    -  local fd = io.open("/sys/class/leds/omnia-led\:"..ledname.."/color", "w")
    +  ledname = ledname:gsub("_","-")
    +  local fd = io.open("/sys/class/leds/rgb\:"..ledname.."/multi_intensity", "w")
       fd:write(r .. " " .. g .. " " .. b)
       fd:close()
     end

And in /etc/config/omnia-led-colors the led names pciX must be renamed to wlan_X.

2 Likes

@MASHtm Did these changes alone do the magic for you?

I tried to adapt the script (before reading this post) by the following changes:

The LEDs need to be addressed by a different “file”:

  • local fd = io.open("/sys/class/leds/rgb\:"..ledname.."/multi_intensity", "w")
  • In /etc/config/omnia-led-colors I changed, for example, lan0 to lan-0

However, the LED color based on the bandwidth does not work.

I enabled print(s) in function debug_print(s) and added an output (debug_print("x")) in function setled(led) . As there is no output, it seems like function setled(led) is never called.

Any ideas?

You need the “gsub” line as well and lan-0 has to be lan_0 with “_”. uci does not allow names with “-”. Honestly I do not use it for lan devices, only for wan and wlan. So YMMV.

2 Likes

Thank you very much! That did the magic. :slight_smile: (Together with the gsub line.)