Yuri de Groot wrote:
> Well, do we get to see this script?
> Gee whiz, what a tease!
> You wet our appetites and don't even post the script!
Very well :) (see further down for how I also get the connection speed
on the display). All code here is... well... public domain now, I
suppose. Please preserve copyright if you use it.
I have done my best to work around the long-line formatting of my mailer.
To get other interface stats (eg eth0), you could look in /proc/net/dev
but the scripts would have to be arranged a bit differently.
Disclaimer: I am only a beginner at awk, and I'm better at awk than I am
at sh.
---
This bit is in /etc/ppp/ip-up (which pppd runs when the link becomes
active):
/usr/sbin/pppstats -a -w5 | awk -f /etc/lcd/pppstats_lcd.awk &
-a gives absolute values rather than deltas, -w5 gives you updates every
5 seconds.
---
And in /etc/ppp/ip-down I have:
/bin/killall pppstats
It probably dies by itself anyway when ppp0 goes down.
---
The awk script looks like this (notice I'm not using the metric
definition of a kb or a Mb:)
# pppstats_lcd.awk
# Copyright (c) D. Mann, 2002
# Takes the traffic stats from pppstats and formats them nicely.
# Bytes in is in $1, bytes out is in $7.
{
if($1 > 1048576)
traf_in = sprintf("%.2fMb", $1/1048576)
else
traf_in = sprintf("%.1fkb", $1/1024)
if($7 > 1048576)
traf_out = sprintf("%.2fMb", $7/1048576)
else
traf_out = sprintf("%.1fkb", $7/1024)
# pppstats prints a new header line every 20 or so lines.
# I need to close the pipe every time as it expects an EOF before the
# daemon will do anything with it. I should modify my code to look for a
# \n instead.
# When I modify the lcd source to look for \n, I may remove the close()
# statement.
if($1 != "IN") {
printf("%s in, %s out\n", traf_in, traf_out) > "/etc/lcd/lcd.pipe"
close("/etc/lcd/lcd.pipe")
}
}
---
Thats the bytes in/out done, now for the connection speed.
>From /usr/sbin/ppp-on (which is the "master" script that brings up the
connection):
# Re-create the chat report file (I am doing this to stop it growing too
# long).
rm /etc/ppp/chat-report
touch /etc/ppp/chat-report
---
>From my ppp-on-dialer script (no fancy graphical connection managers
here):
exec /usr/sbin/chat -r /etc/ppp/chat-report \
(subsequent lines contain other chat options which I won't clutter the
list with)
---
At this stage, /etc/ppp/chat-report contains this:
chat: Nov 08 20:37:36 CONNECT 33600/ARQ
Yes, I use a 33k modem.
---
>From ip-up:
STRING="PPP Link is UP:"
SPEED=`cat /etc/ppp/chat-report | /bin/cut -d\ -f7 | /bin/cut -d/ -f1 \
| awk -f /etc/lcd/pppspeed_lcd.awk`
MESSAGE=$STRING\ $SPEED
/usr/local/bin/lcd_client -w2 -l2 -v -jcentre -t"$MESSAGE"
---
The awk script... I am using a metric k this time, as thats how we
normally describe modems. Strictly speaking, I think I'm meant to use
'kBps' rather than 'kbps'.
# pppspeed_lcd.awk
# Copyright (c) D. Mann, 2002
# Formats the connection speed from chat into something nicer.
{
printf("%.1fkbps\n", $1/1000)
}
---
And in ip-down:
/usr/local/bin/lcd_client -k11 -k00 -w2 -l2 -jcentre -t"PPP Link is DOWN"
---
Easy!
Cheers,
- Dave
http://www.digistar.com/~dmann/