Wednesday, April 25, 2007

Setting up the Keyboard LED's

OBJECTIVE

My headless ubuntu server does have a tiny keyboard plugged into it - I'd like to be able to see that the server has not crashed and see some information using the LED's on this keyboard. For example
- Network traffic.
- Hard Disk temperature
- CPU Load

There are many solutions to this, here is the one I used.

Install the ledcontrol package

sudo apt-get install ledcontrol hddtemp

This installs the LED Cotnrol software. Then we want to configure it.

sudo vi /etc/ledcontrol.conf



Add a Hard Disk Temperature monitoring LED

To add the a Hard Disk Temperature monitoring LED, need to add a simple script to

/usr/share/ledcontrol

Take a copy of one of the sample scripts,

file.sh load.sh netload.sh ping.sh ppp.sh size.sh startup.sh

and copy it to temp.sh, edit it to look like:

#!/bin/sh

led_temp () {
LOAD=`hddtemp -n /dev/sda`
SUCCESS="$SUCCESS $LOAD"
return 0
}


Now edit the config file for LED Control to include a call to this script

sudo vi /etc/ledcontrol.conf

Add these lines:

COMMAND_2="led_temp" # Command to give
SUCCESS_2="set c5 frequency 20 1000 40 50"
FAILURE_2="nop" # Not used, but must be present
DELAY_2=10 # Test every 10 seconds


Add a Network Load LED

The supplied netload.sh did not work for me, so I modified the ethstats package script and hacked it to only return a total. I created a script net.sh in /usr/share containing

#!/usr/bin/perl

# I received this without copyright, but have since been told by the owner
# that it is released into the public domain.
#
# Any changes I make are released into the public domain.
# - M. Drew Streib

$COLOR = 0;

#uncomment this and set $COLOR = 1 if you have the right Perl module
#
#if ($COLOR) {
# use Term::ANSIColor;
#}

$| = 1;

$period = 10;

#Inter-| Receive | Transmit
# face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed
# lo: 2356 32 0 0 0 0 0 0 2356 32 0 0 0 0 0 0
# eth0: 1217210 9400 0 0 0 8 0 11 1207648 8019 0 0 0 0 0 0
# eth1: 2039952 21982 6 0 0 6 0 0 47000710 34813 0 0 0 821 0 0

$addtime = 1 if($ARGV[0] eq "-t");

$op = $period;
$period = 1;
convert();
sleep $period;

convert();
print time()." " if($addtime==1);
foreach $dev(sort keys %kbin) {
printf "%7.2f", $kbin{$dev}+$kbout{$dev};
print "\n";
}

sub convert {
open(IN, "/proc/net/dev") || die("Can't open ip_acct: $!\n");
; ;
while($l = ) {
chop($l);
($dev, $rest) = split(/:/, $l);
$dev =~ s/\s//g;
$rest =~ s/^\s+//;
@devarr = split(/\s+/, $rest);
$bytesin{$dev} = @devarr[0]; $bytesout{$dev} = @devarr[8];
$packin{$dev} = @devarr[1]; $packout{$dev} = @devarr[9];
}
close(IN);

$numdevs = 0;
$tpackin = 0;
$tpackout = 0;
$tkbin = 0;
$tkbout = 0;

foreach $dev(sort keys %bytesin) {
next if($dev eq "lo");
$numdevs++;
# packets in/out
$packdiffin = ($packin{$dev} - $opackin{$dev});
$packdiffout = ($packout{$dev} - $opackout{$dev});
$packdiffin += 4294967296 if($packdiffin<0);
$packdiffout += 4294967296 if($packdiffout<0);
$opackin{$dev} = $packin{$dev};
$opackout{$dev} = $packout{$dev};
$packin{$dev} = $packdiffin / $period;
$packout{$dev} = $packdiffout / $period;

# bytes in/out
$diffin = ($bytesin{$dev} - $obytesin{$dev});
$diffout = ($bytesout{$dev} - $obytesout{$dev});
$diffin += 4294967296 if($diffin<0);
$diffout += 4294967296 if($diffout<0);
$obytesin{$dev} = $bytesin{$dev};
$obytesout{$dev} = $bytesout{$dev};
$kbin{$dev} = $diffin / $period / 1000000 * 8;
$kbout{$dev} = $diffout / $period / 1000000 * 8;

# increment totals
$tpackin += $packin{$dev};
$tpackout += $packout{$dev};
$tkbin += $kbin{$dev};
$tkbout += $kbout{$dev};
}
}

sub tquad {
$n = shift;
my $a = ($n & 0xff000000) >> 24;
my $b = ($n & 0x00ff0000) >> 16;
my $c = ($n & 0x0000ff00) >> 8;
my $d = $n & 0x000000ff;
return "$a.$b.$c.$d";
}

sub toquad {
$h = shift;
$h =~ /(\S\S)(\S\S)(\S\S)(\S\S)/;
$a = $1; $b = $2; $c = $3; $d = $4;
return sprintf("%d.%d.%d.%d", "0x$a", "0x$b", "0x$c", "0x$d");
}


Then I modified the existing /usr/share/ledcontrol/netload.sh to contain a call to this script

#!/bin/sh

led_netload () {
LOAD=`/usr/share/net.sh`
SUCCESS="$SUCCESS $LOAD"
return 0
}

and then the config file to include

COMMAND_3="led_netload" # Command to give
SUCCESS_3="set s7 dutycycle 500 0.8 50"
FAILURE_3="nop" # Not used, but must be present
DELAY_3=10 # Test every 10 seconds

No comments: