[rrd-users] Can this script be optimised ?
Simon Hobson
linux at thehobsons.co.uk
Thu Nov 29 12:44:51 CET 2012
Karl Fischer wrote:
>you can avoid a couple of external calls/forks and therefor
>save some ressources through the use of bash capabilities:
>(man bash, see Parameter Expansion & BUILTIN COMMANDS)
And I thought I was reasonably familiar with them :( Yet again, I
realise that the more you know, the more you realise you've missed
stuff.
>use /proc/uptime instead of $(date +%s)
>or - even better - let the filesystem do the job:
>touch /tmp/flag.$$ and compare with up2date file in /proc ...
>tmpfile=/tmp/ethupdate.$$.flag
>touch $tmpfile -d '+59 seconds'
>
>while [ $tmpfile -nt /proc/uptime ]
I set off writing :
That's not working for me, the timestamp on /proc/uptime doesn't seem
to update on this system ...
# date
Thu Nov 29 11:07:32 UTC 2012
# ls -l /proc/uptime
-r--r--r-- 1 root root 0 Nov 29 10:11 /proc/uptime
10:11 being the first time I looked at the file.
Then I checked in another terminal window, and suddenly it's working
- very odd :-/
>and instead of using 3 external binaries to get the byte counts
>(ifconfig/grep/sed) - how about using zero by fetching the
>values from /proc (assuming linux) ?
>
>UpdateVal=$( /sbin/ifconfig ethext | \
> /bin/grep 'RX bytes' | \
> /bin/sed -r -e 's/^.*RX bytes:([0-9]+) .* TX bytes:([0-9]+) .*$/\1:\2/' )
>
>can be written as:
>Utmp=$(< /proc/net/dev)
>Utmp=(${Utmp#*ethext:})
>UpdateVal=${Utmp[0]}:${Utmp[7]}
That certainly improves things, cuts a 1 minute run round the loop
from around 5s to 3s :)
>But instead of calling it every minute I'd convert it
>to a daemon-like script that never exits and start it
>through /etc/inittab - then you even save the time
>comparison since there's no end-condition (endless loop)
I did think about that, but for some reason never felt comfortable
having never ending shell scripts. Guess I never trusted myself not
to write something that would leak memory (or similar) and 'kill' the
system.
Plus, if CRON starts it periodically, if it dies then it'll start up
again before too long. Though of course, inittab can respawn it - I
think I'll go and try that now.
I was certainly thinking of making the run cycle longer.
--
Simon Hobson
Visit http://www.magpiesnestpublishing.co.uk/ for books by acclaimed
author Gladys Hobson. Novels - poetry - short stories - ideal as
Christmas stocking fillers. Some available as e-books.
More information about the rrd-users
mailing list