diff options
Diffstat (limited to 'dot_config/i3/scripts/executable_cpu_usage')
-rw-r--r-- | dot_config/i3/scripts/executable_cpu_usage | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/dot_config/i3/scripts/executable_cpu_usage b/dot_config/i3/scripts/executable_cpu_usage new file mode 100644 index 0000000..8d8a267 --- /dev/null +++ b/dot_config/i3/scripts/executable_cpu_usage | |||
@@ -0,0 +1,62 @@ | |||
1 | #!/usr/bin/perl | ||
2 | # | ||
3 | # Copyright 2014 Pierre Mavro <[email protected]> | ||
4 | # Copyright 2014 Vivien Didelot <[email protected]> | ||
5 | # Copyright 2014 Andreas Guldstrand <[email protected]> | ||
6 | # | ||
7 | # Licensed under the terms of the GNU GPL v3, or any later version. | ||
8 | |||
9 | use strict; | ||
10 | use warnings; | ||
11 | use utf8; | ||
12 | use Getopt::Long; | ||
13 | |||
14 | # default values | ||
15 | my $t_warn = $ENV{T_WARN} // 50; | ||
16 | my $t_crit = $ENV{T_CRIT} // 80; | ||
17 | my $cpu_usage = -1; | ||
18 | my $decimals = $ENV{DECIMALS} // 0; | ||
19 | my $label = $ENV{LABEL} // ""; | ||
20 | |||
21 | sub help { | ||
22 | print "Usage: cpu_usage [-w <warning>] [-c <critical>] [-d <decimals>]\n"; | ||
23 | print "-w <percent>: warning threshold to become yellow\n"; | ||
24 | print "-c <percent>: critical threshold to become red\n"; | ||
25 | print "-d <decimals>: Use <decimals> decimals for percentage (default is $decimals) \n"; | ||
26 | exit 0; | ||
27 | } | ||
28 | |||
29 | GetOptions("help|h" => \&help, | ||
30 | "w=i" => \$t_warn, | ||
31 | "c=i" => \$t_crit, | ||
32 | "d=i" => \$decimals, | ||
33 | ); | ||
34 | |||
35 | # Get CPU usage | ||
36 | $ENV{LC_ALL}="en_US"; # if mpstat is not run under en_US locale, things may break, so make sure it is | ||
37 | open (MPSTAT, 'mpstat 1 1 |') or die; | ||
38 | while (<MPSTAT>) { | ||
39 | if (/^.*\s+(\d+\.\d+)[\s\x00]?$/) { | ||
40 | $cpu_usage = 100 - $1; # 100% - %idle | ||
41 | last; | ||
42 | } | ||
43 | } | ||
44 | close(MPSTAT); | ||
45 | |||
46 | $cpu_usage eq -1 and die 'Can\'t find CPU information'; | ||
47 | |||
48 | # Print short_text, full_text | ||
49 | print "${label}"; | ||
50 | printf "%02.${decimals}f%%\n", $cpu_usage; | ||
51 | print "${label}"; | ||
52 | printf "%02.${decimals}f%%\n", $cpu_usage; | ||
53 | |||
54 | # Print color, if needed | ||
55 | if ($cpu_usage >= $t_crit) { | ||
56 | print "#FF0000\n"; | ||
57 | exit 33; | ||
58 | } elsif ($cpu_usage >= $t_warn) { | ||
59 | print "#FFFC00\n"; | ||
60 | } | ||
61 | |||
62 | exit 0; | ||