If you want to use Python, use PySensors
, but really don't re-invent the wheel. Set up any number of monitoring systems like cacti, munin and others and be done with it.
Parsing temperature from lm_sensors command
-
07-03-2022 - |
题
Currently I'm using lm_sensors to get temperature information off of my server. I'd like to run a cron job that runs lm_sensors every five minutes, grabs the temperature data and puts it into a CSV file. However, I'm at a loss at how to parse the lm_sensors output. I'd like to parse it with either Python or bash as they're my most comfortable languages. I'm going to paste the current output of the lm_sensors command as as an example out the data I'm using. If someone could point me in the right direction on how to strip the data, I can figure the rest out from there. Thanks for the help.
Example output:
$ sensors
k10temp-pci-00c3
Adapter: PCI adapter
temp1: +0.0°C (high = +70.0°C, crit = +90.0°C)
atk0110-acpi-0
Adapter: ACPI interface
Vcore Voltage: +1.42 V (min = +0.85 V, max = +1.70 V)
+3.3 Voltage: +3.38 V (min = +2.97 V, max = +3.63 V)
+5 Voltage: +4.95 V (min = +4.50 V, max = +5.50 V)
+12 Voltage: +12.48 V (min = +10.20 V, max = +13.80 V)
CPU FAN Speed: 1510 RPM (min = 600 RPM)
CHASSIS FAN Speed: 1683 RPM (min = 600 RPM)
CPU Temperature: +37.0°C (high = +60.0°C, crit = +95.0°C)
MB Temperature: +25.0°C (high = +45.0°C, crit = +75.0°C)
解决方案
其他提示
Run sensors with the -u switch, for raw output. You'll get is something like this:
Adapter: PCI adapter
power1:
power1_input: 21.950
power1_crit: 124.947
f71889a-isa-0480
Adapter: ISA adapter
+3.3V:
in0_input: 3.264
in1:
in1_input: 0.968
in1_max: 2.040
As you can see, the actual value returned by your hardware sensors will be labled by a string with the form of sensor_input
. From that, it should be fairly easy to get the values you want into your script, like my one-line script to monitor the sensors every second:
#!/bin/sh
watch -n 1 "sensors -u | sed -n 's/_input//p'"