slitaz-forge rev 368

Add pangolin/makegraphs
author Pascal Bellard <pascal.bellard@slitaz.org>
date Mon Jun 03 11:53:42 2013 +0200 (2013-06-03)
parents 3d98f09d077b
children 3fd9f645cdb0
files pangolin/makegraphs
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/pangolin/makegraphs	Mon Jun 03 11:53:42 2013 +0200
     1.3 @@ -0,0 +1,355 @@
     1.4 +#!/bin/sh
     1.5 +#*/5  * * * * /home/bellard/bin/makegraphs >/dev/null
     1.6 +
     1.7 +# RRD database directory
     1.8 +rrdlog="/var/lib/pangolin/rrd"
     1.9 +
    1.10 +# Images directory
    1.11 +rrdgraph="/home/slitaz/www/pangolin/pics/rrd"
    1.12 +
    1.13 +# Colors
    1.14 +rrdcolors="--color SHADEA#FFFFFF --color SHADEB#FFFFFF --color BACK#FFFFFF" 
    1.15 +rrdgraphargs="-aPNG -i -z --alt-y-grid -w 600 -h 100 -r $rrdcolors"
    1.16 +
    1.17 +[ -d $rrdlog ] || mkdir -p $rrdlog
    1.18 +[ -d $rrdgraph ] || mkdir -p $rrdgraph
    1.19 +
    1.20 +updatecpudata() {
    1.21 +	[ -e "$rrdlog/cpu.rrd" ] || rrdtool create $rrdlog/cpu.rrd --step=300 \
    1.22 +			DS:user:COUNTER:600:0:500000000 \
    1.23 +			DS:nice:COUNTER:600:0:500000000 \
    1.24 +			DS:system:COUNTER:600:0:500000000 \
    1.25 +			DS:idle:COUNTER:600:0:500000000 \
    1.26 +			DS:iowait:COUNTER:600:0:500000000 \
    1.27 +			DS:irq:COUNTER:600:0:500000000 \
    1.28 +			DS:softirq:COUNTER:600:0:500000000 \
    1.29 +			DS:celsius:GAUGE:600:0:100000 \
    1.30 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
    1.31 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
    1.32 +	grep '^cpu' /proc/stat | while read cpu user nice system idle iowait irq softirq misc; do
    1.33 +		celsius=$(find /sys | grep /temp._input | xargs cat | \
    1.34 +			awk '{ if ($0 > max) max=$0 } END { print max/1 }')
    1.35 +		rrdtool update $rrdlog/cpu.rrd \
    1.36 +			-t celsius:nice:user:system:idle:iowait:irq:softirq \
    1.37 +			N:$celsius:$nice:$user:$system:$idle:$iowait:$irq:$softirq
    1.38 +		break
    1.39 +	done
    1.40 +}
    1.41 +
    1.42 +updatecpugraph() {
    1.43 +	period=$1
    1.44 +	info="$(grep '^model name' /proc/cpuinfo | cut -d: -f2 \
    1.45 +		| sed 's/ * / /g' | awk '
    1.46 +{ s=$0 ; n++ }                    
    1.47 +END { if (n > 1) printf " %dx",n; print s }')"
    1.48 +	rrdtool graph "$rrdgraph/cpu-$period.png" --start -1$period \
    1.49 +		$rrdgraphargs -l 0 -u 100 -t "cpu usage per $period [$info ]" \
    1.50 +		DEF:user=$rrdlog/cpu.rrd:user:AVERAGE \
    1.51 +		DEF:system=$rrdlog/cpu.rrd:system:AVERAGE \
    1.52 +		DEF:idle=$rrdlog/cpu.rrd:idle:AVERAGE \
    1.53 +		DEF:nice=$rrdlog/cpu.rrd:nice:AVERAGE \
    1.54 +		DEF:iowait=$rrdlog/cpu.rrd:iowait:AVERAGE \
    1.55 +		DEF:celsius=$rrdlog/cpu.rrd:celsius:AVERAGE \
    1.56 +		'CDEF:total=user,system,idle,nice,iowait,+,+,+,+' \
    1.57 +		'CDEF:userpct=100,user,total,/,*' \
    1.58 +		'CDEF:systempct=100,system,total,/,*' \
    1.59 +		'CDEF:idlepct=100,idle,total,/,*' \
    1.60 +		'CDEF:nicepct=100,nice,total,/,*' \
    1.61 +		'CDEF:iopct=100,iowait,total,/,*' \
    1.62 +		'CDEF:temp=celsius,1000,/' \
    1.63 +		'AREA:userpct#0000FF:user cpu' \
    1.64 +		'STACK:nicepct#C0C0FF:nice cpu' \
    1.65 +		'STACK:systempct#FF0000:system cpu' \
    1.66 +		'STACK:iopct#FFFF00:I/O wait' \
    1.67 +		'STACK:idlepct#00FF00:idle cpu' \
    1.68 +		'LINE1:temp#000000:temperature\g' \
    1.69 +		'GPRINT:temp:MAX:max %2.0lfC\j'
    1.70 +}
    1.71 +
    1.72 +updatememgraph() {
    1.73 +	period=$1
    1.74 +	info="$(free | awk '\
    1.75 +{ \
    1.76 +  if (/Mem:/) { \
    1.77 +	if ($2 < 10000) printf "%d KB",$2; \
    1.78 +	else if ($2 < 10000000) printf "%d MB",$2/1024; \
    1.79 +	else printf "%d GB",$2/1024/1024; \
    1.80 +  } \
    1.81 +}')"
    1.82 +	info2="$(free | awk '\
    1.83 +{ \
    1.84 +  if (/Swap:/) { \
    1.85 +	if ($2 < 10000) printf "%d KB",$2; \
    1.86 +	else if ($2 < 10000000) printf "%d MB",$2/1024; \
    1.87 +	else printf "%d GB",$2/1024/1024; \
    1.88 +  } \
    1.89 +}')"
    1.90 +	rrdtool graph "$rrdgraph/memory-$period.png" --start -1$period \
    1.91 +		$rrdgraphargs -l 0 -u 100 \
    1.92 +		-t "memory usage per $period [ $info + $info2 swap ]" \
    1.93 +		DEF:used=$rrdlog/mem.rrd:memused:AVERAGE \
    1.94 +		DEF:free=$rrdlog/mem.rrd:memfree:AVERAGE \
    1.95 +		DEF:shared=$rrdlog/mem.rrd:memshared:AVERAGE \
    1.96 +		DEF:buffer=$rrdlog/mem.rrd:membuffers:AVERAGE \
    1.97 +		DEF:cache=$rrdlog/mem.rrd:memcache:AVERAGE \
    1.98 +		DEF:swused=$rrdlog/mem.rrd:swapused:AVERAGE \
    1.99 +		DEF:swfree=$rrdlog/mem.rrd:swapfree:AVERAGE \
   1.100 +		'CDEF:total=used,free,+' \
   1.101 +		'CDEF:used2=used,buffer,cache,shared,+,+,-' \
   1.102 +		'CDEF:usedpct=100,used2,total,/,*' \
   1.103 +		'CDEF:sharedpct=100,shared,total,/,*' \
   1.104 +		'CDEF:bufferpct=100,buffer,total,/,*' \
   1.105 +		'CDEF:cachepct=100,cache,total,/,*' \
   1.106 +		'CDEF:freepct=100,free,total,/,*' \
   1.107 +		'CDEF:swtotal=swused,swfree,+' \
   1.108 +		'CDEF:swusedpct=100,swused,swtotal,/,*' \
   1.109 +		'AREA:usedpct#0000FF:used memory' \
   1.110 +		'STACK:sharedpct#FF7F00:shared memory' \
   1.111 +		'STACK:bufferpct#FF00FF:buffered memory' \
   1.112 +		'STACK:cachepct#FFFF00:cached memory' \
   1.113 +		'STACK:freepct#00FF00:free memory' \
   1.114 +		'LINE2:swusedpct#FF0000:used swap\g' \
   1.115 +		'GPRINT:swusedpct:MAX:%1.0lf%%\j'
   1.116 +}
   1.117 +
   1.118 +updatememdata () {
   1.119 +	[ -e "$rrdlog/mem.rrd" ] ||
   1.120 +		rrdtool create "$rrdlog/mem.rrd" --step=300 \
   1.121 +			DS:memused:ABSOLUTE:600:0:5000000000 \
   1.122 +			DS:memfree:ABSOLUTE:600:0:5000000000 \
   1.123 +			DS:memshared:ABSOLUTE:600:0:5000000000 \
   1.124 +			DS:membuffers:ABSOLUTE:600:0:5000000000 \
   1.125 +			DS:memcache:ABSOLUTE:600:0:5000000000 \
   1.126 +			DS:swapused:ABSOLUTE:600:0:5000000000 \
   1.127 +			DS:swapfree:ABSOLUTE:600:0:5000000000 \
   1.128 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   1.129 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   1.130 +
   1.131 +	while read tag count unit; do
   1.132 +		case "$tag" in
   1.133 +		MemTotal:)  memtotal=$count;;
   1.134 +		MemFree:)   memfree=$count
   1.135 +			    memused=$(($memtotal - $memfree))
   1.136 +			    memshared=0;;
   1.137 +		MemShared:) memshared=$count;;
   1.138 +		Buffers:)   membuffers=$count;;
   1.139 +		Cached:)    memcache=$count;;
   1.140 +		SwapTotal:) swaptotal=$count;;
   1.141 +		SwapFree:)  swapfree=$count
   1.142 +			    swapused=$(( $swaptotal - $swapfree));;
   1.143 +		esac
   1.144 +	done < /proc/meminfo
   1.145 +
   1.146 +	rrdtool update "$rrdlog/mem.rrd" \
   1.147 +		-t memused:memfree:memshared:membuffers:memcache:swapused:swapfree \
   1.148 +		"N:$memused:$memfree:$memshared:$membuffers:$memcache:$swapused:$swapfree"
   1.149 +}
   1.150 +
   1.151 +getmax() {
   1.152 +	rrdtool fetch $rrdlog/$1.rrd AVERAGE | awk '\
   1.153 +BEGIN {max=0} \
   1.154 +/^[0-9]/ { \
   1.155 +   if ($2 != "nan" && $2 > max) max=$2; \
   1.156 +   if ($3 != "nan" && $3 > max) max=$3; \
   1.157 +} \
   1.158 +END { print max }' | sed 's/,/./'
   1.159 +}
   1.160 +
   1.161 +updatediskgraph() {
   1.162 +	period=$1
   1.163 +	[ "$period" == "day" ] && maxdisk="$(getmax disk)"
   1.164 +	info=""
   1.165 +	[ -r $2 ] &&
   1.166 +	info="[ $(fdisk -l 2> /dev/null | grep "^Disk $2:" | \
   1.167 +		  sed "s|Disk $2: \(.*\), .*|\1|") ]"
   1.168 +		#--logarithmic --lower-limit 1 -v "Sectors/second" --units=si
   1.169 +	rrdtool graph "$rrdgraph/disk-$period.png" --start -1$period \
   1.170 +		$rrdgraphargs -t "disk access per $period $info" \
   1.171 +		-v "Sectors/second" --units=si \
   1.172 +		DEF:read=$rrdlog/disk.rrd:readsect:AVERAGE \
   1.173 +		DEF:write=$rrdlog/disk.rrd:writesect:AVERAGE \
   1.174 +		DEF:blk=$rrdlog/usagedisk.rrd:bhome:AVERAGE \
   1.175 +		DEF:ino=$rrdlog/usagedisk.rrd:ihome:AVERAGE \
   1.176 +		"CDEF:readpct=100,read,$maxdisk,/,*" \
   1.177 +		"CDEF:writepct=100,write,$maxdisk,/,*" \
   1.178 +		'AREA:readpct#0000FF:sectors read from disk' \
   1.179 +		'STACK:writepct#00FF00:sectors written to disk' \
   1.180 +		'LINE1:ino#FF00FF:% inodes used in /home' \
   1.181 +		'GPRINT:ino:MAX:%1.0lf%%' \
   1.182 +		'LINE1:blk#FF0000:% blocks used in /home' \
   1.183 +		'GPRINT:blk:MAX:%1.0lf%%\j'
   1.184 +}
   1.185 +
   1.186 +updatediskdata() {
   1.187 +	dev=$1
   1.188 +	[ -e "$rrdlog/disk.rrd" ] ||
   1.189 +		rrdtool create "$rrdlog/disk.rrd" --step=300 \
   1.190 +			DS:readsect:COUNTER:600:0:5000000000 \
   1.191 +			DS:writesect:COUNTER:600:0:5000000000 \
   1.192 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   1.193 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   1.194 +	[ -e "$rrdlog/iodisk.rrd" ] ||
   1.195 +		rrdtool create "$rrdlog/iodisk.rrd" --step=300 \
   1.196 +			DS:done:GAUGE:600:0:U  DS:err:GAUGE:600:0:U \
   1.197 +			DS:req:GAUGE:600:0:U \
   1.198 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   1.199 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   1.200 +	[ -e "$rrdlog/usagedisk.rrd" ] ||
   1.201 +		rrdtool create "$rrdlog/usagedisk.rrd" --step=300 \
   1.202 +			DS:broot:GAUGE:600:0:U  DS:iroot:GAUGE:600:0:U \
   1.203 +			DS:bhome:GAUGE:600:0:U  DS:ihome:GAUGE:600:0:U \
   1.204 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   1.205 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   1.206 +
   1.207 +	while read major minor name readreq readsect writereq writesect misc; do
   1.208 +		[ $major = $(( 0x$(stat -c %t $dev) )) ] || continue
   1.209 +		[ $minor = $(( 0x$(stat -c %T $dev) )) ] || continue
   1.210 +		rrdtool update "$rrdlog/disk.rrd" -t readsect:writesect \
   1.211 +			N:$readsect:$writesect
   1.212 +	done < /proc/diskstats
   1.213 +	disk=${dev:0:8}
   1.214 +	dir=/sys/block/${disk#/dev/}/device
   1.215 +	done=$(printf "%d\n" $(cat $dir/iodone_cnt 2> /dev/null) )
   1.216 +	err=$(printf "%d\n" $(cat $dir/ioerr_cnt 2> /dev/null) )
   1.217 +	req=$(printf "%d\n" $(cat $dir/iorequest_cnt 2> /dev/null) )
   1.218 +	rrdtool update "$rrdlog/iodisk.rrd" -t done:err:req N:$done:$err:$req
   1.219 +	iroot=$(df -i / | sed '$!d;s/.* \([0-9]*\)% \/.*/\1/')
   1.220 +	broot=$(df / | sed '$!d;s/.* \([0-9]*\)% \/.*/\1/')
   1.221 +	ihome=$(df -i /home | sed '$!d;s/.* \([0-9]*\)% \/.*/\1/')
   1.222 +	bhome=$(df /home | sed '$!d;s/.* \([0-9]*\)% \/.*/\1/')
   1.223 +	rrdtool update "$rrdlog/usagedisk.rrd" -t broot:iroot:bhome:ihome N:$broot:$iroot:$bhome:$ihome
   1.224 +}
   1.225 +
   1.226 +updateifgraph() {
   1.227 +	interface=$1
   1.228 +	period=$2
   1.229 +	rrdtool graph "$rrdgraph/$interface-$period.png" --start -1$period \
   1.230 +		$rrdgraphargs -t "traffic on $interface graph per $period" \
   1.231 +		--logarithmic -A -v "Bytes/second" --units=si \
   1.232 +		DEF:incoming=$rrdlog/$interface.rrd:incoming:AVERAGE \
   1.233 +		DEF:outgoing=$rrdlog/$interface.rrd:outgoing:AVERAGE \
   1.234 +		DEF:tcp=$rrdlog/proto-$interface.rrd:tcp:AVERAGE \
   1.235 +		'AREA:incoming#00FF00:incoming traffic' \
   1.236 +		'GPRINT:incoming:MAX:max%8.3lf %sBps' \
   1.237 +		'LINE1:outgoing#0000FF:outgoing traffic' \
   1.238 +		'GPRINT:outgoing:MAX:max%8.3lf %sBps' \
   1.239 +		'LINE1:tcp#000000:connections' \
   1.240 +		'GPRINT:tcp:MAX:max %2.0lf\j'
   1.241 +}
   1.242 +
   1.243 +netframes() {
   1.244 +ifconfig $1 | grep "$2 packets" | sed -re "s/.*$3:([0-9]+).*/\1/g"
   1.245 +}
   1.246 +
   1.247 +netstats() {
   1.248 +ifconfig $1 | grep bytes | sed -re "s/.*$2 bytes:([0-9]+).*/\1/g"
   1.249 +}
   1.250 +
   1.251 +netproto()
   1.252 +{
   1.253 +	proto=${1:-tcp}
   1.254 +	if [ -n "$2" ]; then
   1.255 +		netstat -an 2> /dev/null | grep -v '0.0.0.0:*' | grep "^$proto"| grep ":$2 " | wc -l
   1.256 +	else
   1.257 +		netstat -an 2> /dev/null | grep -v '0.0.0.0:*' | grep "^$proto"| wc -l
   1.258 +	fi	
   1.259 +}
   1.260 +
   1.261 +updateifdata() {
   1.262 +	interface=$1
   1.263 +	[ -e "$rrdlog/$interface.rrd" ] ||
   1.264 +		rrdtool create "$rrdlog/$interface.rrd" --step=300 \
   1.265 +			DS:incoming:COUNTER:600:0:U \
   1.266 +			DS:outgoing:COUNTER:600:0:U \
   1.267 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   1.268 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   1.269 +	[ -e "$rrdlog/packets-$interface.rrd" ] ||
   1.270 +		rrdtool create "$rrdlog/packets-$interface.rrd" --step=300 \
   1.271 +			DS:in:COUNTER:600:0:U      DS:out:COUNTER:600:0:U \
   1.272 +			DS:inerr:COUNTER:600:0:U   DS:outerr:COUNTER:600:0:U \
   1.273 +			DS:indrop:COUNTER:600:0:U  DS:outdrop:COUNTER:600:0:U \
   1.274 +			DS:inov:COUNTER:600:0:U    DS:outov:COUNTER:600:0:U \
   1.275 +			DS:frame:COUNTER:600:0:U   DS:carrier:COUNTER:600:0:U \
   1.276 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   1.277 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   1.278 +	[ -e "$rrdlog/proto-$interface.rrd" ] ||
   1.279 +		rrdtool create "$rrdlog/proto-$interface.rrd" --step=300 \
   1.280 +			DS:tcp:GAUGE:600:0:U     DS:udp:GAUGE:600:0:U \
   1.281 +			DS:rsync:GAUGE:600:0:U   DS:www:GAUGE:600:0:U \
   1.282 +			DS:ssh:GAUGE:600:0:U \
   1.283 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   1.284 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   1.285 +	rx=$(netstats $interface RX)
   1.286 +	tx=$(netstats $interface TX)
   1.287 +	rrdtool update "$rrdlog/$interface.rrd" -t incoming:outgoing \
   1.288 +		N:${rx:-U}:${tx:-U}
   1.289 +	rx=$(netframes $interface RX packets)
   1.290 +	tx=$(netframes $interface TX packets)
   1.291 +	rxerr=$(netframes $interface RX errors)
   1.292 +	txerr=$(netframes $interface TX errors)
   1.293 +	rxdrop=$(netframes $interface RX dropped)
   1.294 +	txdrop=$(netframes $interface TX dropped)
   1.295 +	rxov=$(netframes $interface RX overruns)
   1.296 +	txov=$(netframes $interface TX overruns)
   1.297 +	frame=$(netframes $interface RX frame)
   1.298 +	carrier=$(netframes $interface TX carrier)
   1.299 +	rrdtool update "$rrdlog/packets-$interface.rrd" \
   1.300 +		-t in:out:inerr:outerr:indrop:outdrop:inov:outov:frame:carrier \
   1.301 +		N:${rx:-U}:${tx:-U}:${rxerr:-U}:${txerr:-U}:${rxdrop:-U}:${txdrop:-U}:${rxov:-U}:${txov:-U}:${frame:-U}:${carrier:-U}
   1.302 +	rsync=$(netproto tcp 873)
   1.303 +	www=$(netproto tcp 80)
   1.304 +	ssh=$(netproto tcp 22)
   1.305 +	tcp=$(netproto tcp)
   1.306 +	udp=$(netproto udp)
   1.307 +	rrdtool update "$rrdlog/proto-$interface.rrd" \
   1.308 +		-t tcp:udp:rsync:www:ssh \
   1.309 +		N:${tcp:-U}:${udp:-U}:${rsync:-U}:${www:-U}:${ssh:-U}
   1.310 +}
   1.311 +
   1.312 +getdisk()
   1.313 +{
   1.314 +	local d
   1.315 +	local i
   1.316 +	d=$(stat -c %04D $1)
   1.317 +	for i in /dev/* ; do 
   1.318 +		[ $(stat -c "%02t%02T" $i) == $d ] || continue
   1.319 +		echo $i
   1.320 +		break
   1.321 +	done
   1.322 +}
   1.323 +
   1.324 +###
   1.325 +### System graphs
   1.326 +###
   1.327 +
   1.328 +updatecpudata
   1.329 +updatecpugraph day
   1.330 +updatecpugraph week
   1.331 +updatecpugraph month
   1.332 +updatecpugraph year
   1.333 +
   1.334 +updatememdata
   1.335 +updatememgraph day
   1.336 +updatememgraph week
   1.337 +updatememgraph month
   1.338 +updatememgraph year
   1.339 +
   1.340 +if [ -e /proc/diskstats ]; then
   1.341 +	disk=$(getdisk $0)
   1.342 +	updatediskdata $disk
   1.343 +	updatediskgraph day ${disk:0:8}
   1.344 +	updatediskgraph week ${disk:0:8}
   1.345 +	updatediskgraph month ${disk:0:8}
   1.346 +	updatediskgraph year ${disk:0:8}
   1.347 +fi
   1.348 +
   1.349 +iface=$(/sbin/route -n | awk '{ if (/^0.0.0.0/) print $8 }')
   1.350 +updateifdata $iface
   1.351 +updateifgraph $iface day
   1.352 +updateifgraph $iface week
   1.353 +updateifgraph $iface month
   1.354 +updateifgraph $iface year
   1.355 +
   1.356 +#rsync --bwlimit=40 -t -e 'ssh -p 222 -i /home/bellard/.ssh/id_rsa' $rrdlog/* \
   1.357 +#	bellard@mirror.slitaz.org:/var/spool/rrd/tank/ > /dev/null 2>&1
   1.358 +