slitaz-forge rev 134

chub: Try to fix RRD script
author Christophe Lincoln <pankso@slitaz.org>
date Thu Mar 15 05:18:50 2012 +0100 (2012-03-15)
parents 90f3032b61f2
children 2d34850550df
files chub/chub chub/web/lib/chubrrd chub/web/lib/makegraphs
line diff
     1.1 --- a/chub/chub	Thu Mar 15 05:06:23 2012 +0100
     1.2 +++ b/chub/chub	Thu Mar 15 05:18:50 2012 +0100
     1.3 @@ -50,7 +50,8 @@
     1.4  		echo "======== Connected users ========"
     1.5  		who ;;
     1.6  	rrd)
     1.7 -		$VHOST/lib/chubrrd >/dev/null ;;
     1.8 +		echo "Making RRD graphs images..."
     1.9 +		$VHOST/lib/makegraphs >/dev/null ;;
    1.10  	backup)
    1.11  		echo "TODO: backup MySQL, SCN files, etc" ;;
    1.12  	*)
     2.1 --- a/chub/web/lib/chubrrd	Thu Mar 15 05:06:23 2012 +0100
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,331 +0,0 @@
     2.4 -#!/bin/sh
     2.5 -#
     2.6 -# */5  * * * * /home/slitaz/www/chub/lib/chubrrd >/dev/null
     2.7 -# */5  * * * * /usr/bin/chub rrd >/dev/null
     2.8 -#
     2.9 -. /etc/slitaz/chub.conf || exit 1
    2.10 -
    2.11 -# RRD database directory
    2.12 -rrdlog="$VHOST/log/rrd"
    2.13 -
    2.14 -# Images directory
    2.15 -rrdgraph="$VHOST/images/rdd"
    2.16 -
    2.17 -# Colors
    2.18 -rrdcolors="--color SHADEA#FFFFFF --color SHADEB#FFFFFF --color BACK#FFFFFF" 
    2.19 -rrdgraphargs="-aPNG -i -z --alt-y-grid -w 600 -h 100 -r $rrdcolors"
    2.20 -
    2.21 -[ -d $rrdlog ] || mkdir -p $rrdlog
    2.22 -[ -d $rrdgraph ] || mkdir -p $rrdgraph
    2.23 -
    2.24 -updatecpudata() {
    2.25 -	[ -e "$rrdlog/cpu.rrd" ] || rrdtool create $rrdlog/cpu.rrd --step=300 \
    2.26 -			DS:user:COUNTER:600:0:500000000 \
    2.27 -			DS:nice:COUNTER:600:0:500000000 \
    2.28 -			DS:system:COUNTER:600:0:500000000 \
    2.29 -			DS:idle:COUNTER:600:0:500000000 \
    2.30 -			DS:iowait:COUNTER:600:0:500000000 \
    2.31 -			DS:irq:COUNTER:600:0:500000000 \
    2.32 -			DS:softirq:COUNTER:600:0:500000000 \
    2.33 -			DS:celsius:GAUGE:600:0:50000 \
    2.34 -			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
    2.35 -			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
    2.36 -	grep '^cpu' /proc/stat | while read cpu user nice system idle iowait irq softirq misc; do
    2.37 -		celsius=$(find /sys | grep /temp._input | xargs cat | \
    2.38 -			awk '{ if ($0 > max) max=$0 } END { print max/1 }')
    2.39 -		rrdtool update $rrdlog/cpu.rrd \
    2.40 -			-t celsius:nice:user:system:idle:iowait:irq:softirq \
    2.41 -			N:$celsius:$nice:$user:$system:$idle:$iowait:$irq:$softirq
    2.42 -		break
    2.43 -	done
    2.44 -}
    2.45 -
    2.46 -updatecpugraph() {
    2.47 -	period=$1
    2.48 -	info="$(grep '^model name' /proc/cpuinfo | cut -d: -f2 \
    2.49 -		| sed 's/ * / /g' | awk '
    2.50 -{ s=$0 ; n++ }                    
    2.51 -END { if (n > 1) printf " %dx",n; print s }')"
    2.52 -	rrdtool graph "$rrdgraph/cpu-$period.png" --start -1$period \
    2.53 -		$rrdgraphargs -l 0 -u 100 -t "cpu usage per $period [$info ]" \
    2.54 -		DEF:user=$rrdlog/cpu.rrd:user:AVERAGE \
    2.55 -		DEF:system=$rrdlog/cpu.rrd:system:AVERAGE \
    2.56 -		DEF:idle=$rrdlog/cpu.rrd:idle:AVERAGE \
    2.57 -		DEF:nice=$rrdlog/cpu.rrd:nice:AVERAGE \
    2.58 -		DEF:celsius=$rrdlog/cpu.rrd:celsius:AVERAGE \
    2.59 -		'CDEF:total=user,system,idle,nice,+,+,+' \
    2.60 -		'CDEF:userpct=100,user,total,/,*' \
    2.61 -		'CDEF:systempct=100,system,total,/,*' \
    2.62 -		'CDEF:idlepct=100,idle,total,/,*' \
    2.63 -		'CDEF:nicepct=100,nice,total,/,*' \
    2.64 -		'CDEF:temp=celsius,1000,/' \
    2.65 -		'AREA:userpct#0000FF:user cpu usage' \
    2.66 -		'STACK:nicepct#C0C0FF:nice cpu usage' \
    2.67 -		'STACK:systempct#FF0000:system cpu usage' \
    2.68 -		'STACK:idlepct#00FF00:idle cpu usage' \
    2.69 -		'LINE1:temp#000000:temperature\g' \
    2.70 -		'GPRINT:temp:MAX:max %2.0lfC\j'
    2.71 -}
    2.72 -
    2.73 -updatememgraph() {
    2.74 -	period=$1
    2.75 -	info="$(free | awk '\
    2.76 -{ \
    2.77 -  if (/Mem:/) { \
    2.78 -	if ($2 < 10000) printf "%d KB",$2; \
    2.79 -	else if ($2 < 10000000) printf "%d MB",$2/1024; \
    2.80 -	else printf "%d GB",$2/1024/1024; \
    2.81 -  } \
    2.82 -}')"
    2.83 -	info2="$(free | awk '\
    2.84 -{ \
    2.85 -  if (/Swap:/) { \
    2.86 -	if ($2 < 10000) printf "%d KB",$2; \
    2.87 -	else if ($2 < 10000000) printf "%d MB",$2/1024; \
    2.88 -	else printf "%d GB",$2/1024/1024; \
    2.89 -  } \
    2.90 -}')"
    2.91 -	rrdtool graph "$rrdgraph/memory-$period.png" --start -1$period \
    2.92 -		$rrdgraphargs -l 0 -u 100 \
    2.93 -		-t "memory usage per $period [ $info + $info2 swap ]" \
    2.94 -		DEF:used=$rrdlog/mem.rrd:memused:AVERAGE \
    2.95 -		DEF:free=$rrdlog/mem.rrd:memfree:AVERAGE \
    2.96 -		DEF:shared=$rrdlog/mem.rrd:memshared:AVERAGE \
    2.97 -		DEF:buffer=$rrdlog/mem.rrd:membuffers:AVERAGE \
    2.98 -		DEF:cache=$rrdlog/mem.rrd:memcache:AVERAGE \
    2.99 -		DEF:swused=$rrdlog/mem.rrd:swapused:AVERAGE \
   2.100 -		DEF:swfree=$rrdlog/mem.rrd:swapfree:AVERAGE \
   2.101 -		'CDEF:total=used,free,+' \
   2.102 -		'CDEF:used2=used,buffer,cache,shared,+,+,-' \
   2.103 -		'CDEF:usedpct=100,used2,total,/,*' \
   2.104 -		'CDEF:sharedpct=100,shared,total,/,*' \
   2.105 -		'CDEF:bufferpct=100,buffer,total,/,*' \
   2.106 -		'CDEF:cachepct=100,cache,total,/,*' \
   2.107 -		'CDEF:freepct=100,free,total,/,*' \
   2.108 -		'CDEF:swtotal=swused,swfree,+' \
   2.109 -		'CDEF:swusedpct=100,swused,swtotal,/,*' \
   2.110 -		'AREA:usedpct#0000FF:used memory' \
   2.111 -		'STACK:sharedpct#FF7F00:shared memory' \
   2.112 -		'STACK:bufferpct#FF00FF:buffered memory' \
   2.113 -		'STACK:cachepct#FFFF00:cached memory' \
   2.114 -		'STACK:freepct#00FF00:free memory' \
   2.115 -		'LINE2:swusedpct#FF0000:used swap\j'
   2.116 -}
   2.117 -
   2.118 -updatememdata () {
   2.119 -	[ -e "$rrdlog/mem.rrd" ] ||
   2.120 -		rrdtool create "$rrdlog/mem.rrd" --step=300 \
   2.121 -			DS:memused:ABSOLUTE:600:0:5000000000 \
   2.122 -			DS:memfree:ABSOLUTE:600:0:5000000000 \
   2.123 -			DS:memshared:ABSOLUTE:600:0:5000000000 \
   2.124 -			DS:membuffers:ABSOLUTE:600:0:5000000000 \
   2.125 -			DS:memcache:ABSOLUTE:600:0:5000000000 \
   2.126 -			DS:swapused:ABSOLUTE:600:0:5000000000 \
   2.127 -			DS:swapfree:ABSOLUTE:600:0:5000000000 \
   2.128 -			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   2.129 -			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   2.130 -
   2.131 -	while read tag count unit; do
   2.132 -		case "$tag" in
   2.133 -		MemTotal:)  memtotal=$count;;
   2.134 -		MemFree:)   memfree=$count
   2.135 -			    memused=$(($memtotal - $memfree))
   2.136 -			    memshared=0;;
   2.137 -		MemShared:) memshared=$count;;
   2.138 -		Buffers:)   membuffers=$count;;
   2.139 -		Cached:)    memcache=$count;;
   2.140 -		SwapTotal:) swaptotal=$count;;
   2.141 -		SwapFree:)  swapfree=$count
   2.142 -			    swapused=$(( $swaptotal - $swapfree));;
   2.143 -		esac
   2.144 -	done < /proc/meminfo
   2.145 -
   2.146 -	rrdtool update "$rrdlog/mem.rrd" \
   2.147 -		-t memused:memfree:memshared:membuffers:memcache:swapused:swapfree \
   2.148 -		"N:$memused:$memfree:$memshared:$membuffers:$memcache:$swapused:$swapfree"
   2.149 -}
   2.150 -
   2.151 -getmax() {
   2.152 -	rrdtool fetch $rrdlog/$1.rrd AVERAGE | awk '\
   2.153 -BEGIN {max=0} \
   2.154 -/^[0-9]/ { \
   2.155 -   if ($2 != "nan" && $2 > max) max=$2; \
   2.156 -   if ($3 != "nan" && $3 > max) max=$3; \
   2.157 -} \
   2.158 -END { print max }' | sed 's/,/./'
   2.159 -}
   2.160 -
   2.161 -updatediskgraph() {
   2.162 -	period=$1
   2.163 -	[ "$period" == "day" ] && maxdisk="$(getmax disk)"
   2.164 -	info=""
   2.165 -	[ -r $2 ] &&
   2.166 -	info="[ $(fdisk -l 2> /dev/null | grep "^Disk $2:" | \
   2.167 -		  sed "s|Disk $2: \(.*\), .*|\1|") ]"
   2.168 -	if [ -e /sys/block/${2#/dev/}/device/iodone_cnt ]; then
   2.169 -	err=$(printf "%d\n" $(cat /sys/block/${2#/dev/}/device/ioerr_cnt) )
   2.170 -	done=$(printf "%d\n" $(cat /sys/block/${2#/dev/}/device/iodone_cnt) )
   2.171 -	rate=$(echo | awk "BEGIN { printf \"%.0e\\n\",$err/$done }")
   2.172 -	[ $err -eq 0 ] &&  rate="0"
   2.173 -#		--right-axis-label "I/O state %"
   2.174 -	rrdtool graph "$rrdgraph/disk-$period.png" --start -1$period \
   2.175 -		$rrdgraphargs -t "disk access per $period $info" \
   2.176 -		--logarithmic --lower-limit 1 -v "Sectors/second" --units=si \
   2.177 -		DEF:read=$rrdlog/disk.rrd:readsect:AVERAGE \
   2.178 -		DEF:write=$rrdlog/disk.rrd:writesect:AVERAGE \
   2.179 -		DEF:req=$rrdlog/iodisk.rrd:req:AVERAGE \
   2.180 -		DEF:done=$rrdlog/iodisk.rrd:done:AVERAGE \
   2.181 -		DEF:err=$rrdlog/iodisk.rrd:err:AVERAGE \
   2.182 -		"CDEF:readpct=100,read,$maxdisk,/,*" \
   2.183 -		"CDEF:writepct=100,write,$maxdisk,/,*" \
   2.184 -		"CDEF:errpct=100,err,req,/,*" \
   2.185 -		"CDEF:donepct=100,done,req,/,*" \
   2.186 -		"CDEF:errrate=err,done,/" \
   2.187 -		'AREA:readpct#0000FF:sectors read from disk' \
   2.188 -		"COMMENT:I/O error rate $rate" \
   2.189 -		'STACK:writepct#00FF00:sectors written to disk' \
   2.190 -		'LINE2:donepct#FFFF00:% I/O complete' \
   2.191 -		'LINE2:errpct#FF0000:% I/O error\j'
   2.192 -	else
   2.193 -	rrdtool graph "$rrdgraph/disk-$period.png" --start -1$period \
   2.194 -		$rrdgraphargs -t "disk access per $period $info" \
   2.195 -		--logarithmic --lower-limit 1 -v "Sectors/second" --units=si \
   2.196 -		DEF:read=$rrdlog/disk.rrd:readsect:AVERAGE \
   2.197 -		DEF:write=$rrdlog/disk.rrd:writesect:AVERAGE \
   2.198 -		"CDEF:readpct=100,read,$maxdisk,/,*" \
   2.199 -		"CDEF:writepct=100,write,$maxdisk,/,*" \
   2.200 -		'AREA:readpct#0000FF:sectors read from disk' \
   2.201 -		'STACK:writepct#00FF00:sectors written to disk'
   2.202 -	fi
   2.203 -}
   2.204 -
   2.205 -updatediskdata() {
   2.206 -	dev=$1
   2.207 -	[ -e "$rrdlog/disk.rrd" ] ||
   2.208 -		rrdtool create "$rrdlog/disk.rrd" --step=300 \
   2.209 -			DS:readsect:COUNTER:600:0:5000000000 \
   2.210 -			DS:writesect:COUNTER:600:0:5000000000 \
   2.211 -			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   2.212 -			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   2.213 -	[ -e "$rrdlog/iodisk.rrd" ] ||
   2.214 -		rrdtool create "$rrdlog/iodisk.rrd" --step=300 \
   2.215 -			DS:done:GAUGE:600:0:U  DS:err:GAUGE:600:0:U \
   2.216 -			DS:req:GAUGE:600:0:U \
   2.217 -			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   2.218 -			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   2.219 -
   2.220 -	while read major minor name readreq readsect writereq writesect misc; do
   2.221 -		[ $major = $(( 0x$(stat -c %t $dev) )) ] || continue
   2.222 -		[ $minor = $(( 0x$(stat -c %T $dev) )) ] || continue
   2.223 -		rrdtool update "$rrdlog/disk.rrd" -t readsect:writesect \
   2.224 -			N:$readsect:$writesect
   2.225 -	done < /proc/diskstats
   2.226 -	disk=${dev:0:8}
   2.227 -	dir=/sys/block/${disk#/dev/}/device
   2.228 -	done=$(printf "%d\n" $(cat $dir/iodone_cnt 2> /dev/null) )
   2.229 -	err=$(printf "%d\n" $(cat $dir/ioerr_cnt 2> /dev/null) )
   2.230 -	req=$(printf "%d\n" $(cat $dir/iorequest_cnt 2> /dev/null) )
   2.231 -	rrdtool update "$rrdlog/iodisk.rrd" -t done:err:req N:$done:$err:$req
   2.232 -}
   2.233 -
   2.234 -updateifgraph() {
   2.235 -	interface=$1
   2.236 -	period=$2
   2.237 -	rrdtool graph "$rrdgraph/$interface-$period.png" --start -1$period \
   2.238 -		$rrdgraphargs -t "traffic on $interface graph per $period" \
   2.239 -		--logarithmic -A -v "Bytes/second" --units=si \
   2.240 -		DEF:incoming=$rrdlog/$interface.rrd:incoming:AVERAGE \
   2.241 -		DEF:outgoing=$rrdlog/$interface.rrd:outgoing:AVERAGE \
   2.242 -		'AREA:incoming#00FF00:incoming traffic' \
   2.243 -		'GPRINT:incoming:MAX:max input%8.3lf %sBps' \
   2.244 -		'GPRINT:outgoing:MAX:max output%8.3lf %sBps' \
   2.245 -		'LINE1:outgoing#0000FF:outgoing traffic\j'
   2.246 -}
   2.247 -
   2.248 -netframes() {
   2.249 -ifconfig $1 | grep "$2 packets" | sed -re "s/.*$3:([0-9]+).*/\1/g"
   2.250 -}
   2.251 -
   2.252 -netstats() {
   2.253 -ifconfig $1 | grep bytes | sed -re "s/.*$2 bytes:([0-9]+).*/\1/g"
   2.254 -}
   2.255 -
   2.256 -updateifdata() {
   2.257 -	interface=$1
   2.258 -	[ -e "$rrdlog/$interface.rrd" ] ||
   2.259 -		rrdtool create "$rrdlog/$interface.rrd" --step=300 \
   2.260 -			DS:incoming:COUNTER:600:0:U \
   2.261 -			DS:outgoing:COUNTER:600:0:U \
   2.262 -			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   2.263 -			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   2.264 -	[ -e "$rrdlog/packets-$interface.rrd" ] ||
   2.265 -		rrdtool create "$rrdlog/packets-$interface.rrd" --step=300 \
   2.266 -			DS:in:COUNTER:600:0:U      DS:out:COUNTER:600:0:U \
   2.267 -			DS:inerr:COUNTER:600:0:U   DS:outerr:COUNTER:600:0:U \
   2.268 -			DS:indrop:COUNTER:600:0:U  DS:outdrop:COUNTER:600:0:U \
   2.269 -			DS:inov:COUNTER:600:0:U    DS:outov:COUNTER:600:0:U \
   2.270 -			DS:frame:COUNTER:600:0:U   DS:carrier:COUNTER:600:0:U \
   2.271 -			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   2.272 -			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   2.273 -	rx=$(netstats $interface RX)
   2.274 -	tx=$(netstats $interface TX)
   2.275 -	rrdtool update "$rrdlog/$interface.rrd" -t incoming:outgoing \
   2.276 -		N:${rx:-U}:${tx:-U}
   2.277 -	rx=$(netframes $interface RX packets)
   2.278 -	tx=$(netframes $interface TX packets)
   2.279 -	rxerr=$(netframes $interface RX errors)
   2.280 -	txerr=$(netframes $interface TX errors)
   2.281 -	rxdrop=$(netframes $interface RX dropped)
   2.282 -	txdrop=$(netframes $interface TX dropped)
   2.283 -	rxov=$(netframes $interface RX overruns)
   2.284 -	txov=$(netframes $interface TX overruns)
   2.285 -	frame=$(netframes $interface RX frame)
   2.286 -	carrier=$(netframes $interface TX carrier)
   2.287 -	rrdtool update "$rrdlog/packets-$interface.rrd" \
   2.288 -		-t in:out:inerr:outerr:indrop:outdrop:inov:outov:frame:carrier \
   2.289 -		N:${rx:-U}:${tx:-U}:${rxerr:-U}:${txerr:-U}:${rxdrop:-U}:${txdrop:-U}:${rxov:-U}:${txov:-U}:${frame:-U}:${carrier:-U}
   2.290 -}
   2.291 -
   2.292 -getdisk()
   2.293 -{
   2.294 -	local d
   2.295 -	local i
   2.296 -	d=$(stat -c %04D $1)
   2.297 -	for i in /dev/* ; do 
   2.298 -		[ $(stat -c "%02t%02T" $i) == $d ] || continue
   2.299 -		echo $i
   2.300 -		break
   2.301 -	done
   2.302 -}
   2.303 -
   2.304 -###
   2.305 -### System graphs
   2.306 -###
   2.307 -
   2.308 -updatecpudata
   2.309 -updatecpugraph day
   2.310 -updatecpugraph week
   2.311 -updatecpugraph month
   2.312 -updatecpugraph year
   2.313 -
   2.314 -updatememdata
   2.315 -updatememgraph day
   2.316 -updatememgraph week
   2.317 -updatememgraph month
   2.318 -updatememgraph year
   2.319 -
   2.320 -if [ -e /proc/diskstats ]; then
   2.321 -	disk=$(getdisk $0)
   2.322 -	updatediskdata $disk
   2.323 -	updatediskgraph day ${disk:0:8}
   2.324 -	updatediskgraph week ${disk:0:8}
   2.325 -	updatediskgraph month ${disk:0:8}
   2.326 -	updatediskgraph year ${disk:0:8}
   2.327 -fi
   2.328 -
   2.329 -iface=$(/sbin/route -n | awk '{ if (/^0.0.0.0/) print $8 }')
   2.330 -updateifdata $iface
   2.331 -updateifgraph $iface day
   2.332 -updateifgraph $iface week
   2.333 -updateifgraph $iface month
   2.334 -updateifgraph $iface year
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/chub/web/lib/makegraphs	Thu Mar 15 05:18:50 2012 +0100
     3.3 @@ -0,0 +1,327 @@
     3.4 +#!/bin/sh
     3.5 +#*/5  * * * * /home/slitaz/www/chub/lib/makegraphs >/dev/null
     3.6 +
     3.7 +# RRD database directory
     3.8 +rrdlog="/home/slitaz/www/chub/log/rrd"
     3.9 +
    3.10 +# Images directory
    3.11 +rrdgraph="/home/slitaz/www/chub/images/rrd"
    3.12 +
    3.13 +# Colors
    3.14 +rrdcolors="--color SHADEA#FFFFFF --color SHADEB#FFFFFF --color BACK#FFFFFF" 
    3.15 +rrdgraphargs="-aPNG -i -z --alt-y-grid -w 600 -h 100 -r $rrdcolors"
    3.16 +
    3.17 +[ -d $rrdlog ] || mkdir -p $rrdlog
    3.18 +[ -d $rrdgraph ] || mkdir -p $rrdgraph
    3.19 +
    3.20 +updatecpudata() {
    3.21 +	[ -e "$rrdlog/cpu.rrd" ] || rrdtool create $rrdlog/cpu.rrd --step=300 \
    3.22 +			DS:user:COUNTER:600:0:500000000 \
    3.23 +			DS:nice:COUNTER:600:0:500000000 \
    3.24 +			DS:system:COUNTER:600:0:500000000 \
    3.25 +			DS:idle:COUNTER:600:0:500000000 \
    3.26 +			DS:iowait:COUNTER:600:0:500000000 \
    3.27 +			DS:irq:COUNTER:600:0:500000000 \
    3.28 +			DS:softirq:COUNTER:600:0:500000000 \
    3.29 +			DS:celsius:GAUGE:600:0:50000 \
    3.30 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
    3.31 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
    3.32 +	grep '^cpu' /proc/stat | while read cpu user nice system idle iowait irq softirq misc; do
    3.33 +		celsius=$(find /sys | grep /temp._input | xargs cat | \
    3.34 +			awk '{ if ($0 > max) max=$0 } END { print max/1 }')
    3.35 +		rrdtool update $rrdlog/cpu.rrd \
    3.36 +			-t celsius:nice:user:system:idle:iowait:irq:softirq \
    3.37 +			N:$celsius:$nice:$user:$system:$idle:$iowait:$irq:$softirq
    3.38 +		break
    3.39 +	done
    3.40 +}
    3.41 +
    3.42 +updatecpugraph() {
    3.43 +	period=$1
    3.44 +	info="$(grep '^model name' /proc/cpuinfo | cut -d: -f2 \
    3.45 +		| sed 's/ * / /g' | awk '
    3.46 +{ s=$0 ; n++ }                    
    3.47 +END { if (n > 1) printf " %dx",n; print s }')"
    3.48 +	rrdtool graph "$rrdgraph/cpu-$period.png" --start -1$period \
    3.49 +		$rrdgraphargs -l 0 -u 100 -t "cpu usage per $period [$info ]" \
    3.50 +		DEF:user=$rrdlog/cpu.rrd:user:AVERAGE \
    3.51 +		DEF:system=$rrdlog/cpu.rrd:system:AVERAGE \
    3.52 +		DEF:idle=$rrdlog/cpu.rrd:idle:AVERAGE \
    3.53 +		DEF:nice=$rrdlog/cpu.rrd:nice:AVERAGE \
    3.54 +		DEF:celsius=$rrdlog/cpu.rrd:celsius:AVERAGE \
    3.55 +		'CDEF:total=user,system,idle,nice,+,+,+' \
    3.56 +		'CDEF:userpct=100,user,total,/,*' \
    3.57 +		'CDEF:systempct=100,system,total,/,*' \
    3.58 +		'CDEF:idlepct=100,idle,total,/,*' \
    3.59 +		'CDEF:nicepct=100,nice,total,/,*' \
    3.60 +		'CDEF:temp=celsius,1000,/' \
    3.61 +		'AREA:userpct#0000FF:user cpu usage' \
    3.62 +		'STACK:nicepct#C0C0FF:nice cpu usage' \
    3.63 +		'STACK:systempct#FF0000:system cpu usage' \
    3.64 +		'STACK:idlepct#00FF00:idle cpu usage' \
    3.65 +		'LINE1:temp#000000:temperature\g' \
    3.66 +		'GPRINT:temp:MAX:max %2.0lfC\j'
    3.67 +}
    3.68 +
    3.69 +updatememgraph() {
    3.70 +	period=$1
    3.71 +	info="$(free | awk '\
    3.72 +{ \
    3.73 +  if (/Mem:/) { \
    3.74 +	if ($2 < 10000) printf "%d KB",$2; \
    3.75 +	else if ($2 < 10000000) printf "%d MB",$2/1024; \
    3.76 +	else printf "%d GB",$2/1024/1024; \
    3.77 +  } \
    3.78 +}')"
    3.79 +	info2="$(free | awk '\
    3.80 +{ \
    3.81 +  if (/Swap:/) { \
    3.82 +	if ($2 < 10000) printf "%d KB",$2; \
    3.83 +	else if ($2 < 10000000) printf "%d MB",$2/1024; \
    3.84 +	else printf "%d GB",$2/1024/1024; \
    3.85 +  } \
    3.86 +}')"
    3.87 +	rrdtool graph "$rrdgraph/memory-$period.png" --start -1$period \
    3.88 +		$rrdgraphargs -l 0 -u 100 \
    3.89 +		-t "memory usage per $period [ $info + $info2 swap ]" \
    3.90 +		DEF:used=$rrdlog/mem.rrd:memused:AVERAGE \
    3.91 +		DEF:free=$rrdlog/mem.rrd:memfree:AVERAGE \
    3.92 +		DEF:shared=$rrdlog/mem.rrd:memshared:AVERAGE \
    3.93 +		DEF:buffer=$rrdlog/mem.rrd:membuffers:AVERAGE \
    3.94 +		DEF:cache=$rrdlog/mem.rrd:memcache:AVERAGE \
    3.95 +		DEF:swused=$rrdlog/mem.rrd:swapused:AVERAGE \
    3.96 +		DEF:swfree=$rrdlog/mem.rrd:swapfree:AVERAGE \
    3.97 +		'CDEF:total=used,free,+' \
    3.98 +		'CDEF:used2=used,buffer,cache,shared,+,+,-' \
    3.99 +		'CDEF:usedpct=100,used2,total,/,*' \
   3.100 +		'CDEF:sharedpct=100,shared,total,/,*' \
   3.101 +		'CDEF:bufferpct=100,buffer,total,/,*' \
   3.102 +		'CDEF:cachepct=100,cache,total,/,*' \
   3.103 +		'CDEF:freepct=100,free,total,/,*' \
   3.104 +		'CDEF:swtotal=swused,swfree,+' \
   3.105 +		'CDEF:swusedpct=100,swused,swtotal,/,*' \
   3.106 +		'AREA:usedpct#0000FF:used memory' \
   3.107 +		'STACK:sharedpct#FF7F00:shared memory' \
   3.108 +		'STACK:bufferpct#FF00FF:buffered memory' \
   3.109 +		'STACK:cachepct#FFFF00:cached memory' \
   3.110 +		'STACK:freepct#00FF00:free memory' \
   3.111 +		'LINE2:swusedpct#FF0000:used swap\j'
   3.112 +}
   3.113 +
   3.114 +updatememdata () {
   3.115 +	[ -e "$rrdlog/mem.rrd" ] ||
   3.116 +		rrdtool create "$rrdlog/mem.rrd" --step=300 \
   3.117 +			DS:memused:ABSOLUTE:600:0:5000000000 \
   3.118 +			DS:memfree:ABSOLUTE:600:0:5000000000 \
   3.119 +			DS:memshared:ABSOLUTE:600:0:5000000000 \
   3.120 +			DS:membuffers:ABSOLUTE:600:0:5000000000 \
   3.121 +			DS:memcache:ABSOLUTE:600:0:5000000000 \
   3.122 +			DS:swapused:ABSOLUTE:600:0:5000000000 \
   3.123 +			DS:swapfree:ABSOLUTE:600:0:5000000000 \
   3.124 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   3.125 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   3.126 +
   3.127 +	while read tag count unit; do
   3.128 +		case "$tag" in
   3.129 +		MemTotal:)  memtotal=$count;;
   3.130 +		MemFree:)   memfree=$count
   3.131 +			    memused=$(($memtotal - $memfree))
   3.132 +			    memshared=0;;
   3.133 +		MemShared:) memshared=$count;;
   3.134 +		Buffers:)   membuffers=$count;;
   3.135 +		Cached:)    memcache=$count;;
   3.136 +		SwapTotal:) swaptotal=$count;;
   3.137 +		SwapFree:)  swapfree=$count
   3.138 +			    swapused=$(( $swaptotal - $swapfree));;
   3.139 +		esac
   3.140 +	done < /proc/meminfo
   3.141 +
   3.142 +	rrdtool update "$rrdlog/mem.rrd" \
   3.143 +		-t memused:memfree:memshared:membuffers:memcache:swapused:swapfree \
   3.144 +		"N:$memused:$memfree:$memshared:$membuffers:$memcache:$swapused:$swapfree"
   3.145 +}
   3.146 +
   3.147 +getmax() {
   3.148 +	rrdtool fetch $rrdlog/$1.rrd AVERAGE | awk '\
   3.149 +BEGIN {max=0} \
   3.150 +/^[0-9]/ { \
   3.151 +   if ($2 != "nan" && $2 > max) max=$2; \
   3.152 +   if ($3 != "nan" && $3 > max) max=$3; \
   3.153 +} \
   3.154 +END { print max }' | sed 's/,/./'
   3.155 +}
   3.156 +
   3.157 +updatediskgraph() {
   3.158 +	period=$1
   3.159 +	[ "$period" == "day" ] && maxdisk="$(getmax disk)"
   3.160 +	info=""
   3.161 +	[ -r $2 ] &&
   3.162 +	info="[ $(fdisk -l 2> /dev/null | grep "^Disk $2:" | \
   3.163 +		  sed "s|Disk $2: \(.*\), .*|\1|") ]"
   3.164 +	if [ -e /sys/block/${2#/dev/}/device/iodone_cnt ]; then
   3.165 +	err=$(printf "%d\n" $(cat /sys/block/${2#/dev/}/device/ioerr_cnt) )
   3.166 +	done=$(printf "%d\n" $(cat /sys/block/${2#/dev/}/device/iodone_cnt) )
   3.167 +	rate=$(echo | awk "BEGIN { printf \"%.0e\\n\",$err/$done }")
   3.168 +	[ $err -eq 0 ] &&  rate="0"
   3.169 +#		--right-axis-label "I/O state %"
   3.170 +	rrdtool graph "$rrdgraph/disk-$period.png" --start -1$period \
   3.171 +		$rrdgraphargs -t "disk access per $period $info" \
   3.172 +		--logarithmic --lower-limit 1 -v "Sectors/second" --units=si \
   3.173 +		DEF:read=$rrdlog/disk.rrd:readsect:AVERAGE \
   3.174 +		DEF:write=$rrdlog/disk.rrd:writesect:AVERAGE \
   3.175 +		DEF:req=$rrdlog/iodisk.rrd:req:AVERAGE \
   3.176 +		DEF:done=$rrdlog/iodisk.rrd:done:AVERAGE \
   3.177 +		DEF:err=$rrdlog/iodisk.rrd:err:AVERAGE \
   3.178 +		"CDEF:readpct=100,read,$maxdisk,/,*" \
   3.179 +		"CDEF:writepct=100,write,$maxdisk,/,*" \
   3.180 +		"CDEF:errpct=100,err,req,/,*" \
   3.181 +		"CDEF:donepct=100,done,req,/,*" \
   3.182 +		"CDEF:errrate=err,done,/" \
   3.183 +		'AREA:readpct#0000FF:sectors read from disk' \
   3.184 +		"COMMENT:I/O error rate $rate" \
   3.185 +		'STACK:writepct#00FF00:sectors written to disk' \
   3.186 +		'LINE2:donepct#FFFF00:% I/O complete' \
   3.187 +		'LINE2:errpct#FF0000:% I/O error\j'
   3.188 +	else
   3.189 +	rrdtool graph "$rrdgraph/disk-$period.png" --start -1$period \
   3.190 +		$rrdgraphargs -t "disk access per $period $info" \
   3.191 +		--logarithmic --lower-limit 1 -v "Sectors/second" --units=si \
   3.192 +		DEF:read=$rrdlog/disk.rrd:readsect:AVERAGE \
   3.193 +		DEF:write=$rrdlog/disk.rrd:writesect:AVERAGE \
   3.194 +		"CDEF:readpct=100,read,$maxdisk,/,*" \
   3.195 +		"CDEF:writepct=100,write,$maxdisk,/,*" \
   3.196 +		'AREA:readpct#0000FF:sectors read from disk' \
   3.197 +		'STACK:writepct#00FF00:sectors written to disk'
   3.198 +	fi
   3.199 +}
   3.200 +
   3.201 +updatediskdata() {
   3.202 +	dev=$1
   3.203 +	[ -e "$rrdlog/disk.rrd" ] ||
   3.204 +		rrdtool create "$rrdlog/disk.rrd" --step=300 \
   3.205 +			DS:readsect:COUNTER:600:0:5000000000 \
   3.206 +			DS:writesect:COUNTER:600:0:5000000000 \
   3.207 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   3.208 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   3.209 +	[ -e "$rrdlog/iodisk.rrd" ] ||
   3.210 +		rrdtool create "$rrdlog/iodisk.rrd" --step=300 \
   3.211 +			DS:done:GAUGE:600:0:U  DS:err:GAUGE:600:0:U \
   3.212 +			DS:req:GAUGE:600:0:U \
   3.213 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   3.214 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   3.215 +
   3.216 +	while read major minor name readreq readsect writereq writesect misc; do
   3.217 +		[ $major = $(( 0x$(stat -c %t $dev) )) ] || continue
   3.218 +		[ $minor = $(( 0x$(stat -c %T $dev) )) ] || continue
   3.219 +		rrdtool update "$rrdlog/disk.rrd" -t readsect:writesect \
   3.220 +			N:$readsect:$writesect
   3.221 +	done < /proc/diskstats
   3.222 +	disk=${dev:0:8}
   3.223 +	dir=/sys/block/${disk#/dev/}/device
   3.224 +	done=$(printf "%d\n" $(cat $dir/iodone_cnt 2> /dev/null) )
   3.225 +	err=$(printf "%d\n" $(cat $dir/ioerr_cnt 2> /dev/null) )
   3.226 +	req=$(printf "%d\n" $(cat $dir/iorequest_cnt 2> /dev/null) )
   3.227 +	rrdtool update "$rrdlog/iodisk.rrd" -t done:err:req N:$done:$err:$req
   3.228 +}
   3.229 +
   3.230 +updateifgraph() {
   3.231 +	interface=$1
   3.232 +	period=$2
   3.233 +	rrdtool graph "$rrdgraph/$interface-$period.png" --start -1$period \
   3.234 +		$rrdgraphargs -t "traffic on $interface graph per $period" \
   3.235 +		--logarithmic -A -v "Bytes/second" --units=si \
   3.236 +		DEF:incoming=$rrdlog/$interface.rrd:incoming:AVERAGE \
   3.237 +		DEF:outgoing=$rrdlog/$interface.rrd:outgoing:AVERAGE \
   3.238 +		'AREA:incoming#00FF00:incoming traffic' \
   3.239 +		'GPRINT:incoming:MAX:max input%8.3lf %sBps' \
   3.240 +		'GPRINT:outgoing:MAX:max output%8.3lf %sBps' \
   3.241 +		'LINE1:outgoing#0000FF:outgoing traffic\j'
   3.242 +}
   3.243 +
   3.244 +netframes() {
   3.245 +ifconfig $1 | grep "$2 packets" | sed -re "s/.*$3:([0-9]+).*/\1/g"
   3.246 +}
   3.247 +
   3.248 +netstats() {
   3.249 +ifconfig $1 | grep bytes | sed -re "s/.*$2 bytes:([0-9]+).*/\1/g"
   3.250 +}
   3.251 +
   3.252 +updateifdata() {
   3.253 +	interface=$1
   3.254 +	[ -e "$rrdlog/$interface.rrd" ] ||
   3.255 +		rrdtool create "$rrdlog/$interface.rrd" --step=300 \
   3.256 +			DS:incoming:COUNTER:600:0:U \
   3.257 +			DS:outgoing:COUNTER:600:0:U \
   3.258 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   3.259 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   3.260 +	[ -e "$rrdlog/packets-$interface.rrd" ] ||
   3.261 +		rrdtool create "$rrdlog/packets-$interface.rrd" --step=300 \
   3.262 +			DS:in:COUNTER:600:0:U      DS:out:COUNTER:600:0:U \
   3.263 +			DS:inerr:COUNTER:600:0:U   DS:outerr:COUNTER:600:0:U \
   3.264 +			DS:indrop:COUNTER:600:0:U  DS:outdrop:COUNTER:600:0:U \
   3.265 +			DS:inov:COUNTER:600:0:U    DS:outov:COUNTER:600:0:U \
   3.266 +			DS:frame:COUNTER:600:0:U   DS:carrier:COUNTER:600:0:U \
   3.267 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   3.268 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   3.269 +	rx=$(netstats $interface RX)
   3.270 +	tx=$(netstats $interface TX)
   3.271 +	rrdtool update "$rrdlog/$interface.rrd" -t incoming:outgoing \
   3.272 +		N:${rx:-U}:${tx:-U}
   3.273 +	rx=$(netframes $interface RX packets)
   3.274 +	tx=$(netframes $interface TX packets)
   3.275 +	rxerr=$(netframes $interface RX errors)
   3.276 +	txerr=$(netframes $interface TX errors)
   3.277 +	rxdrop=$(netframes $interface RX dropped)
   3.278 +	txdrop=$(netframes $interface TX dropped)
   3.279 +	rxov=$(netframes $interface RX overruns)
   3.280 +	txov=$(netframes $interface TX overruns)
   3.281 +	frame=$(netframes $interface RX frame)
   3.282 +	carrier=$(netframes $interface TX carrier)
   3.283 +	rrdtool update "$rrdlog/packets-$interface.rrd" \
   3.284 +		-t in:out:inerr:outerr:indrop:outdrop:inov:outov:frame:carrier \
   3.285 +		N:${rx:-U}:${tx:-U}:${rxerr:-U}:${txerr:-U}:${rxdrop:-U}:${txdrop:-U}:${rxov:-U}:${txov:-U}:${frame:-U}:${carrier:-U}
   3.286 +}
   3.287 +
   3.288 +getdisk()
   3.289 +{
   3.290 +	local d
   3.291 +	local i
   3.292 +	d=$(stat -c %04D $1)
   3.293 +	for i in /dev/* ; do 
   3.294 +		[ $(stat -c "%02t%02T" $i) == $d ] || continue
   3.295 +		echo $i
   3.296 +		break
   3.297 +	done
   3.298 +}
   3.299 +
   3.300 +###
   3.301 +### System graphs
   3.302 +###
   3.303 +
   3.304 +updatecpudata
   3.305 +updatecpugraph day
   3.306 +updatecpugraph week
   3.307 +updatecpugraph month
   3.308 +updatecpugraph year
   3.309 +
   3.310 +updatememdata
   3.311 +updatememgraph day
   3.312 +updatememgraph week
   3.313 +updatememgraph month
   3.314 +updatememgraph year
   3.315 +
   3.316 +if [ -e /proc/diskstats ]; then
   3.317 +	disk=$(getdisk $0)
   3.318 +	updatediskdata $disk
   3.319 +	updatediskgraph day ${disk:0:8}
   3.320 +	updatediskgraph week ${disk:0:8}
   3.321 +	updatediskgraph month ${disk:0:8}
   3.322 +	updatediskgraph year ${disk:0:8}
   3.323 +fi
   3.324 +
   3.325 +iface=$(/sbin/route -n | awk '{ if (/^0.0.0.0/) print $8 }')
   3.326 +updateifdata $iface
   3.327 +updateifgraph $iface day
   3.328 +updateifgraph $iface week
   3.329 +updateifgraph $iface month
   3.330 +updateifgraph $iface year