lm_sensors to MySQL Script

Author: Matt Kava
Dependencies: lm_sensors utility (The lm_sensors package includes a collection of modules for general SMBus: access and hardware monitoring.) Website: http://www.lm-sensors.org/
Created on: Fedora Core 13, v3.1.2 of lm_sensors
License: Creative Commons License Use it and share it.

The Purpose: Log information directly from the utilities output into a MySQL table using bash. No real reason for the script other than “Can I do it?” and “I wonder if I can analyse/graph the data later..”. This is not going to be supported. What you see is what you get.

The Script:

#! /bin/bash
# Takes the output from /usr/bin/sensors, parses it into temperate and fan statistics, and inserts it into a local MySQL database/table.
# Author: Matt Kava of http://mattkava.com

locsen=”/usr/bin/sensors” # The location of the sensors utility
sensors=”lm_sensors” # The sensors pkg name

# Test if the sensors pkg is installed
rp=`/bin/rpm -q “$sensors”| grep not`
if [ ${#rp} -ne 0 ]; then
echo “The pkg $sensors is not installed. Please install it. Exiting.”
return 1
fi

sql=`/sbin/service mysqld status| grep stopped`
if [ ${#sql} -ne 0 ]; then
echo “MySQLd is not started. Cannot log any information. Exiting.”
return 1
fi

# ready information
c1=0
c2=0
t1=0
t2=0
f1=0
f2=0

cores=`”$locsen”|grep Core`
fans=`”$locsen”|grep fan`
temps=`”$locsen”|grep thermistor`

#echo -e “Temps isn’$temps’”
#echo -e “Cores isn’$cores’”
#echo -e “Fans isn’$fans’”

# 1) Concat the string
# 2) Get everything after the :
# 3) Get everything before the R in RPM
# 4) Remove the leading whitespace
# 5) Get everything prior to the first whitespace
# -) Each line is a number
fan=`echo “$fans” | cut -d ‘:’ -f 2 | cut -d ‘R’ -f 1 | sed -e ‘s/^[ t]*//’ | cut -d ‘ ‘ -f 1`

fan=`echo “$fan” | tr ‘n’ ‘|’`
f1=`echo “$fan” | cut -d ‘|’ -f 1`
f2=`echo “$fan” | cut -d ‘|’ -f 2`

# 2) Get everything before the degree symbol
# 3) Get everything after the + symbol
# -) Each line is a number
core=`echo “$cores” | cut -d ‘:’ -f 2 | cut -d ‘C’ -f 1 | cut -d ‘+’ -f 2 | cut -c 1-4`
core=`echo “$core” | tr ‘n’ ‘|’`
c1=`echo “$core” | cut -d ‘|’ -f 1`
c2=`echo “$core” | cut -d ‘|’ -f 2`

# 2) Get everything before the degree symbol
# 3) Get everything after the + symbol
# -) Each line is a number
temp=`echo “$temps” | cut -d ‘C’ -f 1 | cut -d ‘+’ -f 2 | cut -c 1-4`
temp=`echo “$temp” | tr ‘n’ ‘|’`
t1=`echo “$temp” | cut -d ‘|’ -f 1`
t2=`echo “$temp” | cut -d ‘|’ -f 2`

#echo -e “t1=$t1tt2=$t2nc1=$c1tc2=$c2nf1=$f1tf2=$f2″
#exit 0

# —

# Table structure
#CREATE TABLE IF NOT EXISTS `sensor_stats` (
#  `id` int(16) NOT NULL AUTO_INCREMENT,
#  `curtime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
#  `core1` float(3,1) NOT NULL DEFAULT ’0.0′,
#  `core2` float(3,1) NOT NULL DEFAULT ’0.0′,
#  `temp1` float(3,1) NOT NULL DEFAULT ’0.0′,
#  `temp2` float(3,1) NOT NULL DEFAULT ’0.0′,
#  `fan1` int(4) NOT NULL DEFAULT ’0′,
#  `fan2` int(4) NOT NULL DEFAULT ’0′,
#  PRIMARY KEY (`id`)
#) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=0 ;

db=”DATABASE”
user=”USER”
pass=”PASSWORD”
table=”sensors_stats”
cols=”(core1, core2, temp1, temp2, fan1, fan2)”
vals=”values(‘$c1′, ‘$c2′, ‘$t1′, ‘$t2′, ‘$f1′, ‘$f2′)”

auth=”-u$user -p$pass”
# Perform the query. What is written is exactly what will happen.
mysql $auth <<QUERY
use $db;
insert into $table $cols $vals;
exit
QUERY

This entry was posted in DevBlog and tagged , , . Bookmark the permalink.

Leave a Reply