1
0

rrd.sh 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. # init() function to create .rrd-file
  10. # $1=VAR-PRAEFIX $2=VAR-POSTFIX
  11. ######################################
  12. init()
  13. {
  14. # Calculate optimal step by assuming highest precision on the last-xhour plot
  15. OPTSTEP=`expr $HOURS \* 3600 \/ $PIXWIDTH \* $H_RES`
  16. [ $OPTSTEP != $STEP ] && echo "WARNING: Your optimal step is: $OPTSTEP. But you are using: $STEP"
  17. ARH_AVG=`expr $HOURS \* 3600 \/ $PIXWIDTH \* $H_RES \/ $STEP`; ARH_CNT=`expr $HOURS \* 3600 \/ $ARH_AVG \/ $STEP`
  18. ARD_AVG=`expr $DAYS \* 86400 \/ $PIXWIDTH \* $D_RES \/ $STEP`; ARD_CNT=`expr $DAYS \* 86400 \/ $ARD_AVG \/ $STEP`
  19. ARW_AVG=`expr $WEEKS \* 604800 \/ $PIXWIDTH \* $W_RES \/ $STEP`; ARW_CNT=`expr $WEEKS \* 604800 \/ $ARW_AVG \/ $STEP`
  20. PRAEFIX=$1; POSTFIX=$2
  21. DSOURCE=""
  22. echo "Init storage for $HOURS hours on averages: $ARH_AVG x $STEP seconds ($ARH_CNT total values)"
  23. echo "Init storage for $DAYS days on averages: $ARD_AVG x $STEP seconds ($ARD_CNT total values)"
  24. echo "Init storage for $WEEKS weeks on averages: $ARW_AVG x $STEP seconds ($ARW_CNT total values)"
  25. eval SOURCE=\$${PRAEFIX}_${POSTFIX}
  26. for i in $SOURCE
  27. do
  28. eval var=\$${PRAEFIX}_${i}
  29. ifs="$IFS"; IFS="|"; set -- $var
  30. if [ "`echo ${1} | cut -b1`" = "-" ]
  31. then
  32. VAR="`echo ${1} | cut -b2-`"
  33. else
  34. VAR=`echo $1 | sed "s/ //g"`
  35. fi
  36. DSOURCE="$DSOURCE DS:${VAR}:${3}:$MAXSTEP:0:${4}"
  37. IFS="$ifs"
  38. done
  39. $DEBUG $RRDTOOL create $RRDDIR/$PRAEFIX.rrd \
  40. --step ${STEP} \
  41. $DSOURCE \
  42. RRA:AVERAGE:0.5:$ARH_AVG:$ARH_CNT \
  43. RRA:AVERAGE:0.5:$ARD_AVG:$ARD_CNT \
  44. RRA:AVERAGE:0.5:$ARW_AVG:$ARW_CNT \
  45. RRA:MAX:0.5:$ARH_AVG:$ARH_CNT \
  46. RRA:MAX:0.5:$ARD_AVG:$ARD_CNT \
  47. RRA:MAX:0.5:$ARW_AVG:$ARW_CNT
  48. return $?
  49. }
  50. #################################################################################################################
  51. # graph() function to create png-graphics
  52. # $1=IMG-DIR $2=VAR-PRAEFIX, $3=VAR-POSTFIX, $4=Headline, $5=end, $6=start, $7=base,
  53. # $8=DEFS/ENTRIES $9=other rrdgraph options (Vertlabel,...)
  54. #################################################################################################################
  55. # COMMENT:"$DATE\c"
  56. do_rrdgraph()
  57. {
  58. $DEBUG $NICE $RRDTOOL graph "$1/$2_$3.png" \
  59. --title "$4" \
  60. --imgformat $PIXFORMAT \
  61. --width $PIXWIDTH \
  62. --height $PIXHEIGHT \
  63. --end "$5" \
  64. --start "$6" \
  65. --base "$7" \
  66. $8 $9
  67. }
  68. #################################################################################################################
  69. # graph() function to create png-graphics
  70. # $1=IMG-DIR $2=VAR-PRAEFIX, $3=VAR-POSTFIX, $4=Headline, $5=end, $6=start, $7=base,
  71. # $8=DEFS/ENTRIES $9=other rrdgraph options (Vertlabel,...)
  72. #################################################################################################################
  73. # COMMENT:"<RRD::TIME::NOW %c>\c"
  74. do_rrdcgi()
  75. {
  76. cat << EOF > $1/$2_$3.def
  77. <RRD::GRAPH $1/<RRD::CV START>$2_$3.png \
  78. --title "$4" \
  79. --imgformat $PIXFORMAT \
  80. --width $PIXWIDTH \
  81. --height $PIXHEIGHT \
  82. --end "$5" \
  83. --start <RRD::CV START> \
  84. --base $7 \
  85. --imginfo "<IMG NAME=$2_$3 SRC=$RRDCGIIMGPATH/%s OnClick=zoomToggle('${SCGIPIXWIDTH}px','${NCGIPIXWIDTH}px',this); WIDTH=${SCGIPIXWIDTH}px>" \
  86. --lazy \
  87. $8 $9 >
  88. EOF
  89. cat << EOF >> $RRDCGISCRIPT
  90. $NEWLINE <TD valign=top>$4<br>
  91. <RRD::INCLUDE $1/$2_$3.def>
  92. </TD>
  93. EOF
  94. }
  95. gen_cgi_header()
  96. {
  97. cat << EOF > $RRDCGISCRIPT
  98. #!$RRDCGI
  99. <HEAD><TITLE>RRDCGI Router Statistics</TITLE></HEAD>
  100. <SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">
  101. function update_radio_check(robj,value) { for (var i=0; i<robj.length; i++) if (robj[i].value==value) robj[i].checked=1; }
  102. var nW,oW;
  103. function zoomToggle(iWideSmall,iWideLarge,whichImage){
  104. oW=whichImage.style.width;
  105. if(oW==iWideLarge){
  106. nW=iWideSmall;}else{
  107. nW=iWideLarge;}
  108. whichImage.style.width=nW;
  109. }
  110. </SCRIPT>
  111. <BODY onload="update_radio_check(document.RangeForm.START,'<RRD::CV START>')">
  112. <H1>RRDCGI Router Statistics <font size-=2><em>(<RRD::TIME::NOW %c>)</em></font></H1>
  113. <FORM NAME=RangeForm>
  114. <INPUT NAME=START TYPE=RADIO VALUE=end-${HOURS}h checked>${HOURS} hours ago
  115. <INPUT NAME=START TYPE=RADIO VALUE=end-${DAYS}d>${DAYS} days ago
  116. <INPUT NAME=START TYPE=RADIO VALUE=end-${WEEKS}w>${WEEKS} weeks ago
  117. <INPUT TYPE=SUBMIT>
  118. </FORM>
  119. <P>
  120. <TABLE>
  121. EOF
  122. }
  123. gen_cgi_footer()
  124. {
  125. cat << EOF >> $RRDCGISCRIPT
  126. </TABLE>
  127. </P>
  128. </BODY>
  129. </HTML>
  130. EOF
  131. chmod +x $RRDCGISCRIPT
  132. }
  133. #################################################################################################################
  134. # $1=mode $2=VAR-PRAEFIX, $3=VAR-POSTFIX, $4=Headline, $5=Vertlabel, $6=start, $7=end, $8=base, $9=other rrdgraph options
  135. #################################################################################################################
  136. do_graph()
  137. {
  138. MODE=$1; PRAEFIX=$2; POSTFIX=$3; HEADLINE=$4; VERTLABEL=$5; START=$6; END=$7; BASE=$8; SRC=${RRDDIR}/${PRAEFIX}.rrd
  139. if [ ! -r "$SRC" ]
  140. then
  141. echo "$SRC not found. Aborting all operations now..."
  142. exit 1
  143. fi
  144. # For Graphing Memory BASE should be set to 1024, default is 1000
  145. [ -n "$8" ] && BASE=$8 || BASE=1000
  146. [ -n "$9" ] && OPTS=$9 || OPTS="--lower-limit 0"
  147. DEFS=""; ENTRIES=""
  148. eval SOURCE=\$${PRAEFIX}_${POSTFIX}
  149. for i in $SOURCE
  150. do
  151. eval var=\$${PRAEFIX}_${i}
  152. ifs="$IFS"; IFS="|"; set -- $var
  153. if [ "`echo ${1} | cut -b1`" = "-" ]
  154. then
  155. VAR="`echo ${1} | cut -b2-`"
  156. NEG=1
  157. else
  158. VAR=`echo $1 | sed "s/ //g"`
  159. NEG=0
  160. fi
  161. if [ "$NEG" = 1 ]
  162. then
  163. [ -n "${8}" ] && DEFS="$DEFS DEF:${VAR}=$SRC:${VAR}:AVERAGE DEF:MAX${VAR}=$SRC:${VAR}:MAX ${8} CDEF:N${VAR}C=${VAR}C,-1,* " \
  164. || DEFS="$DEFS DEF:${VAR}=$SRC:${VAR}:AVERAGE DEF:MAX${VAR}=$SRC:${VAR}:MAX CDEF:N${VAR}=${VAR},-1,*"
  165. else
  166. [ -n "${8}" ] && DEFS="$DEFS DEF:${VAR}=$SRC:${VAR}:AVERAGE DEF:MAX${VAR}=$SRC:${VAR}:MAX ${8}" \
  167. || DEFS="$DEFS DEF:${VAR}=$SRC:${VAR}:AVERAGE DEF:MAX${VAR}=$SRC:${VAR}:MAX"
  168. fi
  169. # Now this is a bit ugly, but i don't know how to escape the blanks in a proper way.
  170. # If you know any better way to keep the blanks in the legend of the graphics please inform me.
  171. #L=`echo ${2} | tr -s "[= =]" '\014'`
  172. #L=`echo ${2} | tr -s "[= =]" '_'`
  173. L=`echo ${2} | sed "s/ /_/g"`
  174. if [ -n "${7}" ]
  175. then
  176. if [ -n "${8}" ]
  177. then
  178. [ "$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" \
  179. || 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"
  180. else
  181. [ "$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" \
  182. || ENTRIES="$ENTRIES ${6}:${VAR}${5}:${L}: GPRINT:${VAR}:AVERAGE:Average\:${7} GPRINT:MAX${VAR}:MAX:Max\:${7} GPRINT:${VAR}:LAST:Last\:${7}\\j"
  183. fi
  184. else
  185. [ "$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" \
  186. || 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"
  187. fi
  188. IFS="$ifs"
  189. done
  190. #[ "$POSTFIX" = "CPU_STATS" ] && (echo $OPTS; echo $DEFS ; echo $ENTRIES; echo $OPTS)
  191. $MODE "$IMGDIR" "$PRAEFIX" "$POSTFIX" "$HEADLINE" "$END" "$START" "$BASE" "$DEFS $ENTRIES" "--vertical-label $VERTLABEL $OPTS"
  192. }
  193. # Check if all rrd-files are present
  194. # and create them if not
  195. init_loop()
  196. {
  197. for j in $ALL_PRAEFIXES
  198. do
  199. if [ ! -f $RRDDIR/$j.rrd ]
  200. then
  201. echo "Creating $RRDDIR/$j.rrd ..."
  202. init $j ALL
  203. [ ! -f $RRDDIR/$j.rrd ] && EXIT=1
  204. fi
  205. done
  206. [ "$EXIT" = 1 ] && return 1 || return 0
  207. }
  208. graph_loop()
  209. {
  210. mode=$1; start=$2; end=$3; c=1
  211. while [ $c -le $GRAPH_MAX ]
  212. do
  213. eval var=\$GRAPH_${c}
  214. # $1=VAR-PRAEFIX $2=VAR-POSTFIX $3=Headline $4=Vertlabel $5=base $6=other rrdgraph options
  215. ifs="$IFS"; IFS="|"; set -- $var
  216. IFS="$ifs"
  217. [ "$1" != "$OLD" -o "`expr \( $c - 1 \) % 2`" = 0 ] && NEWLINE="</TR><TR>" || NEWLINE=""
  218. do_graph $mode $1 $2 "$3" "$4" $start $end $5 "$6"
  219. c=`expr $c + 1`; OLD=$1
  220. done
  221. }
  222. case "$1" in
  223. init)
  224. if init_loop
  225. then
  226. exit 0
  227. else
  228. echo "Initialisation of rrd-database-files failed"
  229. exit 1
  230. fi
  231. ;;
  232. cgi)
  233. echo "Updating $RRDCGISCRIPT and $IMGDIR/*.def"
  234. gen_cgi_header
  235. # The start-time will be not used
  236. graph_loop do_rrdcgi end now
  237. gen_cgi_footer
  238. ;;
  239. images)
  240. # Create the graphs
  241. [ -n "$2" ] && STH=$2 || STH=24h
  242. echo "Updating Graphs ($STH ago -> now) for $RRDDIR/*.rrd"
  243. graph_loop do_rrdgraph "end-${STH}" now
  244. ;;
  245. fetch)
  246. # Fetch data from rrdcollect daemon
  247. if [ ! -n "$3" ]
  248. then
  249. echo "Usage: $0 fetch <host> <port>"
  250. exit 1
  251. else
  252. echo "Fetching ascii-logs from rrdcollect daemon"
  253. (cd $RRDDIR && $NETCAT $2 $3 | sed -n "s:^\(update .*\):$RRDTOOL \1:p" | sh -x ) 2>&1 | wc -l #>/dev/null
  254. exit 0
  255. fi
  256. ;;
  257. *) echo "Usage: $0 <init|cgi|fetch|images>"
  258. echo " init: Initialize rrd-datafiles if neeeded"
  259. echo " cgi: Creates cgi-script for use with rrdcgi"
  260. echo " fetch: Fetching data from rrdcollect via netcat"
  261. echo " images: Generates images"
  262. exit 1
  263. ;;
  264. esac
  265. exit 0