3 # Author: Christian Rost (chr@baltic-online.de)
4 # Purpose: Shell-Interface to rrdtool
9 ######################################
10 # init() function to create .rrd-file
11 # $1=VAR-PRAEFIX $2=VAR-POSTFIX
12 ######################################
15 # Calculate optimal step by assuming highest precision on the last-xhour plot
16 OPTSTEP
=`expr $HOURS \* 3600 \/ $PIXWIDTH \* $H_RES`
17 [ $OPTSTEP != $STEP ] && echo "WARNING: Your optimal step is: $OPTSTEP. But you are using: $STEP"
19 ARH_AVG
=`expr $HOURS \* 3600 \/ $PIXWIDTH \* $H_RES \/ $STEP`; ARH_CNT
=`expr $HOURS \* 3600 \/ $ARH_AVG \/ $STEP`
20 ARD_AVG
=`expr $DAYS \* 86400 \/ $PIXWIDTH \* $D_RES \/ $STEP`; ARD_CNT
=`expr $DAYS \* 86400 \/ $ARD_AVG \/ $STEP`
21 ARW_AVG
=`expr $WEEKS \* 604800 \/ $PIXWIDTH \* $W_RES \/ $STEP`; ARW_CNT
=`expr $WEEKS \* 604800 \/ $ARW_AVG \/ $STEP`
23 PRAEFIX
=$1; POSTFIX
=$2
26 echo "Init storage for $HOURS hours on averages: $ARH_AVG x $STEP seconds ($ARH_CNT total values)"
27 echo "Init storage for $DAYS days on averages: $ARD_AVG x $STEP seconds ($ARD_CNT total values)"
28 echo "Init storage for $WEEKS weeks on averages: $ARW_AVG x $STEP seconds ($ARW_CNT total values)"
30 eval SOURCE
=\$
${PRAEFIX}_
${POSTFIX}
33 eval var
=\$
${PRAEFIX}_
${i}
34 ifs
="$IFS"; IFS
="|"; set -- $var
35 if [ "`echo ${1} | cut -b1`" = "-" ]
37 VAR
="`echo ${1} | cut -b2-`"
39 VAR
=`echo $1 | sed "s/ //g"`
41 DSOURCE
="$DSOURCE DS:${VAR}:${3}:$MAXSTEP:0:${4}"
44 $DEBUG $RRDTOOL create
$RRDDIR/$PRAEFIX.rrd \
47 RRA
:AVERAGE
:0.5:$ARH_AVG:$ARH_CNT \
48 RRA
:AVERAGE
:0.5:$ARD_AVG:$ARD_CNT \
49 RRA
:AVERAGE
:0.5:$ARW_AVG:$ARW_CNT \
50 RRA
:MAX
:0.5:$ARH_AVG:$ARH_CNT \
51 RRA
:MAX
:0.5:$ARD_AVG:$ARD_CNT \
52 RRA
:MAX
:0.5:$ARW_AVG:$ARW_CNT
56 #################################################################################################################
57 # graph() function to create png-graphics
58 # $1=IMG-DIR $2=VAR-PRAEFIX, $3=VAR-POSTFIX, $4=Headline, $5=end, $6=start, $7=base,
59 # $8=DEFS/ENTRIES $9=other rrdgraph options (Vertlabel,...)
60 #################################################################################################################
64 $DEBUG $NICE $RRDTOOL graph
"$1/$2_$3.png" \
66 --imgformat $PIXFORMAT \
75 #################################################################################################################
76 # graph() function to create png-graphics
77 # $1=IMG-DIR $2=VAR-PRAEFIX, $3=VAR-POSTFIX, $4=Headline, $5=end, $6=start, $7=base,
78 # $8=DEFS/ENTRIES $9=other rrdgraph options (Vertlabel,...)
79 #################################################################################################################
80 # COMMENT:"<RRD::TIME::NOW %c>\c"
83 cat << EOF > $1/$2_$3.def
85 <RRD::GRAPH $1/<RRD::CV START>$2_$3.png \
87 --imgformat $PIXFORMAT \
91 --start <RRD::CV START> \
93 --imginfo "<IMG NAME=$2_$3 SRC=$RRDCGIIMGPATH/%s OnClick=zoomToggle('${SCGIPIXWIDTH}px','${NCGIPIXWIDTH}px',this); WIDTH=${SCGIPIXWIDTH}px>" \
98 cat << EOF >> $RRDCGISCRIPT
99 $NEWLINE <TD valign=top>$4<br>
100 <RRD::INCLUDE $1/$2_$3.def>
109 cat << EOF > $RRDCGISCRIPT
111 <HEAD><TITLE>RRDCGI Router Statistics</TITLE></HEAD>
112 <SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">
113 function update_radio_check(robj,value) { for (var i=0; i<robj.length; i++) if (robj[i].value==value) robj[i].checked=1; }
115 function zoomToggle(iWideSmall,iWideLarge,whichImage){
116 oW=whichImage.style.width;
120 whichImage.style.width=nW;
123 <BODY onload="update_radio_check(document.RangeForm.START,'<RRD::CV START>')">
124 <H1>RRDCGI Router Statistics <font size-=2><em>(<RRD::TIME::NOW %c>)</em></font></H1>
125 <FORM NAME=RangeForm>
126 <INPUT NAME=START TYPE=RADIO VALUE=end-${HOURS}h checked>${HOURS} hours ago
127 <INPUT NAME=START TYPE=RADIO VALUE=end-${DAYS}d>${DAYS} days ago
128 <INPUT NAME=START TYPE=RADIO VALUE=end-${WEEKS}w>${WEEKS} weeks ago
138 cat << EOF >> $RRDCGISCRIPT
144 chmod +x
$RRDCGISCRIPT
147 #################################################################################################################
148 # $1=mode $2=VAR-PRAEFIX, $3=VAR-POSTFIX, $4=Headline, $5=Vertlabel, $6=start, $7=end, $8=base, $9=other rrdgraph options
149 #################################################################################################################
152 MODE
=$1; PRAEFIX
=$2; POSTFIX
=$3; HEADLINE
=$4; VERTLABEL
=$5; START
=$6; END
=$7; BASE
=$8; SRC
=${RRDDIR}/${PRAEFIX}.rrd
155 echo "$SRC not found. Aborting all operations now..."
159 # For Graphing Memory BASE should be set to 1024, default is 1000
160 [ -n "$8" ] && BASE
=$8 || BASE
=1000
161 [ -n "$9" ] && OPTS
=$9 || OPTS
="--lower-limit 0"
163 eval SOURCE
=\$
${PRAEFIX}_
${POSTFIX}
166 eval var
=\$
${PRAEFIX}_
${i}
167 ifs
="$IFS"; IFS
="|"; set -- $var
168 if [ "`echo ${1} | cut -b1`" = "-" ]
170 VAR
="`echo ${1} | cut -b2-`"
173 VAR
=`echo $1 | sed "s/ //g"`
179 [ -n "${8}" ] && DEFS="$DEFS DEF:${VAR}=$SRC:${VAR}:AVERAGE DEF:MAX${VAR}=$SRC:${VAR}:MAX ${8} CDEF:N${VAR}C=${VAR}C,-1,* " \
180 || DEFS
="$DEFS DEF:${VAR}=$SRC:${VAR}:AVERAGE DEF:MAX${VAR}=$SRC:${VAR}:MAX CDEF:N${VAR}=${VAR},-1,*"
182 [ -n "${8}" ] && DEFS="$DEFS DEF:${VAR}=$SRC:${VAR}:AVERAGE DEF:MAX${VAR}=$SRC:${VAR}:MAX ${8}" \
183 || DEFS
="$DEFS DEF:${VAR}=$SRC:${VAR}:AVERAGE DEF:MAX${VAR}=$SRC:${VAR}:MAX"
185 # Now this is a bit ugly, but i don't know how to escape the blanks in a proper way.
186 # If you know any better way to keep the blanks in the legend of the graphics please inform me.
187 #L=`echo ${2} | tr -s "[= =]" '\014'`
188 #L=`echo ${2} | tr -s "[= =]" '_'`
189 L
=`echo ${2} | sed "s/ /_/g"`
194 [ "$NEG" = 1 ] && ENTRIES
="$ENTRIES ${6}:N${VAR}C${5}:${L}: GPRINT:${VAR}C:AVERAGE:Average\:${7} GPRINT:MAX${VAR}C:MAX:Max\:${7} GPRINT:${VAR}C:LAST:Last\:${7}\\j" \
195 || ENTRIES
="$ENTRIES ${6}:${VAR}C${5}:${L}: GPRINT:${VAR}C:AVERAGE:Average\:${7} GPRINT:MAX${VAR}C:MAX:Max\:${7} GPRINT:${VAR}C:LAST:Last\:${7}\\j"
197 [ "$NEG" = 1 ] && ENTRIES
="$ENTRIES ${6}:N${VAR}${5}:${L}: GPRINT:${VAR}:AVERAGE:Average\:${7} GPRINT:MAX${VAR}:MAX:Max\:${7} GPRINT:${VAR}:LAST:Last\:${7}\\j" \
198 || ENTRIES
="$ENTRIES ${6}:${VAR}${5}:${L}: GPRINT:${VAR}:AVERAGE:Average\:${7} GPRINT:MAX${VAR}:MAX:Max\:${7} GPRINT:${VAR}:LAST:Last\:${7}\\j"
201 [ "$NEG" = 1 ] && ENTRIES
="$ENTRIES ${6}:N${VAR}${5}:${L}: GPRINT:${VAR}:AVERAGE:Average\:%6.2lf%s GPRINT:MAX${VAR}:MAX:Max\:%6.0lf%s GPRINT:${VAR}:LAST:Last\:%6.2lf%s\\j" \
202 || ENTRIES
="$ENTRIES ${6}:${VAR}${5}:${L}: GPRINT:${VAR}:AVERAGE:Average\:%6.2lf%s GPRINT:MAX${VAR}:MAX:Max\:%6.0lf%s GPRINT:${VAR}:LAST:Last\:%6.2lf%s\\j"
207 #[ "$POSTFIX" = "CPU_STATS" ] && (echo $OPTS; echo $DEFS ; echo $ENTRIES; echo $OPTS)
209 $MODE "$IMGDIR" "$PRAEFIX" "$POSTFIX" "$HEADLINE" "$END" "$START" "$BASE" "$DEFS $ENTRIES" "--vertical-label $VERTLABEL $OPTS"
212 # Check if all rrd-files are present
213 # and create them if not
216 for j
in $ALL_PRAEFIXES
218 if [ ! -f $RRDDIR/$j.rrd
]
220 echo "Creating $RRDDIR/$j.rrd ..."
222 [ ! -f $RRDDIR/$j.rrd
] && EXIT
=1
225 [ "$EXIT" = 1 ] && return 1 ||
return 0
231 mode
=$1; start
=$2; end
=$3; c
=1
232 while [ $c -le $GRAPH_MAX ]
234 eval var
=\
$GRAPH_${c}
235 # $1=VAR-PRAEFIX $2=VAR-POSTFIX $3=Headline $4=Vertlabel $5=base $6=other rrdgraph options
236 ifs
="$IFS"; IFS
="|"; set -- $var
238 [ "$1" != "$OLD" -o "`expr \( $c - 1 \) % 2`" = 0 ] && NEWLINE
="</TR><TR>" || NEWLINE
=""
239 do_graph
$mode $1 $2 "$3" "$4" $start $end $5 "$6"
240 c
=`expr $c + 1`; OLD
=$1
251 echo "Initialisation of rrd-database-files failed"
256 echo "Updating $RRDCGISCRIPT and $IMGDIR/*.def"
258 # The start-time will be not used
259 graph_loop do_rrdcgi end now
264 [ -n "$2" ] && STH
=$2 || STH
=24h
265 echo "Updating Graphs ($STH ago -> now) for $RRDDIR/*.rrd"
266 graph_loop do_rrdgraph
"end-${STH}" now
269 # Fetch data from rrdcollect daemon
272 echo "Usage: $0 fetch <host> <port>"
275 echo "Fetching ascii-logs from rrdcollect daemon"
276 (cd $RRDDIR && $NETCAT $2 $3 |
sed -n "s:^\(update .*\):$RRDTOOL \1:p" | sh
-x ) 2>&1 |
wc -l #>/dev/null
280 *) echo "Usage: $0 <init|cgi|fetch|images>"
281 echo " init: Initialize rrd-datafiles if neeeded"
282 echo " cgi: Creates cgi-script for use with rrdcgi"
283 echo " fetch: Fetching data from rrdcollect via netcat"
284 echo " images: Generates images"