[rrd-users] Re: how do I do calculations to STDOUT?

Chris Majewski majewski at cs.ubc.ca
Tue May 21 20:13:24 MEST 2002


Try  "rrdtool  fetch".  I   enclose  sample  scripts  for  generating,
updating, and graphing rrd's of monthly router data.

-chris

#!/bin/bash
# create monthly rrd

if [ $# -ne 4 ]; then
    exit 2
fi

_year=$1
shift
_month=$1
_day=$2
_interface=$3
_rrdName=./rrd/monthly/$_year/$_month/$_day/$_interface.rrd
_days=`ls ./data/save/${_year}-${_month}/$_day`
_firstDay=
_start=
_tmpInDS=/tmp/InDS.$$
_tmpOutDS=/tmp/OutDS.$$
_tmpExec=/tmp/exec.$$
_rrdDirName=`dirname $_rrdName`
if [ ! -d $_rrdDirName ]; then
	mkdir -p $_rrdDirName
fi
for i in $_days
do
    if [ "$_firstDay" = "" ]; then
	_firstDay=$i
	declare -a _times
	_times=(`./bin/day-start-and-end.sh ${_year}-${_month}/$_firstDay`)
	_start=${_times[0]}
    fi
    echo -n "DS:${_day}_${i}_in:GAUGE:400:U:U RRA:AVERAGE:0.5:1:300" \
    >> $_tmpInDS
    echo -n "DS:${_day}_${i}_out:GAUGE:400:U:U RRA:AVERAGE:0.5:1:300" \
    >> $_tmpOutDS
    echo " \\" >> $_tmpInDS
    echo " \\" >> $_tmpOutDS
done

echo "rrdtool create $_rrdName --start $_start \\" > $_tmpExec
cat $_tmpInDS $_tmpOutDS >> $_tmpExec
echo "" >> $_tmpExec
echo "creating $_rrdName"
source $_tmpExec

rm -f $_tmpInDS $_tmpOutDS $_tmpExec

#!/bin/bash
# update monthly rrd

if [ $# -ne 4 ]; then
    exit 2
fi

_year=$1
shift
_month=$1
_whichDay=$2
_interface=$3
_rrdName=./rrd/monthly/$_year/$_month/$_whichDay/$_interface.rrd
_tmpInPrefix=/tmp/$_month.input.$$
_tmpOutPrefix=/tmp/$_month.output.$$
_tmpIns=
_tmpOuts=

# read data into tmp files
for i in `ls ./data/save/${_year}-${_month}/$_whichDay`
do
    _day=$i
    _tmpIn=$_tmpInPrefix.$_day
    _tmpOut=$_tmpOutPrefix.$_day
    _tmpIns="$_tmpIns $_tmpIn"
    _tmpOuts="$_tmpOuts $_tmpOut"
    declare -a _times
    _times=(`./bin/day-start-and-end.sh ${_year}-${_month}/$_day`)
    _start=${_times[0]}
    _end=${_times[1]}
    rrdtool fetch ./rrd/ifInput.$_interface.rrd AVERAGE \
    --start $_start --end $_end | tail +3 | awk -F: '{print $1,$2}' > $_tmpIn
    rrdtool fetch ./rrd/ifOutput.$_interface.rrd AVERAGE \
    --start $_start --end $_end | tail +3 | awk -F: '{print $1,$2}' > $_tmpOut
done

# feed data to rrd
echo "updating $_rrdName"
./bin/update-monthly.awk --assign rrd=$_rrdName $_tmpIns $_tmpOuts

# nuke tmp files
for i in $_tmpIns $_tmpOuts
do
    rm -f $i
done

#!/usr/bin/awk -f
# update monthly rrd (used by shell script above)

BEGIN {
    while ( getline < ARGV[1] == 1 ) {
	time = $1;
	data1 = $2;
	fileNo = 2;
	line=sprintf("%s:%d", time, data1);
	while ( fileNo < ARGC && ( getline < ARGV[fileNo] == 1 )) {
	    data = $2;
	    line=line sprintf(":%d", data);
	    fileNo++;
	}
	line=line sprintf("\n");
	system("rrdtool update "rrd" "line);
    }
}

#!/bin/sh
# sample script for graphing monthly data (Mondays)

if [ $# -ne 2 ]; then
    exit 2
fi

_month=$1
_interface=$2
_rrdName=$_month.$_interface.rrd
_tmp=/tmp/graph.$$

for i in `ls ./data/$_month/Mon`
do
    _monday=$i
    _ds=monday_${i}_in
    _def=mon_${i}_in
    echo "DEF:${_def}=./rrd/$_rrdName:$_ds:AVERAGE \" >> $_tmp
    echo "CDEF:

rrdtool graph ./rrd/$_rrdName ./img/foo.gif --start $_start --end $_end \

-chris

On Mon, 20 May 2002, John Giordano wrote:

>
>
> Hello,
>
> Just for everyone's edification- I have read and re-read the docs and also looked all over the web but I can't seem to find my answer.
>
> Basically, all I want to do is pull monthly bandwidth calculations from a .rrd in plain text so I can stuff them into a database.
>
> What I can do now is this:
>
> ./rrdtool graph foo.png --start -1m --imgformat PNG --title="Monthly UU.Net" --vertical-label "KiloBytes per Second" DEF:inoctets=myrouter.rrd:ds0:AVERAGE DEF:outoctets=myrouter.rrd:ds1:AVERAGE AREA:inoctets#00FF00:"In traffic" LINE1:outoctets#0000FF:"Out traffic"
>
> but I can't seem to figure out what the command syntax is to dump this to STDOUT.
>
> I read the doc on rrdgraph but I couldn't seem to figure out how I can make this simply go to STDOUT so I can use it in a custom Perl script I want to write.
>
> Any insight would be greatly appreciated.
>
> Thanks,
>
> jg
>
> --
> Unsubscribe mailto:rrd-users-request at list.ee.ethz.ch?subject=unsubscribe
> Help        mailto:rrd-users-request at list.ee.ethz.ch?subject=help
> Archive     http://www.ee.ethz.ch/~slist/rrd-users
> WebAdmin    http://www.ee.ethz.ch/~slist/lsg2.cgi
>

--
Unsubscribe mailto:rrd-users-request at list.ee.ethz.ch?subject=unsubscribe
Help        mailto:rrd-users-request at list.ee.ethz.ch?subject=help
Archive     http://www.ee.ethz.ch/~slist/rrd-users
WebAdmin    http://www.ee.ethz.ch/~slist/lsg2.cgi



More information about the rrd-users mailing list