MRTG使用脚本抓取传感器数据

MRTG使用脚本抓取传感器数据

我正在使用此脚本,使用 MRTG 从 ubuntu 服务器 12.04 的传感器中获取数据。

#!/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}

不幸的是,当我运行传感器时,有两个“temp1”,有两个名为“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

我想获取 radeon-pci-0100 的信息,但是我该怎么做呢?

sensors这是我使用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

答案1

好吧,最简单的方法就是抓住最后一行:

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

tail -n 1打印文件的最后一行。

或者

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

tac反转输入,因此第一行现在是最后一行。这意味着第一个匹配项temp1是您关心的,并且由于grep -m 1只会打印第一个匹配项,所以这就是您将得到的。

就我个人而言,既然您已经在使用awk,我会完成整个事情awk

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

这里的想法是,每当一行匹配时temp1k就设置为int($2)。但是,仅在处理完文件的其余部分后执行的块k中打印,因此只会打印找到的最后一个值。END{}

答案2

最简单的答案是安装mrtgutils-sensors,其中包括mrtg-sensors自动解析传感器输出的包。

mrtg-sensors radeon-pci-0100 temp1 

会给你正确的答案。

相关内容