#!/bin/bash 

###  $Header: /var/lib/cvs/ops/ganglia/sensors_gmetric.sh,v 1.3 2006/07/11 17:29:27 ben Exp $

###  this script reports thermal sensors metrics to ganglia.
###  It should be called from cron every n minutes.
###  It will report blocks per second on each disk,
###  and will automatically adjust for whatever 
###  timeframe it is called
###  It is written for the Tyan motherboard, but may work
###  for other boards as well.

###  Copyright Simply Hired, Inc. 2006
###  License to use, modify, and distribute under the GPL
###  http://www.gnu.org/licenses/gpl.txt

VERSION=1.3

GMETRIC="/usr/bin/gmetric"
GMETRIC_ARGS="-c /etc/gmond.conf"
STATEFILE="/var/lib/ganglia/metrics/sensors.stats"
date=`date +%s`
sensors="/usr/bin/sensors"
chipset="adm1027-i2c-0-2e"

ERROR_CREATE="/tmp/sensors_gmetric_create_statefile_failed"
ERROR_SENSOR="/tmp/sensors_gmetric_no_sensors"
ERROR_DEVNAMES="/tmp/sensors_gmetric_bad_devname"
ERROR_DEVNAMES2="/tmp/sensors_gmetric_bad_devname_didnt_fix"
ERROR_GMETRIC="/tmp/sensors_gmetric_no_gmetric"
ERROR_TIMEDIFF="/tmp/sensors_gmetric_timediff"
ERROR_NOTROOT="/tmp/sensors_gmetric_notroot"

if [ $UID -ne 0 ]
then
  if [ -e $ERROR_NOTROOT ] ; then exit 1; fi
  echo "Error: this script must be run as root."
  touch $ERROR_NOTROOT
  exit 1
fi
rm -f $ERROR_NOTROOT

if [ "x$1" == "x-h" ]
then
  echo "Usage: sensors_gmetric.sh [--clean]"
  echo "  --clean	delete all tmp files"
  exit 0
fi

if [ "x$1" == "x--clean" ]
then
  rm -f $ERROR_CREATE $ERROR_SENSOR $ERROR_DEVNAME $ERROR_DEVNAME2 $ERROR_GMETRIC $ERROR_TIMEDIFF $ERROR_NOTROOT $STATEFILE
  retval=$?
  if [ $retval -ne 0 ]
  then
    echo "failed to clean up."
    exit 1
  else
    echo "All cleaned up."
    exit 0
  fi
fi

# save and turn off /STDERR for th estatefile tests
exec 3>&2
exec 2>/dev/null

# if the GMETRIC program isn't installed, compain
if [ ! -e $GMETRIC ]
then
  if [ -e $ERROR_GMETRIC ] ; then exit 1; fi
  echo ""
  echo "Error: GMETRIC doesn't seem to be installed."
  echo "$GMETRIC doesn't exist."
  echo ""
  touch $ERROR_GMETRIC
  exit 1
fi

# if the iostat program isn't installed, compain
if [ ! -e $sensors ]
then
  if [ -e $ERROR_SENSOR ]
  then
    exit 1
  fi
  echo ""
  echo "Error: sensors doesn't seem to be installed."
  echo "$sensors doesn't exist."
  echo ""
  touch $ERROR_SENSOR
  exit 1
fi

# if the statefile doesn't exist, we either havn't 
# run yet or there's something bigger wrong.
if [ ! -e $STATEFILE ]
then
  if [ ! -d `dirname $STATEFILE` ]
  then
    mkdir -p `dirname $STATEFILE`
  fi
  echo "$date" > $STATEFILE 
#  $iostat -d | tail +4 >> $STATEFILE 
  if [ ! -e $STATEFILE ]
  then
    # if it didn't exist and we couldn't create
    #  it, we should just scream bloody murder and die.
    #  only scream once though...
    if [ -e $ERROR_CREATE ]
    then
      exit 1
    fi
    echo ""
    echo "ERROR: couldn't create $STATEFILE"
    echo ""
    touch $ERROR_CREATE
    exit 1
  fi
  echo "Created statefile.  Exitting."
  exit 0
fi
 
# restore stderr
exec 2>&3
exec 3>&-

# this script uses iostat (part of the sysstat packag) 
# to retrieve disk metrics
#stats=(`$sensors $chipset`)
#old_stats=(`cat $STATEFILE`) 
#old_date=${old_stats[0]}

temps=(`sensors adm1027-i2c-0-2e | grep "Temp" | cut -b 12-16`)
cpu1temp=${temps[0]}
cpu2temp=${temps[2]}
systemp=${temps[1]}

$GMETRIC $GMETRIC_ARGS --name="temp_cpu1" --value="$cpu1temp" --type="float" --units="degrees centigrade"
$GMETRIC $GMETRIC_ARGS --name="temp_cpu2" --value="$cpu2temp" --type="float" --units="degrees centigrade"
$GMETRIC $GMETRIC_ARGS --name="temp_sys" --value="$systemp" --type="float" --units="degrees centigrade"

volts=(`sensors adm1027-i2c-0-2e | grep "V" | cut -b 12-17 | sed -e "s/+//"`)
ddr25=${volts[0]}
ddrvtt=${volts[1]}
three=${volts[2]}
five=${volts[3]}
twelve=${volts[4]}
vid=${volts[5]}

$GMETRIC $GMETRIC_ARGS --name="volts_ddr25" --value="$ddr25" --type="float" --units="volts"
$GMETRIC $GMETRIC_ARGS --name="volts_ddrvtt" --value="$ddrvtt" --type="float" --units="volts"
$GMETRIC $GMETRIC_ARGS --name="volts_three" --value="$three" --type="float" --units="volts"
$GMETRIC $GMETRIC_ARGS --name="volts_five" --value="$five" --type="float" --units="volts"
$GMETRIC $GMETRIC_ARGS --name="volts_twelve" --value="$twelve" --type="float" --units="volts"
$GMETRIC $GMETRIC_ARGS --name="volts_vid" --value="$vid" --type="float" --units="volts"

echo "$date" > $STATEFILE

rm -f $ERROR_CREATE $ERROR_SENSOR $ERROR_DEVNAME2 $ERROR_DEVNAME $ERROR_GMETRIC $ERROR_TIMEDIFF $ERROR_NOTROOT

