3

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
2

Well, the simplest approach would be to just grab the last line:

sensors | grep temp1 | tail -n 1 | awk '{print int($3)}' 

tail -n 1 prints the last line of the file.

or

sensors | tac | grep -m 1 temp1 | awk '{print int($3)}' 

tac reverses it's input so the 1st line is now the last. That means that the first match for temp1 is the one you care about and since grep -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 in awk:

sensors | awk '/temp1/{k=int($2)}END{print k}

The idea here is that each time a line matches temp1, k is set to int($2). However, k is only printed in the END{} block which is executed after the rest of the file has been processed so only the last value found will be printed.

  • This was a the class and you was the teacher! very good answer, every choice worked like a charm! just a append is that some times I had to use '{print int($2)}' because it was returning me just 0 with $3` – André M. Faria Jul 3 '14 at 17:49
2

The easiest answer is to install mrtgutils-sensors, which includes the mrtg-sensors package that automatically parses the output of sensors.

mrtg-sensors radeon-pci-0100 temp1 

will give you the correct answer.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.