#!/bin/sh
#------------------------------------------------------------------------------
# OPT_EMBEDDED  /etc/rrdtool/p640.val                              __FLI4LVER__
# Creation:     2005-12-23 gdw
# Last Update:  $Id$
# 
# rrdtool p640 Datenerfassung
#------------------------------------------------------------------------------

[ -z "$RDTOOL_CONF_LOADED" ] && . /var/run/rrdtool.conf

local source=p640
eval local idx='$RRDTOOL_'$source'_ID'
eval local options='$RRDTOOL_'$idx'_OPTIONS_STRING'

p640_io_path='/sys/class/hwmon/hwmon0/device'

for item in $options
do
    local db_file=$RRDTOOL_DB_PATH/$source/$item.rrd
    case "$item" in
    "cpuspeed")
        value1=''
        [ -f /var/run/scaling_cur_freq ]&&value1=`sed 's/\([0-9]\{3\}\)$//' /var/run/scaling_cur_freq`
        if [ -n "$value1" ]
        then
          value1=`expr $value1 + 3`
        else
          value1=`sed -n 's/cpu MHz.*: //p' /proc/cpuinfo | sed 's/\..*//'`
        fi
        rrdtool update $db_file N:$value1
        ;;
    "fan") # Normally not useable as connectors not soldiered in
        [ -f $p640_io_path/fan1_input ]&&value1=`cat $p640_io_path/fan1_input`
        [ -f $p640_io_path/fan2_input ]&&value2=`cat $p640_io_path/fan2_input`
        rrdtool update $db_file N:$value1:$value2
        ;;
    "temperature")
        value1=`cat /sys/class/hwmon/hwmon0/device/temp1_input | sed 's/^\([0-9]\+\)\([0-9]\{3\}\)/\1.\2/'`
        value2=`cat /sys/class/hwmon/hwmon0/device/temp2_input | sed 's/^\([0-9]\+\)\([0-9]\{3\}\)/\1.\2/'`
        rrdtool update $db_file \
        N:$value1:$value2
        ;;
    "voltage")
	V5=`cat /sys/class/hwmon/hwmon0/device/in3_input`
	V12=`cat /sys/class/hwmon/hwmon0/device/in4_input`
	VCC=`cat /sys/class/hwmon/hwmon0/device/in7_input`
	value0=`cat /sys/class/hwmon/hwmon0/device/in0_input | sed 's/^\([0-9]\)\([0-9]\)/\1.\2/'`
	value2=`cat /sys/class/hwmon/hwmon0/device/in2_input | sed 's/^\([0-9]\)\([0-9]\)/\1.\2/'`
	value3=`expr $V5 \* 168 | sed 's/^\([0-9]\)\([0-9]\{3\}\).*/\1.\2/'`
	value1=`expr $V12 \* 380 | sed 's/^\([0-9]\{2\}\)\([0-9]\{3\}\).*/\1.\2/'`
	value4=`expr $VCC \* 168 | sed 's/^\([0-9]\)\([0-9]\{3\}\).*/\1.\2/'`
	value5=`cat /sys/class/hwmon/hwmon0/device/in8_input | sed 's/^\([0-9]\)\([0-9]\)/\1.\2/'`
        rrdtool update $db_file \
        N:$value0:$value1:$value2:$value3:$value4:$value5
        ;;
    esac
done
