3

I use sensors to keep an eye on CPU temperatures on the console. This is part of the output:

coretemp-isa-0001
Adapter: ISA adapter
Physical id 1:  +45.0°C  (high = +80.0°C, crit = +90.0°C)
Core 0:         +39.0°C  (high = +80.0°C, crit = +90.0°C)
Core 1:         +39.0°C  (high = +80.0°C, crit = +90.0°C)
Core 2:         +40.0°C  (high = +80.0°C, crit = +90.0°C)
Core 3:         +38.0°C  (high = +80.0°C, crit = +90.0°C)
Core 4:         +40.0°C  (high = +80.0°C, crit = +90.0°C)
Core 8:         +39.0°C  (high = +80.0°C, crit = +90.0°C)
Core 9:         +38.0°C  (high = +80.0°C, crit = +90.0°C)
Core 10:        +38.0°C  (high = +80.0°C, crit = +90.0°C)
Core 11:        +39.0°C  (high = +80.0°C, crit = +90.0°C)
Core 12:        +39.0°C  (high = +80.0°C, crit = +90.0°C)

nouveau-pci-0200
Adapter: PCI adapter
GPU core:     +0.92 V  (min =  +0.92 V, max =  +1.00 V)
fan1:        2220 RPM
temp1:        +48.0°C  (high = +95.0°C, hyst =  +3.0°C)
                       (crit = +105.0°C, hyst =  +5.0°C)
                       (emerg = +135.0°C, hyst =  +5.0°C)

I would like to 'colorize' this output. In particular, if temperatures are above a certain threshold, I would like them to be shown in red. So, for example, let's say the threshold is 60, then any occurence of +60.0°C, +61.0°C, +62.0°C, and so on should be in red (ideally, I would like an orange level and a red level based on two different thresholds, but a one level solution would be great as well). Ideally, this should also work with watch sensors.

4

Usage: sensors | ./color_sensors.awk

Usage with watch: watch -c 'sensors | ./color_sensors.awk'

#!/usr/bin/awk -f

BEGIN {
    DEFAULT_COLOR = "\033[;m";
    RED           = "\033[1;31m";
    MAGENTA       = "\033[1;35m";

    # CPU_thresholds
    cpu_high = 60; 
    cpu_middle = 50; 

    # GPU_thresholds
    gpu_high = 80; 
    gpu_middle = 70; 
}

function colorize(temp, mid_trsh, high_trsh) {
    new_color = "";  

    temp_number = temp;
    gsub("[^0-9]","",temp_number);
    gsub(".$","",temp_number);

    if(temp_number >= high_trsh) 
        new_color = RED;
    else if (temp_number >= mid_trsh) 
        new_color = MAGENTA;

    return new_color temp DEFAULT_COLOR;
}

/Core/          { $3 = "\t" colorize($3, cpu_middle, cpu_high); }
/Physical id/   { $4 = "\t" colorize($4, cpu_middle, cpu_high); }
# Multiple spaces added for alignment here - "\t      ".
/temp1/         { $2 = "\t      " colorize($2, gpu_middle, gpu_high) " "; }
                { print; }

Result: enter image description here

| improve this answer | |
  • 1
    Thanks! I use light text on a black background, but I just adjusted the colors to make this work better for me. Also watch -c 'sensors | ./color_sensors.awk' works nicely. – Wolfgang Jun 6 '17 at 11:32
  • 1
    @Wolfgang I solved the problem with unwanted font color change. Now, script work with any previous font color - manual code change don't needed. – MiniMax Jun 7 '17 at 12:43
  • Great, thanks! Do you see the temp1: +48.0°C part under GPU core? Is there a way to colorize that one too? – Wolfgang Jun 7 '17 at 15:42
  • 1
    It's easy, just add one line /temp1/ { $2 = "\t " colorize($2) " "; } in the bottom, after /Physical id/ line. But I think this temperature value should have own threshold range: like 80 degree - middle, 90 - high, because CPU's 50,60 will be small for GPU. Right? Then, we are needed change colorize function for taking custom thresholds in the arguments, like: colorize(temp, mid_trsh, high_trsh). And pass arguments to it, like colorize($3, 50, 60) for CPU, and colorize($2, 80, 90) for GPU – MiniMax Jun 7 '17 at 18:39
  • 1
    I added aforementioned improvements to the answer. – MiniMax Jun 7 '17 at 19:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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