123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- use strict;
- use warnings;
- use Nagios::Plugin;
- use Data::Dumper;
- sub readStatFile
- {
- my $np = shift;
- my $file = shift;
- my $names = shift;
- my $stat = ();
- open( FILE, "<", $file ) or $np->nagios_die( "cant open file $file" );
- while ( <FILE> )
- {
- next unless ( $_ =~ /^cpu/ );
- my ( $name, @cpudata ) = split( '\s+', $_ );
- $stat->{ $name } = \@cpudata;
- }
- close( FILE );
- return $stat;
- }
- sub writeStatFile
- {
- my $np = shift;
- my $objects = shift;
- my $names = shift;
- open( FILE, ">", $np->opts->gapfile ) or $np->nagios_die( "cant open file $np->opts->gapfile" );
- foreach ( keys %{ $objects->{ newdata } } )
- {
- my $string = $_ . " ";
- $string .= join( " ", @{ $objects->{ newdata }->{ $_ } } );
- print FILE $string . "\n";
- }
- close( FILE );
- }
- sub calculateData
- {
- my $name = shift;
- my $objects = shift;
- my $names = shift;
- my @results;
- my $sum = 0;
- my $cpu = ();
- for ( my $i = 0; $i < scalar @$names; $i++ )
- {
- if ( defined( $objects->{ olddata }->{ $name }->[ $i ] ) )
- {
- push( @results, $objects->{ newdata }->{ $name }->[ $i ] - $objects->{ olddata }->{ $name }->[ $i ] );
- $sum += $results[ $i ];
- }
- else
- {
- push( @results, 0 );
- }
- }
- for ( my $i = 0; $i < scalar @$names; $i++ )
- {
- $cpu->[ $i ] = $results[ $i ] * 100 / $sum;
- }
- return $cpu;
- }
- sub processData
- {
- my $np = shift;
- my $objects = shift;
- my $names = shift;
- my $percent = ();
- $percent->{ cpu } = calculateData( "cpu", $objects, $names );
- if ( $np->opts->details )
- {
- foreach ( sort keys %{ $objects->{ olddata } } )
- {
- next if ( $_ eq 'cpu' );
- $percent->{ $_ } = calculateData( $_, $objects, $names );
- }
- }
- return $percent;
- }
- sub compareData
- {
- my $np = shift;
- my $objects = shift;
- my $names = shift;
- my $warnings = [ split( ',', $np->opts->warning ) ];
- my $criticals = [ split( ',', $np->opts->critical ) ];
- my $string = "";
- my $result = 0;
- my $res;
- foreach ( sort keys %{ $objects->{ olddata } } )
- {
-
- unless ( $np->opts->details )
- {
- next unless ( $_ eq 'cpu' );
- }
-
- $string .= uc( $_ );
- for ( my $i = 0; $i < scalar @$names; $i++ )
- {
- if ( defined( $$warnings[ $i ] ) and defined( $$criticals[ $i ] ) and ( $$warnings[ $i ] ne "none" ) and ( $$criticals[ $i ] ne "none" ) )
- {
- $res = $np->check_threshold( check => $objects->{ percent }->{ $_ }->[ $i ],
- warning => $$warnings[ $i ],
- critical => $$criticals[ $i ] );
-
- $result = ( $res > $result ? $res : $result );
- $np->add_perfdata(
- label => $_ . "_" . $$names[ $i ],
- value => $objects->{ percent }->{ $_ }->[ $i ],
- warning => $$warnings[ $i ],
- critical => $$criticals[ $i ],
- uom => "%"
- );
- }
- else
- {
- $np->add_perfdata(
- label => $_ . "_" . $$names[ $i ],
- value => $objects->{ percent }->{ $_ }->[ $i ],
- uom => "%"
- );
- }
-
- if ( $$names[ $i ] =~ /^user/ or $$names[ $i ] =~ /^system/ or $$names[ $i ] =~ /^idle/ or $$names[ $i ] =~ /^iowait/ or $$names[ $i ] =~ /^steal/ )
- {
- $string .= sprintf( " (%s=%3.2f)", $$names[ $i ], $objects->{ percent }->{ $_ }->[ $i ] );
- }
- }
- $string .= ", ";
- }
-
- return ( $result, substr( $string, 0, -2 ) );
- }
- my $objects = ();
- my $names = [];
- my $np = Nagios::Plugin->new(
- shortname => "Linux CPU Usage",
- usage => "Usage: %s < arguments > " .
- "arguments: \n" .
- " [ -t|--timeout=<timeout> ] timeout\n" .
- " [ -c|--critical=<threshold> ] critical threshold\n" .
- " [ -w|--warning=<threshold> ] warning threshold\n" .
- " [ -s|--statfile=<file> ] name of the stat file (default /proc/stat)\n" .
- " [ -g|--gapfile=<file> ] name of the gap file (default /tmp/check_cpu_usage.gap.tmp)\n" .
- " [ -n|--names=<list> ] comma separated list of names representing the column in the stats file\n" .
- " [ -d|--details ] show detailed information for each core\n",
- plugin => 'check_cpu_usage'
- );
- $np->add_arg(
- spec => 'warning|w=s',
- help => "--warning -w\n a list of threshold for warning in the same order as names\n" .
- " (default none,none,none,none,none,none,none,none,none,none,none,none,none,none)",
- default => "none,none,none,none,none,none,none,none,none,none,none,none,none,none",
- required => 0
- );
- $np->add_arg(
- spec => 'critical|c=s',
- help => "--critical -c\n a list of threshold for critical in the same order as names\n" .
- " (default none,none,none,none,none,none,none,none,none,none,none,none,none,none)",
- default => "none,none,none,none,none,none,none,none,none,none,none,none,none,none",
- required => 0
- );
- $np->add_arg(
- spec => 'statfile|s=s',
- help => "--statfile -s\n name of the stat file (default /proc/stat)",
- default => "/proc/stat",
- required => 0
- );
- $np->add_arg(
- spec => 'gapfile|g=s',
- help => "--gapfile -g\n name of the gap file (default /tmp/check_cpu_usage.gap.tmp)",
- default => "/tmp/check_cpu_usage.gap.tmp",
- required => 0
- );
- $np->add_arg(
- spec => 'details|d',
- help => "--details -d\n show detailed information for each core",
- required => 0
- );
- $np->add_arg(
- spec => 'names|n=s',
- help => "--names -n\n a comma separated list of names representing the column in the stats file. See 'man proc' for details\n" .
- " (default user,nice,system,idle,iowait,irq,softirq,steal,guest,guest_nice,nyd1,nyd2,nyd3)",
- default => "user,nice,system,idle,iowait,irq,softirq,steal,guest,guest_nice,nyd1,nyd2,nyd3",
- required => 0
- );
- $np->getopts;
- $names = [ split( ',', $np->opts->names ) ];
- $objects->{ newdata } = readStatFile( $np, $np->opts->statfile, $names );
- if ( -e $np->opts->gapfile )
- {
-
- $objects->{ olddata } = readStatFile( $np, $np->opts->gapfile, $names );
-
- $objects->{ percent } = processData( $np, $objects, $names );
- }
- writeStatFile( $np, $objects, $names );
- print Dumper( $names, $objects ) if ( $np->opts->verbose );
- $np->nagios_exit( compareData( $np, $objects, $names ) );
|