センサー出力のカラー化

センサー出力のカラー化

私はsensorsコンソールのCPU温度を見ていました。これは出力の一部です。

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)

この出力を「カラー化」したいです。具体的には、温度が特定のしきい値を超える場合は赤で表示したいと思います。たとえば、しきい値が60であると仮定すると、などのすべての項目は赤でなければなりません(理想的には、2+60.0°Cつの異なるしきい値に基づくオレンジと赤のレベルが必要ですが、単一レベルのソリューションも非常に良いです)。 。理想的にはこれにも適用する必要があります。+61.0°C+62.0°Cwatch sensors

ベストアンサー1

使用法: sensors | ./color_sensors.awk

時計で使用: 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; }

結果: ここに画像の説明を入力してください。

おすすめ記事