slitaz-modular rev 147

initramfs: Add makegraphs script.
author Christopher Rogers <slaxemulator@gmail.com>
date Wed Nov 16 16:17:22 2011 +0000 (2011-11-16)
parents 6ae8e01ab09d
children 1d1dade306e8
files initramfs/usr/bin/makegraphs
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/initramfs/usr/bin/makegraphs	Wed Nov 16 16:17:22 2011 +0000
     1.3 @@ -0,0 +1,311 @@
     1.4 +#!/bin/sh
     1.5 +#*/5  * * * * /var/www/mirror-info/bin/makegraphs >/dev/null
     1.6 +
     1.7 +# RRD database directory
     1.8 +rrdlog="/var/spool/rrd"
     1.9 +
    1.10 +# Images directory
    1.11 +rrdgraph="/var/www/vhosts/pics/rrd"
    1.12 +
    1.13 +# Colors
    1.14 +#rrdcolors="--color SHADEA#EAE9EE --color SHADEB#EAE9EE --color BACK#EAE9EE" 
    1.15 +rrdcolors="--color SHADEA#FFFFFF --color SHADEB#FFFFFF --color BACK#FFFFFF" 
    1.16 +rrdgraphargs="-aPNG -i -z --alt-y-grid -w 600 -h 100 -r $rrdcolors"
    1.17 +
    1.18 +[ -d $rrdlog ] || mkdir -p $rrdlog
    1.19 +[ -d $rrdgraph ] || mkdir -p $rrdgraph
    1.20 +
    1.21 +updatecpudata() {
    1.22 +	[ -e "$rrdlog/cpu.rrd" ] || rrdtool create $rrdlog/cpu.rrd --step=300 \
    1.23 +			DS:user:COUNTER:600:0:500000000 \
    1.24 +			DS:system:COUNTER:600:0:500000000 \
    1.25 +			DS:idle:COUNTER:600:0:500000000 \
    1.26 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
    1.27 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
    1.28 +	grep '^cpu' /proc/stat | while read cpu user nice system idle misc; do
    1.29 +		rrdtool update $rrdlog/cpu.rrd -t user:system:idle \
    1.30 +			N:$(( $user + $nice )):$system:$idle
    1.31 +		break
    1.32 +	done
    1.33 +
    1.34 +	[ -e "$rrdlog/cpu2.rrd" ] &&
    1.35 +	grep '^cpu' /proc/stat | while read cpu user nice system idle misc; do
    1.36 +		rrdtool update $rrdlog/cpu2.rrd -t nice:user:system:idle \
    1.37 +			N:$nice:$user:$system:$idle
    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' | head -1)"
    1.46 +	rrdtool graph "$rrdgraph/cpu-$period.png" --start -1$period \
    1.47 +		$rrdgraphargs -l 0 -u 100 -t "cpu usage per $period [$info ]" \
    1.48 +		DEF:user=$rrdlog/cpu.rrd:user:AVERAGE \
    1.49 +		DEF:system=$rrdlog/cpu.rrd:system:AVERAGE \
    1.50 +		DEF:idle=$rrdlog/cpu.rrd:idle:AVERAGE \
    1.51 +		'CDEF:total=user,system,idle,+,+' \
    1.52 +		'CDEF:userpct=100,user,total,/,*' \
    1.53 +		'CDEF:systempct=100,system,total,/,*' \
    1.54 +		'CDEF:idlepct=100,idle,total,/,*' \
    1.55 +		'AREA:userpct#0000FF:user cpu usage' \
    1.56 +		'STACK:systempct#FF0000:system cpu usage' \
    1.57 +		'STACK:idlepct#00FF00:idle cpu usage\j'
    1.58 +}
    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 +#		--right-axis-label "I/O state %"
   1.158 +	rrdtool graph "$rrdgraph/disk-$period.png" --start -1$period \
   1.159 +		$rrdgraphargs -t "disk access per $period $info" \
   1.160 +		--logarithmic --lower-limit 1 -v "Sectors/second" --units=si \
   1.161 +		DEF:read=$rrdlog/disk.rrd:readsect:AVERAGE \
   1.162 +		DEF:write=$rrdlog/disk.rrd:writesect:AVERAGE \
   1.163 +		DEF:req=$rrdlog/iodisk.rrd:req:AVERAGE \
   1.164 +		DEF:done=$rrdlog/iodisk.rrd:done:AVERAGE \
   1.165 +		DEF:err=$rrdlog/iodisk.rrd:err:AVERAGE \
   1.166 +		"CDEF:readpct=100,read,$maxdisk,/,*" \
   1.167 +		"CDEF:writepct=100,write,$maxdisk,/,*" \
   1.168 +		"CDEF:errpct=100,err,req,/,*" \
   1.169 +		"CDEF:donepct=100,done,req,/,*" \
   1.170 +		'AREA:readpct#0000FF:sectors read from disk' \
   1.171 +		'STACK:writepct#00FF00:sectors written to disk' \
   1.172 +		'LINE2:donepct#FFFF00:I/O complete' \
   1.173 +		'LINE2:errpct#FF0000:I/O error\j'
   1.174 +	else
   1.175 +	rrdtool graph "$rrdgraph/disk-$period.png" --start -1$period \
   1.176 +		$rrdgraphargs -t "disk access per $period $info" \
   1.177 +		--logarithmic --lower-limit 1 -v "Sectors/second" --units=si \
   1.178 +		DEF:read=$rrdlog/disk.rrd:readsect:AVERAGE \
   1.179 +		DEF:write=$rrdlog/disk.rrd:writesect:AVERAGE \
   1.180 +		"CDEF:readpct=100,read,$maxdisk,/,*" \
   1.181 +		"CDEF:writepct=100,write,$maxdisk,/,*" \
   1.182 +		'AREA:readpct#0000FF:sectors read from disk' \
   1.183 +		'STACK:writepct#00FF00:sectors written to disk'
   1.184 +	fi
   1.185 +}
   1.186 +
   1.187 +updatediskdata() {
   1.188 +	dev=$1
   1.189 +	[ -e "$rrdlog/disk.rrd" ] ||
   1.190 +		rrdtool create "$rrdlog/disk.rrd" --step=300 \
   1.191 +			DS:readsect:COUNTER:600:0:5000000000 \
   1.192 +			DS:writesect:COUNTER:600:0:5000000000 \
   1.193 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   1.194 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   1.195 +	[ -e "$rrdlog/iodisk.rrd" ] ||
   1.196 +		rrdtool create "$rrdlog/iodisk.rrd" --step=300 \
   1.197 +			DS:done:GAUGE:600:0:U  DS:err:GAUGE:600:0:U \
   1.198 +			DS:req:GAUGE:600:0:U \
   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 +
   1.202 +	while read major minor name readreq readsect writereq writesect misc; do
   1.203 +		[ $major = $(( 0x$(stat -c %t $dev) )) ] || continue
   1.204 +		[ $minor = $(( 0x$(stat -c %T $dev) )) ] || continue
   1.205 +		rrdtool update "$rrdlog/disk.rrd" -t readsect:writesect \
   1.206 +			N:$readsect:$writesect
   1.207 +	done < /proc/diskstats
   1.208 +	disk=${dev:0:8}
   1.209 +	dir=/sys/block/${disk#/dev/}/device
   1.210 +	done=$(printf "%d\n" $(cat $dir/iodone_cnt 2> /dev/null) )
   1.211 +	err=$(printf "%d\n" $(cat $dir/ioerr_cnt 2> /dev/null) )
   1.212 +	req=$(printf "%d\n" $(cat $dir/iorequest_cnt 2> /dev/null) )
   1.213 +	rrdtool update "$rrdlog/iodisk.rrd" -t done:err:req N:$done:$err:$req
   1.214 +}
   1.215 +
   1.216 +updateifgraph() {
   1.217 +	interface=$1
   1.218 +	period=$2
   1.219 +	rrdtool graph "$rrdgraph/$interface-$period.png" --start -1$period \
   1.220 +		$rrdgraphargs -t "traffic on $interface graph per $period" \
   1.221 +		--logarithmic -A -v "Bytes/second" --units=si \
   1.222 +		DEF:incoming=$rrdlog/$interface.rrd:incoming:AVERAGE \
   1.223 +		DEF:outgoing=$rrdlog/$interface.rrd:outgoing:AVERAGE \
   1.224 +		'AREA:incoming#00FF00:incoming traffic' \
   1.225 +		'LINE1:outgoing#0000FF:outgoing traffic\j'
   1.226 +}
   1.227 +
   1.228 +netframes() {
   1.229 +ifconfig $1 | grep "$2 packets" | sed -re "s/.*$3:([0-9]+).*/\1/g"
   1.230 +}
   1.231 +
   1.232 +netstats() {
   1.233 +ifconfig $1 | grep bytes | sed -re "s/.*$2 bytes:([0-9]+).*/\1/g"
   1.234 +}
   1.235 +
   1.236 +updateifdata() {
   1.237 +	interface=$1
   1.238 +	[ -e "$rrdlog/$interface.rrd" ] ||
   1.239 +		rrdtool create "$rrdlog/$interface.rrd" --step=300 \
   1.240 +			DS:incoming:COUNTER:600:0:U \
   1.241 +			DS:outgoing:COUNTER:600:0:U \
   1.242 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   1.243 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   1.244 +	[ -e "$rrdlog/packets-$interface.rrd" ] ||
   1.245 +		rrdtool create "$rrdlog/packets-$interface.rrd" --step=300 \
   1.246 +			DS:in:COUNTER:600:0:U      DS:out:COUNTER:600:0:U \
   1.247 +			DS:inerr:COUNTER:600:0:U   DS:outerr:COUNTER:600:0:U \
   1.248 +			DS:indrop:COUNTER:600:0:U  DS:outdrop:COUNTER:600:0:U \
   1.249 +			DS:inov:COUNTER:600:0:U    DS:outov:COUNTER:600:0:U \
   1.250 +			DS:frame:COUNTER:600:0:U   DS:carrier:COUNTER:600:0:U \
   1.251 +			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
   1.252 +			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
   1.253 +	rx=$(netstats $interface RX)
   1.254 +	tx=$(netstats $interface TX)
   1.255 +	rrdtool update "$rrdlog/$interface.rrd" -t incoming:outgoing \
   1.256 +		N:${rx:-U}:${tx:-U}
   1.257 +	rx=$(netframes $interface RX packets)
   1.258 +	tx=$(netframes $interface TX packets)
   1.259 +	rxerr=$(netframes $interface RX errors)
   1.260 +	txerr=$(netframes $interface TX errors)
   1.261 +	rxdrop=$(netframes $interface RX dropped)
   1.262 +	txdrop=$(netframes $interface TX dropped)
   1.263 +	rxov=$(netframes $interface RX overruns)
   1.264 +	txov=$(netframes $interface TX overruns)
   1.265 +	frame=$(netframes $interface RX frame)
   1.266 +	carrier=$(netframes $interface TX carrier)
   1.267 +	rrdtool update "$rrdlog/packets-$interface.rrd" \
   1.268 +		-t in:out:inerr:outerr:indrop:outdrop:inov:outov:frame:carrier \
   1.269 +		N:${rx:-U}:${tx:-U}:${rxerr:-U}:${txerr:-U}:${rxdrop:-U}:${txdrop:-U}:${rxov:-U}:${txov:-U}:${frame:-U}:${carrier:-U}
   1.270 +}
   1.271 +
   1.272 +getdisk()
   1.273 +{
   1.274 +	local d
   1.275 +	local i
   1.276 +	d=$(stat -c %04D $1)
   1.277 +	for i in /dev/* ; do 
   1.278 +		[ $(stat -c "%02t%02T" $i) == $d ] || continue
   1.279 +		echo $i
   1.280 +		break
   1.281 +	done
   1.282 +}
   1.283 +
   1.284 +###
   1.285 +### System graphs
   1.286 +###
   1.287 +
   1.288 +updatecpudata
   1.289 +updatecpugraph day
   1.290 +updatecpugraph week
   1.291 +updatecpugraph month
   1.292 +updatecpugraph year
   1.293 +
   1.294 +updatememdata
   1.295 +updatememgraph day
   1.296 +updatememgraph week
   1.297 +updatememgraph month
   1.298 +updatememgraph year
   1.299 +
   1.300 +if [ -e /proc/diskstats ]; then
   1.301 +	disk=$(getdisk $0)
   1.302 +	updatediskdata $disk
   1.303 +	updatediskgraph day ${disk:0:8}
   1.304 +	updatediskgraph week ${disk:0:8}
   1.305 +	updatediskgraph month ${disk:0:8}
   1.306 +	updatediskgraph year ${disk:0:8}
   1.307 +fi
   1.308 +
   1.309 +iface=$(/sbin/route -n | awk '{ if (/^0.0.0.0/) print $8 }')
   1.310 +updateifdata $iface
   1.311 +updateifgraph $iface day
   1.312 +updateifgraph $iface week
   1.313 +updateifgraph $iface month
   1.314 +updateifgraph $iface year