Text-Processing
著色感測器的輸出
我
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,那麼
+60.0°C
,+61.0°C
,+62.0°C
等的任何出現都應該是紅色的(理想情況下,我想要一個基於兩個不同門檻值的橙色級別和一個紅色級別,但是一個一級解決方案也會很棒)。理想情況下,這也應該與watch sensors
.
用法:
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; }