I'm using MRTG to grab data from sensors of a ubuntu server 12.04, with this script.
#!/bin/bash
SENSORS=/usr/bin/sensors
UPTIME=$(uptime | awk -F, '{print $3}' )
TEXT="Graphic Card Temperature"
GPCTEMP1=$( ${SENSORS} | grep "temp1" | awk '{print int($3)}' )
# http://people.ee.ethz.ch/~oetiker/webtools/mrtg/reference.html
# "The external command must return 4 lines of output:
# Line 1
# current state of the first variable, normally 'incoming bytes count'
# Line 2
# current state of the second variable, normally 'outgoing bytes count'
# Line 3
# string (in any human readable format), telling the uptime of the target.
# Line 4
# string, telling the name of the target. "
echo ${GPCTEMP1}
echo ${GPCTEMP1}
echo ${UPTIME}
echo ${TEXT}
Unfortunally there is two "temp1" when I run sensors there is two sensors with the name "temp1"
/etc/mrtg/cfg/mrtg-scripts$ sensors
adt7490-i2c-0-2e
Adapter: SMBus I801 adapter at f000
in0: +1.12 V (min = +0.00 V, max = +3.31 V)
Vcore: +1.09 V (min = +0.00 V, max = +2.99 V)
+3.3V: +3.25 V (min = +2.96 V, max = +3.61 V)
+5V: +5.03 V (min = +4.48 V, max = +5.50 V)
+12V: +11.90 V (min = +0.00 V, max = +15.69 V)
in5: +2.10 V (min = +0.00 V, max = +4.48 V)
fan1: 1312 RPM (min = 0 RPM)
fan2: 0 RPM (min = 0 RPM)
fan3: 0 RPM (min = 0 RPM)
fan4: 0 RPM (min = 0 RPM)
temp1: +38.5°C (low = +5.0°C, high = +65.0°C)
(crit = +70.0°C, hyst = +66.0°C)
M/B Temp: +39.8°C (low = +5.0°C, high = +65.0°C)
(crit = +70.0°C, hyst = +66.0°C)
temp3: +42.2°C (low = +5.0°C, high = +65.0°C)
(crit = +70.0°C, hyst = +66.0°C)
coretemp-isa-0000
Adapter: ISA adapter
Core 0: +59.0°C (high = +74.0°C, crit = +100.0°C)
Core 1: +55.0°C (high = +74.0°C, crit = +100.0°C)
Core 2: +55.0°C (high = +74.0°C, crit = +100.0°C)
Core 3: +57.0°C (high = +74.0°C, crit = +100.0°C)
radeon-pci-0100
Adapter: PCI adapter
temp1: +60.5°C
I want to grab the information of the radeon-pci-0100, but how can I do that?
This is the result when I use sensors
with grep
/etc/mrtg/cfg/mrtg-scripts$ sensors | grep "temp1"
temp1: +38.8°C (low = +5.0°C, high = +65.0°C)
temp1: +60.5°C
Well, the simplest approach would be to just grab the last line:
tail -n 1
prints the last line of the file.or
tac
reverses it's input so the 1st line is now the last. That means that the first match fortemp1
is the one you care about and sincegrep -m 1
will print only the first match, that's what you will get.Personally, since you're using
awk
already, I'd do the whole thing inawk
:The idea here is that each time a line matches
temp1
,k
is set toint($2)
. However,k
is only printed in theEND{}
block which is executed after the rest of the file has been processed so only the last value found will be printed.The easiest answer is to install
mrtgutils-sensors
, which includes themrtg-sensors
package that automatically parses the output of sensors.will give you the correct answer.