#!/bin/bash
##########################################################################
#
#  gather.sh          --  gather.sh for the local variation
#                         of tellerstats
#
#  $Id: gather.sh,v 1.1.0.12 2004/09/14 03:59:16 floyd Exp floyd $
#
#  See file ChangeLog for the RCS history.
#
##########################################################################
#
#  Copyright 2004 by Floyd Davidson, floyd@barrow.com
#  File created:  Mon Sep  6 03:57:49 2004
#  Last updated:  Sat Sep 11 18:58:25 2004
#
##########################################################################
#
#    gather.sh
#	gather the data.
#	run from cron every 5 minutes.
#
#    Copyright 2001 The lm_sensors group
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

# generic tellerstats init BEGIN

PATH=/usr/local/sbin:/usr/X11R6/bin:/bin:/bin/sbin:/usr/bin/:/usr/sbin/:/usr/local/bin

# get config information from /etc/tellerstats.conf or whereever we are pointed

if [ -z "$TELLERSTATS_CONF" ]
then
   TELLERSTATS_CONF=/etc/tellerstats.conf
fi   

export TELLERSTATS_CONF

if [ ! -r $TELLERSTATS_CONF ]
then
   echo "$0: Could not find config file $TELLERSTATS_CONF"
   exit 1
fi   

. $TELLERSTATS_CONF

if [ ! -d $DBPATH ]
then
   echo "$0: data directory $DBPATH does not exist"
   exit 1
fi

if [ ! -d $SENSORPATH1 ]
then
   echo "$0: sensor information directory $SENSORPATH1 does not exist."
   exit 1
fi

if [ ! -d $SENSORPATH2 ]
then
   echo "$0: sensor information directory $SENSORPATH2 does not exist."
   exit 1
fi

if [ ! -d $HTMLROOT ]
then
   echo "$0: The root of your webserver - $HTMLROOT - does not exist..bailing out"
   exit 1
fi

if [ ! -d $HTMLPATH ]
then
   echo "$0: The place where we keep HTML files and pictures - $HTMLPATH - does not exist..bailing out"
   exit 1
fi

if [ ! -r $GNUPLOTSCRIPT_TMPL ]
then
   echo "$0: The gnuplot script template $GNUPLOTSCRIPT_TMPL does not exist..bailing out"
   exit 1
fi

export DBPATH SENSORPATH1 SENSORPATH2 TEMPPATH HTMLROOT HTMLPATH GNUPLOTSCRIPT_TMPL

if [ -n "$DEBUG" ]
then
   echo "DBPATH = $DBPATH"
   echo "SENSORPATH1 = $SENSORPATH1"
   echo "SENSORPATH2 = $SENSORPATH2"
   echo "TEMPPATH = $TEMPPATH"
   echo "HTMLROOT = $HTMLROOT"
   echo "HTMLPATH = $HTMLPATH"
   echo "GNUPLOTSCRIPT_TMPL = $GNUPLOTSCRIPT_TMPL"
fi

# generic tellerstats init END

#date=yyyyMMddHHmmss, the same format gnuplot expects for the x-axis

# W83782D  in2   AGP voltage      1.50
DATE=$(date +%Y%m%d%H%M%S)
T=$(cat $SENSORPATH1/in2_input   | awk '{print $1}')
T=$((echo 'scale=3'; echo "($T / 1000)") | /usr/bin/bc)
echo $DATE $T >> $DBPATH/agpv

# W83782D  in3   +5 voltage       5.00
T=$(cat $SENSORPATH1/in3_input   | awk '{print $1}')
T=$((echo 'scale=3'; echo "($T / 595) + 0.15") | /usr/bin/bc)
echo $DATE $T >> $DBPATH/pos5v

# W83782D  in4   DDR voltage      2.50
T=$(cat $SENSORPATH1/in4_input   | awk '{print $1}')
T=$((echo 'scale=3'; echo "($T / 500) + 0.05") | /usr/bin/bc)
echo $DATE $T >> $DBPATH/ddrv

# W83782D  in6   3VSB voltage     3.30
T=$(cat $SENSORPATH1/in6_input   | awk '{print $1}')
T=$((echo 'scale=3'; echo "($T / 1000)") | bc)
echo $DATE $T >> $DBPATH/3vsb

# W83782D  in8   Bat voltage      3.00
T=$(cat $SENSORPATH1/in8_input   | awk '{print $1}')
T=$((echo 'scale=3'; echo "($T / 229.3)") | bc)
echo $DATE $T >> $DBPATH/vbat

# W83627HF in0   Vcore 1 voltage  1.76
T=$(cat $SENSORPATH2/in0_input   | awk '{print $1}')
T=$((echo 'scale=3'; echo "($T / 1000) + 0.008") | bc)
echo $DATE $T >> $DBPATH/vcore1

# W83627HF in1   Vcore 2 voltage  1.76
T=$(cat $SENSORPATH2/in1_input   | awk '{print $1}')
T=$((echo 'scale=3'; echo "($T / 1000) + 0.008") | bc)
echo $DATE $T >> $DBPATH/vcore2

# W83627HF in2   +3.3 voltage     3.30
T=$(cat $SENSORPATH2/in2_input   | awk '{print $1}')
T=$((echo 'scale=3'; echo "($T /1000)") | bc)
echo $DATE $T >> $DBPATH/pos33v

# W83627HF in4   +12 voltage      12.0    ((28/10)+1)*@ , @/(-1*(28/10)+1)
T=$(cat $SENSORPATH2/in4_input   | awk '{print $1}')
T=$((echo 'scale=3'; echo "($T / 263) - 0.2") | bc)
echo $DATE $T >> $DBPATH/pos12v

# W83627HF in5   -12 voltage      -12.0    (5.14*@)-14.91 , (@+14.91)/5.14
T=$(cat $SENSORPATH2/in5_input   | awk '{print $1}')
T=$((echo 'scale=3'; echo "($T / 195.6) - 14.31") | bc)
echo $DATE $T >> $DBPATH/neg12v

# W83782D  temp1 VRM2
T=$(cat $SENSORPATH1/temp1_input | awk '{print $1}')
T=$((echo 'scale=1'; echo "$T / 1000") | bc)
echo $DATE $T >> $DBPATH/vrm2_temp

# W83782D  temp2 CPU1
T=$(cat $SENSORPATH1/temp2_input | awk '{print $1}')
T=$((echo 'scale=1'; echo "$T / 1000") | bc)
echo $DATE $T >> $DBPATH/cpu1_temp

# W83782D  temp2 CPU2
T=$(cat $SENSORPATH1/temp3_input | awk '{print $1}')
T=$((echo 'scale=1'; echo "$T / 1000") | bc)
echo $DATE $T >> $DBPATH/cpu2_temp

# W83627HF temp2 VRM1
T=$(cat $SENSORPATH2/temp1_input | awk '{print $1}')
T=$((echo 'scale=1'; echo "$T / 1000") | bc)
echo $DATE $T >> $DBPATH/vrm1_temp

# W83627HF temp2 AGP
T=$(cat $SENSORPATH2/temp2_input | awk '{print $1}')
T=$((echo 'scale=1'; echo "$T / 1000") | bc)
echo $DATE $T >> $DBPATH/agp_temp

# W83627HF temp3 DDR
T=$(cat $SENSORPATH2/temp3_input | awk '{print $1}')
T=$((echo 'scale=1'; echo "$T / 1000") | bc)
echo $DATE $T >> $DBPATH/ddr_temp

# W83782D  fan1 CPU1 Fan
T=$(cat $SENSORPATH1/fan1_input  | awk '{print $1}')
echo $DATE $T >> $DBPATH/cpu1_fan

# W83782D  fan2 CPU2 Fan
T=$(cat $SENSORPATH1/fan2_input  | awk '{print $1}')
echo $DATE $T >> $DBPATH/cpu2_fan

#T=$(cat $SENSORPATH1/fan3_input  | awk '{print $1}')
#echo $DATE $T >> $DBPATH/chs1_fan

#T=$(cat $SENSORPATH2/fan1_input  | awk '{print $1}')
#echo $DATE $T >> $DBPATH/chs3_fan

#T=$(cat $SENSORPATH2/fan2_input  | awk '{print $1}')
#echo $DATE $T >> $DBPATH/chs5_fan

#T=$(cat $SENSORPATH2/fan3_input  | awk '{print $1}')
#echo $DATE $T >> $DBPATH/chs6_fan

#T=$(cat $SENSORPATH1/in2_input   | perl -p -e 's/^.+ ([^ ]+)$/$1/')
T=$(cat /proc/loadavg | awk '{print $1}')
echo $DATE $T >> $DBPATH/load

exit 0