#!/bin/sh # # $VER: cpuGuardian.sh 0.6 (28.6.2005) Riccardo "VIC" Torrini # tmp=/tmp/tmp_$$ debug= trap 'Break' 1 2 15 CleanUp () { rm -f ${tmp}* } Break () { echo "${Program}: ** User Break" CleanUp exit 2 } Debug () { [ ${debug} ] && echo "DEBUG: ${1}" } Error () { echo "** Error ! ${1}" CleanUp exit 2 } Warning () { echo "* Warning ! ${1}" CleanUp exit 1 } Panic () { echo "** Internal error ! ${1}" CleanUp exit 3 } Message () { echo "* ${1}" return 1 } isNotNull () { [ "_${2}" = "_" ] && Error "${1}" } CheckFile () { isNotNull "Invalid parameter to CheckFile()" "${1}" if [ ! -f ${1} ] then if [ "_${2}" = "_" ] then Error "File not found (${1})" else Error "${2}" fi fi } CheckDir () { isNotNull "Invalid parameter to CheckDir()" "${1}" if [ ! -d ${1} ] then if [ "_${2}" = "_" ] then Error "Directory not found (${1})" else Error "${2}" fi fi } FileNotFound () { Message "File not found (`basename ${1}`)" } DirectoryNotFound () { Message "Directory not found (${1})" } FileExist () { Message "File exist (`basename ${1}`)" } # Main PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin:/usr/local/vbin dbHome=/var/db/rrd db=${dbHome}/cpu.rrd images=/home/web/data/stats/cpu host=`hostname` CheckDir ${images} CheckDir ${dbHome} cd ${dbHome} case ${1:-NULL} in NULL) Error "missing command" ;; createAndInitializeDB) test -f ${db} && Error "we are on-air, don't try to destroy existing db (${db})" rrdtool create ${db} \ --start `date -v2000y -v6m -v21d -v0H -v0M -v0S +%s` \ --step 300 \ DS:cpuHeat:GAUGE:600:0:200 \ DS:fan0Speed:GAUGE:600:0:9000 \ DS:fan1Speed:GAUGE:600:0:9000 \ DS:fan2Speed:GAUGE:600:0:9000 \ DS:Vcore:GAUGE:600:0:5 \ DS:cpuLoad:GAUGE:600:U:U \ DS:swapFree:GAUGE:600:0:100 \ DS:rootFree:GAUGE:600:0:100 \ DS:varFree:GAUGE:600:0:100 \ DS:tmpFree:GAUGE:600:0:100 \ DS:homeFree:GAUGE:600:0:100 \ DS:usrFree:GAUGE:600:0:100 \ RRA:AVERAGE:0.5:1:600 \ RRA:AVERAGE:0.5:12:840 \ RRA:AVERAGE:0.5:288:900 \ RRA:MAX:0.5:1:600 \ RRA:MAX:0.5:12:840 \ RRA:MAX:0.5:288:900 \ RRA:LAST:0.5:1:600 \ RRA:LAST:0.5:12:840 \ RRA:LAST:0.5:288:900 ;; collect) CheckFile ${db} "db not found (${db})" rrdtool update ${db} N:0:0:0:0:0:`uptime | awk '{gsub(",", ""); print $(NF-2)}'`:`swapinfo | awk '/Interleaved/ {gsub("%", ""); print $(NF-1)}'`:`df | awk '{gsub("%", "", $5); if($6 == "/") $6 = "/root"; gsub("/", "", $6); used[$6] = $5;} END {printf( "%d:%d:%d:%d:%d\n", used["root"], used["var"], used["tmp"], used["home"], used["usr"] )}'` ;; dump) ;; reload) ;; daily|weekly|monthly|yearly) CheckFile ${db} "db not found (${db})" type=${1:-NULL} case ${type} in daily) dfrom="-s `date -v-1d +%s`" dend="" ;; weekly) dfrom="-s `date -v-1w +%s`" dend="" ;; monthly) dfrom="-s `date -v-1m +%s`" dend="" ;; yearly) dfrom="-s `date -v-12m +%s`" dend="" ;; esac ##thisCanvas="--imgformat PNG --width 400 --height 100" ##thisGeometry="--lower-limit 0 --base 1000 --upper-limit 100 --rigid" ##thisRange="--start -24h --end now" thisCanvas="--imgformat PNG" thisGeometry="" thisRange="${dfrom} ${dend}" rrdtool graph ${images}/${type}-load.png \ ${thisCanvas} ${thisGeometry} ${thisRange} \ --title "${host} cpuLoad (${type})" \ --vertical-label load \ DEF:my=${db}:cpuLoad:AVERAGE \ LINE2:my#ff0000 rrdtool graph ${images}/${type}-disk.png \ ${thisCanvas} ${thisGeometry} ${thisRange} \ --title "${host} diskSpace (${type})" \ --vertical-label space \ DEF:swap=${db}:swapFree:AVERAGE \ DEF:root=${db}:rootFree:AVERAGE \ DEF:var=${db}:varFree:AVERAGE \ DEF:tmp=${db}:tmpFree:AVERAGE \ DEF:home=${db}:homeFree:AVERAGE \ DEF:usr=${db}:usrFree:AVERAGE \ LINE2:swap#ff00FF:swap \ LINE2:root#c00000:/root \ LINE2:var#00ff00:/var \ LINE2:tmp#00c000:/tmp \ LINE2:home#0000ff:/home \ LINE2:usr#0000c0:/usr ##GPRINT:swap:MAX:%3.0lf%% ;; help) echo "Usage: ${0} [create|collect|update|dump|reload|daily|weekly|monthly|yearly|help]" exit 1 ;; *) echo "bad command" exit 2 ;; esac exit 0