lemonbar-config/lemoncpu

36 lines
848 B
Plaintext
Raw Normal View History

2020-12-13 12:51:52 +01:00
#!/usr/bin/perl
#
# Copyright 2014 Pierre Mavro <deimos@deimos.fr>
# Copyright 2014 Vivien Didelot <vivien@didelot.org>
# Copyright 2014 Andreas Guldstrand <andreas.guldstrand@gmail.com>
#
# Licensed under the terms of the GNU GPL v3, or any later version.
use strict;
use utf8;
use Getopt::Long;
# default values
my $cpu_usage = -1;
my $decimals = $ENV{DECIMALS} // 2;
my $label ="C ";
# Get CPU usage
$ENV{LC_ALL}="en_US"; # if mpstat is not run under en_US locale, things may break, so make sure it is
open (MPSTAT, 'mpstat 1 1 |') or die;
while (<MPSTAT>) {
if (/^.*\s+(\d+\.\d+)[\s\x00]?$/) {
$cpu_usage = 100 - $1; # 100% - %idle
last;
}
}
close(MPSTAT);
$cpu_usage eq -1 and die 'Can\'t find CPU information';
# Print short_text, full_text
printf "${label}";
printf "%.${decimals}f%%\n", $cpu_usage;
exit 0;