[53e5e13] | 1 | #!/usr/bin/env perl
|
---|
| 2 | #
|
---|
| 3 | # Copyright 2014 Pierre Mavro <deimos@deimos.fr>
|
---|
| 4 | # Copyright 2014 Vivien Didelot <vivien@didelot.org>
|
---|
| 5 | # Copyright 2014 Andreas Guldstrand <andreas.guldstrand@gmail.com>
|
---|
| 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} // 2;
|
---|
| 19 | my $label = $ENV{LABEL} // "CPU: ";
|
---|
| 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 "%.${decimals}f%%\n", $cpu_usage;
|
---|
| 51 | print "${label}";
|
---|
| 52 | printf "%.${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;
|
---|