Skip to content
Snippets Groups Projects
Commit f67a018d authored by cheko's avatar cheko
Browse files

first commit

parent 416e4e21
No related branches found
No related tags found
No related merge requests found
check_cpu_usage check_cpu_usage
=============== ===============
This script is intended to be a icinga, nagios or naemon plugin This script is intended to be an icinga, nagios or naemon plugin which measures the CPU usage in percent. By default you get the total usage of all cores but you can also measure all the cores.
The php script is needed by pnp4nagios and is made for only one CPU. If you need more, extend the script to yur needs.
Usage
=====
First of all, dont run this script as root. It will create a temporary file called /tmp/check_cpu_usage.gap.tmp, whith the actual measures. The script comes with a help option.
'''
$ ./check_cpu_usage --help
check_cpu_usage
This nagios plugin is free software, and comes with ABSOLUTELY NO WARRANTY.
It may be used, redistributed and/or modified under the terms of the GNU
General Public Licence (see http://www.fsf.org/licensing/licenses/gpl.txt).
Usage: check_cpu_usage < arguments > arguments:
[ -t|--timeout=<timeout> ] timeout
[ -c|--critical=<threshold> ] critical threshold
[ -w|--warning=<threshold> ] warning threshold
[ -s|--statfile=<file> ] name of the stat file (default /proc/stat)
[ -g|--gapfile=<file> ] name of the gap file (default /tmp/check_cpu_usage.gap.tmp)
[ -n|--names=<list> ] comma separated list of names representing the column in the stats file
[ -d|--details ] show detailed information for each core
-?, --usage
Print usage information
-h, --help
Print detailed help screen
-V, --version
Print version information
--extra-opts=[section][@file]
Read options from an ini file. See http://nagiosplugins.org/extra-opts for usage
--warning -c
a list of threshold for warning in the same order as names
(default none,none,none,none,none,none,none,none,none,none,none,none,none,none)
--critical -c
a list of threshold for critical in the same order as names
(default none,none,none,none,none,none,none,none,none,none,none,none,none,none)
--statfile -s
name of the stat file (default /proc/stat)
--gapfile -g
name of the gap file (default /tmp/check_cpu_usage.gap.tmp)
--details -d
show detailed information for each core
--names -n
a comma separated list of names representing the column in the stats file. See 'man proc' for details
(default user,nice,system,idle,iowait,irq,softirq,steal,guest,guest_nice,nyd1,nyd2,nyd3)
-t, --timeout=INTEGER
Seconds before plugin times out (default: 15)
-v, --verbose
Show details for command-line debugging (can repeat up to 3 times)
'''
#!/usr/bin/perl
#-------------------------------------------------------------------------------
#
# Programm : check_cpu_usage
#
#-------------------------------------------------------------------------------
#
# Beschreibung : Nagios check script to check the CPU Usage by monitoring
# /proc/stat.
#
# Author : Marek Zavesicky
# Version : $Revision: $
# Erstellt : 2013/08/26
# Letztes Update : $Date: $
#
# $Id: $
# Change history :
# $Log: $
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# Pragma
#-------------------------------------------------------------------------------
use strict;
use warnings;
#-------------------------------------------------------------------------------
# Used Modules
#-------------------------------------------------------------------------------
# use lib "/usr/local/nrpe/perl/lib";
# use Nagios::Plugin;
use Monitoring::Plugin;
use Data::Dumper;
#-------------------------------------------------------------------------------
# Function : readStatFile
#-------------------------------------------------------------------------------
# Description : Parses the stat file and extract the counters, then store
# them in a hash
#
# Input : $np Nagios::Plugins reference
# $file A file with status informations
# $names Array that has the order of the fields
# Output : $stat A hash object with the newdata stats
#-------------------------------------------------------------------------------
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;
}
#-------------------------------------------------------------------------------
# Function : writeStatFile
#-------------------------------------------------------------------------------
# Description : Write the olddata object to the gapfile
#
# Input : $np Nagios::Plugins reference
# $objects the data objects
# $names Array that has the order of the fields
# Output : -
#-------------------------------------------------------------------------------
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 );
}
#-------------------------------------------------------------------------------
# Function : calculateData
#-------------------------------------------------------------------------------
# Description : Calculate the diference between old and new data and store
# the result in an array. Summarize all differences and
# calculate the percentage of each value.
#
# Input : $np Nagios::Plugins reference
# $objects the data objects
# $names Array that has the order of the fields
# Output : -
#-------------------------------------------------------------------------------
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;
}
#-------------------------------------------------------------------------------
# Function : processData
#-------------------------------------------------------------------------------
# Description : Process the hashes of cpu's.
#
# Input : $np Nagios::Plugins reference
# $objects the data objects
# $names Array that has the order of the fields
# Output : -
#-------------------------------------------------------------------------------
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;
}
#-------------------------------------------------------------------------------
# Function : compareData
#-------------------------------------------------------------------------------
# Description : Compare data with threshold if needed and generate the
# performance data string.
#
# Input : $np Nagios::Plugins reference
# $objects the data objects
# $names Array that has the order of the fields
# Output : -
#-------------------------------------------------------------------------------
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 } } )
{
# if details are not needed, grab the next line
unless ( $np->opts->details )
{
next unless ( $_ eq 'cpu' );
}
# compute all columns and check the thresholds
$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 ] );
# Evaluate the new return code, do not overwrite higher severity by lower
$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 => "%"
);
}
# create the message string
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 .= ", ";
}
# strip the last two characters from string
return ( $result, substr( $string, 0, -2 ) );
}
#-------------------------------------------------------------------------------
# Main
#-------------------------------------------------------------------------------
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 -c\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
);
# read the cli options
$np->getopts;
# rearange scalar lists into arrays
$names = [ split( ',', $np->opts->names ) ];
# read data from stats and store it to a hash
$objects->{ newdata } = readStatFile( $np, $np->opts->statfile, $names );
if ( -e $np->opts->gapfile )
{
# read data from stored file and store it to a hash
$objects->{ olddata } = readStatFile( $np, $np->opts->gapfile, $names );
# calculate difference and percent for the data
$objects->{ percent } = processData( $np, $objects, $names );
}
# write the new data to the gapfile
writeStatFile( $np, $objects, $names );
print Dumper( $names, $objects ) if ( $np->opts->verbose );
# compare data with warning and critical threshold and exit
$np->nagios_exit( compareData( $np, $objects, $names ) );
<?php
#-------------------------------------------------------------------------------
#
# Configuration : check_cpu_usage
#
#-------------------------------------------------------------------------------
#
# Description : PNP Graph Template for check_cpu_usage
#
# Author : Marek Zavesicky
# Version : $Revision: $
# Erstellt : 2013/02/06
# Letztes Update : $Date: $
#
# $Id: $
# Change history :
# $Log: $
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# General settings
#-------------------------------------------------------------------------------
# Arrays start with 0 while DS starts with 1. Fill index 0 with some garbage.
$A_COLORS = array(
'#000',
'#FF3633', '#FF7E05', '#FAE100', '#00E613', '#0AFFFB', '#00AEFF',
'#004CFF', '#2205FF', '#6F0FFF', '#B30FFF', '#E70FFF', '#FF00AA',
'#B43C3C', '#BB573E', '#C3894B', '#BBB33E', '#77BF40', '#40BF79',
);
$L_COLORS = array(
'#000', '#316BBB',
'#71B905', '#CC3118', '#CC7016', '#C9B215', '#1598C3', '#B415C7', '#4D18E4',
'#000', '#000', '#000', '#000', '#000', '#000'
);
# Fill in some usefull lables
$LABELS = array(
'', 'All CPU User', 'All CPU Nice', 'All CPU System', 'All CPU Idle',
'All CPU IO Wait', 'All CPU IRQ', 'All CPU Soft IRQ', 'All CPU Steal',
'All CPU Guest', 'All CPU Guest Nice', '', '', ''
);
#
# Settings
#-------------------------------------------------------------------------------
# Length of the string in legend
$slen = 20;
# Set Labels of the graphs
$ds_name[1] = "All CPU Usage Detail";
#
# Initialize graphs
#-------------------------------------------------------------------------------
$opt[1] = "";
$def[1] = "";
#
# Label and Titel settings
#-------------------------------------------------------------------------------
$opt[1] .= "--vertical-label \"%\" ";
$opt[1] .= "--title \"$ds_name[1] of $hostname\" ";
$opt[1] .= "--lower=0 ";
#
# Body definition graph
#-------------------------------------------------------------------------------
foreach ( $DS as $I )
{
if ( preg_match( '/.*nyd.*/', $NAME[$I] ) )
{
continue;
}
if ( $I == 1 )
{
$def[1] .= rrd::def( "var$I", $rrdfile, $DS[$I], 'AVERAGE' );
$def[1] .= rrd::area( "var$I", $A_COLORS[$I], rrd::cut( $LABELS[$I], $slen ) );
$def[1] .= rrd::gprint("var$I", array("AVERAGE", "MAX", "LAST"), "%8.2lf%s");
}
else
{
$def[1] .= rrd::def( "var$I", $rrdfile, $DS[$I], 'AVERAGE' );
$def[1] .= rrd::area( "var$I", $A_COLORS[$I], rrd::cut( $LABELS[$I], $slen ), 'STACK' );
$def[1] .= rrd::gprint("var$I", array("AVERAGE", "MAX", "LAST"), "%8.2lf%s");
}
if ( "$WARN[$I]" != "")
{
$def[1] .= rrd::hrule($WARN[$I], "#FFFF00", "Warning $WARN[$I]$UNIT[$I]\\t");
}
if ( "$CRIT[$I]" != "")
{
$def[1] .= rrd::hrule($CRIT[$I], "#FF0000", "Critical $CRIT[$I]$UNIT[$I]\\n");
}
}
?>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment