slitaz-forge diff tank/web/bin/makegraphs @ rev 12
Add tank/web folder.
author | Christopher Rogers <slaxemulator@gmail.com> |
---|---|
date | Wed Feb 23 22:16:38 2011 +0000 (2011-02-23) |
parents | |
children | 08ebcbf62731 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/tank/web/bin/makegraphs Wed Feb 23 22:16:38 2011 +0000 1.3 @@ -0,0 +1,318 @@ 1.4 +#!/bin/sh 1.5 +#*/5 * * * * /home/slitaz/www/tank/bin/makegraphs >/dev/null 1.6 + 1.7 +# RRD database directory 1.8 +rrdlog="/home/slitaz/www/tank/rrd" 1.9 + 1.10 +# Images directory 1.11 +rrdgraph="/home/slitaz/www/tank/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:system:COUNTER:600:0:500000000 \ 1.24 + DS:idle:COUNTER:600:0:500000000 \ 1.25 + RRA:AVERAGE:0.5:1:576 RRA:AVERAGE:0.5:6:672 \ 1.26 + RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460 1.27 + grep '^cpu' /proc/stat | while read cpu user nice system idle misc; do 1.28 + rrdtool update $rrdlog/cpu.rrd -t user:system:idle \ 1.29 + N:$(( $user + $nice )):$system:$idle 1.30 + break 1.31 + done 1.32 + 1.33 + [ -e "$rrdlog/cpu2.rrd" ] && 1.34 + grep '^cpu' /proc/stat | while read cpu user nice system idle misc; do 1.35 + rrdtool update $rrdlog/cpu2.rrd -t nice:user:system:idle \ 1.36 + N:$nice:$user:$system:$idle 1.37 + break 1.38 + done 1.39 +} 1.40 + 1.41 +updatecpugraph() { 1.42 + period=$1 1.43 + info="$(grep '^model name' /proc/cpuinfo | cut -d: -f2 \ 1.44 + | sed 's/ * / /g' | awk ' 1.45 +{ s=$0 ; n++ } 1.46 +END { if (n > 1) printf " %dx",n; print s }')" 1.47 + rrdtool graph "$rrdgraph/cpu-$period.png" --start -1$period \ 1.48 + $rrdgraphargs -l 0 -u 100 -t "cpu usage per $period [$info ]" \ 1.49 + DEF:user=$rrdlog/cpu.rrd:user:AVERAGE \ 1.50 + DEF:system=$rrdlog/cpu.rrd:system:AVERAGE \ 1.51 + DEF:idle=$rrdlog/cpu.rrd:idle:AVERAGE \ 1.52 + 'CDEF:total=user,system,idle,+,+' \ 1.53 + 'CDEF:userpct=100,user,total,/,*' \ 1.54 + 'CDEF:systempct=100,system,total,/,*' \ 1.55 + 'CDEF:idlepct=100,idle,total,/,*' \ 1.56 + 'AREA:userpct#0000FF:user cpu usage' \ 1.57 + 'STACK:systempct#FF0000:system cpu usage' \ 1.58 + 'STACK:idlepct#00FF00:idle cpu usage\j' 1.59 +} 1.60 + 1.61 +updatememgraph() { 1.62 + period=$1 1.63 + info="$(free | awk '\ 1.64 +{ \ 1.65 + if (/Mem:/) { \ 1.66 + if ($2 < 10000) printf "%d KB",$2; \ 1.67 + else if ($2 < 10000000) printf "%d MB",$2/1024; \ 1.68 + else printf "%d GB",$2/1024/1024; \ 1.69 + } \ 1.70 +}')" 1.71 + info2="$(free | awk '\ 1.72 +{ \ 1.73 + if (/Swap:/) { \ 1.74 + if ($2 < 10000) printf "%d KB",$2; \ 1.75 + else if ($2 < 10000000) printf "%d MB",$2/1024; \ 1.76 + else printf "%d GB",$2/1024/1024; \ 1.77 + } \ 1.78 +}')" 1.79 + rrdtool graph "$rrdgraph/memory-$period.png" --start -1$period \ 1.80 + $rrdgraphargs -l 0 -u 100 \ 1.81 + -t "memory usage per $period [ $info + $info2 swap ]" \ 1.82 + DEF:used=$rrdlog/mem.rrd:memused:AVERAGE \ 1.83 + DEF:free=$rrdlog/mem.rrd:memfree:AVERAGE \ 1.84 + DEF:shared=$rrdlog/mem.rrd:memshared:AVERAGE \ 1.85 + DEF:buffer=$rrdlog/mem.rrd:membuffers:AVERAGE \ 1.86 + DEF:cache=$rrdlog/mem.rrd:memcache:AVERAGE \ 1.87 + DEF:swused=$rrdlog/mem.rrd:swapused:AVERAGE \ 1.88 + DEF:swfree=$rrdlog/mem.rrd:swapfree:AVERAGE \ 1.89 + 'CDEF:total=used,free,+' \ 1.90 + 'CDEF:used2=used,buffer,cache,shared,+,+,-' \ 1.91 + 'CDEF:usedpct=100,used2,total,/,*' \ 1.92 + 'CDEF:sharedpct=100,shared,total,/,*' \ 1.93 + 'CDEF:bufferpct=100,buffer,total,/,*' \ 1.94 + 'CDEF:cachepct=100,cache,total,/,*' \ 1.95 + 'CDEF:freepct=100,free,total,/,*' \ 1.96 + 'CDEF:swtotal=swused,swfree,+' \ 1.97 + 'CDEF:swusedpct=100,swused,swtotal,/,*' \ 1.98 + 'AREA:usedpct#0000FF:used memory' \ 1.99 + 'STACK:sharedpct#FF7F00:shared memory' \ 1.100 + 'STACK:bufferpct#FF00FF:buffered memory' \ 1.101 + 'STACK:cachepct#FFFF00:cached memory' \ 1.102 + 'STACK:freepct#00FF00:free memory' \ 1.103 + 'LINE2:swusedpct#FF0000:used swap\j' 1.104 +} 1.105 + 1.106 +updatememdata () { 1.107 + [ -e "$rrdlog/mem.rrd" ] || 1.108 + rrdtool create "$rrdlog/mem.rrd" --step=300 \ 1.109 + DS:memused:ABSOLUTE:600:0:5000000000 \ 1.110 + DS:memfree:ABSOLUTE:600:0:5000000000 \ 1.111 + DS:memshared:ABSOLUTE:600:0:5000000000 \ 1.112 + DS:membuffers:ABSOLUTE:600:0:5000000000 \ 1.113 + DS:memcache:ABSOLUTE:600:0:5000000000 \ 1.114 + DS:swapused:ABSOLUTE:600:0:5000000000 \ 1.115 + DS:swapfree:ABSOLUTE:600:0:5000000000 \ 1.116 + RRA:AVERAGE:0.5:1:576 RRA:AVERAGE:0.5:6:672 \ 1.117 + RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460 1.118 + 1.119 + while read tag count unit; do 1.120 + case "$tag" in 1.121 + MemTotal:) memtotal=$(($count * 1024));; 1.122 + MemFree:) memfree=$(($count * 1024)) 1.123 + memused=$(($memtotal - $memfree)) 1.124 + memshared=0;; 1.125 + MemShared:) memshared=$(($count * 1024));; 1.126 + Buffers:) membuffers=$(($count * 1024));; 1.127 + Cached:) memcache=$(($count * 1024));; 1.128 + SwapTotal:) swaptotal=$(($count * 1024));; 1.129 + SwapFree:) swapfree=$(($count * 1024)) 1.130 + swapused=$(( $swaptotal - $swapfree));; 1.131 + esac 1.132 + done < /proc/meminfo 1.133 + 1.134 + rrdtool update "$rrdlog/mem.rrd" \ 1.135 + -t memused:memfree:memshared:membuffers:memcache:swapused:swapfree \ 1.136 + "N:$memused:$memfree:$memshared:$membuffers:$memcache:$swapused:$swapfree" 1.137 +} 1.138 + 1.139 +getmax() { 1.140 + rrdtool fetch $rrdlog/$1.rrd AVERAGE | awk '\ 1.141 +BEGIN {max=0} \ 1.142 +/^[0-9]/ { \ 1.143 + if ($2 != "nan" && $2 > max) max=$2; \ 1.144 + if ($3 != "nan" && $3 > max) max=$3; \ 1.145 +} \ 1.146 +END { print max }' | sed 's/,/./' 1.147 +} 1.148 + 1.149 +updatediskgraph() { 1.150 + period=$1 1.151 + [ "$period" == "day" ] && maxdisk="$(getmax disk)" 1.152 + info="" 1.153 + [ -r $2 ] && 1.154 + info="[ $(fdisk -l 2> /dev/null | grep "^Disk $2:" | \ 1.155 + sed "s|Disk $2: \(.*\), .*|\1|") ]" 1.156 + if [ -e /sys/block/${2#/dev/}/device/iodone_cnt ]; then 1.157 + err=$(printf "%d\n" $(cat /sys/block/${2#/dev/}/device/ioerr_cnt) ) 1.158 + done=$(printf "%d\n" $(cat /sys/block/${2#/dev/}/device/iodone_cnt) ) 1.159 + rate=$(echo | awk "BEGIN { printf \"%.0e\\n\",$err/$done }") 1.160 + [ $err -eq 0 ] && rate="0" 1.161 +# --right-axis-label "I/O state %" 1.162 + rrdtool graph "$rrdgraph/disk-$period.png" --start -1$period \ 1.163 + $rrdgraphargs -t "disk access per $period $info" \ 1.164 + --logarithmic --lower-limit 1 -v "Sectors/second" --units=si \ 1.165 + DEF:read=$rrdlog/disk.rrd:readsect:AVERAGE \ 1.166 + DEF:write=$rrdlog/disk.rrd:writesect:AVERAGE \ 1.167 + DEF:req=$rrdlog/iodisk.rrd:req:AVERAGE \ 1.168 + DEF:done=$rrdlog/iodisk.rrd:done:AVERAGE \ 1.169 + DEF:err=$rrdlog/iodisk.rrd:err:AVERAGE \ 1.170 + "CDEF:readpct=100,read,$maxdisk,/,*" \ 1.171 + "CDEF:writepct=100,write,$maxdisk,/,*" \ 1.172 + "CDEF:errpct=100,err,req,/,*" \ 1.173 + "CDEF:donepct=100,done,req,/,*" \ 1.174 + "CDEF:errrate=err,done,/" \ 1.175 + 'AREA:readpct#0000FF:sectors read from disk' \ 1.176 + "COMMENT:I/O error rate $rate" \ 1.177 + 'STACK:writepct#00FF00:sectors written to disk' \ 1.178 + 'LINE2:donepct#FFFF00:% I/O complete' \ 1.179 + 'LINE2:errpct#FF0000:% I/O error\j' 1.180 + else 1.181 + rrdtool graph "$rrdgraph/disk-$period.png" --start -1$period \ 1.182 + $rrdgraphargs -t "disk access per $period $info" \ 1.183 + --logarithmic --lower-limit 1 -v "Sectors/second" --units=si \ 1.184 + DEF:read=$rrdlog/disk.rrd:readsect:AVERAGE \ 1.185 + DEF:write=$rrdlog/disk.rrd:writesect:AVERAGE \ 1.186 + "CDEF:readpct=100,read,$maxdisk,/,*" \ 1.187 + "CDEF:writepct=100,write,$maxdisk,/,*" \ 1.188 + 'AREA:readpct#0000FF:sectors read from disk' \ 1.189 + 'STACK:writepct#00FF00:sectors written to disk' 1.190 + fi 1.191 +} 1.192 + 1.193 +updatediskdata() { 1.194 + dev=$1 1.195 + [ -e "$rrdlog/disk.rrd" ] || 1.196 + rrdtool create "$rrdlog/disk.rrd" --step=300 \ 1.197 + DS:readsect:COUNTER:600:0:5000000000 \ 1.198 + DS:writesect:COUNTER:600:0:5000000000 \ 1.199 + RRA:AVERAGE:0.5:1:576 RRA:AVERAGE:0.5:6:672 \ 1.200 + RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460 1.201 + [ -e "$rrdlog/iodisk.rrd" ] || 1.202 + rrdtool create "$rrdlog/iodisk.rrd" --step=300 \ 1.203 + DS:done:GAUGE:600:0:U DS:err:GAUGE:600:0:U \ 1.204 + DS:req:GAUGE:600:0:U \ 1.205 + RRA:AVERAGE:0.5:1:576 RRA:AVERAGE:0.5:6:672 \ 1.206 + RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460 1.207 + 1.208 + while read major minor name readreq readsect writereq writesect misc; do 1.209 + [ $major = $(( 0x$(stat -c %t $dev) )) ] || continue 1.210 + [ $minor = $(( 0x$(stat -c %T $dev) )) ] || continue 1.211 + rrdtool update "$rrdlog/disk.rrd" -t readsect:writesect \ 1.212 + N:$readsect:$writesect 1.213 + done < /proc/diskstats 1.214 + disk=${dev:0:8} 1.215 + dir=/sys/block/${disk#/dev/}/device 1.216 + done=$(printf "%d\n" $(cat $dir/iodone_cnt 2> /dev/null) ) 1.217 + err=$(printf "%d\n" $(cat $dir/ioerr_cnt 2> /dev/null) ) 1.218 + req=$(printf "%d\n" $(cat $dir/iorequest_cnt 2> /dev/null) ) 1.219 + rrdtool update "$rrdlog/iodisk.rrd" -t done:err:req N:$done:$err:$req 1.220 +} 1.221 + 1.222 +updateifgraph() { 1.223 + interface=$1 1.224 + period=$2 1.225 + rrdtool graph "$rrdgraph/$interface-$period.png" --start -1$period \ 1.226 + $rrdgraphargs -t "traffic on $interface graph per $period" \ 1.227 + --logarithmic -A -v "Bytes/second" --units=si \ 1.228 + DEF:incoming=$rrdlog/$interface.rrd:incoming:AVERAGE \ 1.229 + DEF:outgoing=$rrdlog/$interface.rrd:outgoing:AVERAGE \ 1.230 + 'AREA:incoming#00FF00:incoming traffic' \ 1.231 + 'GPRINT:outgoing:MAX:max output%8.3lf %sBps' \ 1.232 + 'LINE1:outgoing#0000FF:outgoing traffic\j' 1.233 +} 1.234 + 1.235 +netframes() { 1.236 +ifconfig $1 | grep "$2 packets" | sed -re "s/.*$3:([0-9]+).*/\1/g" 1.237 +} 1.238 + 1.239 +netstats() { 1.240 +ifconfig $1 | grep bytes | sed -re "s/.*$2 bytes:([0-9]+).*/\1/g" 1.241 +} 1.242 + 1.243 +updateifdata() { 1.244 + interface=$1 1.245 + [ -e "$rrdlog/$interface.rrd" ] || 1.246 + rrdtool create "$rrdlog/$interface.rrd" --step=300 \ 1.247 + DS:incoming:COUNTER:600:0:U \ 1.248 + DS:outgoing:COUNTER:600:0:U \ 1.249 + RRA:AVERAGE:0.5:1:576 RRA:AVERAGE:0.5:6:672 \ 1.250 + RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460 1.251 + [ -e "$rrdlog/packets-$interface.rrd" ] || 1.252 + rrdtool create "$rrdlog/packets-$interface.rrd" --step=300 \ 1.253 + DS:in:COUNTER:600:0:U DS:out:COUNTER:600:0:U \ 1.254 + DS:inerr:COUNTER:600:0:U DS:outerr:COUNTER:600:0:U \ 1.255 + DS:indrop:COUNTER:600:0:U DS:outdrop:COUNTER:600:0:U \ 1.256 + DS:inov:COUNTER:600:0:U DS:outov:COUNTER:600:0:U \ 1.257 + DS:frame:COUNTER:600:0:U DS:carrier:COUNTER:600:0:U \ 1.258 + RRA:AVERAGE:0.5:1:576 RRA:AVERAGE:0.5:6:672 \ 1.259 + RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460 1.260 + rx=$(netstats $interface RX) 1.261 + tx=$(netstats $interface TX) 1.262 + rrdtool update "$rrdlog/$interface.rrd" -t incoming:outgoing \ 1.263 + N:${rx:-U}:${tx:-U} 1.264 + rx=$(netframes $interface RX packets) 1.265 + tx=$(netframes $interface TX packets) 1.266 + rxerr=$(netframes $interface RX errors) 1.267 + txerr=$(netframes $interface TX errors) 1.268 + rxdrop=$(netframes $interface RX dropped) 1.269 + txdrop=$(netframes $interface TX dropped) 1.270 + rxov=$(netframes $interface RX overruns) 1.271 + txov=$(netframes $interface TX overruns) 1.272 + frame=$(netframes $interface RX frame) 1.273 + carrier=$(netframes $interface TX carrier) 1.274 + rrdtool update "$rrdlog/packets-$interface.rrd" \ 1.275 + -t in:out:inerr:outerr:indrop:outdrop:inov:outov:frame:carrier \ 1.276 + N:${rx:-U}:${tx:-U}:${rxerr:-U}:${txerr:-U}:${rxdrop:-U}:${txdrop:-U}:${rxov:-U}:${txov:-U}:${frame:-U}:${carrier:-U} 1.277 +} 1.278 + 1.279 +getdisk() 1.280 +{ 1.281 + local d 1.282 + local i 1.283 + d=$(stat -c %04D $1) 1.284 + for i in /dev/* ; do 1.285 + [ $(stat -c "%02t%02T" $i) == $d ] || continue 1.286 + echo $i 1.287 + break 1.288 + done 1.289 +} 1.290 + 1.291 +### 1.292 +### System graphs 1.293 +### 1.294 + 1.295 +updatecpudata 1.296 +updatecpugraph day 1.297 +updatecpugraph week 1.298 +updatecpugraph month 1.299 +updatecpugraph year 1.300 + 1.301 +updatememdata 1.302 +updatememgraph day 1.303 +updatememgraph week 1.304 +updatememgraph month 1.305 +updatememgraph year 1.306 + 1.307 +if [ -e /proc/diskstats ]; then 1.308 + disk=$(getdisk $0) 1.309 + updatediskdata $disk 1.310 + updatediskgraph day ${disk:0:8} 1.311 + updatediskgraph week ${disk:0:8} 1.312 + updatediskgraph month ${disk:0:8} 1.313 + updatediskgraph year ${disk:0:8} 1.314 +fi 1.315 + 1.316 +iface=$(/sbin/route -n | awk '{ if (/^0.0.0.0/) print $8 }') 1.317 +updateifdata $iface 1.318 +updateifgraph $iface day 1.319 +updateifgraph $iface week 1.320 +updateifgraph $iface month 1.321 +updateifgraph $iface year