summaryrefslogtreecommitdiff
path: root/openwrt/package/rrdcollect/files/rrd.sh
blob: 3faa4fb8d7458972854d351d24729fd2e0de032e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/bin/sh
#
# Author:   Christian Rost (chr@baltic-online.de)
# Purpose:  Shell-Interface to rrdtool
#
RRDCNF=/etc/rrd.conf
. $RRDCNF

######################################
# init() function to create .rrd-file
# $1=VAR-PRAEFIX $2=VAR-POSTFIX 
######################################
init()
{
	# Calculate optimal step by assuming highest precision on the last-xhour plot
	OPTSTEP=`expr $HOURS \* 3600 \/ $PIXWIDTH \* $H_RES`
	[ $OPTSTEP != $STEP ] && echo "WARNING: Your optimal step is: $OPTSTEP. But you are using: $STEP"
	
	ARH_AVG=`expr $HOURS \* 3600   \/ $PIXWIDTH \* $H_RES \/ $STEP`; ARH_CNT=`expr $HOURS \* 3600   \/ $ARH_AVG \/ $STEP`
	ARD_AVG=`expr $DAYS  \* 86400  \/ $PIXWIDTH \* $D_RES \/ $STEP`; ARD_CNT=`expr $DAYS  \* 86400  \/ $ARD_AVG \/ $STEP`
	ARW_AVG=`expr $WEEKS \* 604800 \/ $PIXWIDTH \* $W_RES \/ $STEP`; ARW_CNT=`expr $WEEKS \* 604800 \/ $ARW_AVG \/ $STEP`
	
	PRAEFIX=$1; POSTFIX=$2
	DSOURCE=""
	
	echo "Init storage for $HOURS hours on averages: $ARH_AVG x $STEP seconds ($ARH_CNT total values)"
	echo "Init storage for $DAYS days on averages: $ARD_AVG x $STEP seconds ($ARD_CNT total values)"
	echo "Init storage for $WEEKS weeks on averages: $ARW_AVG x $STEP seconds ($ARW_CNT total values)"

	eval  SOURCE=\$${PRAEFIX}_${POSTFIX}
	for i in $SOURCE
	do
		eval var=\$${PRAEFIX}_${i}
		ifs="$IFS"; IFS="|"; set -- $var
		if  [ "`echo ${1} | cut -b1`" = "-" ]
		then
			VAR="`echo ${1} | cut -b2-`"
		else
			VAR=`echo $1 | sed "s/ //g"`
		fi
                DSOURCE="$DSOURCE DS:${VAR}:${3}:$MAXSTEP:0:${4}"
		IFS="$ifs"
        done
        $DEBUG $RRDTOOL create $RRDDIR/$PRAEFIX.rrd       \
                --step ${STEP}                            \
		$DSOURCE                                  \
                        RRA:AVERAGE:0.5:$ARH_AVG:$ARH_CNT \
                        RRA:AVERAGE:0.5:$ARD_AVG:$ARD_CNT \
                        RRA:AVERAGE:0.5:$ARW_AVG:$ARW_CNT \
                        RRA:MAX:0.5:$ARH_AVG:$ARH_CNT     \
                        RRA:MAX:0.5:$ARD_AVG:$ARD_CNT     \
                        RRA:MAX:0.5:$ARW_AVG:$ARW_CNT
	return $?
}

#################################################################################################################
# graph() function to create png-graphics
# $1=IMG-DIR $2=VAR-PRAEFIX, $3=VAR-POSTFIX, $4=Headline, $5=end, $6=start, $7=base, 
# $8=DEFS/ENTRIES $9=other rrdgraph options (Vertlabel,...)
#################################################################################################################
# COMMENT:"$DATE\c"
do_rrdgraph()
{	
        $DEBUG $NICE $RRDTOOL graph "$1/$2_$3.png" \
          --title          "$4"             \
          --imgformat      $PIXFORMAT       \
          --width          $PIXWIDTH        \
          --height         $PIXHEIGHT       \
          --end            "$5"             \
          --start          "$6"             \
	  --base "$7"   	            \
	  $8 $9
}

#################################################################################################################
# graph() function to create png-graphics
# $1=IMG-DIR $2=VAR-PRAEFIX, $3=VAR-POSTFIX, $4=Headline, $5=end, $6=start, $7=base, 
# $8=DEFS/ENTRIES $9=other rrdgraph options (Vertlabel,...)
#################################################################################################################
# COMMENT:"<RRD::TIME::NOW %c>\c"
do_rrdcgi()
{
	cat << EOF > $1/$2_$3.def
              
<RRD::GRAPH $1/<RRD::CV START>$2_$3.png \
--title          "$4"              \
--imgformat      $PIXFORMAT        \
--width          $PIXWIDTH         \
--height         $PIXHEIGHT        \
--end            "$5"              \
--start          <RRD::CV START>   \
--base 	         $7   	           \
--imginfo        "<IMG NAME=$2_$3 SRC=$RRDCGIIMGPATH/%s OnClick=zoomToggle('${SCGIPIXWIDTH}px','${NCGIPIXWIDTH}px',this); WIDTH=${SCGIPIXWIDTH}px>" \
--lazy                             \
$8 $9 >
EOF

	cat << EOF >> $RRDCGISCRIPT
$NEWLINE <TD valign=top>$4<br>
  <RRD::INCLUDE $1/$2_$3.def>
</TD>
EOF
}



gen_cgi_header()
{
	cat << EOF > $RRDCGISCRIPT
#!$RRDCGI
<HEAD><TITLE>RRDCGI Router Statistics</TITLE></HEAD>
<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">
function update_radio_check(robj,value) { for (var i=0; i<robj.length; i++) if (robj[i].value==value) robj[i].checked=1; }
var nW,oW;
function zoomToggle(iWideSmall,iWideLarge,whichImage){
oW=whichImage.style.width;
if(oW==iWideLarge){
nW=iWideSmall;}else{
nW=iWideLarge;}
whichImage.style.width=nW;
}
</SCRIPT>
<BODY onload="update_radio_check(document.RangeForm.START,'<RRD::CV START>')">
<H1>RRDCGI Router Statistics <font size-=2><em>(<RRD::TIME::NOW %c>)</em></font></H1>
<FORM NAME=RangeForm>
      <INPUT NAME=START TYPE=RADIO VALUE=end-${HOURS}h checked>${HOURS} hours ago
      <INPUT NAME=START TYPE=RADIO VALUE=end-${DAYS}d>${DAYS} days ago
      <INPUT NAME=START TYPE=RADIO VALUE=end-${WEEKS}w>${WEEKS} weeks ago
      <INPUT TYPE=SUBMIT>
</FORM>
<P>
<TABLE>
EOF
}

gen_cgi_footer()
{
	cat << EOF >> $RRDCGISCRIPT
</TABLE>
</P>
</BODY>
</HTML>
EOF
	chmod +x $RRDCGISCRIPT
}

#################################################################################################################
# $1=mode $2=VAR-PRAEFIX, $3=VAR-POSTFIX, $4=Headline, $5=Vertlabel, $6=start, $7=end, $8=base, $9=other rrdgraph options
#################################################################################################################
do_graph()
{
        MODE=$1; PRAEFIX=$2; POSTFIX=$3; HEADLINE=$4; VERTLABEL=$5; START=$6; END=$7; BASE=$8; SRC=${RRDDIR}/${PRAEFIX}.rrd
	if [ ! -r "$SRC" ] 
	then
		echo "$SRC not found. Aborting all operations now..."
		exit 1
	fi

	# For Graphing Memory BASE should be set to 1024, default is 1000
	[ -n "$8" ] && BASE=$8 || BASE=1000
	[ -n "$9" ] && OPTS=$9 || OPTS="--lower-limit 0"
	DEFS=""; ENTRIES=""
	eval  SOURCE=\$${PRAEFIX}_${POSTFIX}
	for i in $SOURCE
	do
		eval var=\$${PRAEFIX}_${i}
		ifs="$IFS"; IFS="|"; set -- $var
		if  [ "`echo ${1} | cut -b1`" = "-" ]
		then
			VAR="`echo ${1} | cut -b2-`"
			NEG=1
		else
			VAR=`echo $1 | sed "s/ //g"`
			NEG=0
		fi
		
		if [ "$NEG" = 1 ]	
		then
			[ -n "${8}" ] && DEFS="$DEFS DEF:${VAR}=$SRC:${VAR}:AVERAGE  DEF:MAX${VAR}=$SRC:${VAR}:MAX ${8} CDEF:N${VAR}C=${VAR}C,-1,* " \
			              || DEFS="$DEFS DEF:${VAR}=$SRC:${VAR}:AVERAGE  DEF:MAX${VAR}=$SRC:${VAR}:MAX CDEF:N${VAR}=${VAR},-1,*"
		else
			[ -n "${8}" ] && DEFS="$DEFS DEF:${VAR}=$SRC:${VAR}:AVERAGE  DEF:MAX${VAR}=$SRC:${VAR}:MAX ${8}" \
				      || DEFS="$DEFS DEF:${VAR}=$SRC:${VAR}:AVERAGE  DEF:MAX${VAR}=$SRC:${VAR}:MAX"
		fi
		# Now this is a bit ugly, but i don't know how to escape the blanks in a proper way.
		# If you know any better way to keep the blanks in the legend of the graphics please inform me.
		#L=`echo ${2} | tr -s "[= =]" '\014'`
		#L=`echo ${2} | tr -s "[= =]" '_'`
		L=`echo ${2} | sed "s/ /_/g"`
		if [ -n "${7}" ]
		then
			if [ -n "${8}" ]                     
			then
				[ "$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" \
					       ||  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"
			else
				[ "$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" \
					       ||  ENTRIES="$ENTRIES ${6}:${VAR}${5}:${L}:  GPRINT:${VAR}:AVERAGE:Average\:${7} GPRINT:MAX${VAR}:MAX:Max\:${7} GPRINT:${VAR}:LAST:Last\:${7}\\j"
			fi
		else
			[ "$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" \
       				       || 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"
		fi
		IFS="$ifs"
	done

	#[ "$POSTFIX" = "CPU_STATS" ] && (echo $OPTS; echo $DEFS ; echo $ENTRIES; echo $OPTS)

	$MODE "$IMGDIR" "$PRAEFIX" "$POSTFIX" "$HEADLINE" "$END" "$START" "$BASE" "$DEFS $ENTRIES"  "--vertical-label $VERTLABEL $OPTS"
}

# Check if all rrd-files are present 
# and create them if not
init_loop()
{
	for j in $ALL_PRAEFIXES
	do
		if [ ! -f $RRDDIR/$j.rrd ]  
		then
		        echo "Creating $RRDDIR/$j.rrd ..."
                        init $j ALL
                        [ ! -f $RRDDIR/$j.rrd ] && EXIT=1
		fi
	done
	[ "$EXIT" = 1 ] && return 1 || return 0
}


graph_loop()
{
	mode=$1; start=$2; end=$3; c=1 
	while [ $c -le $GRAPH_MAX ]
	do
		eval var=\$GRAPH_${c}
        	# $1=VAR-PRAEFIX $2=VAR-POSTFIX $3=Headline $4=Vertlabel $5=base $6=other rrdgraph options
		ifs="$IFS"; IFS="|"; set -- $var
		IFS="$ifs"
		[ "$1" != "$OLD" -o "`expr \( $c - 1 \) % 2`" = 0 ] && NEWLINE="</TR><TR>" || NEWLINE=""
		do_graph $mode $1 $2 "$3" "$4" $start $end $5 "$6"
		c=`expr $c + 1`; OLD=$1
	done
}


case "$1" in
	init)
			if init_loop 
			then
				exit 0
			else
			 	echo "Initialisation of rrd-database-files failed"
				exit 1
			fi
			;;
	cgi)
			echo "Updating $RRDCGISCRIPT and $IMGDIR/*.def"
			gen_cgi_header
			# The start-time will be not used
			graph_loop do_rrdcgi end now
			gen_cgi_footer
			;;
	images)
			# Create the graphs
			[ -n "$2" ] && STH=$2 || STH=24h
			echo "Updating Graphs ($STH ago -> now) for $RRDDIR/*.rrd"
			graph_loop do_rrdgraph "end-${STH}" now
			;;
	fetch)
			# Fetch data from rrdcollect daemon
			if [ ! -n "$3" ]
			then
				echo "Usage: $0 fetch <host> <port>"
				exit 1
			else
        			echo "Fetching ascii-logs from rrdcollect daemon"
        			(cd $RRDDIR && $NETCAT $2 $3 | sed -n "s:^\(update .*\):$RRDTOOL \1:p" | sh -x ) 2>&1 | wc -l #>/dev/null
				exit 0
			fi
			;;
	*)		echo "Usage: $0 <init|cgi|fetch|images>"
			echo "	init:   Initialize rrd-datafiles if neeeded"
			echo "	cgi:    Creates cgi-script for use with rrdcgi"
			echo "	fetch:  Fetching data from rrdcollect via netcat"
			echo "	images: Generates images"
			exit 1
			;;
esac
exit 0