From ec6d0db61c33e65a1c3e414b07638a55022dfbf6 Mon Sep 17 00:00:00 2001
From: Ton Voon <tonvoon@users.sourceforge.net>
Date: Fri, 2 Dec 2005 22:28:06 +0000
Subject: Support for Nagios 1 and Nagios 2 status files (Gerhard Lausser -
 1296242)

git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1294 f882894a-f735-0410-b71e-b25c423dba1c

diff --git a/THANKS.in b/THANKS.in
index e527b64..fa61ddb 100644
--- a/THANKS.in
+++ b/THANKS.in
@@ -172,3 +172,4 @@ Bob Ingraham
 Hans Engelen
 Rick Frey
 Serhan Kiymaz
+Gerhard Lausser
diff --git a/doc/developer-guidelines.sgml b/doc/developer-guidelines.sgml
index 193bc9b..433a302 100644
--- a/doc/developer-guidelines.sgml
+++ b/doc/developer-guidelines.sgml
@@ -86,7 +86,7 @@
 
 		<para>Output should be in the format:</para>
 		<literallayout>
-		METRIC STATUS: Information text
+		SERVICE STATUS: Information text
 		</literallayout>
 		<para>However, note that this is not a requirement of the API, so you cannot depend on this
 		being an accurate reflection of the status of the service - the status should always 
@@ -289,15 +289,10 @@
 		</section>
 
 	<section><title>Translations</title>
-	<para>If possible, use translation tools for all output. Currently, most of the core C plugins 
-	use gettext for translation. General guidelines are:</para>
-
-	<orderedlist>
-	<listitem><para>short help is not translated</para></listitem>
-	<listitem><para>long help has options in English language, but text translated</para></listitem>
-	<listitem><para>"Copyright" kept in English</para></listitem>
-	<listitem><para>copyright holder names kept in original text</para></listitem>
-	</orderedlist>
+	<para>If possible, use translation tools for all output to respect the user's language 
+		settings. See <xref linkend="translations_developers"> for guidelines 
+		for the core plugins. 
+	</para>
 	</section>
 </section>
 
@@ -611,17 +606,24 @@
 update the THANKS.in file.</para>
 	</section>
 
-	<section><title>Translations for developers</title>
-	<para>To make the job easier for translators please follow these guidelines:</para>
+	<section id="translations_developers"><title>Translations for developers</title>
+	<para>To make the job easier for translators, please follow these guidelines:</para>
 	<orderedlist>
 	  <listitem><para>
-	    before creating new strings, check the po/de.po file to see if a similar string
+	    Before creating new strings, check the po/nagios-plugins.pot file to 
+	    see if a similar string
 	    already exists
 	  </para></listitem>
 	  <listitem><para>
-	    for help texts, break into individual options so that these can be reused
+	    For help texts, break into individual options so that these can be reused
 	    between plugins
 	  </para></listitem>
+	  <listitem><para>Try to avoid linefeeds unless you are working on a block of text</para></listitem>
+	  <listitem><para>Short help is not translated</para></listitem>
+	  <listitem><para>Long help has options in English language, but text translated</para></listitem>
+	  <listitem><para>"Copyright" kept in English</para></listitem>
+	  <listitem><para>Copyright holder names kept in original text</para></listitem>
+	  <listitem><para>Debugging output does not need to be translated</para></listitem>
 	</orderedlist>
 	</section>
 
diff --git a/plugins/check_nagios.c b/plugins/check_nagios.c
index ab9c877..45514f1 100644
--- a/plugins/check_nagios.c
+++ b/plugins/check_nagios.c
@@ -85,22 +85,25 @@ main (int argc, char **argv)
 	/* open the status log */
 	fp = fopen (status_log, "r");
 	if (fp == NULL) {
-		printf (_("CRITICAL - Cannot open status log for reading!\n"));
-		return STATE_CRITICAL;
+		die (STATE_CRITICAL, "NAGIOS %s: %s\n", _("CRITICAL"), _("Cannot open status log for reading!"));
 	}
 
 	/* get the date/time of the last item updated in the log */
 	while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
-		temp_ptr = strtok (input_buffer, "]");
-		temp_entry_time =
-			(temp_ptr == NULL) ? 0L : strtoul (temp_ptr + 1, NULL, 10);
-		if (temp_entry_time > latest_entry_time)
+		if ((temp_ptr = strstr (input_buffer, "created=")) != NULL) {
+			temp_entry_time = strtoul (temp_ptr + 8, NULL, 10);
 			latest_entry_time = temp_entry_time;
+			break;
+		} else if ((temp_ptr = strtok (input_buffer, "]")) != NULL) {
+			temp_entry_time = strtoul (temp_ptr + 1, NULL, 10);
+			if (temp_entry_time > latest_entry_time)
+				latest_entry_time = temp_entry_time;
+		}
 	}
 	fclose (fp);
 
 	if (verbose >= 2)
-		printf(_("command: %s\n"), PS_COMMAND);
+		printf("command: %s\n", PS_COMMAND);
 
 	/* run the command to check for the Nagios process.. */
 	if((result = np_runcmd(PS_COMMAND, &chld_out, &chld_err, 0)) != 0)
@@ -146,22 +149,29 @@ main (int argc, char **argv)
 	alarm (0);
 
 	if (proc_entries == 0) {
-		printf (_("Could not locate a running Nagios process!\n"));
-		return STATE_CRITICAL;
+		die (STATE_CRITICAL, "NAGIOS %s: %s\n", _("CRITICAL"), _("Could not locate a running Nagios process!"));
 	}
 
-	result = STATE_OK;
+	if (latest_entry_time == 0L) {
+		die (STATE_CRITICAL, "NAGIOS %s: %s\n", _("CRITICAL"), _("Cannot parse Nagios log file for valid time"));
+	}
 
 	time (&current_time);
-	if ((int)(current_time - latest_entry_time) > (expire_minutes * 60))
+	if ((int)(current_time - latest_entry_time) > (expire_minutes * 60)) {
 		result = STATE_WARNING;
+	} else {
+		result = STATE_OK;
+	}
 
-	printf
-		(_("Nagios %s: located %d process%s, status log updated %d second%s ago\n"),
-		 (result == STATE_OK) ? "ok" : "problem", proc_entries,
-		 (proc_entries == 1) ? "" : "es",
-		 (int) (current_time - latest_entry_time),
-		 ((int) (current_time - latest_entry_time) == 1) ? "" : "s");
+	printf ("NAGIOS %s: ", (result == STATE_OK) ? _("OK") : _("WARNING"));
+	printf (ngettext ("%d process", "%d processes", proc_entries), proc_entries);
+	printf (", ");
+	printf (
+	  ngettext ("status log updated %d second ago", 
+	    "status log updated %d seconds ago", 
+	    (int) (current_time - latest_entry_time) ),
+	    (int) (current_time - latest_entry_time) );
+	printf ("\n");
 
 	return result;
 }
diff --git a/plugins/t/check_nagios.nagios1.status.log b/plugins/t/check_nagios.nagios1.status.log
new file mode 100644
index 0000000..64d9ce7
--- /dev/null
+++ b/plugins/t/check_nagios.nagios1.status.log
@@ -0,0 +1,5 @@
+# Nagios 1.2 Status File
+[1133537544] PROGRAM;1133537484;21980;1;1133537534;0;1;1;1;1;0;1;1;1
+[1133537544] HOST;ADSL;PENDING;0;0;0;0;0;0;0;0;1;1;1;1;0;0.0;0;1;1;(Not enough data to determine host status yet)
+[1133537544] HOST;Internet;UP;1133537486;1132135282;0;1402203;0;0;0;0;1;1;1;1;0;0.00;0;1;1;(Host assumed to be up)
+[1133537544] SERVICE;Internet;TCP/IP;OK;1/3;HARD;1133537486;1133537786;ACTIVE;1;1;1;1132135282;0;OK;1402203;0;0;0;0;0;1;0;4;1;0;0.00;0;1;1;0;PING OK - Packet loss = 0%, RTA = 0.09 ms
diff --git a/plugins/t/check_nagios.nagios2.status.dat b/plugins/t/check_nagios.nagios2.status.dat
new file mode 100644
index 0000000..6f62bac
--- /dev/null
+++ b/plugins/t/check_nagios.nagios2.status.dat
@@ -0,0 +1,127 @@
+########################################
+#          NAGIOS STATUS FILE
+#
+# THIS FILE IS AUTOMATICALLY GENERATED
+# BY NAGIOS.  DO NOT MODIFY THIS FILE!
+########################################
+
+info {
+	created=1133537302
+	version=2.0b5
+	}
+
+program {
+	modified_host_attributes=0
+	modified_service_attributes=0
+	nagios_pid=2750
+	daemon_mode=1
+	program_start=1133537167
+	last_command_check=1133537297
+	last_log_rotation=0
+	enable_notifications=1
+	active_service_checks_enabled=1
+	passive_service_checks_enabled=1
+	active_host_checks_enabled=1
+	passive_host_checks_enabled=1
+	enable_event_handlers=1
+	obsess_over_services=0
+	obsess_over_hosts=0
+	check_service_freshness=1
+	check_host_freshness=0
+	enable_flap_detection=1
+	enable_failure_prediction=1
+	process_performance_data=0
+	global_host_event_handler=
+	global_service_event_handler=
+	}
+
+host {
+	host_name=ADSL-derby-office
+	modified_attributes=0
+	check_command=check_host_alive_ping
+	event_handler=
+	has_been_checked=0
+	should_be_scheduled=0
+	check_execution_time=0.000
+	check_latency=0.000
+	check_type=0
+	current_state=0
+	last_hard_state=0
+	plugin_output=
+	performance_data=
+	last_check=0
+	next_check=0
+	current_attempt=1
+	max_attempts=3
+	state_type=1
+	last_state_change=0
+	last_hard_state_change=0
+	last_time_up=0
+	last_time_down=0
+	last_time_unreachable=0
+	last_notification=0
+	next_notification=0
+	no_more_notifications=0
+	current_notification_number=0
+	notifications_enabled=1
+	problem_has_been_acknowledged=0
+	acknowledgement_type=0
+	active_checks_enabled=1
+	passive_checks_enabled=1
+	event_handler_enabled=1
+	flap_detection_enabled=1
+	failure_prediction_enabled=1
+	process_performance_data=1
+	obsess_over_host=1
+	last_update=1133537302
+	is_flapping=0
+	percent_state_change=0.00
+	scheduled_downtime_depth=0
+	}
+
+service {
+	host_name=ADSL-derby-office
+	service_description=TCP/IP
+	modified_attributes=0
+	check_command=host5_service23_check_ping
+	event_handler=
+	has_been_checked=0
+	should_be_scheduled=1
+	check_execution_time=0.000
+	check_latency=0.000
+	check_type=0
+	current_state=0
+	last_hard_state=0
+	current_attempt=1
+	max_attempts=3
+	state_type=1
+	last_state_change=0
+	last_hard_state_change=0
+	last_time_ok=0
+	last_time_warning=0
+	last_time_unknown=0
+	last_time_critical=0
+	plugin_output=(Service assumed to be ok)
+	performance_data=
+	last_check=0
+	next_check=1133537317
+	current_notification_number=0
+	last_notification=0
+	next_notification=0
+	no_more_notifications=0
+	notifications_enabled=0
+	active_checks_enabled=1
+	passive_checks_enabled=1
+	event_handler_enabled=1
+	problem_has_been_acknowledged=0
+	acknowledgement_type=0
+	flap_detection_enabled=1
+	failure_prediction_enabled=1
+	process_performance_data=1
+	obsess_over_service=0
+	last_update=1133537302
+	is_flapping=0
+	percent_state_change=0.00
+	scheduled_downtime_depth=0
+	}
+
diff --git a/plugins/t/check_nagios.t b/plugins/t/check_nagios.t
new file mode 100644
index 0000000..7722071
--- /dev/null
+++ b/plugins/t/check_nagios.t
@@ -0,0 +1,81 @@
+#! /usr/bin/perl -w -I ..
+#
+# check_nagios tests
+#
+# $Id$
+#
+
+use strict;
+use Test::More tests => 13;
+use NPTest;
+
+my $successOutput = '/^NAGIOS OK: /';
+my $warningOutput = '/^NAGIOS WARNING: /';
+my $failureOutput = '/^NAGIOS CRITICAL: /';
+
+my $nagios1 = "t/check_nagios.nagios1.status.log";
+my $nagios2 = "t/check_nagios.nagios2.status.dat";
+
+my $result;
+
+$result = NPTest->testCmd(
+	"./check_nagios -F $nagios1 -e 5 -C init"
+	);
+cmp_ok( $result->return_code, '==', 1, "Log over 5 minutes old" );
+like  ( $result->output, $warningOutput, "Output for warning correct" );
+
+my $now = time;
+# This substitution is dependant on the testcase
+system( "perl -pe 's/1133537544/$now/' $nagios1 > $nagios1.tmp" ) == 0 or die "Problem with munging $nagios1";
+
+$result = NPTest->testCmd(
+	"./check_nagios -F $nagios1.tmp -e 1 -C init"
+	);
+cmp_ok( $result->return_code, "==", 0, "Log up to date" );
+like  ( $result->output, $successOutput, "Output for success correct" );
+
+my $later = $now - 61;
+system( "perl -pe 's/1133537544/$later/' $nagios1 > $nagios1.tmp" ) == 0 or die "Problem with munging $nagios1";
+
+$result = NPTest->testCmd(
+        "./check_nagios -F $nagios1.tmp -e 1 -C init"
+        );
+cmp_ok( $result->return_code, "==", 1, "Log correctly seen as over 1 minute old" );
+my ($age) = ($_ = $result->output) =~ /status log updated (\d+) seconds ago/;
+like( $age, '/^6[0-9]$/', "Log correctly seen as between 60-69 seconds old" );
+
+$result = NPTest->testCmd(
+	"./check_nagios -F $nagios1.tmp -e 5 -C unlikely_command_string"
+	);
+cmp_ok( $result->return_code, "==", 2, "Nagios command not found" );
+like  ( $result->output, $failureOutput, "Output for failure correct" );
+
+$result = NPTest->testCmd(
+	"./check_nagios -F $nagios2 -e 5 -C init"
+	);
+cmp_ok( $result->return_code, "==", 1, "Nagios2 for logfile over 5 mins old" );
+
+$now = time;
+system( "perl -pe 's/1133537302/$now/' $nagios2 > $nagios2.tmp" ) == 0 or die "Problem with munging $nagios2";
+
+$result = NPTest->testCmd(
+	"./check_nagios -F $nagios2.tmp -e 1 -C init"
+	);
+cmp_ok( $result->return_code, "==", 0, "Nagios2 log up to date" );
+
+$later = $now - 61;
+system( "perl -pe 's/1133537302/$later/' $nagios2 > $nagios2.tmp" ) == 0 or die "Problem with munging $nagios2";
+
+$result = NPTest->testCmd(
+        "./check_nagios -F $nagios2.tmp -e 1 -C init"
+        );
+cmp_ok( $result->return_code, "==", 1, "Nagios2 log correctly seen as over 1 minute old" );
+($age) = ($_ = $result->output) =~ /status log updated (\d+) seconds ago/;
+like( $age, '/^6[0-9]$/', "Log correctly seen as between 60-69 seconds old" );
+
+$result = NPTest->testCmd(
+	"./check_nagios -F t/check_nagios.t -e 1 -C init"
+	);
+cmp_ok( $result->return_code, "==", 2, "Invalid log file" );
+
+
-- 
cgit v0.10-9-g596f

