wok rev 4583

mirror-tools: add makegraphs
author Pascal Bellard <pascal.bellard@slitaz.org>
date Thu Dec 10 15:51:45 2009 +0100 (2009-12-10)
parents 354fd704a2fb
children 346f16db0be2
files mirror-tools/receipt mirror-tools/stuff/usr/share/rrd/makegraphs
line diff
     1.1 --- a/mirror-tools/receipt	Wed Dec 09 19:58:54 2009 +0000
     1.2 +++ b/mirror-tools/receipt	Thu Dec 10 15:51:45 2009 +0100
     1.3 @@ -6,6 +6,7 @@
     1.4  SHORT_DESC="Mirrors toolset."
     1.5  MAINTAINER="pascal.bellard@slitaz.org"
     1.6  WEB_SITE="http://www.slitaz.org/"
     1.7 +DEPENDS="rrdtools"
     1.8  
     1.9  # Rules to gen a SliTaz package suitable for Tazpkg.
    1.10  genpkg_rules()
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/mirror-tools/stuff/usr/share/rrd/makegraphs	Thu Dec 10 15:51:45 2009 +0100
     2.3 @@ -0,0 +1,299 @@
     2.4 +#!/bin/sh
     2.5 +#*/5  * * * * /usr/share/rrd/makegraphs >/dev/null
     2.6 +
     2.7 +# RRD database directory
     2.8 +rrdlog="/var/spool/rrd"
     2.9 +
    2.10 +# Images directory
    2.11 +rrdgraph="/var/spool/rrd"
    2.12 +
    2.13 +# Colors
    2.14 +#rrdcolors="--color SHADEA#EAE9EE --color SHADEB#EAE9EE --color BACK#EAE9EE" 
    2.15 +rrdcolors="--color SHADEA#FFFFFF --color SHADEB#FFFFFF --color BACK#FFFFFF" 
    2.16 +rrdgraphargs="-aPNG -i -z --alt-y-grid -w 600 -h 100 -r $rrdcolors"
    2.17 +
    2.18 +[ -d $rrdlog ] || mkdir -p $rrdlog
    2.19 +[ -d $rrdgraph ] || mkdir -p $rrdgraph
    2.20 +
    2.21 +updatecpudata() {
    2.22 +	[ -e "$rrdlog/cpu.rrd" ] || rrdtool create $rrdlog/cpu.rrd --step=300 \
    2.23 +			DS:user:COUNTER:600:0:500000000 \
    2.24 +			DS:system:COUNTER:600:0:500000000 \
    2.25 +			DS:idle:COUNTER:600:0:500000000 \
    2.26 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
    2.27 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
    2.28 +	grep '^cpu' /proc/stat | while read cpu user nice system idle misc; do
    2.29 +		rrdtool update $rrdlog/cpu.rrd -t user:system:idle \
    2.30 +			N:$(( $user + $nice )):$system:$idle
    2.31 +		break
    2.32 +	done
    2.33 +
    2.34 +	[ -e "$rrdlog/cpu2.rrd" ] &&
    2.35 +	grep '^cpu' /proc/stat | while read cpu user nice system idle misc; do
    2.36 +		rrdtool update $rrdlog/cpu2.rrd -t nice:user:system:idle \
    2.37 +			N:$nice:$user:$system:$idle
    2.38 +		break
    2.39 +	done
    2.40 +}
    2.41 +
    2.42 +updatecpugraph() {
    2.43 +	period=$1
    2.44 +	info="$(grep '^model name' /proc/cpuinfo | cut -d: -f2 | head -1)"
    2.45 +	rrdtool graph "$rrdgraph/cpu-$period.png" --start -1$period \
    2.46 +		$rrdgraphargs -l 0 -u 100 -t "cpu usage per $period [$info ]" \
    2.47 +		DEF:user=$rrdlog/cpu.rrd:user:AVERAGE \
    2.48 +		DEF:system=$rrdlog/cpu.rrd:system:AVERAGE \
    2.49 +		DEF:idle=$rrdlog/cpu.rrd:idle:AVERAGE \
    2.50 +		'CDEF:total=user,system,idle,+,+' \
    2.51 +		'CDEF:userpct=100,user,total,/,*' \
    2.52 +		'CDEF:systempct=100,system,total,/,*' \
    2.53 +		'CDEF:idlepct=100,idle,total,/,*' \
    2.54 +		'AREA:userpct#0000FF:user cpu usage' \
    2.55 +		'STACK:systempct#FF0000:system cpu usage' \
    2.56 +		'STACK:idlepct#00FF00:idle cpu usage\j'
    2.57 +}
    2.58 +
    2.59 +
    2.60 +updatememgraph() {
    2.61 +	period=$1
    2.62 +	info="$(free | awk '\
    2.63 +{ \
    2.64 +  if (/Mem:/) { \
    2.65 +	if ($2 < 10000) printf "%d KB",$2; \
    2.66 +	else if ($2 < 10000000) printf "%d MB",$2/1024; \
    2.67 +	else printf "%d GB",$2/1024/1024; \
    2.68 +  } \
    2.69 +}')"
    2.70 +	info2="$(free | awk '\
    2.71 +{ \
    2.72 +  if (/Swap:/) { \
    2.73 +	if ($2 < 10000) printf "%d KB",$2; \
    2.74 +	else if ($2 < 10000000) printf "%d MB",$2/1024; \
    2.75 +	else printf "%d GB",$2/1024/1024; \
    2.76 +  } \
    2.77 +}')"
    2.78 +	rrdtool graph "$rrdgraph/memory-$period.png" --start -1$period \
    2.79 +		$rrdgraphargs -l 0 -u 100 \
    2.80 +		-t "memory usage per $period [ $info + $info2 swap ]" \
    2.81 +		DEF:used=$rrdlog/mem.rrd:memused:AVERAGE \
    2.82 +		DEF:free=$rrdlog/mem.rrd:memfree:AVERAGE \
    2.83 +		DEF:shared=$rrdlog/mem.rrd:memshared:AVERAGE \
    2.84 +		DEF:buffer=$rrdlog/mem.rrd:membuffers:AVERAGE \
    2.85 +		DEF:cache=$rrdlog/mem.rrd:memcache:AVERAGE \
    2.86 +		DEF:swused=$rrdlog/mem.rrd:swapused:AVERAGE \
    2.87 +		DEF:swfree=$rrdlog/mem.rrd:swapfree:AVERAGE \
    2.88 +		'CDEF:total=used,free,+' \
    2.89 +		'CDEF:used2=used,buffer,cache,shared,+,+,-' \
    2.90 +		'CDEF:usedpct=100,used2,total,/,*' \
    2.91 +		'CDEF:sharedpct=100,shared,total,/,*' \
    2.92 +		'CDEF:bufferpct=100,buffer,total,/,*' \
    2.93 +		'CDEF:cachepct=100,cache,total,/,*' \
    2.94 +		'CDEF:freepct=100,free,total,/,*' \
    2.95 +		'CDEF:swtotal=swused,swfree,+' \
    2.96 +		'CDEF:swusedpct=100,swused,swtotal,/,*' \
    2.97 +		'AREA:usedpct#0000FF:used memory' \
    2.98 +		'STACK:sharedpct#FF7F00:shared memory' \
    2.99 +		'STACK:bufferpct#FF00FF:buffered memory' \
   2.100 +		'STACK:cachepct#FFFF00:cached memory' \
   2.101 +		'STACK:freepct#00FF00:free memory' \
   2.102 +		'LINE2:swusedpct#FF0000:used swap\j'
   2.103 +}
   2.104 +
   2.105 +updatememdata () {
   2.106 +	[ -e "$rrdlog/mem.rrd" ] ||
   2.107 +		rrdtool create "$rrdlog/mem.rrd" --step=300 \
   2.108 +			DS:memused:ABSOLUTE:600:0:5000000000 \
   2.109 +			DS:memfree:ABSOLUTE:600:0:5000000000 \
   2.110 +			DS:memshared:ABSOLUTE:600:0:5000000000 \
   2.111 +			DS:membuffers:ABSOLUTE:600:0:5000000000 \
   2.112 +			DS:memcache:ABSOLUTE:600:0:5000000000 \
   2.113 +			DS:swapused:ABSOLUTE:600:0:5000000000 \
   2.114 +			DS:swapfree:ABSOLUTE:600:0:5000000000 \
   2.115 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   2.116 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   2.117 +
   2.118 +	while read tag count unit; do
   2.119 +		case "$tag" in
   2.120 +		MemTotal:)  memtotal=$(($count * 1024));;
   2.121 +		MemFree:)   memfree=$(($count * 1024))
   2.122 +			    memused=$(($memtotal - $memfree))
   2.123 +			    memshared=0;;
   2.124 +		MemShared:) memshared=$(($count * 1024));;
   2.125 +		Buffers:)   membuffers=$(($count * 1024));;
   2.126 +		Cached:)    memcache=$(($count * 1024));;
   2.127 +		SwapTotal:) swaptotal=$(($count * 1024));;
   2.128 +		SwapFree:)  swapfree=$(($count * 1024))
   2.129 +			    swapused=$(( $swaptotal - $swapfree));;
   2.130 +		esac
   2.131 +	done < /proc/meminfo
   2.132 +
   2.133 +	rrdtool update "$rrdlog/mem.rrd" \
   2.134 +		-t memused:memfree:memshared:membuffers:memcache:swapused:swapfree \
   2.135 +		"N:$memused:$memfree:$memshared:$membuffers:$memcache:$swapused:$swapfree"
   2.136 +}
   2.137 +
   2.138 +getmax() {
   2.139 +	rrdtool fetch $rrdlog/$1.rrd AVERAGE | awk '\
   2.140 +BEGIN {max=0} \
   2.141 +/^[0-9]/ { \
   2.142 +   if ($2 != "nan" && $2 > max) max=$2; \
   2.143 +   if ($3 != "nan" && $3 > max) max=$3; \
   2.144 +} \
   2.145 +END { print max }' | sed 's/,/./'
   2.146 +}
   2.147 +
   2.148 +updatediskgraph() {
   2.149 +	period=$1
   2.150 +	[ "$period" == "day" ] && maxdisk="$(getmax disk)"
   2.151 +	info=""
   2.152 +	[ -r $2 ] &&
   2.153 +	info="[ $(fdisk -l | grep "^Disk $2:" | \
   2.154 +		  sed "s|Disk $2: \(.*\), .*|\1|") ]"
   2.155 +	rrdtool graph "$rrdgraph/disk-$period.png" --start -1$period \
   2.156 +		$rrdgraphargs -t "disk access per $period $info" \
   2.157 +		--logarithmic --lower-limit 1 -v "Sectors/second" --units=si \
   2.158 +		DEF:read=$rrdlog/disk.rrd:readsect:AVERAGE \
   2.159 +		DEF:write=$rrdlog/disk.rrd:writesect:AVERAGE \
   2.160 +		DEF:req=$rrdlog/iodisk.rrd:req:AVERAGE \
   2.161 +		DEF:done=$rrdlog/iodisk.rrd:done:AVERAGE \
   2.162 +		DEF:err=$rrdlog/iodisk.rrd:err:AVERAGE \
   2.163 +		"CDEF:readpct=100,read,$maxdisk,/,*" \
   2.164 +		"CDEF:writepct=100,write,$maxdisk,/,*" \
   2.165 +		"CDEF:errpct=100,err,req,/,*" \
   2.166 +		"CDEF:donepct=100,done,req,/,*" \
   2.167 +		'AREA:readpct#0000FF:sectors read from disk' \
   2.168 +		'STACK:writepct#00FF00:sectors written to disk' \
   2.169 +		'LINE2:donepct#FFFF00:I/O complete' \
   2.170 +		'LINE2:errpct#FF0000:I/O error\j'
   2.171 +}
   2.172 +
   2.173 +updatediskdata() {
   2.174 +	dev=$1
   2.175 +	[ -e "$rrdlog/disk.rrd" ] ||
   2.176 +		rrdtool create "$rrdlog/disk.rrd" --step=300 \
   2.177 +			DS:readsect:COUNTER:600:0:5000000000 \
   2.178 +			DS:writesect:COUNTER:600:0:5000000000 \
   2.179 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   2.180 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   2.181 +	[ -e "$rrdlog/iodisk.rrd" ] ||
   2.182 +		rrdtool create "$rrdlog/iodisk.rrd" --step=300 \
   2.183 +			DS:done:GAUGE:600:0:U  DS:err:GAUGE:600:0:U \
   2.184 +			DS:req:GAUGE:600:0:U \
   2.185 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   2.186 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   2.187 +
   2.188 +	while read major minor name readreq readsect writereq writesect misc; do
   2.189 +		[ $major = $(( 0x$(stat -c %t $dev) )) ] || continue
   2.190 +		[ $minor = $(( 0x$(stat -c %T $dev) )) ] || continue
   2.191 +		rrdtool update "$rrdlog/disk.rrd" -t readsect:writesect \
   2.192 +			N:$readsect:$writesect
   2.193 +	done < /proc/diskstats
   2.194 +	dir=/sys/block/${dev#/dev/}/device
   2.195 +	done=$(printf "%d\n" $(cat $dir/iodone_cnt 2> /dev/null) )
   2.196 +	err=$(printf "%d\n" $(cat $dir/ioerr_cnt 2> /dev/null) )
   2.197 +	req=$(printf "%d\n" $(cat $dir/iorequest_cnt 2> /dev/null) )
   2.198 +	rrdtool update "$rrdlog/iodisk.rrd" -t done:err:req N:$done:$err:$req
   2.199 +}
   2.200 +
   2.201 +updateifgraph() {
   2.202 +	interface=$1
   2.203 +	period=$2
   2.204 +	[ "$period" == "day" ] && maxif="$(getmax $interface)"
   2.205 +	rrdtool graph "$rrdgraph/$interface-$period.png" --start -1$period \
   2.206 +		$rrdgraphargs -t "traffic on $interface graph per $period" \
   2.207 +		--logarithmic -A -v "Bytes/second" --units=si \
   2.208 +		DEF:incoming=$rrdlog/$interface.rrd:incoming:AVERAGE \
   2.209 +		DEF:outgoing=$rrdlog/$interface.rrd:outgoing:AVERAGE \
   2.210 +		"CDEF:inpct=100,incoming,$maxif,/,*" \
   2.211 +		"CDEF:outpct=100,outgoing,$maxif,/,*" \
   2.212 +		'AREA:inpct#00FF00:incoming traffic' \
   2.213 +		'LINE1:outpct#0000FF:outgoing traffic\j'
   2.214 +}
   2.215 +
   2.216 +netframes() {
   2.217 +ifconfig $1 | grep "$2 packets" | sed -re "s/.*$3:([0-9]+).*/\1/g"
   2.218 +}
   2.219 +
   2.220 +netstats() {
   2.221 +ifconfig $1 | grep bytes | sed -re "s/.*$2 bytes:([0-9]+).*/\1/g"
   2.222 +}
   2.223 +
   2.224 +updateifdata() {
   2.225 +	interface=$1
   2.226 +	[ -e "$rrdlog/$interface.rrd" ] ||
   2.227 +		rrdtool create "$rrdlog/$interface.rrd" --step=300 \
   2.228 +			DS:incoming:COUNTER:600:0:U \
   2.229 +			DS:outgoing:COUNTER:600:0:U \
   2.230 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   2.231 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   2.232 +	[ -e "$rrdlog/packets-$interface.rrd" ] ||
   2.233 +		rrdtool create "$rrdlog/packets-$interface.rrd" --step=300 \
   2.234 +			DS:in:COUNTER:600:0:U      DS:out:COUNTER:600:0:U \
   2.235 +			DS:inerr:COUNTER:600:0:U   DS:outerr:COUNTER:600:0:U \
   2.236 +			DS:indrop:COUNTER:600:0:U  DS:outdrop:COUNTER:600:0:U \
   2.237 +			DS:inov:COUNTER:600:0:U    DS:outov:COUNTER:600:0:U \
   2.238 +			DS:frame:COUNTER:600:0:U   DS:carrier:COUNTER:600:0:U \
   2.239 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   2.240 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   2.241 +	rx=$(netstats $interface RX)
   2.242 +	tx=$(netstats $interface TX)
   2.243 +	rrdtool update "$rrdlog/$interface.rrd" -t incoming:outgoing \
   2.244 +		N:${rx:-U}:${tx:-U}
   2.245 +	rx=$(netframes $interface RX packets)
   2.246 +	tx=$(netframes $interface TX packets)
   2.247 +	rxerr=$(netframes $interface RX errors)
   2.248 +	txerr=$(netframes $interface TX errors)
   2.249 +	rxdrop=$(netframes $interface RX dropped)
   2.250 +	txdrop=$(netframes $interface TX dropped)
   2.251 +	rxov=$(netframes $interface RX overruns)
   2.252 +	txov=$(netframes $interface TX overruns)
   2.253 +	frame=$(netframes $interface RX frame)
   2.254 +	carrier=$(netframes $interface TX carrier)
   2.255 +	rrdtool update "$rrdlog/packets-$interface.rrd" \
   2.256 +		-t in:out:inerr:outerr:indrop:outdrop:inov:outov:frame:carrier \
   2.257 +		N:${rx:-U}:${tx:-U}:${rxerr:-U}:${txerr:-U}:${rxdrop:-U}:${txdrop:-U}:${rxov:-U}:${txov:-U}:${frame:-U}:${carrier:-U}
   2.258 +}
   2.259 +
   2.260 +getdisk()
   2.261 +{
   2.262 +	local d
   2.263 +	local i
   2.264 +	d=$(stat -c %D $1)
   2.265 +	for i in /dev/* ; do 
   2.266 +		[ $(stat -c "%02t%02T" $i) == $d ] || continue
   2.267 +		echo $i
   2.268 +		break
   2.269 +	done
   2.270 +}
   2.271 +
   2.272 +###
   2.273 +### System graphs
   2.274 +###
   2.275 +
   2.276 +updatecpudata
   2.277 +updatecpugraph day
   2.278 +updatecpugraph week
   2.279 +updatecpugraph month
   2.280 +updatecpugraph year
   2.281 +
   2.282 +updatememdata
   2.283 +updatememgraph day
   2.284 +updatememgraph week
   2.285 +updatememgraph month
   2.286 +updatememgraph year
   2.287 +
   2.288 +if [ -e /proc/diskstats ]; then
   2.289 +	disk=$(getdisk $0)
   2.290 +	updatediskdata $disk
   2.291 +	updatediskgraph day ${disk:0:8}
   2.292 +	updatediskgraph week ${disk:0:8}
   2.293 +	updatediskgraph month ${disk:0:8}
   2.294 +	updatediskgraph year ${disk:0:8}
   2.295 +fi
   2.296 +
   2.297 +iface=$(/sbin/route -n | awk '{ if (/^0.0.0.0/) print $8 }')
   2.298 +updateifdata $iface
   2.299 +updateifgraph $iface day
   2.300 +updateifgraph $iface week
   2.301 +updateifgraph $iface month
   2.302 +updateifgraph $iface year