#!/bin/sh
#*/5  * * * * /usr/share/rrd/makegraphs >/dev/null

# RRD database directory
rrdlog="/var/spool/rrd"

# Images directory
rrdgraph="/var/spool/rrd"

# Colors
#rrdcolors="--color SHADEA#EAE9EE --color SHADEB#EAE9EE --color BACK#EAE9EE" 
rrdcolors="--color SHADEA#FFFFFF --color SHADEB#FFFFFF --color BACK#FFFFFF" 
rrdgraphargs="-aPNG -i -z --alt-y-grid -w 600 -h 100 -r $rrdcolors"

[ -d $rrdlog ] || mkdir -p $rrdlog
[ -d $rrdgraph ] || mkdir -p $rrdgraph

updatecpudata() {
	[ -e "$rrdlog/cpu.rrd" ] || rrdtool create $rrdlog/cpu.rrd --step=300 \
			DS:user:COUNTER:600:0:500000000 \
			DS:system:COUNTER:600:0:500000000 \
			DS:idle:COUNTER:600:0:500000000 \
			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
	grep '^cpu' /proc/stat | while read cpu user nice system idle misc; do
		rrdtool update $rrdlog/cpu.rrd -t user:system:idle \
			N:$(( $user + $nice )):$system:$idle
		break
	done

	[ -e "$rrdlog/cpu2.rrd" ] &&
	grep '^cpu' /proc/stat | while read cpu user nice system idle misc; do
		rrdtool update $rrdlog/cpu2.rrd -t nice:user:system:idle \
			N:$nice:$user:$system:$idle
		break
	done
}

updatecpugraph() {
	period=$1
	info="$(grep '^model name' /proc/cpuinfo | cut -d: -f2 | head -1)"
	rrdtool graph "$rrdgraph/cpu-$period.png" --start -1$period \
		$rrdgraphargs -l 0 -u 100 -t "cpu usage per $period [$info ]" \
		DEF:user=$rrdlog/cpu.rrd:user:AVERAGE \
		DEF:system=$rrdlog/cpu.rrd:system:AVERAGE \
		DEF:idle=$rrdlog/cpu.rrd:idle:AVERAGE \
		'CDEF:total=user,system,idle,+,+' \
		'CDEF:userpct=100,user,total,/,*' \
		'CDEF:systempct=100,system,total,/,*' \
		'CDEF:idlepct=100,idle,total,/,*' \
		'AREA:userpct#0000FF:user cpu usage' \
		'STACK:systempct#FF0000:system cpu usage' \
		'STACK:idlepct#00FF00:idle cpu usage\j'
}


updatememgraph() {
	period=$1
	info="$(free | awk '\
{ \
  if (/Mem:/) { \
	if ($2 < 10000) printf "%d KB",$2; \
	else if ($2 < 10000000) printf "%d MB",$2/1024; \
	else printf "%d GB",$2/1024/1024; \
  } \
}')"
	info2="$(free | awk '\
{ \
  if (/Swap:/) { \
	if ($2 < 10000) printf "%d KB",$2; \
	else if ($2 < 10000000) printf "%d MB",$2/1024; \
	else printf "%d GB",$2/1024/1024; \
  } \
}')"
	rrdtool graph "$rrdgraph/memory-$period.png" --start -1$period \
		$rrdgraphargs -l 0 -u 100 \
		-t "memory usage per $period [ $info + $info2 swap ]" \
		DEF:used=$rrdlog/mem.rrd:memused:AVERAGE \
		DEF:free=$rrdlog/mem.rrd:memfree:AVERAGE \
		DEF:shared=$rrdlog/mem.rrd:memshared:AVERAGE \
		DEF:buffer=$rrdlog/mem.rrd:membuffers:AVERAGE \
		DEF:cache=$rrdlog/mem.rrd:memcache:AVERAGE \
		DEF:swused=$rrdlog/mem.rrd:swapused:AVERAGE \
		DEF:swfree=$rrdlog/mem.rrd:swapfree:AVERAGE \
		'CDEF:total=used,free,+' \
		'CDEF:used2=used,buffer,cache,shared,+,+,-' \
		'CDEF:usedpct=100,used2,total,/,*' \
		'CDEF:sharedpct=100,shared,total,/,*' \
		'CDEF:bufferpct=100,buffer,total,/,*' \
		'CDEF:cachepct=100,cache,total,/,*' \
		'CDEF:freepct=100,free,total,/,*' \
		'CDEF:swtotal=swused,swfree,+' \
		'CDEF:swusedpct=100,swused,swtotal,/,*' \
		'AREA:usedpct#0000FF:used memory' \
		'STACK:sharedpct#FF7F00:shared memory' \
		'STACK:bufferpct#FF00FF:buffered memory' \
		'STACK:cachepct#FFFF00:cached memory' \
		'STACK:freepct#00FF00:free memory' \
		'LINE2:swusedpct#FF0000:used swap\j'
}

updatememdata () {
	[ -e "$rrdlog/mem.rrd" ] ||
		rrdtool create "$rrdlog/mem.rrd" --step=300 \
			DS:memused:ABSOLUTE:600:0:5000000000 \
			DS:memfree:ABSOLUTE:600:0:5000000000 \
			DS:memshared:ABSOLUTE:600:0:5000000000 \
			DS:membuffers:ABSOLUTE:600:0:5000000000 \
			DS:memcache:ABSOLUTE:600:0:5000000000 \
			DS:swapused:ABSOLUTE:600:0:5000000000 \
			DS:swapfree:ABSOLUTE:600:0:5000000000 \
			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460

	while read tag count unit; do
		case "$tag" in
		MemTotal:)  memtotal=$(($count * 1024));;
		MemFree:)   memfree=$(($count * 1024))
			    memused=$(($memtotal - $memfree))
			    memshared=0;;
		MemShared:) memshared=$(($count * 1024));;
		Buffers:)   membuffers=$(($count * 1024));;
		Cached:)    memcache=$(($count * 1024));;
		SwapTotal:) swaptotal=$(($count * 1024));;
		SwapFree:)  swapfree=$(($count * 1024))
			    swapused=$(( $swaptotal - $swapfree));;
		esac
	done < /proc/meminfo

	rrdtool update "$rrdlog/mem.rrd" \
		-t memused:memfree:memshared:membuffers:memcache:swapused:swapfree \
		"N:$memused:$memfree:$memshared:$membuffers:$memcache:$swapused:$swapfree"
}

getmax() {
	rrdtool fetch $rrdlog/$1.rrd AVERAGE | awk '\
BEGIN {max=0} \
/^[0-9]/ { \
   if ($2 != "nan" && $2 > max) max=$2; \
   if ($3 != "nan" && $3 > max) max=$3; \
} \
END { print max }' | sed 's/,/./'
}

updatediskgraph() {
	period=$1
	[ "$period" == "day" ] && maxdisk="$(getmax disk)"
	info=""
	[ -r $2 ] &&
	info="[ $(fdisk -l | grep "^Disk $2:" | \
		  sed "s|Disk $2: \(.*\), .*|\1|") ]"
	rrdtool graph "$rrdgraph/disk-$period.png" --start -1$period \
		$rrdgraphargs -t "disk access per $period $info" \
		--logarithmic --lower-limit 1 -v "Sectors/second" --units=si \
		DEF:read=$rrdlog/disk.rrd:readsect:AVERAGE \
		DEF:write=$rrdlog/disk.rrd:writesect:AVERAGE \
		DEF:req=$rrdlog/iodisk.rrd:req:AVERAGE \
		DEF:done=$rrdlog/iodisk.rrd:done:AVERAGE \
		DEF:err=$rrdlog/iodisk.rrd:err:AVERAGE \
		"CDEF:readpct=100,read,$maxdisk,/,*" \
		"CDEF:writepct=100,write,$maxdisk,/,*" \
		"CDEF:errpct=100,err,req,/,*" \
		"CDEF:donepct=100,done,req,/,*" \
		'AREA:readpct#0000FF:sectors read from disk' \
		'STACK:writepct#00FF00:sectors written to disk' \
		'LINE2:donepct#FFFF00:I/O complete' \
		'LINE2:errpct#FF0000:I/O error\j'
}

updatediskdata() {
	dev=$1
	[ -e "$rrdlog/disk.rrd" ] ||
		rrdtool create "$rrdlog/disk.rrd" --step=300 \
			DS:readsect:COUNTER:600:0:5000000000 \
			DS:writesect:COUNTER:600:0:5000000000 \
			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
	[ -e "$rrdlog/iodisk.rrd" ] ||
		rrdtool create "$rrdlog/iodisk.rrd" --step=300 \
			DS:done:GAUGE:600:0:U  DS:err:GAUGE:600:0:U \
			DS:req:GAUGE:600:0:U \
			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460

	while read major minor name readreq readsect writereq writesect misc; do
		[ $major = $(( 0x$(stat -c %t $dev) )) ] || continue
		[ $minor = $(( 0x$(stat -c %T $dev) )) ] || continue
		rrdtool update "$rrdlog/disk.rrd" -t readsect:writesect \
			N:$readsect:$writesect
	done < /proc/diskstats
	dir=/sys/block/${dev#/dev/}/device
	done=$(printf "%d\n" $(cat $dir/iodone_cnt 2> /dev/null) )
	err=$(printf "%d\n" $(cat $dir/ioerr_cnt 2> /dev/null) )
	req=$(printf "%d\n" $(cat $dir/iorequest_cnt 2> /dev/null) )
	rrdtool update "$rrdlog/iodisk.rrd" -t done:err:req N:$done:$err:$req
}

updateifgraph() {
	interface=$1
	period=$2
	[ "$period" == "day" ] && maxif="$(getmax $interface)"
	rrdtool graph "$rrdgraph/$interface-$period.png" --start -1$period \
		$rrdgraphargs -t "traffic on $interface graph per $period" \
		--logarithmic -A -v "Bytes/second" --units=si \
		DEF:incoming=$rrdlog/$interface.rrd:incoming:AVERAGE \
		DEF:outgoing=$rrdlog/$interface.rrd:outgoing:AVERAGE \
		"CDEF:inpct=100,incoming,$maxif,/,*" \
		"CDEF:outpct=100,outgoing,$maxif,/,*" \
		'AREA:inpct#00FF00:incoming traffic' \
		'LINE1:outpct#0000FF:outgoing traffic\j'
}

netframes() {
ifconfig $1 | grep "$2 packets" | sed -re "s/.*$3:([0-9]+).*/\1/g"
}

netstats() {
ifconfig $1 | grep bytes | sed -re "s/.*$2 bytes:([0-9]+).*/\1/g"
}

updateifdata() {
	interface=$1
	[ -e "$rrdlog/$interface.rrd" ] ||
		rrdtool create "$rrdlog/$interface.rrd" --step=300 \
			DS:incoming:COUNTER:600:0:U \
			DS:outgoing:COUNTER:600:0:U \
			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
	[ -e "$rrdlog/packets-$interface.rrd" ] ||
		rrdtool create "$rrdlog/packets-$interface.rrd" --step=300 \
			DS:in:COUNTER:600:0:U      DS:out:COUNTER:600:0:U \
			DS:inerr:COUNTER:600:0:U   DS:outerr:COUNTER:600:0:U \
			DS:indrop:COUNTER:600:0:U  DS:outdrop:COUNTER:600:0:U \
			DS:inov:COUNTER:600:0:U    DS:outov:COUNTER:600:0:U \
			DS:frame:COUNTER:600:0:U   DS:carrier:COUNTER:600:0:U \
			RRA:AVERAGE:0.5:1:576  RRA:AVERAGE:0.5:6:672 \
			RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460
	rx=$(netstats $interface RX)
	tx=$(netstats $interface TX)
	rrdtool update "$rrdlog/$interface.rrd" -t incoming:outgoing \
		N:${rx:-U}:${tx:-U}
	rx=$(netframes $interface RX packets)
	tx=$(netframes $interface TX packets)
	rxerr=$(netframes $interface RX errors)
	txerr=$(netframes $interface TX errors)
	rxdrop=$(netframes $interface RX dropped)
	txdrop=$(netframes $interface TX dropped)
	rxov=$(netframes $interface RX overruns)
	txov=$(netframes $interface TX overruns)
	frame=$(netframes $interface RX frame)
	carrier=$(netframes $interface TX carrier)
	rrdtool update "$rrdlog/packets-$interface.rrd" \
		-t in:out:inerr:outerr:indrop:outdrop:inov:outov:frame:carrier \
		N:${rx:-U}:${tx:-U}:${rxerr:-U}:${txerr:-U}:${rxdrop:-U}:${txdrop:-U}:${rxov:-U}:${txov:-U}:${frame:-U}:${carrier:-U}
}

getdisk()
{
	local d
	local i
	d=$(stat -c %D $1)
	for i in /dev/* ; do 
		[ $(stat -c "%02t%02T" $i) == $d ] || continue
		echo $i
		break
	done
}

###
### System graphs
###

updatecpudata
updatecpugraph day
updatecpugraph week
updatecpugraph month
updatecpugraph year

updatememdata
updatememgraph day
updatememgraph week
updatememgraph month
updatememgraph year

if [ -e /proc/diskstats ]; then
	disk=$(getdisk $0)
	updatediskdata $disk
	updatediskgraph day ${disk:0:8}
	updatediskgraph week ${disk:0:8}
	updatediskgraph month ${disk:0:8}
	updatediskgraph year ${disk:0:8}
fi

iface=$(/sbin/route -n | awk '{ if (/^0.0.0.0/) print $8 }')
updateifdata $iface
updateifgraph $iface day
updateifgraph $iface week
updateifgraph $iface month
updateifgraph $iface year
