slitaz-forge rev 132

chub: add command rrd
author Christophe Lincoln <pankso@slitaz.org>
date Thu Mar 15 05:03:18 2012 +0100 (2012-03-15)
parents 3703a87dc776
children 90f3032b61f2
files chub/chub chub/files/var/spool/cron/crontabs/root chub/web/images/network.png chub/web/lib/chubrrd chub/web/lib/rrdtool
line diff
     1.1 --- a/chub/chub	Thu Mar 15 04:46:41 2012 +0100
     1.2 +++ b/chub/chub	Thu Mar 15 05:03:18 2012 +0100
     1.3 @@ -21,7 +21,8 @@
     1.4  Commands:
     1.5    up-www       Update website http://www.slitaz.org/
     1.6    up-chub      Update Chub web interface http://chub.slitaz.org/
     1.7 -  stats        Display some server stats.
     1.8 +  stats        Display some server stats
     1.9 +  rrd          Update RRD graphs images
    1.10    backup       Backup MySQL DB and files"
    1.11  
    1.12  EOT
    1.13 @@ -36,9 +37,9 @@
    1.14  		# Update Chub web interface from repo.
    1.15  		echo "Updating: chub.slitaz.org..."
    1.16  		cd $REPOS/slitaz-forge
    1.17 -		#hg update when repos will be on Chub
    1.18 +		#hg update if repos are on Chub
    1.19  		hg pull -u
    1.20 -		rm -rf $VHOST/*.*
    1.21 +		rm -rf $VHOST/*.* $VHOST/images/*.png
    1.22  		cp -a chub/web/* $VHOST ;;
    1.23  	stats)
    1.24  		# Echo some stats.
    1.25 @@ -48,6 +49,8 @@
    1.26  		free -m | grep ^Mem
    1.27  		echo "======== Connected users ========"
    1.28  		who ;;
    1.29 +	rrd)
    1.30 +		$VHOST/lib/chubrrd >/dev/null
    1.31  	backup)
    1.32  		echo "TODO: backup MySQL, SCN files, etc" ;;
    1.33  	*)
     2.1 --- a/chub/files/var/spool/cron/crontabs/root	Thu Mar 15 04:46:41 2012 +0100
     2.2 +++ b/chub/files/var/spool/cron/crontabs/root	Thu Mar 15 05:03:18 2012 +0100
     2.3 @@ -1,5 +1,5 @@
     2.4  # Cron tasks for SliTaz Community HUB aka chub
     2.5  #
     2.6  
     2.7 -# Update RRD images
     2.8 -*/5 * * * * /home/slitaz/www/chub/lib/rrdtool >/dev/null
     2.9 +# Update RRD graphs images
    2.10 +*/5  * * * * /usr/bin/chub rrd >/dev/null
     3.1 Binary file chub/web/images/network.png has changed
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/chub/web/lib/chubrrd	Thu Mar 15 05:03:18 2012 +0100
     4.3 @@ -0,0 +1,331 @@
     4.4 +#!/bin/sh
     4.5 +#
     4.6 +# */5  * * * * /home/slitaz/www/chub/lib/chubrrd >/dev/null
     4.7 +# */5  * * * * /usr/bin/chub rrd >/dev/null
     4.8 +#
     4.9 +. /etc/slitaz/chub.conf || exit 1
    4.10 +
    4.11 +# RRD database directory
    4.12 +rrdlog="$VHOST/log/rrd"
    4.13 +
    4.14 +# Images directory
    4.15 +rrdgraph="$VHOST/images/rdd"
    4.16 +
    4.17 +# Colors
    4.18 +rrdcolors="--color SHADEA#FFFFFF --color SHADEB#FFFFFF --color BACK#FFFFFF" 
    4.19 +rrdgraphargs="-aPNG -i -z --alt-y-grid -w 600 -h 100 -r $rrdcolors"
    4.20 +
    4.21 +[ -d $rrdlog ] || mkdir -p $rrdlog
    4.22 +[ -d $rrdgraph ] || mkdir -p $rrdgraph
    4.23 +
    4.24 +updatecpudata() {
    4.25 +	[ -e "$rrdlog/cpu.rrd" ] || rrdtool create $rrdlog/cpu.rrd --step=300 \
    4.26 +			DS:user:COUNTER:600:0:500000000 \
    4.27 +			DS:nice:COUNTER:600:0:500000000 \
    4.28 +			DS:system:COUNTER:600:0:500000000 \
    4.29 +			DS:idle:COUNTER:600:0:500000000 \
    4.30 +			DS:iowait:COUNTER:600:0:500000000 \
    4.31 +			DS:irq:COUNTER:600:0:500000000 \
    4.32 +			DS:softirq:COUNTER:600:0:500000000 \
    4.33 +			DS:celsius:GAUGE:600:0:50000 \
    4.34 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
    4.35 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
    4.36 +	grep '^cpu' /proc/stat | while read cpu user nice system idle iowait irq softirq misc; do
    4.37 +		celsius=$(find /sys | grep /temp._input | xargs cat | \
    4.38 +			awk '{ if ($0 > max) max=$0 } END { print max/1 }')
    4.39 +		rrdtool update $rrdlog/cpu.rrd \
    4.40 +			-t celsius:nice:user:system:idle:iowait:irq:softirq \
    4.41 +			N:$celsius:$nice:$user:$system:$idle:$iowait:$irq:$softirq
    4.42 +		break
    4.43 +	done
    4.44 +}
    4.45 +
    4.46 +updatecpugraph() {
    4.47 +	period=$1
    4.48 +	info="$(grep '^model name' /proc/cpuinfo | cut -d: -f2 \
    4.49 +		| sed 's/ * / /g' | awk '
    4.50 +{ s=$0 ; n++ }                    
    4.51 +END { if (n > 1) printf " %dx",n; print s }')"
    4.52 +	rrdtool graph "$rrdgraph/cpu-$period.png" --start -1$period \
    4.53 +		$rrdgraphargs -l 0 -u 100 -t "cpu usage per $period [$info ]" \
    4.54 +		DEF:user=$rrdlog/cpu.rrd:user:AVERAGE \
    4.55 +		DEF:system=$rrdlog/cpu.rrd:system:AVERAGE \
    4.56 +		DEF:idle=$rrdlog/cpu.rrd:idle:AVERAGE \
    4.57 +		DEF:nice=$rrdlog/cpu.rrd:nice:AVERAGE \
    4.58 +		DEF:celsius=$rrdlog/cpu.rrd:celsius:AVERAGE \
    4.59 +		'CDEF:total=user,system,idle,nice,+,+,+' \
    4.60 +		'CDEF:userpct=100,user,total,/,*' \
    4.61 +		'CDEF:systempct=100,system,total,/,*' \
    4.62 +		'CDEF:idlepct=100,idle,total,/,*' \
    4.63 +		'CDEF:nicepct=100,nice,total,/,*' \
    4.64 +		'CDEF:temp=celsius,1000,/' \
    4.65 +		'AREA:userpct#0000FF:user cpu usage' \
    4.66 +		'STACK:nicepct#C0C0FF:nice cpu usage' \
    4.67 +		'STACK:systempct#FF0000:system cpu usage' \
    4.68 +		'STACK:idlepct#00FF00:idle cpu usage' \
    4.69 +		'LINE1:temp#000000:temperature\g' \
    4.70 +		'GPRINT:temp:MAX:max %2.0lfC\j'
    4.71 +}
    4.72 +
    4.73 +updatememgraph() {
    4.74 +	period=$1
    4.75 +	info="$(free | awk '\
    4.76 +{ \
    4.77 +  if (/Mem:/) { \
    4.78 +	if ($2 < 10000) printf "%d KB",$2; \
    4.79 +	else if ($2 < 10000000) printf "%d MB",$2/1024; \
    4.80 +	else printf "%d GB",$2/1024/1024; \
    4.81 +  } \
    4.82 +}')"
    4.83 +	info2="$(free | awk '\
    4.84 +{ \
    4.85 +  if (/Swap:/) { \
    4.86 +	if ($2 < 10000) printf "%d KB",$2; \
    4.87 +	else if ($2 < 10000000) printf "%d MB",$2/1024; \
    4.88 +	else printf "%d GB",$2/1024/1024; \
    4.89 +  } \
    4.90 +}')"
    4.91 +	rrdtool graph "$rrdgraph/memory-$period.png" --start -1$period \
    4.92 +		$rrdgraphargs -l 0 -u 100 \
    4.93 +		-t "memory usage per $period [ $info + $info2 swap ]" \
    4.94 +		DEF:used=$rrdlog/mem.rrd:memused:AVERAGE \
    4.95 +		DEF:free=$rrdlog/mem.rrd:memfree:AVERAGE \
    4.96 +		DEF:shared=$rrdlog/mem.rrd:memshared:AVERAGE \
    4.97 +		DEF:buffer=$rrdlog/mem.rrd:membuffers:AVERAGE \
    4.98 +		DEF:cache=$rrdlog/mem.rrd:memcache:AVERAGE \
    4.99 +		DEF:swused=$rrdlog/mem.rrd:swapused:AVERAGE \
   4.100 +		DEF:swfree=$rrdlog/mem.rrd:swapfree:AVERAGE \
   4.101 +		'CDEF:total=used,free,+' \
   4.102 +		'CDEF:used2=used,buffer,cache,shared,+,+,-' \
   4.103 +		'CDEF:usedpct=100,used2,total,/,*' \
   4.104 +		'CDEF:sharedpct=100,shared,total,/,*' \
   4.105 +		'CDEF:bufferpct=100,buffer,total,/,*' \
   4.106 +		'CDEF:cachepct=100,cache,total,/,*' \
   4.107 +		'CDEF:freepct=100,free,total,/,*' \
   4.108 +		'CDEF:swtotal=swused,swfree,+' \
   4.109 +		'CDEF:swusedpct=100,swused,swtotal,/,*' \
   4.110 +		'AREA:usedpct#0000FF:used memory' \
   4.111 +		'STACK:sharedpct#FF7F00:shared memory' \
   4.112 +		'STACK:bufferpct#FF00FF:buffered memory' \
   4.113 +		'STACK:cachepct#FFFF00:cached memory' \
   4.114 +		'STACK:freepct#00FF00:free memory' \
   4.115 +		'LINE2:swusedpct#FF0000:used swap\j'
   4.116 +}
   4.117 +
   4.118 +updatememdata () {
   4.119 +	[ -e "$rrdlog/mem.rrd" ] ||
   4.120 +		rrdtool create "$rrdlog/mem.rrd" --step=300 \
   4.121 +			DS:memused:ABSOLUTE:600:0:5000000000 \
   4.122 +			DS:memfree:ABSOLUTE:600:0:5000000000 \
   4.123 +			DS:memshared:ABSOLUTE:600:0:5000000000 \
   4.124 +			DS:membuffers:ABSOLUTE:600:0:5000000000 \
   4.125 +			DS:memcache:ABSOLUTE:600:0:5000000000 \
   4.126 +			DS:swapused:ABSOLUTE:600:0:5000000000 \
   4.127 +			DS:swapfree:ABSOLUTE:600:0:5000000000 \
   4.128 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   4.129 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   4.130 +
   4.131 +	while read tag count unit; do
   4.132 +		case "$tag" in
   4.133 +		MemTotal:)  memtotal=$count;;
   4.134 +		MemFree:)   memfree=$count
   4.135 +			    memused=$(($memtotal - $memfree))
   4.136 +			    memshared=0;;
   4.137 +		MemShared:) memshared=$count;;
   4.138 +		Buffers:)   membuffers=$count;;
   4.139 +		Cached:)    memcache=$count;;
   4.140 +		SwapTotal:) swaptotal=$count;;
   4.141 +		SwapFree:)  swapfree=$count
   4.142 +			    swapused=$(( $swaptotal - $swapfree));;
   4.143 +		esac
   4.144 +	done < /proc/meminfo
   4.145 +
   4.146 +	rrdtool update "$rrdlog/mem.rrd" \
   4.147 +		-t memused:memfree:memshared:membuffers:memcache:swapused:swapfree \
   4.148 +		"N:$memused:$memfree:$memshared:$membuffers:$memcache:$swapused:$swapfree"
   4.149 +}
   4.150 +
   4.151 +getmax() {
   4.152 +	rrdtool fetch $rrdlog/$1.rrd AVERAGE | awk '\
   4.153 +BEGIN {max=0} \
   4.154 +/^[0-9]/ { \
   4.155 +   if ($2 != "nan" && $2 > max) max=$2; \
   4.156 +   if ($3 != "nan" && $3 > max) max=$3; \
   4.157 +} \
   4.158 +END { print max }' | sed 's/,/./'
   4.159 +}
   4.160 +
   4.161 +updatediskgraph() {
   4.162 +	period=$1
   4.163 +	[ "$period" == "day" ] && maxdisk="$(getmax disk)"
   4.164 +	info=""
   4.165 +	[ -r $2 ] &&
   4.166 +	info="[ $(fdisk -l 2> /dev/null | grep "^Disk $2:" | \
   4.167 +		  sed "s|Disk $2: \(.*\), .*|\1|") ]"
   4.168 +	if [ -e /sys/block/${2#/dev/}/device/iodone_cnt ]; then
   4.169 +	err=$(printf "%d\n" $(cat /sys/block/${2#/dev/}/device/ioerr_cnt) )
   4.170 +	done=$(printf "%d\n" $(cat /sys/block/${2#/dev/}/device/iodone_cnt) )
   4.171 +	rate=$(echo | awk "BEGIN { printf \"%.0e\\n\",$err/$done }")
   4.172 +	[ $err -eq 0 ] &&  rate="0"
   4.173 +#		--right-axis-label "I/O state %"
   4.174 +	rrdtool graph "$rrdgraph/disk-$period.png" --start -1$period \
   4.175 +		$rrdgraphargs -t "disk access per $period $info" \
   4.176 +		--logarithmic --lower-limit 1 -v "Sectors/second" --units=si \
   4.177 +		DEF:read=$rrdlog/disk.rrd:readsect:AVERAGE \
   4.178 +		DEF:write=$rrdlog/disk.rrd:writesect:AVERAGE \
   4.179 +		DEF:req=$rrdlog/iodisk.rrd:req:AVERAGE \
   4.180 +		DEF:done=$rrdlog/iodisk.rrd:done:AVERAGE \
   4.181 +		DEF:err=$rrdlog/iodisk.rrd:err:AVERAGE \
   4.182 +		"CDEF:readpct=100,read,$maxdisk,/,*" \
   4.183 +		"CDEF:writepct=100,write,$maxdisk,/,*" \
   4.184 +		"CDEF:errpct=100,err,req,/,*" \
   4.185 +		"CDEF:donepct=100,done,req,/,*" \
   4.186 +		"CDEF:errrate=err,done,/" \
   4.187 +		'AREA:readpct#0000FF:sectors read from disk' \
   4.188 +		"COMMENT:I/O error rate $rate" \
   4.189 +		'STACK:writepct#00FF00:sectors written to disk' \
   4.190 +		'LINE2:donepct#FFFF00:% I/O complete' \
   4.191 +		'LINE2:errpct#FF0000:% I/O error\j'
   4.192 +	else
   4.193 +	rrdtool graph "$rrdgraph/disk-$period.png" --start -1$period \
   4.194 +		$rrdgraphargs -t "disk access per $period $info" \
   4.195 +		--logarithmic --lower-limit 1 -v "Sectors/second" --units=si \
   4.196 +		DEF:read=$rrdlog/disk.rrd:readsect:AVERAGE \
   4.197 +		DEF:write=$rrdlog/disk.rrd:writesect:AVERAGE \
   4.198 +		"CDEF:readpct=100,read,$maxdisk,/,*" \
   4.199 +		"CDEF:writepct=100,write,$maxdisk,/,*" \
   4.200 +		'AREA:readpct#0000FF:sectors read from disk' \
   4.201 +		'STACK:writepct#00FF00:sectors written to disk'
   4.202 +	fi
   4.203 +}
   4.204 +
   4.205 +updatediskdata() {
   4.206 +	dev=$1
   4.207 +	[ -e "$rrdlog/disk.rrd" ] ||
   4.208 +		rrdtool create "$rrdlog/disk.rrd" --step=300 \
   4.209 +			DS:readsect:COUNTER:600:0:5000000000 \
   4.210 +			DS:writesect:COUNTER:600:0:5000000000 \
   4.211 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   4.212 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   4.213 +	[ -e "$rrdlog/iodisk.rrd" ] ||
   4.214 +		rrdtool create "$rrdlog/iodisk.rrd" --step=300 \
   4.215 +			DS:done:GAUGE:600:0:U  DS:err:GAUGE:600:0:U \
   4.216 +			DS:req:GAUGE:600:0:U \
   4.217 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   4.218 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   4.219 +
   4.220 +	while read major minor name readreq readsect writereq writesect misc; do
   4.221 +		[ $major = $(( 0x$(stat -c %t $dev) )) ] || continue
   4.222 +		[ $minor = $(( 0x$(stat -c %T $dev) )) ] || continue
   4.223 +		rrdtool update "$rrdlog/disk.rrd" -t readsect:writesect \
   4.224 +			N:$readsect:$writesect
   4.225 +	done < /proc/diskstats
   4.226 +	disk=${dev:0:8}
   4.227 +	dir=/sys/block/${disk#/dev/}/device
   4.228 +	done=$(printf "%d\n" $(cat $dir/iodone_cnt 2> /dev/null) )
   4.229 +	err=$(printf "%d\n" $(cat $dir/ioerr_cnt 2> /dev/null) )
   4.230 +	req=$(printf "%d\n" $(cat $dir/iorequest_cnt 2> /dev/null) )
   4.231 +	rrdtool update "$rrdlog/iodisk.rrd" -t done:err:req N:$done:$err:$req
   4.232 +}
   4.233 +
   4.234 +updateifgraph() {
   4.235 +	interface=$1
   4.236 +	period=$2
   4.237 +	rrdtool graph "$rrdgraph/$interface-$period.png" --start -1$period \
   4.238 +		$rrdgraphargs -t "traffic on $interface graph per $period" \
   4.239 +		--logarithmic -A -v "Bytes/second" --units=si \
   4.240 +		DEF:incoming=$rrdlog/$interface.rrd:incoming:AVERAGE \
   4.241 +		DEF:outgoing=$rrdlog/$interface.rrd:outgoing:AVERAGE \
   4.242 +		'AREA:incoming#00FF00:incoming traffic' \
   4.243 +		'GPRINT:incoming:MAX:max input%8.3lf %sBps' \
   4.244 +		'GPRINT:outgoing:MAX:max output%8.3lf %sBps' \
   4.245 +		'LINE1:outgoing#0000FF:outgoing traffic\j'
   4.246 +}
   4.247 +
   4.248 +netframes() {
   4.249 +ifconfig $1 | grep "$2 packets" | sed -re "s/.*$3:([0-9]+).*/\1/g"
   4.250 +}
   4.251 +
   4.252 +netstats() {
   4.253 +ifconfig $1 | grep bytes | sed -re "s/.*$2 bytes:([0-9]+).*/\1/g"
   4.254 +}
   4.255 +
   4.256 +updateifdata() {
   4.257 +	interface=$1
   4.258 +	[ -e "$rrdlog/$interface.rrd" ] ||
   4.259 +		rrdtool create "$rrdlog/$interface.rrd" --step=300 \
   4.260 +			DS:incoming:COUNTER:600:0:U \
   4.261 +			DS:outgoing:COUNTER:600:0:U \
   4.262 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   4.263 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   4.264 +	[ -e "$rrdlog/packets-$interface.rrd" ] ||
   4.265 +		rrdtool create "$rrdlog/packets-$interface.rrd" --step=300 \
   4.266 +			DS:in:COUNTER:600:0:U      DS:out:COUNTER:600:0:U \
   4.267 +			DS:inerr:COUNTER:600:0:U   DS:outerr:COUNTER:600:0:U \
   4.268 +			DS:indrop:COUNTER:600:0:U  DS:outdrop:COUNTER:600:0:U \
   4.269 +			DS:inov:COUNTER:600:0:U    DS:outov:COUNTER:600:0:U \
   4.270 +			DS:frame:COUNTER:600:0:U   DS:carrier:COUNTER:600:0:U \
   4.271 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   4.272 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   4.273 +	rx=$(netstats $interface RX)
   4.274 +	tx=$(netstats $interface TX)
   4.275 +	rrdtool update "$rrdlog/$interface.rrd" -t incoming:outgoing \
   4.276 +		N:${rx:-U}:${tx:-U}
   4.277 +	rx=$(netframes $interface RX packets)
   4.278 +	tx=$(netframes $interface TX packets)
   4.279 +	rxerr=$(netframes $interface RX errors)
   4.280 +	txerr=$(netframes $interface TX errors)
   4.281 +	rxdrop=$(netframes $interface RX dropped)
   4.282 +	txdrop=$(netframes $interface TX dropped)
   4.283 +	rxov=$(netframes $interface RX overruns)
   4.284 +	txov=$(netframes $interface TX overruns)
   4.285 +	frame=$(netframes $interface RX frame)
   4.286 +	carrier=$(netframes $interface TX carrier)
   4.287 +	rrdtool update "$rrdlog/packets-$interface.rrd" \
   4.288 +		-t in:out:inerr:outerr:indrop:outdrop:inov:outov:frame:carrier \
   4.289 +		N:${rx:-U}:${tx:-U}:${rxerr:-U}:${txerr:-U}:${rxdrop:-U}:${txdrop:-U}:${rxov:-U}:${txov:-U}:${frame:-U}:${carrier:-U}
   4.290 +}
   4.291 +
   4.292 +getdisk()
   4.293 +{
   4.294 +	local d
   4.295 +	local i
   4.296 +	d=$(stat -c %04D $1)
   4.297 +	for i in /dev/* ; do 
   4.298 +		[ $(stat -c "%02t%02T" $i) == $d ] || continue
   4.299 +		echo $i
   4.300 +		break
   4.301 +	done
   4.302 +}
   4.303 +
   4.304 +###
   4.305 +### System graphs
   4.306 +###
   4.307 +
   4.308 +updatecpudata
   4.309 +updatecpugraph day
   4.310 +updatecpugraph week
   4.311 +updatecpugraph month
   4.312 +updatecpugraph year
   4.313 +
   4.314 +updatememdata
   4.315 +updatememgraph day
   4.316 +updatememgraph week
   4.317 +updatememgraph month
   4.318 +updatememgraph year
   4.319 +
   4.320 +if [ -e /proc/diskstats ]; then
   4.321 +	disk=$(getdisk $0)
   4.322 +	updatediskdata $disk
   4.323 +	updatediskgraph day ${disk:0:8}
   4.324 +	updatediskgraph week ${disk:0:8}
   4.325 +	updatediskgraph month ${disk:0:8}
   4.326 +	updatediskgraph year ${disk:0:8}
   4.327 +fi
   4.328 +
   4.329 +iface=$(/sbin/route -n | awk '{ if (/^0.0.0.0/) print $8 }')
   4.330 +updateifdata $iface
   4.331 +updateifgraph $iface day
   4.332 +updateifgraph $iface week
   4.333 +updateifgraph $iface month
   4.334 +updateifgraph $iface year
     5.1 --- a/chub/web/lib/rrdtool	Thu Mar 15 04:46:41 2012 +0100
     5.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.3 @@ -1,329 +0,0 @@
     5.4 -#!/bin/sh
     5.5 -#*/5  * * * * /home/slitaz/www/chub/lib/rrdtool >/dev/null
     5.6 -
     5.7 -. /etc/slitaz/chub.conf || exit 1
     5.8 -
     5.9 -# RRD database directory
    5.10 -rrdlog="$VHOST/cache/rrd"
    5.11 -
    5.12 -# Images directory
    5.13 -rrdgraph="$VHOST/images/rdd"
    5.14 -
    5.15 -# Colors
    5.16 -rrdcolors="--color SHADEA#FFFFFF --color SHADEB#FFFFFF --color BACK#FFFFFF" 
    5.17 -rrdgraphargs="-aPNG -i -z --alt-y-grid -w 600 -h 100 -r $rrdcolors"
    5.18 -
    5.19 -[ -d $rrdlog ] || mkdir -p $rrdlog
    5.20 -[ -d $rrdgraph ] || mkdir -p $rrdgraph
    5.21 -
    5.22 -updatecpudata() {
    5.23 -	[ -e "$rrdlog/cpu.rrd" ] || rrdtool create $rrdlog/cpu.rrd --step=300 \
    5.24 -			DS:user:COUNTER:600:0:500000000 \
    5.25 -			DS:nice:COUNTER:600:0:500000000 \
    5.26 -			DS:system:COUNTER:600:0:500000000 \
    5.27 -			DS:idle:COUNTER:600:0:500000000 \
    5.28 -			DS:iowait:COUNTER:600:0:500000000 \
    5.29 -			DS:irq:COUNTER:600:0:500000000 \
    5.30 -			DS:softirq:COUNTER:600:0:500000000 \
    5.31 -			DS:celsius:GAUGE:600:0:50000 \
    5.32 -			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
    5.33 -			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
    5.34 -	grep '^cpu' /proc/stat | while read cpu user nice system idle iowait irq softirq misc; do
    5.35 -		celsius=$(find /sys | grep /temp._input | xargs cat | \
    5.36 -			awk '{ if ($0 > max) max=$0 } END { print max/1 }')
    5.37 -		rrdtool update $rrdlog/cpu.rrd \
    5.38 -			-t celsius:nice:user:system:idle:iowait:irq:softirq \
    5.39 -			N:$celsius:$nice:$user:$system:$idle:$iowait:$irq:$softirq
    5.40 -		break
    5.41 -	done
    5.42 -}
    5.43 -
    5.44 -updatecpugraph() {
    5.45 -	period=$1
    5.46 -	info="$(grep '^model name' /proc/cpuinfo | cut -d: -f2 \
    5.47 -		| sed 's/ * / /g' | awk '
    5.48 -{ s=$0 ; n++ }                    
    5.49 -END { if (n > 1) printf " %dx",n; print s }')"
    5.50 -	rrdtool graph "$rrdgraph/cpu-$period.png" --start -1$period \
    5.51 -		$rrdgraphargs -l 0 -u 100 -t "cpu usage per $period [$info ]" \
    5.52 -		DEF:user=$rrdlog/cpu.rrd:user:AVERAGE \
    5.53 -		DEF:system=$rrdlog/cpu.rrd:system:AVERAGE \
    5.54 -		DEF:idle=$rrdlog/cpu.rrd:idle:AVERAGE \
    5.55 -		DEF:nice=$rrdlog/cpu.rrd:nice:AVERAGE \
    5.56 -		DEF:celsius=$rrdlog/cpu.rrd:celsius:AVERAGE \
    5.57 -		'CDEF:total=user,system,idle,nice,+,+,+' \
    5.58 -		'CDEF:userpct=100,user,total,/,*' \
    5.59 -		'CDEF:systempct=100,system,total,/,*' \
    5.60 -		'CDEF:idlepct=100,idle,total,/,*' \
    5.61 -		'CDEF:nicepct=100,nice,total,/,*' \
    5.62 -		'CDEF:temp=celsius,1000,/' \
    5.63 -		'AREA:userpct#0000FF:user cpu usage' \
    5.64 -		'STACK:nicepct#C0C0FF:nice cpu usage' \
    5.65 -		'STACK:systempct#FF0000:system cpu usage' \
    5.66 -		'STACK:idlepct#00FF00:idle cpu usage' \
    5.67 -		'LINE1:temp#000000:temperature\g' \
    5.68 -		'GPRINT:temp:MAX:max %2.0lfC\j'
    5.69 -}
    5.70 -
    5.71 -updatememgraph() {
    5.72 -	period=$1
    5.73 -	info="$(free | awk '\
    5.74 -{ \
    5.75 -  if (/Mem:/) { \
    5.76 -	if ($2 < 10000) printf "%d KB",$2; \
    5.77 -	else if ($2 < 10000000) printf "%d MB",$2/1024; \
    5.78 -	else printf "%d GB",$2/1024/1024; \
    5.79 -  } \
    5.80 -}')"
    5.81 -	info2="$(free | awk '\
    5.82 -{ \
    5.83 -  if (/Swap:/) { \
    5.84 -	if ($2 < 10000) printf "%d KB",$2; \
    5.85 -	else if ($2 < 10000000) printf "%d MB",$2/1024; \
    5.86 -	else printf "%d GB",$2/1024/1024; \
    5.87 -  } \
    5.88 -}')"
    5.89 -	rrdtool graph "$rrdgraph/memory-$period.png" --start -1$period \
    5.90 -		$rrdgraphargs -l 0 -u 100 \
    5.91 -		-t "memory usage per $period [ $info + $info2 swap ]" \
    5.92 -		DEF:used=$rrdlog/mem.rrd:memused:AVERAGE \
    5.93 -		DEF:free=$rrdlog/mem.rrd:memfree:AVERAGE \
    5.94 -		DEF:shared=$rrdlog/mem.rrd:memshared:AVERAGE \
    5.95 -		DEF:buffer=$rrdlog/mem.rrd:membuffers:AVERAGE \
    5.96 -		DEF:cache=$rrdlog/mem.rrd:memcache:AVERAGE \
    5.97 -		DEF:swused=$rrdlog/mem.rrd:swapused:AVERAGE \
    5.98 -		DEF:swfree=$rrdlog/mem.rrd:swapfree:AVERAGE \
    5.99 -		'CDEF:total=used,free,+' \
   5.100 -		'CDEF:used2=used,buffer,cache,shared,+,+,-' \
   5.101 -		'CDEF:usedpct=100,used2,total,/,*' \
   5.102 -		'CDEF:sharedpct=100,shared,total,/,*' \
   5.103 -		'CDEF:bufferpct=100,buffer,total,/,*' \
   5.104 -		'CDEF:cachepct=100,cache,total,/,*' \
   5.105 -		'CDEF:freepct=100,free,total,/,*' \
   5.106 -		'CDEF:swtotal=swused,swfree,+' \
   5.107 -		'CDEF:swusedpct=100,swused,swtotal,/,*' \
   5.108 -		'AREA:usedpct#0000FF:used memory' \
   5.109 -		'STACK:sharedpct#FF7F00:shared memory' \
   5.110 -		'STACK:bufferpct#FF00FF:buffered memory' \
   5.111 -		'STACK:cachepct#FFFF00:cached memory' \
   5.112 -		'STACK:freepct#00FF00:free memory' \
   5.113 -		'LINE2:swusedpct#FF0000:used swap\j'
   5.114 -}
   5.115 -
   5.116 -updatememdata () {
   5.117 -	[ -e "$rrdlog/mem.rrd" ] ||
   5.118 -		rrdtool create "$rrdlog/mem.rrd" --step=300 \
   5.119 -			DS:memused:ABSOLUTE:600:0:5000000000 \
   5.120 -			DS:memfree:ABSOLUTE:600:0:5000000000 \
   5.121 -			DS:memshared:ABSOLUTE:600:0:5000000000 \
   5.122 -			DS:membuffers:ABSOLUTE:600:0:5000000000 \
   5.123 -			DS:memcache:ABSOLUTE:600:0:5000000000 \
   5.124 -			DS:swapused:ABSOLUTE:600:0:5000000000 \
   5.125 -			DS:swapfree:ABSOLUTE:600:0:5000000000 \
   5.126 -			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   5.127 -			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   5.128 -
   5.129 -	while read tag count unit; do
   5.130 -		case "$tag" in
   5.131 -		MemTotal:)  memtotal=$count;;
   5.132 -		MemFree:)   memfree=$count
   5.133 -			    memused=$(($memtotal - $memfree))
   5.134 -			    memshared=0;;
   5.135 -		MemShared:) memshared=$count;;
   5.136 -		Buffers:)   membuffers=$count;;
   5.137 -		Cached:)    memcache=$count;;
   5.138 -		SwapTotal:) swaptotal=$count;;
   5.139 -		SwapFree:)  swapfree=$count
   5.140 -			    swapused=$(( $swaptotal - $swapfree));;
   5.141 -		esac
   5.142 -	done < /proc/meminfo
   5.143 -
   5.144 -	rrdtool update "$rrdlog/mem.rrd" \
   5.145 -		-t memused:memfree:memshared:membuffers:memcache:swapused:swapfree \
   5.146 -		"N:$memused:$memfree:$memshared:$membuffers:$memcache:$swapused:$swapfree"
   5.147 -}
   5.148 -
   5.149 -getmax() {
   5.150 -	rrdtool fetch $rrdlog/$1.rrd AVERAGE | awk '\
   5.151 -BEGIN {max=0} \
   5.152 -/^[0-9]/ { \
   5.153 -   if ($2 != "nan" && $2 > max) max=$2; \
   5.154 -   if ($3 != "nan" && $3 > max) max=$3; \
   5.155 -} \
   5.156 -END { print max }' | sed 's/,/./'
   5.157 -}
   5.158 -
   5.159 -updatediskgraph() {
   5.160 -	period=$1
   5.161 -	[ "$period" == "day" ] && maxdisk="$(getmax disk)"
   5.162 -	info=""
   5.163 -	[ -r $2 ] &&
   5.164 -	info="[ $(fdisk -l 2> /dev/null | grep "^Disk $2:" | \
   5.165 -		  sed "s|Disk $2: \(.*\), .*|\1|") ]"
   5.166 -	if [ -e /sys/block/${2#/dev/}/device/iodone_cnt ]; then
   5.167 -	err=$(printf "%d\n" $(cat /sys/block/${2#/dev/}/device/ioerr_cnt) )
   5.168 -	done=$(printf "%d\n" $(cat /sys/block/${2#/dev/}/device/iodone_cnt) )
   5.169 -	rate=$(echo | awk "BEGIN { printf \"%.0e\\n\",$err/$done }")
   5.170 -	[ $err -eq 0 ] &&  rate="0"
   5.171 -#		--right-axis-label "I/O state %"
   5.172 -	rrdtool graph "$rrdgraph/disk-$period.png" --start -1$period \
   5.173 -		$rrdgraphargs -t "disk access per $period $info" \
   5.174 -		--logarithmic --lower-limit 1 -v "Sectors/second" --units=si \
   5.175 -		DEF:read=$rrdlog/disk.rrd:readsect:AVERAGE \
   5.176 -		DEF:write=$rrdlog/disk.rrd:writesect:AVERAGE \
   5.177 -		DEF:req=$rrdlog/iodisk.rrd:req:AVERAGE \
   5.178 -		DEF:done=$rrdlog/iodisk.rrd:done:AVERAGE \
   5.179 -		DEF:err=$rrdlog/iodisk.rrd:err:AVERAGE \
   5.180 -		"CDEF:readpct=100,read,$maxdisk,/,*" \
   5.181 -		"CDEF:writepct=100,write,$maxdisk,/,*" \
   5.182 -		"CDEF:errpct=100,err,req,/,*" \
   5.183 -		"CDEF:donepct=100,done,req,/,*" \
   5.184 -		"CDEF:errrate=err,done,/" \
   5.185 -		'AREA:readpct#0000FF:sectors read from disk' \
   5.186 -		"COMMENT:I/O error rate $rate" \
   5.187 -		'STACK:writepct#00FF00:sectors written to disk' \
   5.188 -		'LINE2:donepct#FFFF00:% I/O complete' \
   5.189 -		'LINE2:errpct#FF0000:% I/O error\j'
   5.190 -	else
   5.191 -	rrdtool graph "$rrdgraph/disk-$period.png" --start -1$period \
   5.192 -		$rrdgraphargs -t "disk access per $period $info" \
   5.193 -		--logarithmic --lower-limit 1 -v "Sectors/second" --units=si \
   5.194 -		DEF:read=$rrdlog/disk.rrd:readsect:AVERAGE \
   5.195 -		DEF:write=$rrdlog/disk.rrd:writesect:AVERAGE \
   5.196 -		"CDEF:readpct=100,read,$maxdisk,/,*" \
   5.197 -		"CDEF:writepct=100,write,$maxdisk,/,*" \
   5.198 -		'AREA:readpct#0000FF:sectors read from disk' \
   5.199 -		'STACK:writepct#00FF00:sectors written to disk'
   5.200 -	fi
   5.201 -}
   5.202 -
   5.203 -updatediskdata() {
   5.204 -	dev=$1
   5.205 -	[ -e "$rrdlog/disk.rrd" ] ||
   5.206 -		rrdtool create "$rrdlog/disk.rrd" --step=300 \
   5.207 -			DS:readsect:COUNTER:600:0:5000000000 \
   5.208 -			DS:writesect:COUNTER:600:0:5000000000 \
   5.209 -			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   5.210 -			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   5.211 -	[ -e "$rrdlog/iodisk.rrd" ] ||
   5.212 -		rrdtool create "$rrdlog/iodisk.rrd" --step=300 \
   5.213 -			DS:done:GAUGE:600:0:U  DS:err:GAUGE:600:0:U \
   5.214 -			DS:req:GAUGE:600:0:U \
   5.215 -			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   5.216 -			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   5.217 -
   5.218 -	while read major minor name readreq readsect writereq writesect misc; do
   5.219 -		[ $major = $(( 0x$(stat -c %t $dev) )) ] || continue
   5.220 -		[ $minor = $(( 0x$(stat -c %T $dev) )) ] || continue
   5.221 -		rrdtool update "$rrdlog/disk.rrd" -t readsect:writesect \
   5.222 -			N:$readsect:$writesect
   5.223 -	done < /proc/diskstats
   5.224 -	disk=${dev:0:8}
   5.225 -	dir=/sys/block/${disk#/dev/}/device
   5.226 -	done=$(printf "%d\n" $(cat $dir/iodone_cnt 2> /dev/null) )
   5.227 -	err=$(printf "%d\n" $(cat $dir/ioerr_cnt 2> /dev/null) )
   5.228 -	req=$(printf "%d\n" $(cat $dir/iorequest_cnt 2> /dev/null) )
   5.229 -	rrdtool update "$rrdlog/iodisk.rrd" -t done:err:req N:$done:$err:$req
   5.230 -}
   5.231 -
   5.232 -updateifgraph() {
   5.233 -	interface=$1
   5.234 -	period=$2
   5.235 -	rrdtool graph "$rrdgraph/$interface-$period.png" --start -1$period \
   5.236 -		$rrdgraphargs -t "traffic on $interface graph per $period" \
   5.237 -		--logarithmic -A -v "Bytes/second" --units=si \
   5.238 -		DEF:incoming=$rrdlog/$interface.rrd:incoming:AVERAGE \
   5.239 -		DEF:outgoing=$rrdlog/$interface.rrd:outgoing:AVERAGE \
   5.240 -		'AREA:incoming#00FF00:incoming traffic' \
   5.241 -		'GPRINT:incoming:MAX:max input%8.3lf %sBps' \
   5.242 -		'GPRINT:outgoing:MAX:max output%8.3lf %sBps' \
   5.243 -		'LINE1:outgoing#0000FF:outgoing traffic\j'
   5.244 -}
   5.245 -
   5.246 -netframes() {
   5.247 -ifconfig $1 | grep "$2 packets" | sed -re "s/.*$3:([0-9]+).*/\1/g"
   5.248 -}
   5.249 -
   5.250 -netstats() {
   5.251 -ifconfig $1 | grep bytes | sed -re "s/.*$2 bytes:([0-9]+).*/\1/g"
   5.252 -}
   5.253 -
   5.254 -updateifdata() {
   5.255 -	interface=$1
   5.256 -	[ -e "$rrdlog/$interface.rrd" ] ||
   5.257 -		rrdtool create "$rrdlog/$interface.rrd" --step=300 \
   5.258 -			DS:incoming:COUNTER:600:0:U \
   5.259 -			DS:outgoing:COUNTER:600:0:U \
   5.260 -			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   5.261 -			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   5.262 -	[ -e "$rrdlog/packets-$interface.rrd" ] ||
   5.263 -		rrdtool create "$rrdlog/packets-$interface.rrd" --step=300 \
   5.264 -			DS:in:COUNTER:600:0:U      DS:out:COUNTER:600:0:U \
   5.265 -			DS:inerr:COUNTER:600:0:U   DS:outerr:COUNTER:600:0:U \
   5.266 -			DS:indrop:COUNTER:600:0:U  DS:outdrop:COUNTER:600:0:U \
   5.267 -			DS:inov:COUNTER:600:0:U    DS:outov:COUNTER:600:0:U \
   5.268 -			DS:frame:COUNTER:600:0:U   DS:carrier:COUNTER:600:0:U \
   5.269 -			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   5.270 -			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   5.271 -	rx=$(netstats $interface RX)
   5.272 -	tx=$(netstats $interface TX)
   5.273 -	rrdtool update "$rrdlog/$interface.rrd" -t incoming:outgoing \
   5.274 -		N:${rx:-U}:${tx:-U}
   5.275 -	rx=$(netframes $interface RX packets)
   5.276 -	tx=$(netframes $interface TX packets)
   5.277 -	rxerr=$(netframes $interface RX errors)
   5.278 -	txerr=$(netframes $interface TX errors)
   5.279 -	rxdrop=$(netframes $interface RX dropped)
   5.280 -	txdrop=$(netframes $interface TX dropped)
   5.281 -	rxov=$(netframes $interface RX overruns)
   5.282 -	txov=$(netframes $interface TX overruns)
   5.283 -	frame=$(netframes $interface RX frame)
   5.284 -	carrier=$(netframes $interface TX carrier)
   5.285 -	rrdtool update "$rrdlog/packets-$interface.rrd" \
   5.286 -		-t in:out:inerr:outerr:indrop:outdrop:inov:outov:frame:carrier \
   5.287 -		N:${rx:-U}:${tx:-U}:${rxerr:-U}:${txerr:-U}:${rxdrop:-U}:${txdrop:-U}:${rxov:-U}:${txov:-U}:${frame:-U}:${carrier:-U}
   5.288 -}
   5.289 -
   5.290 -getdisk()
   5.291 -{
   5.292 -	local d
   5.293 -	local i
   5.294 -	d=$(stat -c %04D $1)
   5.295 -	for i in /dev/* ; do 
   5.296 -		[ $(stat -c "%02t%02T" $i) == $d ] || continue
   5.297 -		echo $i
   5.298 -		break
   5.299 -	done
   5.300 -}
   5.301 -
   5.302 -###
   5.303 -### System graphs
   5.304 -###
   5.305 -
   5.306 -updatecpudata
   5.307 -updatecpugraph day
   5.308 -updatecpugraph week
   5.309 -updatecpugraph month
   5.310 -updatecpugraph year
   5.311 -
   5.312 -updatememdata
   5.313 -updatememgraph day
   5.314 -updatememgraph week
   5.315 -updatememgraph month
   5.316 -updatememgraph year
   5.317 -
   5.318 -if [ -e /proc/diskstats ]; then
   5.319 -	disk=$(getdisk $0)
   5.320 -	updatediskdata $disk
   5.321 -	updatediskgraph day ${disk:0:8}
   5.322 -	updatediskgraph week ${disk:0:8}
   5.323 -	updatediskgraph month ${disk:0:8}
   5.324 -	updatediskgraph year ${disk:0:8}
   5.325 -fi
   5.326 -
   5.327 -iface=$(/sbin/route -n | awk '{ if (/^0.0.0.0/) print $8 }')
   5.328 -updateifdata $iface
   5.329 -updateifgraph $iface day
   5.330 -updateifgraph $iface week
   5.331 -updateifgraph $iface month
   5.332 -updateifgraph $iface year