--- nagios-plugins-1.4.5.orig/configure.in +++ nagios-plugins-1.4.5/configure.in @@ -1585,7 +1585,7 @@ fi if test -n "$ac_cv_proc_meminfo"; then AC_DEFINE(HAVE_PROC_MEMINFO,1,[Define if we have /proc/meminfo]) AC_DEFINE_UNQUOTED(PROC_MEMINFO,"$ac_cv_proc_meminfo",[path to /proc/meminfo if name changes]) - EXTRAS="$EXTRAS check_swap" + EXTRAS="$EXTRAS check_swap check_ram" fi AC_PATH_PROG(PATH_TO_DIG,dig) --- nagios-plugins-1.4.5.orig/plugins/Makefile.am +++ nagios-plugins-1.4.5/plugins/Makefile.am @@ -23,7 +23,7 @@ check_tcp_programs = check_ftp check_ima check_udp check_clamd @check_tcp_ssl@ EXTRA_PROGRAMS = check_mysql check_radius check_pgsql check_snmp check_hpjd \ - check_swap check_fping check_ldap check_game check_dig \ + check_swap check_fping check_ldap check_game check_dig check_ram \ check_nagios check_by_ssh check_dns check_nt check_ide_smart \ check_procs check_mysql_query check_apt @@ -76,6 +76,7 @@ check_pgsql_LDADD = $(NETLIBS) $(PGLIBS) check_ping_LDADD = $(NETLIBS) popen.o check_procs_LDADD = $(BASEOBJS) popen.o check_radius_LDADD = $(NETLIBS) $(RADIUSLIBS) +check_ram_LDADD = $(MATHLIBS) $(BASEOBJS) popen.o check_real_LDADD = $(NETLIBS) check_snmp_LDADD = $(BASEOBJS) popen.o check_smtp_LDADD = $(SSLOBJS) $(NETLIBS) @@ -115,6 +116,7 @@ check_pgsql_DEPENDENCIES = check_pgsql.c check_ping_DEPENDENCIES = check_ping.c $(NETOBJS) popen.o $(DEPLIBS) check_procs_DEPENDENCIES = check_procs.c $(BASEOBJS) popen.o $(DEPLIBS) check_radius_DEPENDENCIES = check_radius.c $(NETOBJS) $(DEPLIBS) +check_ram_DEPENDENCIES = check_ram.c $(BASEOBJS) popen.o $(DEPLIBS) check_real_DEPENDENCIES = check_real.c $(NETOBJS) $(DEPLIBS) check_snmp_DEPENDENCIES = check_snmp.c $(BASEOBJS) popen.o $(DEPLIBS) check_smtp_DEPENDENCIES = check_smtp.c $(SSLOBJS) $(NETOBJS) $(DEPLIBS) --- /dev/null +++ nagios-plugins-1.4.5/plugins/check_ram.c @@ -0,0 +1,300 @@ +/****************************************************************************** +* +* Nagios check_ram plugin +* +* License: GPL +* Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net) +* Copyright (c) 2000-2006 nagios-plugins team +* Copyright (c) 2007 Christian Heim (christian.heim@uni-greifswald.de) +* +* Last Modified: $Date: $ +* +* Description: +* +* This file contains the check_ram plugin +* +* License Information: +* +* 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. +* +* 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. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +* +* $Id: check_ram.c $ +* +*****************************************************************************/ + +const char *progname = "check_ram"; +const char *revision = "$Revision: $"; +const char *copyright = "2000-2007"; +const char *email = "nagiosplug-devel@lists.sourceforge.net"; + +#include "common.h" +#include "popen.h" +#include "utils.h" + +int check_ram (int usp, float free_ram_mb); +int process_arguments (int argc, char **argv); +int validate_arguments (void); +void print_usage (void); +void print_help (void); + +int warn_percent = 0; +int crit_percent = 0; +float warn_size_bytes = 0; +float crit_size_bytes= 0; +int verbose; + +int main (int argc, char **argv) +{ + int percent_used, percent; + float total_ram_mb = 0, used_ram_mb = 0, free_ram_mb = 0; + float dsktotal_mb = 0, dskused_mb = 0, dskfree_mb = 0, tmp_mb = 0; + int result = STATE_UNKNOWN; + char input_buffer[MAX_INPUT_BUFFER]; +#ifdef HAVE_PROC_MEMINFO + FILE *fp; +#endif + char str[32]; + char *status; + + setlocale (LC_ALL, ""); + bindtextdomain (PACKAGE, LOCALEDIR); + textdomain (PACKAGE); + + status = strdup (""); + + if (process_arguments (argc, argv) == ERROR) + usage4 (_("Could not parse arguments")); + +#ifdef HAVE_PROC_MEMINFO + if (verbose >= 3) { + printf("Reading PROC_MEMINFO at %s\n", PROC_MEMINFO); + } + fp = fopen (PROC_MEMINFO, "r"); + while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) { + if (sscanf (input_buffer, "%*[M]%*[e]%*[m]%*[:] %f %f %f", &dsktotal_mb, &dskused_mb, &dskfree_mb) == 3) { + dsktotal_mb = dsktotal_mb / 1048576; /* Apply conversion */ + dskused_mb = dskused_mb / 1048576; + dskfree_mb = dskfree_mb / 1048576; + total_ram_mb += dsktotal_mb; + used_ram_mb += dskused_mb; + free_ram_mb += dskfree_mb; + + percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb)); + result = max_state (result, check_ram (percent, dskfree_mb)); + + if (verbose) + asprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent); + } + else if (sscanf (input_buffer, "%*[M]%*[e]%*[m]%[TotalFre]%*[:] %f %*[k]%*[B]", str, &tmp_mb)) { + if (verbose >= 3) { + printf("Got %s with %f\n", str, tmp_mb); + } + /* I think this part is always in Kb, so convert to mb */ + if (strcmp ("Total", str) == 0) { + dsktotal_mb = tmp_mb / 1024; + } + else if (strcmp ("Free", str) == 0) { + dskfree_mb = tmp_mb / 1024; + } + } + } + fclose(fp); + dskused_mb = dsktotal_mb - dskfree_mb; + total_ram_mb = dsktotal_mb; + used_ram_mb = dskused_mb; + free_ram_mb = dskfree_mb; +#endif /* HAVE_PROC_MEMINFO */ + + /* if total_ram_mb == 0, let's not divide by 0 */ + if(total_ram_mb) { + percent_used = 100 * ((double) used_ram_mb) / ((double) total_ram_mb); + } else { + percent_used = 0; + } + + result = max_state (result, check_ram (percent_used, free_ram_mb)); + printf (_("RAM %s - %d%% free (%d MB out of %d MB) %s|"), + state_text (result), + (100 - percent_used), (int) free_ram_mb, (int) total_ram_mb, status); + + puts (perfdata ("ram", (long) free_ram_mb, "MB", + TRUE, (long) max (warn_size_bytes/(1024 * 1024), warn_percent/100.0*total_ram_mb), + TRUE, (long) max (crit_size_bytes/(1024 * 1024), crit_percent/100.0*total_ram_mb), + TRUE, 0, + TRUE, (long) total_ram_mb)); + + return result; +} + +int check_ram (int usp, float free_ram_mb) +{ + int result = STATE_UNKNOWN; + float free_ram = free_ram_mb * (1024 * 1024); /* Convert back to bytes as warn and crit specified in bytes */ + if (usp >= 0 && crit_percent != 0 && usp >= (100.0 - crit_percent)) + result = STATE_CRITICAL; + else if (crit_size_bytes > 0 && free_ram <= crit_size_bytes) + result = STATE_CRITICAL; + else if (usp >= 0 && warn_percent != 0 && usp >= (100.0 - warn_percent)) + result = STATE_WARNING; + else if (warn_size_bytes > 0 && free_ram <= warn_size_bytes) + result = STATE_WARNING; + else if (usp >= 0.0) + result = STATE_OK; + return result; +} + +/* process command-line arguments */ +int process_arguments (int argc, char **argv) +{ + int c = 0; /* option character */ + + int option = 0; + static struct option longopts[] = { + {"warning", required_argument, 0, 'w'}, + {"critical", required_argument, 0, 'c'}, + {"verbose", no_argument, 0, 'v'}, + {"version", no_argument, 0, 'V'}, + {"help", no_argument, 0, 'h'}, + {0, 0, 0, 0} + }; + + if (argc < 2) + return ERROR; + + while (1) { + c = getopt_long (argc, argv, "+?Vvhc:w:", longopts, &option); + + if (c == -1 || c == EOF) + break; + + switch (c) { + case 'w': /* warning size threshold */ + if (is_intnonneg (optarg)) { + warn_size_bytes = (float) atoi (optarg); + break; + } + else if (strstr (optarg, ",") && + strstr (optarg, "%") && + sscanf (optarg, "%f,%d%%", &warn_size_bytes, &warn_percent) == 2) { + warn_size_bytes = floorf(warn_size_bytes); + break; + } + else if (strstr (optarg, "%") && + sscanf (optarg, "%d%%", &warn_percent) == 1) { + break; + } + else { + usage4 (_("Warning threshold must be integer or percentage!")); + } + case 'c': /* critical size threshold */ + if (is_intnonneg (optarg)) { + crit_size_bytes = (float) atoi (optarg); + break; + } + else if (strstr (optarg, ",") && + strstr (optarg, "%") && + sscanf (optarg, "%f,%d%%", &crit_size_bytes, &crit_percent) == 2) { + crit_size_bytes = floorf(crit_size_bytes); + break; + } + else if (strstr (optarg, "%") && + sscanf (optarg, "%d%%", &crit_percent) == 1) { + break; + } + else { + usage4 (_("Critical threshold must be integer or percentage!")); + } + case 'v': /* verbose */ + verbose++; + break; + case 'V': /* version */ + print_revision (progname, revision); + exit (STATE_OK); + case 'h': /* help */ + print_help (); + exit (STATE_OK); + case '?': /* error */ + usage2 (_("Unknown argument"), optarg); + } + } + + c = optind; + if (c == argc) + return validate_arguments (); + if (warn_percent == 0 && is_intnonneg (argv[c])) + warn_percent = atoi (argv[c++]); + + if (c == argc) + return validate_arguments (); + if (crit_percent == 0 && is_intnonneg (argv[c])) + crit_percent = atoi (argv[c++]); + + if (c == argc) + return validate_arguments (); + if (warn_size_bytes == 0 && is_intnonneg (argv[c])) + warn_size_bytes = (float) atoi (argv[c++]); + + if (c == argc) + return validate_arguments (); + if (crit_size_bytes == 0 && is_intnonneg (argv[c])) + crit_size_bytes = (float) atoi (argv[c++]); + + return validate_arguments (); +} + +int validate_arguments (void) +{ + if (warn_percent == 0 && crit_percent == 0 && warn_size_bytes == 0 + && crit_size_bytes == 0) { + return ERROR; + } + else if (warn_percent < crit_percent) { + usage4 + (_("Warning percentage should be more than critical percentage")); + } + else if (warn_size_bytes < crit_size_bytes) { + usage4 + (_("Warning free space should be more than critical free space")); + } + return OK; +} + +void print_help (void) +{ + print_revision (progname, revision); + printf (_(COPYRIGHT), copyright, email); + printf ("%s\n", _("Check RAM on local machine.")); + printf ("\n\n"); + print_usage (); + printf (_(UT_HELP_VRSN)); + printf (" %s\n", "-w, --warning=INTEGER"); + printf (" %s\n", _("Exit with WARNING status if less than INTEGER bytes of RAM are free")); + printf (" %s\n", "-w, --warning=PERCENT%%"); + printf (" %s\n", _("Exit with WARNING status if less than PERCENT of RAM are free")); + printf (" %s\n", "-c, --critical=INTEGER"); + printf (" %s\n", _("Exit with CRITICAL status if less than INTEGER bytes of RAM are free")); + printf (" %s\n", "-c, --critical=PERCENT%%"); + printf (" %s\n", _("Exit with CRITCAL status if less than PERCENT of RAM are free")); + printf (" %s\n", "-v, --verbose"); + printf (" %s\n", _("Verbose output. Up to 3 levels")); + printf ("\n"); + printf (_(UT_SUPPORT)); +} + +void print_usage (void) +{ + printf (_("Usage:")); + printf ("%s [-v] -w %% -c %%\n",progname); + printf ("%s [-v] -w -c \n", progname); +} --- /dev/null +++ nagios-plugins-1.4.5/plugins/t/check_ram.t @@ -0,0 +1,32 @@ +#! /usr/bin/perl -w -I .. +# +# Ram Space Tests via check_ram +# +# $Id: check_raam.t $ +# + +use strict; +use Test::More tests => 8; +use NPTest; + +my $successOutput = '/^RAM OK - [0-9]+\% free \([0-9]+ MB out of [0-9]+ MB\)/'; +my $failureOutput = '/^RAM CRITICAL - [0-9]+\% free \([0-9]+ MB out of [0-9]+ MB\)/'; +my $warnOutput = '/^RAM WARNING - [0-9]+\% free \([0-9]+ MB out of [0-9]+ MB\)/'; + +my $result; + +$result = NPTest->testCmd( "./check_ram -w 1048576 -c 1048576" ); # 1 MB free +cmp_ok( $result->return_code, "==", 0, "At least 1MB free" ); +like( $result->output, $successOutput, "Right output" ); + +$result = NPTest->testCmd( "./check_ram -w 1% -c 1%" ); # 1% free +cmp_ok( $result->return_code, "==", 0, 'At least 1% free' ); +like( $result->output, $successOutput, "Right output" ); + +$result = NPTest->testCmd( "./check_ram -w 100% -c 100%" ); # 100% (always critical) +cmp_ok( $result->return_code, "==", 2, 'Get critical because not 100% free' ); +like( $result->output, $failureOutput, "Right output" ); + +$result = NPTest->testCmd( "./check_ram -w 100% -c 1%" ); # 100% (always warn) +cmp_ok( $result->return_code, "==", 1, 'Get warning because not 100% free' ); +like( $result->output, $warnOutput, "Right output" );