port over more init scripts
[openwrt/svn-archive/archive.git] / utils / rrdcollect / files / rrd.sh
1 #!/bin/sh
2 #
3 # Author: Christian Rost (chr@baltic-online.de)
4 # Purpose: Shell-Interface to rrdtool
5 #
6 RRDCNF=/etc/rrd.conf
7 . $RRDCNF
8
9 ######################################
10 # init() function to create .rrd-file
11 # $1=VAR-PRAEFIX $2=VAR-POSTFIX
12 ######################################
13 init()
14 {
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"
18
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`
22
23 PRAEFIX=$1; POSTFIX=$2
24 DSOURCE=""
25
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)"
29
30 eval SOURCE=\$${PRAEFIX}_${POSTFIX}
31 for i in $SOURCE
32 do
33 eval var=\$${PRAEFIX}_${i}
34 ifs="$IFS"; IFS="|"; set -- $var
35 if [ "`echo ${1} | cut -b1`" = "-" ]
36 then
37 VAR="`echo ${1} | cut -b2-`"
38 else
39 VAR=`echo $1 | sed "s/ //g"`
40 fi
41 DSOURCE="$DSOURCE DS:${VAR}:${3}:$MAXSTEP:0:${4}"
42 IFS="$ifs"
43 done
44 $DEBUG $RRDTOOL create $RRDDIR/$PRAEFIX.rrd \
45 --step ${STEP} \
46 $DSOURCE \
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
53 return $?
54 }
55
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 #################################################################################################################
61 # COMMENT:"$DATE\c"
62 do_rrdgraph()
63 {
64 $DEBUG $NICE $RRDTOOL graph "$1/$2_$3.png" \
65 --title "$4" \
66 --imgformat $PIXFORMAT \
67 --width $PIXWIDTH \
68 --height $PIXHEIGHT \
69 --end "$5" \
70 --start "$6" \
71 --base "$7" \
72 $8 $9
73 }
74
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"
81 do_rrdcgi()
82 {
83 cat << EOF > $1/$2_$3.def
84
85 <RRD::GRAPH $1/<RRD::CV START>$2_$3.png \
86 --title "$4" \
87 --imgformat $PIXFORMAT \
88 --width $PIXWIDTH \
89 --height $PIXHEIGHT \
90 --end "$5" \
91 --start <RRD::CV START> \
92 --base $7 \
93 --imginfo "<IMG NAME=$2_$3 SRC=$RRDCGIIMGPATH/%s OnClick=zoomToggle('${SCGIPIXWIDTH}px','${NCGIPIXWIDTH}px',this); WIDTH=${SCGIPIXWIDTH}px>" \
94 --lazy \
95 $8 $9 >
96 EOF
97
98 cat << EOF >> $RRDCGISCRIPT
99 $NEWLINE <TD valign=top>$4<br>
100 <RRD::INCLUDE $1/$2_$3.def>
101 </TD>
102 EOF
103 }
104
105
106
107 gen_cgi_header()
108 {
109 cat << EOF > $RRDCGISCRIPT
110 #!$RRDCGI
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; }
114 var nW,oW;
115 function zoomToggle(iWideSmall,iWideLarge,whichImage){
116 oW=whichImage.style.width;
117 if(oW==iWideLarge){
118 nW=iWideSmall;}else{
119 nW=iWideLarge;}
120 whichImage.style.width=nW;
121 }
122 </SCRIPT>
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
129 <INPUT TYPE=SUBMIT>
130 </FORM>
131 <P>
132 <TABLE>
133 EOF
134 }
135
136 gen_cgi_footer()
137 {
138 cat << EOF >> $RRDCGISCRIPT
139 </TABLE>
140 </P>
141 </BODY>
142 </HTML>
143 EOF
144 chmod +x $RRDCGISCRIPT
145 }
146
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 #################################################################################################################
150 do_graph()
151 {
152 MODE=$1; PRAEFIX=$2; POSTFIX=$3; HEADLINE=$4; VERTLABEL=$5; START=$6; END=$7; BASE=$8; SRC=${RRDDIR}/${PRAEFIX}.rrd
153 if [ ! -r "$SRC" ]
154 then
155 echo "$SRC not found. Aborting all operations now..."
156 exit 1
157 fi
158
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"
162 DEFS=""; ENTRIES=""
163 eval SOURCE=\$${PRAEFIX}_${POSTFIX}
164 for i in $SOURCE
165 do
166 eval var=\$${PRAEFIX}_${i}
167 ifs="$IFS"; IFS="|"; set -- $var
168 if [ "`echo ${1} | cut -b1`" = "-" ]
169 then
170 VAR="`echo ${1} | cut -b2-`"
171 NEG=1
172 else
173 VAR=`echo $1 | sed "s/ //g"`
174 NEG=0
175 fi
176
177 if [ "$NEG" = 1 ]
178 then
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,*"
181 else
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"
184 fi
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"`
190 if [ -n "${7}" ]
191 then
192 if [ -n "${8}" ]
193 then
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"
196 else
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"
199 fi
200 else
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"
203 fi
204 IFS="$ifs"
205 done
206
207 #[ "$POSTFIX" = "CPU_STATS" ] && (echo $OPTS; echo $DEFS ; echo $ENTRIES; echo $OPTS)
208
209 $MODE "$IMGDIR" "$PRAEFIX" "$POSTFIX" "$HEADLINE" "$END" "$START" "$BASE" "$DEFS $ENTRIES" "--vertical-label $VERTLABEL $OPTS"
210 }
211
212 # Check if all rrd-files are present
213 # and create them if not
214 init_loop()
215 {
216 for j in $ALL_PRAEFIXES
217 do
218 if [ ! -f $RRDDIR/$j.rrd ]
219 then
220 echo "Creating $RRDDIR/$j.rrd ..."
221 init $j ALL
222 [ ! -f $RRDDIR/$j.rrd ] && EXIT=1
223 fi
224 done
225 [ "$EXIT" = 1 ] && return 1 || return 0
226 }
227
228
229 graph_loop()
230 {
231 mode=$1; start=$2; end=$3; c=1
232 while [ $c -le $GRAPH_MAX ]
233 do
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
237 IFS="$ifs"
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
241 done
242 }
243
244
245 case "$1" in
246 init)
247 if init_loop
248 then
249 exit 0
250 else
251 echo "Initialisation of rrd-database-files failed"
252 exit 1
253 fi
254 ;;
255 cgi)
256 echo "Updating $RRDCGISCRIPT and $IMGDIR/*.def"
257 gen_cgi_header
258 # The start-time will be not used
259 graph_loop do_rrdcgi end now
260 gen_cgi_footer
261 ;;
262 images)
263 # Create the graphs
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
267 ;;
268 fetch)
269 # Fetch data from rrdcollect daemon
270 if [ ! -n "$3" ]
271 then
272 echo "Usage: $0 fetch <host> <port>"
273 exit 1
274 else
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
277 exit 0
278 fi
279 ;;
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"
285 exit 1
286 ;;
287 esac
288 exit 0