aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'dot_config/i3/scripts/executable_cpu_usage')
-rw-r--r--dot_config/i3/scripts/executable_cpu_usage62
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
9use strict;
10use warnings;
11use utf8;
12use Getopt::Long;
13
14# default values
15my $t_warn = $ENV{T_WARN} // 50;
16my $t_crit = $ENV{T_CRIT} // 80;
17my $cpu_usage = -1;
18my $decimals = $ENV{DECIMALS} // 0;
19my $label = $ENV{LABEL} // "";
20
21sub 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
29GetOptions("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
37open (MPSTAT, 'mpstat 1 1 |') or die;
38while (<MPSTAT>) {
39 if (/^.*\s+(\d+\.\d+)[\s\x00]?$/) {
40 $cpu_usage = 100 - $1; # 100% - %idle
41 last;
42 }
43}
44close(MPSTAT);
45
46$cpu_usage eq -1 and die 'Can\'t find CPU information';
47
48# Print short_text, full_text
49print "${label}";
50printf "%02.${decimals}f%%\n", $cpu_usage;
51print "${label}";
52printf "%02.${decimals}f%%\n", $cpu_usage;
53
54# Print color, if needed
55if ($cpu_usage >= $t_crit) {
56 print "#FF0000\n";
57 exit 33;
58} elsif ($cpu_usage >= $t_warn) {
59 print "#FFFC00\n";
60}
61
62exit 0;
Powered by cgit v1.2.3 (git 2.41.0)