slitaz-forge rev 128

chub: Use a config file
author Christophe Lincoln <pankso@slitaz.org>
date Thu Mar 15 04:40:30 2012 +0100 (2012-03-15)
parents c67fbfe8b9d2
children 71d727cc53f2
files chub/Makefile chub/chub chub/chub.conf chub/files/var/spool/cron/crontabs/root chub/web/lib/makegraphs chub/web/lib/rrdtool
line diff
     1.1 --- a/chub/Makefile	Thu Mar 15 04:27:09 2012 +0100
     1.2 +++ b/chub/Makefile	Thu Mar 15 04:40:30 2012 +0100
     1.3 @@ -9,6 +9,7 @@
     1.4  install:
     1.5  	install -m 0777 -d /var/spool/cron/crontabs
     1.6  	install -m 0777 chub $(PREFIX)/bin
     1.7 +	install -m 0644 chub.conf /etc/slitaz
     1.8  	install -m 0644 files/etc/lighttpd/vhosts.conf /etc/lighttpd
     1.9  	install -m 0644 files/var/spool/cron/crontabs/root \
    1.10  		/var/spool/cron/crontabs
     2.1 --- a/chub/chub	Thu Mar 15 04:27:09 2012 +0100
     2.2 +++ b/chub/chub	Thu Mar 15 04:40:30 2012 +0100
     2.3 @@ -10,10 +10,8 @@
     2.4  # Authors : Christophe Lincoln <pankso@slitaz.org>
     2.5  #
     2.6  
     2.7 -REPOS="/home/slitaz/repos"
     2.8 -WWW="/home/slitaz/www"
     2.9 -VHOST="$WWW/chub"
    2.10 -WEBSITE="$WWW/website"
    2.11 +[ -f "/etc/slitaz/chub.conf" ] && . /etc/slitaz/chub.conf
    2.12 +[ -f "chub.conf" ] && . ./chub.conf
    2.13  
    2.14  usage() {
    2.15  	echo "\nUsage: $(basename $0) [command]
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/chub/chub.conf	Thu Mar 15 04:40:30 2012 +0100
     3.3 @@ -0,0 +1,10 @@
     3.4 +# /etc/slitaz/chub.conf - Chub configuration file
     3.5 +#
     3.6 +
     3.7 +# Repositorie path.
     3.8 +REPOS="/home/slitaz/repos"
     3.9 +
    3.10 +# Virtual hosts paths.
    3.11 +WWW="/home/slitaz/www"
    3.12 +VHOST="$WWW/chub"
    3.13 +WEBSITE="$WWW/website"
     4.1 --- a/chub/files/var/spool/cron/crontabs/root	Thu Mar 15 04:27:09 2012 +0100
     4.2 +++ b/chub/files/var/spool/cron/crontabs/root	Thu Mar 15 04:40:30 2012 +0100
     4.3 @@ -2,4 +2,4 @@
     4.4  #
     4.5  
     4.6  # Update RRD images
     4.7 -*/5 * * * * /home/slitaz/www/chub/lib/makegraphs >/dev/null
     4.8 +*/5 * * * * /home/slitaz/www/chub/lib/rrdtool >/dev/null
     5.1 --- a/chub/web/lib/makegraphs	Thu Mar 15 04:27:09 2012 +0100
     5.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.3 @@ -1,330 +0,0 @@
     5.4 -#!/bin/sh
     5.5 -#*/5  * * * * /home/slitaz/www/chub/lib/makegraphs >/dev/null
     5.6 -
     5.7 -# Virtual host path
     5.8 -vhost="/home/slitaz/www/chub"
     5.9 -
    5.10 -# RRD database directory
    5.11 -rrdlog="$vhost/rrd/log"
    5.12 -
    5.13 -# Images directory
    5.14 -rrdgraph="$vhost/rrd/images"
    5.15 -
    5.16 -# Colors
    5.17 -rrdcolors="--color SHADEA#FFFFFF --color SHADEB#FFFFFF --color BACK#FFFFFF" 
    5.18 -rrdgraphargs="-aPNG -i -z --alt-y-grid -w 600 -h 100 -r $rrdcolors"
    5.19 -
    5.20 -[ -d $rrdlog ] || mkdir -p $rrdlog
    5.21 -[ -d $rrdgraph ] || mkdir -p $rrdgraph
    5.22 -
    5.23 -updatecpudata() {
    5.24 -	[ -e "$rrdlog/cpu.rrd" ] || rrdtool create $rrdlog/cpu.rrd --step=300 \
    5.25 -			DS:user:COUNTER:600:0:500000000 \
    5.26 -			DS:nice:COUNTER:600:0:500000000 \
    5.27 -			DS:system:COUNTER:600:0:500000000 \
    5.28 -			DS:idle:COUNTER:600:0:500000000 \
    5.29 -			DS:iowait:COUNTER:600:0:500000000 \
    5.30 -			DS:irq:COUNTER:600:0:500000000 \
    5.31 -			DS:softirq:COUNTER:600:0:500000000 \
    5.32 -			DS:celsius:GAUGE:600:0:50000 \
    5.33 -			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
    5.34 -			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
    5.35 -	grep '^cpu' /proc/stat | while read cpu user nice system idle iowait irq softirq misc; do
    5.36 -		celsius=$(find /sys | grep /temp._input | xargs cat | \
    5.37 -			awk '{ if ($0 > max) max=$0 } END { print max/1 }')
    5.38 -		rrdtool update $rrdlog/cpu.rrd \
    5.39 -			-t celsius:nice:user:system:idle:iowait:irq:softirq \
    5.40 -			N:$celsius:$nice:$user:$system:$idle:$iowait:$irq:$softirq
    5.41 -		break
    5.42 -	done
    5.43 -}
    5.44 -
    5.45 -updatecpugraph() {
    5.46 -	period=$1
    5.47 -	info="$(grep '^model name' /proc/cpuinfo | cut -d: -f2 \
    5.48 -		| sed 's/ * / /g' | awk '
    5.49 -{ s=$0 ; n++ }                    
    5.50 -END { if (n > 1) printf " %dx",n; print s }')"
    5.51 -	rrdtool graph "$rrdgraph/cpu-$period.png" --start -1$period \
    5.52 -		$rrdgraphargs -l 0 -u 100 -t "cpu usage per $period [$info ]" \
    5.53 -		DEF:user=$rrdlog/cpu.rrd:user:AVERAGE \
    5.54 -		DEF:system=$rrdlog/cpu.rrd:system:AVERAGE \
    5.55 -		DEF:idle=$rrdlog/cpu.rrd:idle:AVERAGE \
    5.56 -		DEF:nice=$rrdlog/cpu.rrd:nice:AVERAGE \
    5.57 -		DEF:celsius=$rrdlog/cpu.rrd:celsius:AVERAGE \
    5.58 -		'CDEF:total=user,system,idle,nice,+,+,+' \
    5.59 -		'CDEF:userpct=100,user,total,/,*' \
    5.60 -		'CDEF:systempct=100,system,total,/,*' \
    5.61 -		'CDEF:idlepct=100,idle,total,/,*' \
    5.62 -		'CDEF:nicepct=100,nice,total,/,*' \
    5.63 -		'CDEF:temp=celsius,1000,/' \
    5.64 -		'AREA:userpct#0000FF:user cpu usage' \
    5.65 -		'STACK:nicepct#C0C0FF:nice cpu usage' \
    5.66 -		'STACK:systempct#FF0000:system cpu usage' \
    5.67 -		'STACK:idlepct#00FF00:idle cpu usage' \
    5.68 -		'LINE1:temp#000000:temperature\g' \
    5.69 -		'GPRINT:temp:MAX:max %2.0lfC\j'
    5.70 -}
    5.71 -
    5.72 -updatememgraph() {
    5.73 -	period=$1
    5.74 -	info="$(free | awk '\
    5.75 -{ \
    5.76 -  if (/Mem:/) { \
    5.77 -	if ($2 < 10000) printf "%d KB",$2; \
    5.78 -	else if ($2 < 10000000) printf "%d MB",$2/1024; \
    5.79 -	else printf "%d GB",$2/1024/1024; \
    5.80 -  } \
    5.81 -}')"
    5.82 -	info2="$(free | awk '\
    5.83 -{ \
    5.84 -  if (/Swap:/) { \
    5.85 -	if ($2 < 10000) printf "%d KB",$2; \
    5.86 -	else if ($2 < 10000000) printf "%d MB",$2/1024; \
    5.87 -	else printf "%d GB",$2/1024/1024; \
    5.88 -  } \
    5.89 -}')"
    5.90 -	rrdtool graph "$rrdgraph/memory-$period.png" --start -1$period \
    5.91 -		$rrdgraphargs -l 0 -u 100 \
    5.92 -		-t "memory usage per $period [ $info + $info2 swap ]" \
    5.93 -		DEF:used=$rrdlog/mem.rrd:memused:AVERAGE \
    5.94 -		DEF:free=$rrdlog/mem.rrd:memfree:AVERAGE \
    5.95 -		DEF:shared=$rrdlog/mem.rrd:memshared:AVERAGE \
    5.96 -		DEF:buffer=$rrdlog/mem.rrd:membuffers:AVERAGE \
    5.97 -		DEF:cache=$rrdlog/mem.rrd:memcache:AVERAGE \
    5.98 -		DEF:swused=$rrdlog/mem.rrd:swapused:AVERAGE \
    5.99 -		DEF:swfree=$rrdlog/mem.rrd:swapfree:AVERAGE \
   5.100 -		'CDEF:total=used,free,+' \
   5.101 -		'CDEF:used2=used,buffer,cache,shared,+,+,-' \
   5.102 -		'CDEF:usedpct=100,used2,total,/,*' \
   5.103 -		'CDEF:sharedpct=100,shared,total,/,*' \
   5.104 -		'CDEF:bufferpct=100,buffer,total,/,*' \
   5.105 -		'CDEF:cachepct=100,cache,total,/,*' \
   5.106 -		'CDEF:freepct=100,free,total,/,*' \
   5.107 -		'CDEF:swtotal=swused,swfree,+' \
   5.108 -		'CDEF:swusedpct=100,swused,swtotal,/,*' \
   5.109 -		'AREA:usedpct#0000FF:used memory' \
   5.110 -		'STACK:sharedpct#FF7F00:shared memory' \
   5.111 -		'STACK:bufferpct#FF00FF:buffered memory' \
   5.112 -		'STACK:cachepct#FFFF00:cached memory' \
   5.113 -		'STACK:freepct#00FF00:free memory' \
   5.114 -		'LINE2:swusedpct#FF0000:used swap\j'
   5.115 -}
   5.116 -
   5.117 -updatememdata () {
   5.118 -	[ -e "$rrdlog/mem.rrd" ] ||
   5.119 -		rrdtool create "$rrdlog/mem.rrd" --step=300 \
   5.120 -			DS:memused:ABSOLUTE:600:0:5000000000 \
   5.121 -			DS:memfree:ABSOLUTE:600:0:5000000000 \
   5.122 -			DS:memshared:ABSOLUTE:600:0:5000000000 \
   5.123 -			DS:membuffers:ABSOLUTE:600:0:5000000000 \
   5.124 -			DS:memcache:ABSOLUTE:600:0:5000000000 \
   5.125 -			DS:swapused:ABSOLUTE:600:0:5000000000 \
   5.126 -			DS:swapfree:ABSOLUTE:600:0:5000000000 \
   5.127 -			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   5.128 -			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   5.129 -
   5.130 -	while read tag count unit; do
   5.131 -		case "$tag" in
   5.132 -		MemTotal:)  memtotal=$count;;
   5.133 -		MemFree:)   memfree=$count
   5.134 -			    memused=$(($memtotal - $memfree))
   5.135 -			    memshared=0;;
   5.136 -		MemShared:) memshared=$count;;
   5.137 -		Buffers:)   membuffers=$count;;
   5.138 -		Cached:)    memcache=$count;;
   5.139 -		SwapTotal:) swaptotal=$count;;
   5.140 -		SwapFree:)  swapfree=$count
   5.141 -			    swapused=$(( $swaptotal - $swapfree));;
   5.142 -		esac
   5.143 -	done < /proc/meminfo
   5.144 -
   5.145 -	rrdtool update "$rrdlog/mem.rrd" \
   5.146 -		-t memused:memfree:memshared:membuffers:memcache:swapused:swapfree \
   5.147 -		"N:$memused:$memfree:$memshared:$membuffers:$memcache:$swapused:$swapfree"
   5.148 -}
   5.149 -
   5.150 -getmax() {
   5.151 -	rrdtool fetch $rrdlog/$1.rrd AVERAGE | awk '\
   5.152 -BEGIN {max=0} \
   5.153 -/^[0-9]/ { \
   5.154 -   if ($2 != "nan" && $2 > max) max=$2; \
   5.155 -   if ($3 != "nan" && $3 > max) max=$3; \
   5.156 -} \
   5.157 -END { print max }' | sed 's/,/./'
   5.158 -}
   5.159 -
   5.160 -updatediskgraph() {
   5.161 -	period=$1
   5.162 -	[ "$period" == "day" ] && maxdisk="$(getmax disk)"
   5.163 -	info=""
   5.164 -	[ -r $2 ] &&
   5.165 -	info="[ $(fdisk -l 2> /dev/null | grep "^Disk $2:" | \
   5.166 -		  sed "s|Disk $2: \(.*\), .*|\1|") ]"
   5.167 -	if [ -e /sys/block/${2#/dev/}/device/iodone_cnt ]; then
   5.168 -	err=$(printf "%d\n" $(cat /sys/block/${2#/dev/}/device/ioerr_cnt) )
   5.169 -	done=$(printf "%d\n" $(cat /sys/block/${2#/dev/}/device/iodone_cnt) )
   5.170 -	rate=$(echo | awk "BEGIN { printf \"%.0e\\n\",$err/$done }")
   5.171 -	[ $err -eq 0 ] &&  rate="0"
   5.172 -#		--right-axis-label "I/O state %"
   5.173 -	rrdtool graph "$rrdgraph/disk-$period.png" --start -1$period \
   5.174 -		$rrdgraphargs -t "disk access per $period $info" \
   5.175 -		--logarithmic --lower-limit 1 -v "Sectors/second" --units=si \
   5.176 -		DEF:read=$rrdlog/disk.rrd:readsect:AVERAGE \
   5.177 -		DEF:write=$rrdlog/disk.rrd:writesect:AVERAGE \
   5.178 -		DEF:req=$rrdlog/iodisk.rrd:req:AVERAGE \
   5.179 -		DEF:done=$rrdlog/iodisk.rrd:done:AVERAGE \
   5.180 -		DEF:err=$rrdlog/iodisk.rrd:err:AVERAGE \
   5.181 -		"CDEF:readpct=100,read,$maxdisk,/,*" \
   5.182 -		"CDEF:writepct=100,write,$maxdisk,/,*" \
   5.183 -		"CDEF:errpct=100,err,req,/,*" \
   5.184 -		"CDEF:donepct=100,done,req,/,*" \
   5.185 -		"CDEF:errrate=err,done,/" \
   5.186 -		'AREA:readpct#0000FF:sectors read from disk' \
   5.187 -		"COMMENT:I/O error rate $rate" \
   5.188 -		'STACK:writepct#00FF00:sectors written to disk' \
   5.189 -		'LINE2:donepct#FFFF00:% I/O complete' \
   5.190 -		'LINE2:errpct#FF0000:% I/O error\j'
   5.191 -	else
   5.192 -	rrdtool graph "$rrdgraph/disk-$period.png" --start -1$period \
   5.193 -		$rrdgraphargs -t "disk access per $period $info" \
   5.194 -		--logarithmic --lower-limit 1 -v "Sectors/second" --units=si \
   5.195 -		DEF:read=$rrdlog/disk.rrd:readsect:AVERAGE \
   5.196 -		DEF:write=$rrdlog/disk.rrd:writesect:AVERAGE \
   5.197 -		"CDEF:readpct=100,read,$maxdisk,/,*" \
   5.198 -		"CDEF:writepct=100,write,$maxdisk,/,*" \
   5.199 -		'AREA:readpct#0000FF:sectors read from disk' \
   5.200 -		'STACK:writepct#00FF00:sectors written to disk'
   5.201 -	fi
   5.202 -}
   5.203 -
   5.204 -updatediskdata() {
   5.205 -	dev=$1
   5.206 -	[ -e "$rrdlog/disk.rrd" ] ||
   5.207 -		rrdtool create "$rrdlog/disk.rrd" --step=300 \
   5.208 -			DS:readsect:COUNTER:600:0:5000000000 \
   5.209 -			DS:writesect:COUNTER:600:0:5000000000 \
   5.210 -			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   5.211 -			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   5.212 -	[ -e "$rrdlog/iodisk.rrd" ] ||
   5.213 -		rrdtool create "$rrdlog/iodisk.rrd" --step=300 \
   5.214 -			DS:done:GAUGE:600:0:U  DS:err:GAUGE:600:0:U \
   5.215 -			DS:req:GAUGE:600:0:U \
   5.216 -			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   5.217 -			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   5.218 -
   5.219 -	while read major minor name readreq readsect writereq writesect misc; do
   5.220 -		[ $major = $(( 0x$(stat -c %t $dev) )) ] || continue
   5.221 -		[ $minor = $(( 0x$(stat -c %T $dev) )) ] || continue
   5.222 -		rrdtool update "$rrdlog/disk.rrd" -t readsect:writesect \
   5.223 -			N:$readsect:$writesect
   5.224 -	done < /proc/diskstats
   5.225 -	disk=${dev:0:8}
   5.226 -	dir=/sys/block/${disk#/dev/}/device
   5.227 -	done=$(printf "%d\n" $(cat $dir/iodone_cnt 2> /dev/null) )
   5.228 -	err=$(printf "%d\n" $(cat $dir/ioerr_cnt 2> /dev/null) )
   5.229 -	req=$(printf "%d\n" $(cat $dir/iorequest_cnt 2> /dev/null) )
   5.230 -	rrdtool update "$rrdlog/iodisk.rrd" -t done:err:req N:$done:$err:$req
   5.231 -}
   5.232 -
   5.233 -updateifgraph() {
   5.234 -	interface=$1
   5.235 -	period=$2
   5.236 -	rrdtool graph "$rrdgraph/$interface-$period.png" --start -1$period \
   5.237 -		$rrdgraphargs -t "traffic on $interface graph per $period" \
   5.238 -		--logarithmic -A -v "Bytes/second" --units=si \
   5.239 -		DEF:incoming=$rrdlog/$interface.rrd:incoming:AVERAGE \
   5.240 -		DEF:outgoing=$rrdlog/$interface.rrd:outgoing:AVERAGE \
   5.241 -		'AREA:incoming#00FF00:incoming traffic' \
   5.242 -		'GPRINT:incoming:MAX:max input%8.3lf %sBps' \
   5.243 -		'GPRINT:outgoing:MAX:max output%8.3lf %sBps' \
   5.244 -		'LINE1:outgoing#0000FF:outgoing traffic\j'
   5.245 -}
   5.246 -
   5.247 -netframes() {
   5.248 -ifconfig $1 | grep "$2 packets" | sed -re "s/.*$3:([0-9]+).*/\1/g"
   5.249 -}
   5.250 -
   5.251 -netstats() {
   5.252 -ifconfig $1 | grep bytes | sed -re "s/.*$2 bytes:([0-9]+).*/\1/g"
   5.253 -}
   5.254 -
   5.255 -updateifdata() {
   5.256 -	interface=$1
   5.257 -	[ -e "$rrdlog/$interface.rrd" ] ||
   5.258 -		rrdtool create "$rrdlog/$interface.rrd" --step=300 \
   5.259 -			DS:incoming:COUNTER:600:0:U \
   5.260 -			DS:outgoing:COUNTER:600:0:U \
   5.261 -			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   5.262 -			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   5.263 -	[ -e "$rrdlog/packets-$interface.rrd" ] ||
   5.264 -		rrdtool create "$rrdlog/packets-$interface.rrd" --step=300 \
   5.265 -			DS:in:COUNTER:600:0:U      DS:out:COUNTER:600:0:U \
   5.266 -			DS:inerr:COUNTER:600:0:U   DS:outerr:COUNTER:600:0:U \
   5.267 -			DS:indrop:COUNTER:600:0:U  DS:outdrop:COUNTER:600:0:U \
   5.268 -			DS:inov:COUNTER:600:0:U    DS:outov:COUNTER:600:0:U \
   5.269 -			DS:frame:COUNTER:600:0:U   DS:carrier:COUNTER:600:0:U \
   5.270 -			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   5.271 -			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   5.272 -	rx=$(netstats $interface RX)
   5.273 -	tx=$(netstats $interface TX)
   5.274 -	rrdtool update "$rrdlog/$interface.rrd" -t incoming:outgoing \
   5.275 -		N:${rx:-U}:${tx:-U}
   5.276 -	rx=$(netframes $interface RX packets)
   5.277 -	tx=$(netframes $interface TX packets)
   5.278 -	rxerr=$(netframes $interface RX errors)
   5.279 -	txerr=$(netframes $interface TX errors)
   5.280 -	rxdrop=$(netframes $interface RX dropped)
   5.281 -	txdrop=$(netframes $interface TX dropped)
   5.282 -	rxov=$(netframes $interface RX overruns)
   5.283 -	txov=$(netframes $interface TX overruns)
   5.284 -	frame=$(netframes $interface RX frame)
   5.285 -	carrier=$(netframes $interface TX carrier)
   5.286 -	rrdtool update "$rrdlog/packets-$interface.rrd" \
   5.287 -		-t in:out:inerr:outerr:indrop:outdrop:inov:outov:frame:carrier \
   5.288 -		N:${rx:-U}:${tx:-U}:${rxerr:-U}:${txerr:-U}:${rxdrop:-U}:${txdrop:-U}:${rxov:-U}:${txov:-U}:${frame:-U}:${carrier:-U}
   5.289 -}
   5.290 -
   5.291 -getdisk()
   5.292 -{
   5.293 -	local d
   5.294 -	local i
   5.295 -	d=$(stat -c %04D $1)
   5.296 -	for i in /dev/* ; do 
   5.297 -		[ $(stat -c "%02t%02T" $i) == $d ] || continue
   5.298 -		echo $i
   5.299 -		break
   5.300 -	done
   5.301 -}
   5.302 -
   5.303 -###
   5.304 -### System graphs
   5.305 -###
   5.306 -
   5.307 -updatecpudata
   5.308 -updatecpugraph day
   5.309 -updatecpugraph week
   5.310 -updatecpugraph month
   5.311 -updatecpugraph year
   5.312 -
   5.313 -updatememdata
   5.314 -updatememgraph day
   5.315 -updatememgraph week
   5.316 -updatememgraph month
   5.317 -updatememgraph year
   5.318 -
   5.319 -if [ -e /proc/diskstats ]; then
   5.320 -	disk=$(getdisk $0)
   5.321 -	updatediskdata $disk
   5.322 -	updatediskgraph day ${disk:0:8}
   5.323 -	updatediskgraph week ${disk:0:8}
   5.324 -	updatediskgraph month ${disk:0:8}
   5.325 -	updatediskgraph year ${disk:0:8}
   5.326 -fi
   5.327 -
   5.328 -iface=$(/sbin/route -n | awk '{ if (/^0.0.0.0/) print $8 }')
   5.329 -updateifdata $iface
   5.330 -updateifgraph $iface day
   5.331 -updateifgraph $iface week
   5.332 -updateifgraph $iface month
   5.333 -updateifgraph $iface year
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/chub/web/lib/rrdtool	Thu Mar 15 04:40:30 2012 +0100
     6.3 @@ -0,0 +1,329 @@
     6.4 +#!/bin/sh
     6.5 +#*/5  * * * * /home/slitaz/www/chub/lib/rrdtool >/dev/null
     6.6 +
     6.7 +. /etc/slitaz/chub.conf || exit 1
     6.8 +
     6.9 +# RRD database directory
    6.10 +rrdlog="$VHOST/cache/rrd"
    6.11 +
    6.12 +# Images directory
    6.13 +rrdgraph="$VHOST/images/rdd"
    6.14 +
    6.15 +# Colors
    6.16 +rrdcolors="--color SHADEA#FFFFFF --color SHADEB#FFFFFF --color BACK#FFFFFF" 
    6.17 +rrdgraphargs="-aPNG -i -z --alt-y-grid -w 600 -h 100 -r $rrdcolors"
    6.18 +
    6.19 +[ -d $rrdlog ] || mkdir -p $rrdlog
    6.20 +[ -d $rrdgraph ] || mkdir -p $rrdgraph
    6.21 +
    6.22 +updatecpudata() {
    6.23 +	[ -e "$rrdlog/cpu.rrd" ] || rrdtool create $rrdlog/cpu.rrd --step=300 \
    6.24 +			DS:user:COUNTER:600:0:500000000 \
    6.25 +			DS:nice:COUNTER:600:0:500000000 \
    6.26 +			DS:system:COUNTER:600:0:500000000 \
    6.27 +			DS:idle:COUNTER:600:0:500000000 \
    6.28 +			DS:iowait:COUNTER:600:0:500000000 \
    6.29 +			DS:irq:COUNTER:600:0:500000000 \
    6.30 +			DS:softirq:COUNTER:600:0:500000000 \
    6.31 +			DS:celsius:GAUGE:600:0:50000 \
    6.32 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
    6.33 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
    6.34 +	grep '^cpu' /proc/stat | while read cpu user nice system idle iowait irq softirq misc; do
    6.35 +		celsius=$(find /sys | grep /temp._input | xargs cat | \
    6.36 +			awk '{ if ($0 > max) max=$0 } END { print max/1 }')
    6.37 +		rrdtool update $rrdlog/cpu.rrd \
    6.38 +			-t celsius:nice:user:system:idle:iowait:irq:softirq \
    6.39 +			N:$celsius:$nice:$user:$system:$idle:$iowait:$irq:$softirq
    6.40 +		break
    6.41 +	done
    6.42 +}
    6.43 +
    6.44 +updatecpugraph() {
    6.45 +	period=$1
    6.46 +	info="$(grep '^model name' /proc/cpuinfo | cut -d: -f2 \
    6.47 +		| sed 's/ * / /g' | awk '
    6.48 +{ s=$0 ; n++ }                    
    6.49 +END { if (n > 1) printf " %dx",n; print s }')"
    6.50 +	rrdtool graph "$rrdgraph/cpu-$period.png" --start -1$period \
    6.51 +		$rrdgraphargs -l 0 -u 100 -t "cpu usage per $period [$info ]" \
    6.52 +		DEF:user=$rrdlog/cpu.rrd:user:AVERAGE \
    6.53 +		DEF:system=$rrdlog/cpu.rrd:system:AVERAGE \
    6.54 +		DEF:idle=$rrdlog/cpu.rrd:idle:AVERAGE \
    6.55 +		DEF:nice=$rrdlog/cpu.rrd:nice:AVERAGE \
    6.56 +		DEF:celsius=$rrdlog/cpu.rrd:celsius:AVERAGE \
    6.57 +		'CDEF:total=user,system,idle,nice,+,+,+' \
    6.58 +		'CDEF:userpct=100,user,total,/,*' \
    6.59 +		'CDEF:systempct=100,system,total,/,*' \
    6.60 +		'CDEF:idlepct=100,idle,total,/,*' \
    6.61 +		'CDEF:nicepct=100,nice,total,/,*' \
    6.62 +		'CDEF:temp=celsius,1000,/' \
    6.63 +		'AREA:userpct#0000FF:user cpu usage' \
    6.64 +		'STACK:nicepct#C0C0FF:nice cpu usage' \
    6.65 +		'STACK:systempct#FF0000:system cpu usage' \
    6.66 +		'STACK:idlepct#00FF00:idle cpu usage' \
    6.67 +		'LINE1:temp#000000:temperature\g' \
    6.68 +		'GPRINT:temp:MAX:max %2.0lfC\j'
    6.69 +}
    6.70 +
    6.71 +updatememgraph() {
    6.72 +	period=$1
    6.73 +	info="$(free | awk '\
    6.74 +{ \
    6.75 +  if (/Mem:/) { \
    6.76 +	if ($2 < 10000) printf "%d KB",$2; \
    6.77 +	else if ($2 < 10000000) printf "%d MB",$2/1024; \
    6.78 +	else printf "%d GB",$2/1024/1024; \
    6.79 +  } \
    6.80 +}')"
    6.81 +	info2="$(free | awk '\
    6.82 +{ \
    6.83 +  if (/Swap:/) { \
    6.84 +	if ($2 < 10000) printf "%d KB",$2; \
    6.85 +	else if ($2 < 10000000) printf "%d MB",$2/1024; \
    6.86 +	else printf "%d GB",$2/1024/1024; \
    6.87 +  } \
    6.88 +}')"
    6.89 +	rrdtool graph "$rrdgraph/memory-$period.png" --start -1$period \
    6.90 +		$rrdgraphargs -l 0 -u 100 \
    6.91 +		-t "memory usage per $period [ $info + $info2 swap ]" \
    6.92 +		DEF:used=$rrdlog/mem.rrd:memused:AVERAGE \
    6.93 +		DEF:free=$rrdlog/mem.rrd:memfree:AVERAGE \
    6.94 +		DEF:shared=$rrdlog/mem.rrd:memshared:AVERAGE \
    6.95 +		DEF:buffer=$rrdlog/mem.rrd:membuffers:AVERAGE \
    6.96 +		DEF:cache=$rrdlog/mem.rrd:memcache:AVERAGE \
    6.97 +		DEF:swused=$rrdlog/mem.rrd:swapused:AVERAGE \
    6.98 +		DEF:swfree=$rrdlog/mem.rrd:swapfree:AVERAGE \
    6.99 +		'CDEF:total=used,free,+' \
   6.100 +		'CDEF:used2=used,buffer,cache,shared,+,+,-' \
   6.101 +		'CDEF:usedpct=100,used2,total,/,*' \
   6.102 +		'CDEF:sharedpct=100,shared,total,/,*' \
   6.103 +		'CDEF:bufferpct=100,buffer,total,/,*' \
   6.104 +		'CDEF:cachepct=100,cache,total,/,*' \
   6.105 +		'CDEF:freepct=100,free,total,/,*' \
   6.106 +		'CDEF:swtotal=swused,swfree,+' \
   6.107 +		'CDEF:swusedpct=100,swused,swtotal,/,*' \
   6.108 +		'AREA:usedpct#0000FF:used memory' \
   6.109 +		'STACK:sharedpct#FF7F00:shared memory' \
   6.110 +		'STACK:bufferpct#FF00FF:buffered memory' \
   6.111 +		'STACK:cachepct#FFFF00:cached memory' \
   6.112 +		'STACK:freepct#00FF00:free memory' \
   6.113 +		'LINE2:swusedpct#FF0000:used swap\j'
   6.114 +}
   6.115 +
   6.116 +updatememdata () {
   6.117 +	[ -e "$rrdlog/mem.rrd" ] ||
   6.118 +		rrdtool create "$rrdlog/mem.rrd" --step=300 \
   6.119 +			DS:memused:ABSOLUTE:600:0:5000000000 \
   6.120 +			DS:memfree:ABSOLUTE:600:0:5000000000 \
   6.121 +			DS:memshared:ABSOLUTE:600:0:5000000000 \
   6.122 +			DS:membuffers:ABSOLUTE:600:0:5000000000 \
   6.123 +			DS:memcache:ABSOLUTE:600:0:5000000000 \
   6.124 +			DS:swapused:ABSOLUTE:600:0:5000000000 \
   6.125 +			DS:swapfree:ABSOLUTE:600:0:5000000000 \
   6.126 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   6.127 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   6.128 +
   6.129 +	while read tag count unit; do
   6.130 +		case "$tag" in
   6.131 +		MemTotal:)  memtotal=$count;;
   6.132 +		MemFree:)   memfree=$count
   6.133 +			    memused=$(($memtotal - $memfree))
   6.134 +			    memshared=0;;
   6.135 +		MemShared:) memshared=$count;;
   6.136 +		Buffers:)   membuffers=$count;;
   6.137 +		Cached:)    memcache=$count;;
   6.138 +		SwapTotal:) swaptotal=$count;;
   6.139 +		SwapFree:)  swapfree=$count
   6.140 +			    swapused=$(( $swaptotal - $swapfree));;
   6.141 +		esac
   6.142 +	done < /proc/meminfo
   6.143 +
   6.144 +	rrdtool update "$rrdlog/mem.rrd" \
   6.145 +		-t memused:memfree:memshared:membuffers:memcache:swapused:swapfree \
   6.146 +		"N:$memused:$memfree:$memshared:$membuffers:$memcache:$swapused:$swapfree"
   6.147 +}
   6.148 +
   6.149 +getmax() {
   6.150 +	rrdtool fetch $rrdlog/$1.rrd AVERAGE | awk '\
   6.151 +BEGIN {max=0} \
   6.152 +/^[0-9]/ { \
   6.153 +   if ($2 != "nan" && $2 > max) max=$2; \
   6.154 +   if ($3 != "nan" && $3 > max) max=$3; \
   6.155 +} \
   6.156 +END { print max }' | sed 's/,/./'
   6.157 +}
   6.158 +
   6.159 +updatediskgraph() {
   6.160 +	period=$1
   6.161 +	[ "$period" == "day" ] && maxdisk="$(getmax disk)"
   6.162 +	info=""
   6.163 +	[ -r $2 ] &&
   6.164 +	info="[ $(fdisk -l 2> /dev/null | grep "^Disk $2:" | \
   6.165 +		  sed "s|Disk $2: \(.*\), .*|\1|") ]"
   6.166 +	if [ -e /sys/block/${2#/dev/}/device/iodone_cnt ]; then
   6.167 +	err=$(printf "%d\n" $(cat /sys/block/${2#/dev/}/device/ioerr_cnt) )
   6.168 +	done=$(printf "%d\n" $(cat /sys/block/${2#/dev/}/device/iodone_cnt) )
   6.169 +	rate=$(echo | awk "BEGIN { printf \"%.0e\\n\",$err/$done }")
   6.170 +	[ $err -eq 0 ] &&  rate="0"
   6.171 +#		--right-axis-label "I/O state %"
   6.172 +	rrdtool graph "$rrdgraph/disk-$period.png" --start -1$period \
   6.173 +		$rrdgraphargs -t "disk access per $period $info" \
   6.174 +		--logarithmic --lower-limit 1 -v "Sectors/second" --units=si \
   6.175 +		DEF:read=$rrdlog/disk.rrd:readsect:AVERAGE \
   6.176 +		DEF:write=$rrdlog/disk.rrd:writesect:AVERAGE \
   6.177 +		DEF:req=$rrdlog/iodisk.rrd:req:AVERAGE \
   6.178 +		DEF:done=$rrdlog/iodisk.rrd:done:AVERAGE \
   6.179 +		DEF:err=$rrdlog/iodisk.rrd:err:AVERAGE \
   6.180 +		"CDEF:readpct=100,read,$maxdisk,/,*" \
   6.181 +		"CDEF:writepct=100,write,$maxdisk,/,*" \
   6.182 +		"CDEF:errpct=100,err,req,/,*" \
   6.183 +		"CDEF:donepct=100,done,req,/,*" \
   6.184 +		"CDEF:errrate=err,done,/" \
   6.185 +		'AREA:readpct#0000FF:sectors read from disk' \
   6.186 +		"COMMENT:I/O error rate $rate" \
   6.187 +		'STACK:writepct#00FF00:sectors written to disk' \
   6.188 +		'LINE2:donepct#FFFF00:% I/O complete' \
   6.189 +		'LINE2:errpct#FF0000:% I/O error\j'
   6.190 +	else
   6.191 +	rrdtool graph "$rrdgraph/disk-$period.png" --start -1$period \
   6.192 +		$rrdgraphargs -t "disk access per $period $info" \
   6.193 +		--logarithmic --lower-limit 1 -v "Sectors/second" --units=si \
   6.194 +		DEF:read=$rrdlog/disk.rrd:readsect:AVERAGE \
   6.195 +		DEF:write=$rrdlog/disk.rrd:writesect:AVERAGE \
   6.196 +		"CDEF:readpct=100,read,$maxdisk,/,*" \
   6.197 +		"CDEF:writepct=100,write,$maxdisk,/,*" \
   6.198 +		'AREA:readpct#0000FF:sectors read from disk' \
   6.199 +		'STACK:writepct#00FF00:sectors written to disk'
   6.200 +	fi
   6.201 +}
   6.202 +
   6.203 +updatediskdata() {
   6.204 +	dev=$1
   6.205 +	[ -e "$rrdlog/disk.rrd" ] ||
   6.206 +		rrdtool create "$rrdlog/disk.rrd" --step=300 \
   6.207 +			DS:readsect:COUNTER:600:0:5000000000 \
   6.208 +			DS:writesect:COUNTER:600:0:5000000000 \
   6.209 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   6.210 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   6.211 +	[ -e "$rrdlog/iodisk.rrd" ] ||
   6.212 +		rrdtool create "$rrdlog/iodisk.rrd" --step=300 \
   6.213 +			DS:done:GAUGE:600:0:U  DS:err:GAUGE:600:0:U \
   6.214 +			DS:req:GAUGE:600:0:U \
   6.215 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   6.216 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   6.217 +
   6.218 +	while read major minor name readreq readsect writereq writesect misc; do
   6.219 +		[ $major = $(( 0x$(stat -c %t $dev) )) ] || continue
   6.220 +		[ $minor = $(( 0x$(stat -c %T $dev) )) ] || continue
   6.221 +		rrdtool update "$rrdlog/disk.rrd" -t readsect:writesect \
   6.222 +			N:$readsect:$writesect
   6.223 +	done < /proc/diskstats
   6.224 +	disk=${dev:0:8}
   6.225 +	dir=/sys/block/${disk#/dev/}/device
   6.226 +	done=$(printf "%d\n" $(cat $dir/iodone_cnt 2> /dev/null) )
   6.227 +	err=$(printf "%d\n" $(cat $dir/ioerr_cnt 2> /dev/null) )
   6.228 +	req=$(printf "%d\n" $(cat $dir/iorequest_cnt 2> /dev/null) )
   6.229 +	rrdtool update "$rrdlog/iodisk.rrd" -t done:err:req N:$done:$err:$req
   6.230 +}
   6.231 +
   6.232 +updateifgraph() {
   6.233 +	interface=$1
   6.234 +	period=$2
   6.235 +	rrdtool graph "$rrdgraph/$interface-$period.png" --start -1$period \
   6.236 +		$rrdgraphargs -t "traffic on $interface graph per $period" \
   6.237 +		--logarithmic -A -v "Bytes/second" --units=si \
   6.238 +		DEF:incoming=$rrdlog/$interface.rrd:incoming:AVERAGE \
   6.239 +		DEF:outgoing=$rrdlog/$interface.rrd:outgoing:AVERAGE \
   6.240 +		'AREA:incoming#00FF00:incoming traffic' \
   6.241 +		'GPRINT:incoming:MAX:max input%8.3lf %sBps' \
   6.242 +		'GPRINT:outgoing:MAX:max output%8.3lf %sBps' \
   6.243 +		'LINE1:outgoing#0000FF:outgoing traffic\j'
   6.244 +}
   6.245 +
   6.246 +netframes() {
   6.247 +ifconfig $1 | grep "$2 packets" | sed -re "s/.*$3:([0-9]+).*/\1/g"
   6.248 +}
   6.249 +
   6.250 +netstats() {
   6.251 +ifconfig $1 | grep bytes | sed -re "s/.*$2 bytes:([0-9]+).*/\1/g"
   6.252 +}
   6.253 +
   6.254 +updateifdata() {
   6.255 +	interface=$1
   6.256 +	[ -e "$rrdlog/$interface.rrd" ] ||
   6.257 +		rrdtool create "$rrdlog/$interface.rrd" --step=300 \
   6.258 +			DS:incoming:COUNTER:600:0:U \
   6.259 +			DS:outgoing:COUNTER:600:0:U \
   6.260 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   6.261 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   6.262 +	[ -e "$rrdlog/packets-$interface.rrd" ] ||
   6.263 +		rrdtool create "$rrdlog/packets-$interface.rrd" --step=300 \
   6.264 +			DS:in:COUNTER:600:0:U      DS:out:COUNTER:600:0:U \
   6.265 +			DS:inerr:COUNTER:600:0:U   DS:outerr:COUNTER:600:0:U \
   6.266 +			DS:indrop:COUNTER:600:0:U  DS:outdrop:COUNTER:600:0:U \
   6.267 +			DS:inov:COUNTER:600:0:U    DS:outov:COUNTER:600:0:U \
   6.268 +			DS:frame:COUNTER:600:0:U   DS:carrier:COUNTER:600:0:U \
   6.269 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   6.270 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   6.271 +	rx=$(netstats $interface RX)
   6.272 +	tx=$(netstats $interface TX)
   6.273 +	rrdtool update "$rrdlog/$interface.rrd" -t incoming:outgoing \
   6.274 +		N:${rx:-U}:${tx:-U}
   6.275 +	rx=$(netframes $interface RX packets)
   6.276 +	tx=$(netframes $interface TX packets)
   6.277 +	rxerr=$(netframes $interface RX errors)
   6.278 +	txerr=$(netframes $interface TX errors)
   6.279 +	rxdrop=$(netframes $interface RX dropped)
   6.280 +	txdrop=$(netframes $interface TX dropped)
   6.281 +	rxov=$(netframes $interface RX overruns)
   6.282 +	txov=$(netframes $interface TX overruns)
   6.283 +	frame=$(netframes $interface RX frame)
   6.284 +	carrier=$(netframes $interface TX carrier)
   6.285 +	rrdtool update "$rrdlog/packets-$interface.rrd" \
   6.286 +		-t in:out:inerr:outerr:indrop:outdrop:inov:outov:frame:carrier \
   6.287 +		N:${rx:-U}:${tx:-U}:${rxerr:-U}:${txerr:-U}:${rxdrop:-U}:${txdrop:-U}:${rxov:-U}:${txov:-U}:${frame:-U}:${carrier:-U}
   6.288 +}
   6.289 +
   6.290 +getdisk()
   6.291 +{
   6.292 +	local d
   6.293 +	local i
   6.294 +	d=$(stat -c %04D $1)
   6.295 +	for i in /dev/* ; do 
   6.296 +		[ $(stat -c "%02t%02T" $i) == $d ] || continue
   6.297 +		echo $i
   6.298 +		break
   6.299 +	done
   6.300 +}
   6.301 +
   6.302 +###
   6.303 +### System graphs
   6.304 +###
   6.305 +
   6.306 +updatecpudata
   6.307 +updatecpugraph day
   6.308 +updatecpugraph week
   6.309 +updatecpugraph month
   6.310 +updatecpugraph year
   6.311 +
   6.312 +updatememdata
   6.313 +updatememgraph day
   6.314 +updatememgraph week
   6.315 +updatememgraph month
   6.316 +updatememgraph year
   6.317 +
   6.318 +if [ -e /proc/diskstats ]; then
   6.319 +	disk=$(getdisk $0)
   6.320 +	updatediskdata $disk
   6.321 +	updatediskgraph day ${disk:0:8}
   6.322 +	updatediskgraph week ${disk:0:8}
   6.323 +	updatediskgraph month ${disk:0:8}
   6.324 +	updatediskgraph year ${disk:0:8}
   6.325 +fi
   6.326 +
   6.327 +iface=$(/sbin/route -n | awk '{ if (/^0.0.0.0/) print $8 }')
   6.328 +updateifdata $iface
   6.329 +updateifgraph $iface day
   6.330 +updateifgraph $iface week
   6.331 +updateifgraph $iface month
   6.332 +updateifgraph $iface year