slitaz-forge rev 127

chub: Add custom rrdtool script and cron task
author Christophe Lincoln <pankso@slitaz.org>
date Thu Mar 15 04:27:09 2012 +0100 (2012-03-15)
parents 8b2ee3af1cef
children 3897fc67dad5
files chub/Makefile chub/files/var/spool/cron/crontabs/root chub/web/lib/makegraphs
line diff
     1.1 --- a/chub/Makefile	Thu Mar 15 04:01:49 2012 +0100
     1.2 +++ b/chub/Makefile	Thu Mar 15 04:27:09 2012 +0100
     1.3 @@ -7,9 +7,13 @@
     1.4  all:
     1.5  
     1.6  install:
     1.7 +	install -m 0777 -d /var/spool/cron/crontabs
     1.8  	install -m 0777 chub $(PREFIX)/bin
     1.9  	install -m 0644 files/etc/lighttpd/vhosts.conf /etc/lighttpd
    1.10 +	install -m 0644 files/var/spool/cron/crontabs/root \
    1.11 +		/var/spool/cron/crontabs
    1.12  
    1.13  uninstall:
    1.14  	rm -f $(PREFIX)/bin/chub
    1.15  	rm -f /etc/lighttpd/vhosts.conf
    1.16 +	rm -f /var/spool/cron/crontabs/root
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/chub/files/var/spool/cron/crontabs/root	Thu Mar 15 04:27:09 2012 +0100
     2.3 @@ -0,0 +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/makegraphs >/dev/null
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/chub/web/lib/makegraphs	Thu Mar 15 04:27:09 2012 +0100
     3.3 @@ -0,0 +1,330 @@
     3.4 +#!/bin/sh
     3.5 +#*/5  * * * * /home/slitaz/www/chub/lib/makegraphs >/dev/null
     3.6 +
     3.7 +# Virtual host path
     3.8 +vhost="/home/slitaz/www/chub"
     3.9 +
    3.10 +# RRD database directory
    3.11 +rrdlog="$vhost/rrd/log"
    3.12 +
    3.13 +# Images directory
    3.14 +rrdgraph="$vhost/rrd/images"
    3.15 +
    3.16 +# Colors
    3.17 +rrdcolors="--color SHADEA#FFFFFF --color SHADEB#FFFFFF --color BACK#FFFFFF" 
    3.18 +rrdgraphargs="-aPNG -i -z --alt-y-grid -w 600 -h 100 -r $rrdcolors"
    3.19 +
    3.20 +[ -d $rrdlog ] || mkdir -p $rrdlog
    3.21 +[ -d $rrdgraph ] || mkdir -p $rrdgraph
    3.22 +
    3.23 +updatecpudata() {
    3.24 +	[ -e "$rrdlog/cpu.rrd" ] || rrdtool create $rrdlog/cpu.rrd --step=300 \
    3.25 +			DS:user:COUNTER:600:0:500000000 \
    3.26 +			DS:nice:COUNTER:600:0:500000000 \
    3.27 +			DS:system:COUNTER:600:0:500000000 \
    3.28 +			DS:idle:COUNTER:600:0:500000000 \
    3.29 +			DS:iowait:COUNTER:600:0:500000000 \
    3.30 +			DS:irq:COUNTER:600:0:500000000 \
    3.31 +			DS:softirq:COUNTER:600:0:500000000 \
    3.32 +			DS:celsius:GAUGE:600:0:50000 \
    3.33 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
    3.34 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
    3.35 +	grep '^cpu' /proc/stat | while read cpu user nice system idle iowait irq softirq misc; do
    3.36 +		celsius=$(find /sys | grep /temp._input | xargs cat | \
    3.37 +			awk '{ if ($0 > max) max=$0 } END { print max/1 }')
    3.38 +		rrdtool update $rrdlog/cpu.rrd \
    3.39 +			-t celsius:nice:user:system:idle:iowait:irq:softirq \
    3.40 +			N:$celsius:$nice:$user:$system:$idle:$iowait:$irq:$softirq
    3.41 +		break
    3.42 +	done
    3.43 +}
    3.44 +
    3.45 +updatecpugraph() {
    3.46 +	period=$1
    3.47 +	info="$(grep '^model name' /proc/cpuinfo | cut -d: -f2 \
    3.48 +		| sed 's/ * / /g' | awk '
    3.49 +{ s=$0 ; n++ }                    
    3.50 +END { if (n > 1) printf " %dx",n; print s }')"
    3.51 +	rrdtool graph "$rrdgraph/cpu-$period.png" --start -1$period \
    3.52 +		$rrdgraphargs -l 0 -u 100 -t "cpu usage per $period [$info ]" \
    3.53 +		DEF:user=$rrdlog/cpu.rrd:user:AVERAGE \
    3.54 +		DEF:system=$rrdlog/cpu.rrd:system:AVERAGE \
    3.55 +		DEF:idle=$rrdlog/cpu.rrd:idle:AVERAGE \
    3.56 +		DEF:nice=$rrdlog/cpu.rrd:nice:AVERAGE \
    3.57 +		DEF:celsius=$rrdlog/cpu.rrd:celsius:AVERAGE \
    3.58 +		'CDEF:total=user,system,idle,nice,+,+,+' \
    3.59 +		'CDEF:userpct=100,user,total,/,*' \
    3.60 +		'CDEF:systempct=100,system,total,/,*' \
    3.61 +		'CDEF:idlepct=100,idle,total,/,*' \
    3.62 +		'CDEF:nicepct=100,nice,total,/,*' \
    3.63 +		'CDEF:temp=celsius,1000,/' \
    3.64 +		'AREA:userpct#0000FF:user cpu usage' \
    3.65 +		'STACK:nicepct#C0C0FF:nice cpu usage' \
    3.66 +		'STACK:systempct#FF0000:system cpu usage' \
    3.67 +		'STACK:idlepct#00FF00:idle cpu usage' \
    3.68 +		'LINE1:temp#000000:temperature\g' \
    3.69 +		'GPRINT:temp:MAX:max %2.0lfC\j'
    3.70 +}
    3.71 +
    3.72 +updatememgraph() {
    3.73 +	period=$1
    3.74 +	info="$(free | awk '\
    3.75 +{ \
    3.76 +  if (/Mem:/) { \
    3.77 +	if ($2 < 10000) printf "%d KB",$2; \
    3.78 +	else if ($2 < 10000000) printf "%d MB",$2/1024; \
    3.79 +	else printf "%d GB",$2/1024/1024; \
    3.80 +  } \
    3.81 +}')"
    3.82 +	info2="$(free | awk '\
    3.83 +{ \
    3.84 +  if (/Swap:/) { \
    3.85 +	if ($2 < 10000) printf "%d KB",$2; \
    3.86 +	else if ($2 < 10000000) printf "%d MB",$2/1024; \
    3.87 +	else printf "%d GB",$2/1024/1024; \
    3.88 +  } \
    3.89 +}')"
    3.90 +	rrdtool graph "$rrdgraph/memory-$period.png" --start -1$period \
    3.91 +		$rrdgraphargs -l 0 -u 100 \
    3.92 +		-t "memory usage per $period [ $info + $info2 swap ]" \
    3.93 +		DEF:used=$rrdlog/mem.rrd:memused:AVERAGE \
    3.94 +		DEF:free=$rrdlog/mem.rrd:memfree:AVERAGE \
    3.95 +		DEF:shared=$rrdlog/mem.rrd:memshared:AVERAGE \
    3.96 +		DEF:buffer=$rrdlog/mem.rrd:membuffers:AVERAGE \
    3.97 +		DEF:cache=$rrdlog/mem.rrd:memcache:AVERAGE \
    3.98 +		DEF:swused=$rrdlog/mem.rrd:swapused:AVERAGE \
    3.99 +		DEF:swfree=$rrdlog/mem.rrd:swapfree:AVERAGE \
   3.100 +		'CDEF:total=used,free,+' \
   3.101 +		'CDEF:used2=used,buffer,cache,shared,+,+,-' \
   3.102 +		'CDEF:usedpct=100,used2,total,/,*' \
   3.103 +		'CDEF:sharedpct=100,shared,total,/,*' \
   3.104 +		'CDEF:bufferpct=100,buffer,total,/,*' \
   3.105 +		'CDEF:cachepct=100,cache,total,/,*' \
   3.106 +		'CDEF:freepct=100,free,total,/,*' \
   3.107 +		'CDEF:swtotal=swused,swfree,+' \
   3.108 +		'CDEF:swusedpct=100,swused,swtotal,/,*' \
   3.109 +		'AREA:usedpct#0000FF:used memory' \
   3.110 +		'STACK:sharedpct#FF7F00:shared memory' \
   3.111 +		'STACK:bufferpct#FF00FF:buffered memory' \
   3.112 +		'STACK:cachepct#FFFF00:cached memory' \
   3.113 +		'STACK:freepct#00FF00:free memory' \
   3.114 +		'LINE2:swusedpct#FF0000:used swap\j'
   3.115 +}
   3.116 +
   3.117 +updatememdata () {
   3.118 +	[ -e "$rrdlog/mem.rrd" ] ||
   3.119 +		rrdtool create "$rrdlog/mem.rrd" --step=300 \
   3.120 +			DS:memused:ABSOLUTE:600:0:5000000000 \
   3.121 +			DS:memfree:ABSOLUTE:600:0:5000000000 \
   3.122 +			DS:memshared:ABSOLUTE:600:0:5000000000 \
   3.123 +			DS:membuffers:ABSOLUTE:600:0:5000000000 \
   3.124 +			DS:memcache:ABSOLUTE:600:0:5000000000 \
   3.125 +			DS:swapused:ABSOLUTE:600:0:5000000000 \
   3.126 +			DS:swapfree:ABSOLUTE:600:0:5000000000 \
   3.127 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   3.128 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   3.129 +
   3.130 +	while read tag count unit; do
   3.131 +		case "$tag" in
   3.132 +		MemTotal:)  memtotal=$count;;
   3.133 +		MemFree:)   memfree=$count
   3.134 +			    memused=$(($memtotal - $memfree))
   3.135 +			    memshared=0;;
   3.136 +		MemShared:) memshared=$count;;
   3.137 +		Buffers:)   membuffers=$count;;
   3.138 +		Cached:)    memcache=$count;;
   3.139 +		SwapTotal:) swaptotal=$count;;
   3.140 +		SwapFree:)  swapfree=$count
   3.141 +			    swapused=$(( $swaptotal - $swapfree));;
   3.142 +		esac
   3.143 +	done < /proc/meminfo
   3.144 +
   3.145 +	rrdtool update "$rrdlog/mem.rrd" \
   3.146 +		-t memused:memfree:memshared:membuffers:memcache:swapused:swapfree \
   3.147 +		"N:$memused:$memfree:$memshared:$membuffers:$memcache:$swapused:$swapfree"
   3.148 +}
   3.149 +
   3.150 +getmax() {
   3.151 +	rrdtool fetch $rrdlog/$1.rrd AVERAGE | awk '\
   3.152 +BEGIN {max=0} \
   3.153 +/^[0-9]/ { \
   3.154 +   if ($2 != "nan" && $2 > max) max=$2; \
   3.155 +   if ($3 != "nan" && $3 > max) max=$3; \
   3.156 +} \
   3.157 +END { print max }' | sed 's/,/./'
   3.158 +}
   3.159 +
   3.160 +updatediskgraph() {
   3.161 +	period=$1
   3.162 +	[ "$period" == "day" ] && maxdisk="$(getmax disk)"
   3.163 +	info=""
   3.164 +	[ -r $2 ] &&
   3.165 +	info="[ $(fdisk -l 2> /dev/null | grep "^Disk $2:" | \
   3.166 +		  sed "s|Disk $2: \(.*\), .*|\1|") ]"
   3.167 +	if [ -e /sys/block/${2#/dev/}/device/iodone_cnt ]; then
   3.168 +	err=$(printf "%d\n" $(cat /sys/block/${2#/dev/}/device/ioerr_cnt) )
   3.169 +	done=$(printf "%d\n" $(cat /sys/block/${2#/dev/}/device/iodone_cnt) )
   3.170 +	rate=$(echo | awk "BEGIN { printf \"%.0e\\n\",$err/$done }")
   3.171 +	[ $err -eq 0 ] &&  rate="0"
   3.172 +#		--right-axis-label "I/O state %"
   3.173 +	rrdtool graph "$rrdgraph/disk-$period.png" --start -1$period \
   3.174 +		$rrdgraphargs -t "disk access per $period $info" \
   3.175 +		--logarithmic --lower-limit 1 -v "Sectors/second" --units=si \
   3.176 +		DEF:read=$rrdlog/disk.rrd:readsect:AVERAGE \
   3.177 +		DEF:write=$rrdlog/disk.rrd:writesect:AVERAGE \
   3.178 +		DEF:req=$rrdlog/iodisk.rrd:req:AVERAGE \
   3.179 +		DEF:done=$rrdlog/iodisk.rrd:done:AVERAGE \
   3.180 +		DEF:err=$rrdlog/iodisk.rrd:err:AVERAGE \
   3.181 +		"CDEF:readpct=100,read,$maxdisk,/,*" \
   3.182 +		"CDEF:writepct=100,write,$maxdisk,/,*" \
   3.183 +		"CDEF:errpct=100,err,req,/,*" \
   3.184 +		"CDEF:donepct=100,done,req,/,*" \
   3.185 +		"CDEF:errrate=err,done,/" \
   3.186 +		'AREA:readpct#0000FF:sectors read from disk' \
   3.187 +		"COMMENT:I/O error rate $rate" \
   3.188 +		'STACK:writepct#00FF00:sectors written to disk' \
   3.189 +		'LINE2:donepct#FFFF00:% I/O complete' \
   3.190 +		'LINE2:errpct#FF0000:% I/O error\j'
   3.191 +	else
   3.192 +	rrdtool graph "$rrdgraph/disk-$period.png" --start -1$period \
   3.193 +		$rrdgraphargs -t "disk access per $period $info" \
   3.194 +		--logarithmic --lower-limit 1 -v "Sectors/second" --units=si \
   3.195 +		DEF:read=$rrdlog/disk.rrd:readsect:AVERAGE \
   3.196 +		DEF:write=$rrdlog/disk.rrd:writesect:AVERAGE \
   3.197 +		"CDEF:readpct=100,read,$maxdisk,/,*" \
   3.198 +		"CDEF:writepct=100,write,$maxdisk,/,*" \
   3.199 +		'AREA:readpct#0000FF:sectors read from disk' \
   3.200 +		'STACK:writepct#00FF00:sectors written to disk'
   3.201 +	fi
   3.202 +}
   3.203 +
   3.204 +updatediskdata() {
   3.205 +	dev=$1
   3.206 +	[ -e "$rrdlog/disk.rrd" ] ||
   3.207 +		rrdtool create "$rrdlog/disk.rrd" --step=300 \
   3.208 +			DS:readsect:COUNTER:600:0:5000000000 \
   3.209 +			DS:writesect:COUNTER:600:0:5000000000 \
   3.210 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   3.211 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   3.212 +	[ -e "$rrdlog/iodisk.rrd" ] ||
   3.213 +		rrdtool create "$rrdlog/iodisk.rrd" --step=300 \
   3.214 +			DS:done:GAUGE:600:0:U  DS:err:GAUGE:600:0:U \
   3.215 +			DS:req:GAUGE:600:0:U \
   3.216 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   3.217 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   3.218 +
   3.219 +	while read major minor name readreq readsect writereq writesect misc; do
   3.220 +		[ $major = $(( 0x$(stat -c %t $dev) )) ] || continue
   3.221 +		[ $minor = $(( 0x$(stat -c %T $dev) )) ] || continue
   3.222 +		rrdtool update "$rrdlog/disk.rrd" -t readsect:writesect \
   3.223 +			N:$readsect:$writesect
   3.224 +	done < /proc/diskstats
   3.225 +	disk=${dev:0:8}
   3.226 +	dir=/sys/block/${disk#/dev/}/device
   3.227 +	done=$(printf "%d\n" $(cat $dir/iodone_cnt 2> /dev/null) )
   3.228 +	err=$(printf "%d\n" $(cat $dir/ioerr_cnt 2> /dev/null) )
   3.229 +	req=$(printf "%d\n" $(cat $dir/iorequest_cnt 2> /dev/null) )
   3.230 +	rrdtool update "$rrdlog/iodisk.rrd" -t done:err:req N:$done:$err:$req
   3.231 +}
   3.232 +
   3.233 +updateifgraph() {
   3.234 +	interface=$1
   3.235 +	period=$2
   3.236 +	rrdtool graph "$rrdgraph/$interface-$period.png" --start -1$period \
   3.237 +		$rrdgraphargs -t "traffic on $interface graph per $period" \
   3.238 +		--logarithmic -A -v "Bytes/second" --units=si \
   3.239 +		DEF:incoming=$rrdlog/$interface.rrd:incoming:AVERAGE \
   3.240 +		DEF:outgoing=$rrdlog/$interface.rrd:outgoing:AVERAGE \
   3.241 +		'AREA:incoming#00FF00:incoming traffic' \
   3.242 +		'GPRINT:incoming:MAX:max input%8.3lf %sBps' \
   3.243 +		'GPRINT:outgoing:MAX:max output%8.3lf %sBps' \
   3.244 +		'LINE1:outgoing#0000FF:outgoing traffic\j'
   3.245 +}
   3.246 +
   3.247 +netframes() {
   3.248 +ifconfig $1 | grep "$2 packets" | sed -re "s/.*$3:([0-9]+).*/\1/g"
   3.249 +}
   3.250 +
   3.251 +netstats() {
   3.252 +ifconfig $1 | grep bytes | sed -re "s/.*$2 bytes:([0-9]+).*/\1/g"
   3.253 +}
   3.254 +
   3.255 +updateifdata() {
   3.256 +	interface=$1
   3.257 +	[ -e "$rrdlog/$interface.rrd" ] ||
   3.258 +		rrdtool create "$rrdlog/$interface.rrd" --step=300 \
   3.259 +			DS:incoming:COUNTER:600:0:U \
   3.260 +			DS:outgoing:COUNTER:600:0:U \
   3.261 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   3.262 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   3.263 +	[ -e "$rrdlog/packets-$interface.rrd" ] ||
   3.264 +		rrdtool create "$rrdlog/packets-$interface.rrd" --step=300 \
   3.265 +			DS:in:COUNTER:600:0:U      DS:out:COUNTER:600:0:U \
   3.266 +			DS:inerr:COUNTER:600:0:U   DS:outerr:COUNTER:600:0:U \
   3.267 +			DS:indrop:COUNTER:600:0:U  DS:outdrop:COUNTER:600:0:U \
   3.268 +			DS:inov:COUNTER:600:0:U    DS:outov:COUNTER:600:0:U \
   3.269 +			DS:frame:COUNTER:600:0:U   DS:carrier:COUNTER:600:0:U \
   3.270 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   3.271 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   3.272 +	rx=$(netstats $interface RX)
   3.273 +	tx=$(netstats $interface TX)
   3.274 +	rrdtool update "$rrdlog/$interface.rrd" -t incoming:outgoing \
   3.275 +		N:${rx:-U}:${tx:-U}
   3.276 +	rx=$(netframes $interface RX packets)
   3.277 +	tx=$(netframes $interface TX packets)
   3.278 +	rxerr=$(netframes $interface RX errors)
   3.279 +	txerr=$(netframes $interface TX errors)
   3.280 +	rxdrop=$(netframes $interface RX dropped)
   3.281 +	txdrop=$(netframes $interface TX dropped)
   3.282 +	rxov=$(netframes $interface RX overruns)
   3.283 +	txov=$(netframes $interface TX overruns)
   3.284 +	frame=$(netframes $interface RX frame)
   3.285 +	carrier=$(netframes $interface TX carrier)
   3.286 +	rrdtool update "$rrdlog/packets-$interface.rrd" \
   3.287 +		-t in:out:inerr:outerr:indrop:outdrop:inov:outov:frame:carrier \
   3.288 +		N:${rx:-U}:${tx:-U}:${rxerr:-U}:${txerr:-U}:${rxdrop:-U}:${txdrop:-U}:${rxov:-U}:${txov:-U}:${frame:-U}:${carrier:-U}
   3.289 +}
   3.290 +
   3.291 +getdisk()
   3.292 +{
   3.293 +	local d
   3.294 +	local i
   3.295 +	d=$(stat -c %04D $1)
   3.296 +	for i in /dev/* ; do 
   3.297 +		[ $(stat -c "%02t%02T" $i) == $d ] || continue
   3.298 +		echo $i
   3.299 +		break
   3.300 +	done
   3.301 +}
   3.302 +
   3.303 +###
   3.304 +### System graphs
   3.305 +###
   3.306 +
   3.307 +updatecpudata
   3.308 +updatecpugraph day
   3.309 +updatecpugraph week
   3.310 +updatecpugraph month
   3.311 +updatecpugraph year
   3.312 +
   3.313 +updatememdata
   3.314 +updatememgraph day
   3.315 +updatememgraph week
   3.316 +updatememgraph month
   3.317 +updatememgraph year
   3.318 +
   3.319 +if [ -e /proc/diskstats ]; then
   3.320 +	disk=$(getdisk $0)
   3.321 +	updatediskdata $disk
   3.322 +	updatediskgraph day ${disk:0:8}
   3.323 +	updatediskgraph week ${disk:0:8}
   3.324 +	updatediskgraph month ${disk:0:8}
   3.325 +	updatediskgraph year ${disk:0:8}
   3.326 +fi
   3.327 +
   3.328 +iface=$(/sbin/route -n | awk '{ if (/^0.0.0.0/) print $8 }')
   3.329 +updateifdata $iface
   3.330 +updateifgraph $iface day
   3.331 +updateifgraph $iface week
   3.332 +updateifgraph $iface month
   3.333 +updateifgraph $iface year