[rrd-users] Can this script be optimised ?
Karl Fischer
rrd-users at ficos.de
Wed Nov 28 17:08:38 CET 2012
Simon,
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)
eg.
HostName=$( /usr/bin/head -1 /etc/hostname )
can be written as:
HostName=($(< /etc/hostname ))
or:
readarray -n 1 HostName < /etc/hostname
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 ...
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]}
so the total could be:
#!/bin/bash
HostName=($(< /etc/hostname ))
tmpfile=/tmp/ethupdate.$$.flag
touch $tmpfile -d '+59 seconds'
while [ $tmpfile -nt /proc/uptime ]
do
Utmp=$(< /proc/net/dev)
Utmp=(${Utmp#*ethext:})
/usr/bin/rrdtool update ${HostName[0]}/global.rrd \
--daemon w.x.y.z "N:${Utmp[0]}:${Utmp[7]}"
sleep 2
done
rm -f $tmpfile
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)
- Karl
> On 11/28/2012 07:23 AM, Simon Hobson wrote:
>> [ trimmed ]
>>
>> #!/bin/bash
>> # Get traffic count from interface counters of global interface
>>
>> EndTime=$(( `/bin/date +%s` + 58 ))
>> HostName=$( /usr/bin/head -1 /etc/hostname )
>> while [ $( /bin/date +%s ) -lt ${EndTime} ]
>> do
>> UpdateVal=$( /sbin/ifconfig ethext | \
>> /bin/grep 'RX bytes' | \
>> /bin/sed -r -e 's/^.*RX bytes:([0-9]+) .* TX bytes:([0-9]+) .*$/\1:\2/' )
>> /usr/bin/rrdtool update ${HostName}/global.rrd --daemon w.x.y.z
>> "N:${UpdateVal}"
>> sleep 2
>> done
More information about the rrd-users
mailing list