1

I'm trying to setup some basic temperature monitoring on my server - (without using third-party tools).

I've installed a couple of libraries on my linux box to get sensors working on my server, and now I can use the sensors command which might return data like the following:

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)

I then realised I could narrow it down easily by adding | grep temp onto the end of the command, so I tried running sensors | grep temp, and got this:

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)

I realised that temp3 clearly wasn't functioning correctly, so I revised my command to eliminate that result: 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)

Now I want to trim it right down, so all I have is a comma-separated string, which might look like this.

+36.0,+26.5,+25.0,+30.0

I can then set this up to submit this data to a server every 5/10 minutes.

How can I achieve this using grep or other commands?

Alex Coplan
  • 585
  • 1
  • 10
  • 19

1 Answers1

5

Using awk:

sensors | awk '/temp[124]/ {sub("°C", "", $2); print($2)}'

Or comma separated:

sensors | awk -v ORS=, '/temp[124]/ {sub("°C", "", $2); print($2)}' | sed 's/,$//'
Rob Wouters
  • 1,927
  • 1
  • 11
  • 4