我正在尝试在我的服务器上设置一些基本的温度监控function – (不使用第三方工具)。
我已经在我的Linux机器上安装了一些库,以便在我的服务器上运行传感器,现在我可以使用sensors命令来返回如下数据:
asb100-i2c-1-2d Adapter: SMBus nForce2 adapter at 5500 in0: +1.79 V (min = +1.39 V, max = +2.08 V) in1: +1.79 V (min = +1.39 V, max = +2.08 V) in2: +3.34 V (min = +2.96 V, max = +3.63 V) in3: +2.96 V (min = +2.67 V, max = +3.28 V) in4: +3.06 V (min = +2.51 V, max = +3.79 V) in5: +3.06 V (min = +0.00 V, max = +0.00 V) in6: +3.04 V (min = +0.00 V, max = +0.00 V) fan1: 6136 RPM (min = 2777 RPM, div = 2) fan2: 0 RPM (min = 3534 RPM, div = 2) fan3: 0 RPM (min = 10714 RPM, div = 2) temp1: +37.0°C (high = +80.0°C, hyst = +75.0°C) temp2: +26.5°C (high = +80.0°C, hyst = +75.0°C) temp3: -0.5°C (high = +80.0°C, hyst = +75.0°C) temp4: +25.0°C (high = +80.0°C, hyst = +75.0°C) cpu0_vid: +1.750 V w83l785ts-i2c-1-2e Adapter: SMBus nForce2 adapter at 5500 temp1: +30.0°C (high = +85.0°C)
然后我意识到我可以通过添加| grep temp来轻松缩小它 | grep temp到命令结尾,所以我试着运行sensors | grep temp sensors | grep temp ,并得到这个:
temp1: +37.0°C (high = +80.0°C, hyst = +75.0°C) temp2: +26.5°C (high = +80.0°C, hyst = +75.0°C) temp3: -0.5°C (high = +80.0°C, hyst = +75.0°C) temp4: +25.0°C (high = +80.0°C, hyst = +75.0°C) temp1: +30.0°C (high = +85.0°C)
我意识到temp3显然不能正常工作,所以我修改了我的命令以消除这个结果: sensors | grep temp[1,2,4] sensors | grep temp[1,2,4]
temp1: +36.0°C (high = +80.0°C, hyst = +75.0°C) temp2: +26.5°C (high = +80.0°C, hyst = +75.0°C) temp4: +25.0°C (high = +80.0°C, hyst = +75.0°C) temp1: +30.0°C (high = +85.0°C)
现在我想把它修剪下来,所以我只有一个逗号分隔的string,可能看起来像这样。
+36.0,+26.5,+25.0,+30.0
然后我可以设置这个每5/10分钟将这个数据提交给服务器。
我怎样才能实现这个使用grep或其他命令?
使用awk :
sensors | awk '/temp[124]/ {sub("°C", "", $2); print($2)}'
或逗号分隔:
sensors | awk -v ORS=, '/temp[124]/ {sub("°C", "", $2); print($2)}' | sed 's/,$//'