#!/bin/sh

dir=/var/www/localhost/htdocs/rrdtool/

snmp_reply_row=`snmpwalk -v1 -c public localhost ssCpuRawUser`
snmp_reply_array=( $snmp_reply_row )
cpu_user=${snmp_reply_array[${#snmp_reply_array[*]}-1]}

snmp_reply_row=`snmpwalk -v1 -c public localhost ssCpuRawSystem`
snmp_reply_array=( $snmp_reply_row )
cpu_sys=${snmp_reply_array[${#snmp_reply_array[*]}-1]}

snmp_reply_row=`snmpwalk -v1 -c public localhost ssCpuRawNice`
snmp_reply_array=( $snmp_reply_row )
cpu_nice=${snmp_reply_array[${#snmp_reply_array[*]}-1]}

if [ -z $cpu_user ] ; then
	cpu_user=0
fi
if [ -z $cpu_sys ] ; then
	cpu_sys=0
fi
if [ -z $cpu_nice ] ; then
	cpu_nice=0
fi

rrdtool update "$dir"rrd/cpu.rrd N:$cpu_user:$cpu_sys:$cpu_nice

start_hour=$((`date +%s`-7200))
start_day=$((`date +%s`-86400))
start_week=$((`date +%s`-604800))
start_month=$((`date +%s`-2678400))
start_year=$((`date +%s`-31536000))

rrdtool graph "$dir"img/cpu_hour.png \
--title="Dvou-hodinový graf" \
--x-grid MINUTE:5:MINUTE:15:MINUTE:15:0:%H:%M \
--start $start_hour \
--end N \
--vertical-label "Využití CPU [%]" \
--watermark http://pat.kk.zcu.cz \
--width 800 \
--height 120 \
--rigid \
--upper-limit 100 \
--lower-limit 0 \
DEF:cpu_user="$dir"rrd/cpu.rrd:cpu_user:AVERAGE \
DEF:cpu_sys="$dir"rrd/cpu.rrd:cpu_cmpl1:AVERAGE \
DEF:cpu_nice="$dir"rrd/cpu.rrd:cpu_cmpl2:AVERAGE \
CDEF:realcpu_sys=cpu_sys,2,\/ \
CDEF:realcpu_nice=cpu_nice,2,\/ \
CDEF:realcpu_user=cpu_user,2,\/ \
CDEF:realcpu_cmpl1=realcpu_nice,realcpu_user,\+ \
CDEF:realcpu_cmpl2=realcpu_cmpl1,realcpu_sys,\+ \
VDEF:realcpu_cmpl2_max=realcpu_cmpl2,MAXIMUM \
VDEF:realcpu_cmpl2_min=realcpu_cmpl2,MINIMUM \
LINE2:realcpu_cmpl2#FF0000:"CPU system" \
GPRINT:realcpu_cmpl2_max:"             Maximum\\: %.lf%%\\n" \
LINE2:realcpu_cmpl1#00FF00:"CPU user" \
GPRINT:realcpu_cmpl2_min:"               Minimum\\: %.lf%%\\n" \
LINE2:realcpu_nice#0000FF:"CPU nice\\n" \

rrdtool graph "$dir"img/cpu_day.png \
--title="Denní graf" \
--x-grid MINUTE:15:HOUR:1:HOUR:3:0:%H:%M \
--start $start_day \
--end N \
--vertical-label "Využití CPU [%]" \
--watermark http://pat.kk.zcu.cz \
--width 800 \
--height 120 \
--rigid \
--upper-limit 100 \
--lower-limit 0 \
DEF:cpu_user="$dir"rrd/cpu.rrd:cpu_user:AVERAGE \
DEF:cpu_sys="$dir"rrd/cpu.rrd:cpu_cmpl1:AVERAGE \
DEF:cpu_nice="$dir"rrd/cpu.rrd:cpu_cmpl2:AVERAGE \
CDEF:realcpu_sys=cpu_sys,2,\/ \
CDEF:realcpu_nice=cpu_nice,2,\/ \
CDEF:realcpu_user=cpu_user,2,\/ \
CDEF:realcpu_cmpl1=realcpu_nice,realcpu_user,\+ \
CDEF:realcpu_cmpl2=realcpu_cmpl1,realcpu_sys,\+ \
VDEF:realcpu_cmpl2_max=realcpu_cmpl2,MAXIMUM \
VDEF:realcpu_cmpl2_min=realcpu_cmpl2,MINIMUM \
LINE2:realcpu_cmpl2#FF0000:"CPU system" \
GPRINT:realcpu_cmpl2_max:"             Maximum\\: %.lf%%\\n" \
LINE2:realcpu_cmpl1#00FF00:"CPU user" \
GPRINT:realcpu_cmpl2_min:"               Minimum\\: %.lf%%\\n" \
LINE2:realcpu_nice#0000FF:"CPU nice\\n" \

rrdtool graph "$dir"img/cpu_week.png \
--title="Týdenní graf (5-ti minutový průměr)" \
--x-grid HOUR:6:HOUR:24:HOUR:24:0:"                   %A" \
--start $start_week \
--end N \
--vertical-label "Využití CPU [%]" \
--watermark http://pat.kk.zcu.cz \
--width 800 \
--height 120 \
--rigid \
--upper-limit 100 \
--lower-limit 0 \
DEF:cpu_user="$dir"rrd/cpu.rrd:cpu_user:AVERAGE \
DEF:cpu_sys="$dir"rrd/cpu.rrd:cpu_cmpl1:AVERAGE \
DEF:cpu_nice="$dir"rrd/cpu.rrd:cpu_cmpl2:AVERAGE \
CDEF:realcpu_sys=cpu_sys,2,\/ \
CDEF:realcpu_nice=cpu_nice,2,\/ \
CDEF:realcpu_user=cpu_user,2,\/ \
CDEF:realcpu_cmpl1=realcpu_nice,realcpu_user,\+ \
CDEF:realcpu_cmpl2=realcpu_cmpl1,realcpu_sys,\+ \
VDEF:realcpu_cmpl2_max=realcpu_cmpl2,MAXIMUM \
VDEF:realcpu_cmpl2_min=realcpu_cmpl2,MINIMUM \
LINE2:realcpu_cmpl2#FF0000:"CPU system" \
GPRINT:realcpu_cmpl2_max:"             Maximum\\: %.lf%%\\n" \
LINE2:realcpu_cmpl1#00FF00:"CPU user" \
GPRINT:realcpu_cmpl2_min:"               Minimum\\: %.lf%%\\n" \
LINE2:realcpu_nice#0000FF:"CPU nice\\n" \

rrdtool graph "$dir"img/cpu_month.png \
--title="Měsíční graf (hodinový průměr)" \
--x-grid DAY:1:WEEK:1:WEEK:1:0:"                              Týden "%W \
--start $start_month \
--end N \
--vertical-label "Využití CPU [%]" \
--watermark http://pat.kk.zcu.cz \
--width 800 \
--height 120 \
--rigid \
--upper-limit 100 \
--lower-limit 0 \
DEF:cpu_user="$dir"rrd/cpu.rrd:cpu_user:AVERAGE \
DEF:cpu_sys="$dir"rrd/cpu.rrd:cpu_cmpl1:AVERAGE \
DEF:cpu_nice="$dir"rrd/cpu.rrd:cpu_cmpl2:AVERAGE \
CDEF:realcpu_sys=cpu_sys,2,\/ \
CDEF:realcpu_nice=cpu_nice,2,\/ \
CDEF:realcpu_user=cpu_user,2,\/ \
CDEF:realcpu_cmpl1=realcpu_nice,realcpu_user,\+ \
CDEF:realcpu_cmpl2=realcpu_cmpl1,realcpu_sys,\+ \
VDEF:realcpu_cmpl2_max=realcpu_cmpl2,MAXIMUM \
VDEF:realcpu_cmpl2_min=realcpu_cmpl2,MINIMUM \
LINE2:realcpu_cmpl2#FF0000:"CPU system" \
GPRINT:realcpu_cmpl2_max:"             Maximum\\: %.lf%%\\n" \
LINE2:realcpu_cmpl1#00FF00:"CPU user" \
GPRINT:realcpu_cmpl2_min:"               Minimum\\: %.lf%%\\n" \
LINE2:realcpu_nice#0000FF:"CPU nice\\n" \

rrdtool graph "$dir"img/cpu_year.png \
--title="Roční graf (12-ti hodinový průměr)" \
--start $start_year \
--end N \
--vertical-label "Využití CPU [%]" \
--watermark http://pat.kk.zcu.cz \
--width 800 \
--height 120 \
--rigid \
--upper-limit 100 \
--lower-limit 0 \
DEF:cpu_user="$dir"rrd/cpu.rrd:cpu_user:AVERAGE \
DEF:cpu_sys="$dir"rrd/cpu.rrd:cpu_cmpl1:AVERAGE \
DEF:cpu_nice="$dir"rrd/cpu.rrd:cpu_cmpl2:AVERAGE \
CDEF:realcpu_sys=cpu_sys,2,\/ \
CDEF:realcpu_nice=cpu_nice,2,\/ \
CDEF:realcpu_user=cpu_user,2,\/ \
CDEF:realcpu_cmpl1=realcpu_nice,realcpu_user,\+ \
CDEF:realcpu_cmpl2=realcpu_cmpl1,realcpu_sys,\+ \
VDEF:realcpu_cmpl2_max=realcpu_cmpl2,MAXIMUM \
VDEF:realcpu_cmpl2_min=realcpu_cmpl2,MINIMUM \
LINE2:realcpu_cmpl2#FF0000:"CPU system" \
GPRINT:realcpu_cmpl2_max:"             Maximum\\: %.lf%%\\n" \
LINE2:realcpu_cmpl1#00FF00:"CPU user" \
GPRINT:realcpu_cmpl2_min:"               Minimum\\: %.lf%%\\n" \
LINE2:realcpu_nice#0000FF:"CPU nice\\n" \

#rrdtool create /var/www/localhost/htdocs/rrdtool/rrd/cpu.rrd \
#--step 60 \
#--start N \
#DS:cpu_user:COUNTER:180:0:200 \
#DS:cpu_cmpl1:COUNTER:180:0:200 \
#DS:cpu_cmpl2:COUNTER:180:0:200 \
#RRA:AVERAGE:0.5:1:1450 \
#RRA:MIN:0.5:1450:1 \
#RRA:MAX:0.5:1450:1 \
#RRA:AVERAGE:0.5:5:2050 \
#RRA:MIN:0.5:10250:1 \
#RRA:MAX:0.5:10250:1 \
#RRA:AVERAGE:0.5:60:750 \
#RRA:MIN:0.5:45000:1 \
#RRA:MAX:0.5:45000:1 \
#RRA:AVERAGE:0.5:720:730 \
#RRA:MIN:0.5:525600:1 \
#RRA:MAX:0.5:525600:1
