summaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'plugins')
-rw-r--r--plugins/check_nagios.c44
-rw-r--r--plugins/t/check_nagios.nagios1.status.log5
-rw-r--r--plugins/t/check_nagios.nagios2.status.dat127
-rw-r--r--plugins/t/check_nagios.t81
4 files changed, 240 insertions, 17 deletions
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)
85 /* open the status log */ 85 /* open the status log */
86 fp = fopen (status_log, "r"); 86 fp = fopen (status_log, "r");
87 if (fp == NULL) { 87 if (fp == NULL) {
88 printf (_("CRITICAL - Cannot open status log for reading!\n")); 88 die (STATE_CRITICAL, "NAGIOS %s: %s\n", _("CRITICAL"), _("Cannot open status log for reading!"));
89 return STATE_CRITICAL;
90 } 89 }
91 90
92 /* get the date/time of the last item updated in the log */ 91 /* get the date/time of the last item updated in the log */
93 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) { 92 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
94 temp_ptr = strtok (input_buffer, "]"); 93 if ((temp_ptr = strstr (input_buffer, "created=")) != NULL) {
95 temp_entry_time = 94 temp_entry_time = strtoul (temp_ptr + 8, NULL, 10);
96 (temp_ptr == NULL) ? 0L : strtoul (temp_ptr + 1, NULL, 10);
97 if (temp_entry_time > latest_entry_time)
98 latest_entry_time = temp_entry_time; 95 latest_entry_time = temp_entry_time;
96 break;
97 } else if ((temp_ptr = strtok (input_buffer, "]")) != NULL) {
98 temp_entry_time = strtoul (temp_ptr + 1, NULL, 10);
99 if (temp_entry_time > latest_entry_time)
100 latest_entry_time = temp_entry_time;
101 }
99 } 102 }
100 fclose (fp); 103 fclose (fp);
101 104
102 if (verbose >= 2) 105 if (verbose >= 2)
103 printf(_("command: %s\n"), PS_COMMAND); 106 printf("command: %s\n", PS_COMMAND);
104 107
105 /* run the command to check for the Nagios process.. */ 108 /* run the command to check for the Nagios process.. */
106 if((result = np_runcmd(PS_COMMAND, &chld_out, &chld_err, 0)) != 0) 109 if((result = np_runcmd(PS_COMMAND, &chld_out, &chld_err, 0)) != 0)
@@ -146,22 +149,29 @@ main (int argc, char **argv)
146 alarm (0); 149 alarm (0);
147 150
148 if (proc_entries == 0) { 151 if (proc_entries == 0) {
149 printf (_("Could not locate a running Nagios process!\n")); 152 die (STATE_CRITICAL, "NAGIOS %s: %s\n", _("CRITICAL"), _("Could not locate a running Nagios process!"));
150 return STATE_CRITICAL;
151 } 153 }
152 154
153 result = STATE_OK; 155 if (latest_entry_time == 0L) {
156 die (STATE_CRITICAL, "NAGIOS %s: %s\n", _("CRITICAL"), _("Cannot parse Nagios log file for valid time"));
157 }
154 158
155 time (&current_time); 159 time (&current_time);
156 if ((int)(current_time - latest_entry_time) > (expire_minutes * 60)) 160 if ((int)(current_time - latest_entry_time) > (expire_minutes * 60)) {
157 result = STATE_WARNING; 161 result = STATE_WARNING;
162 } else {
163 result = STATE_OK;
164 }
158 165
159 printf 166 printf ("NAGIOS %s: ", (result == STATE_OK) ? _("OK") : _("WARNING"));
160 (_("Nagios %s: located %d process%s, status log updated %d second%s ago\n"), 167 printf (ngettext ("%d process", "%d processes", proc_entries), proc_entries);
161 (result == STATE_OK) ? "ok" : "problem", proc_entries, 168 printf (", ");
162 (proc_entries == 1) ? "" : "es", 169 printf (
163 (int) (current_time - latest_entry_time), 170 ngettext ("status log updated %d second ago",
164 ((int) (current_time - latest_entry_time) == 1) ? "" : "s"); 171 "status log updated %d seconds ago",
172 (int) (current_time - latest_entry_time) ),
173 (int) (current_time - latest_entry_time) );
174 printf ("\n");
165 175
166 return result; 176 return result;
167} 177}
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 @@
1# Nagios 1.2 Status File
2[1133537544] PROGRAM;1133537484;21980;1;1133537534;0;1;1;1;1;0;1;1;1
3[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)
4[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)
5[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 @@
1########################################
2# NAGIOS STATUS FILE
3#
4# THIS FILE IS AUTOMATICALLY GENERATED
5# BY NAGIOS. DO NOT MODIFY THIS FILE!
6########################################
7
8info {
9 created=1133537302
10 version=2.0b5
11 }
12
13program {
14 modified_host_attributes=0
15 modified_service_attributes=0
16 nagios_pid=2750
17 daemon_mode=1
18 program_start=1133537167
19 last_command_check=1133537297
20 last_log_rotation=0
21 enable_notifications=1
22 active_service_checks_enabled=1
23 passive_service_checks_enabled=1
24 active_host_checks_enabled=1
25 passive_host_checks_enabled=1
26 enable_event_handlers=1
27 obsess_over_services=0
28 obsess_over_hosts=0
29 check_service_freshness=1
30 check_host_freshness=0
31 enable_flap_detection=1
32 enable_failure_prediction=1
33 process_performance_data=0
34 global_host_event_handler=
35 global_service_event_handler=
36 }
37
38host {
39 host_name=ADSL-derby-office
40 modified_attributes=0
41 check_command=check_host_alive_ping
42 event_handler=
43 has_been_checked=0
44 should_be_scheduled=0
45 check_execution_time=0.000
46 check_latency=0.000
47 check_type=0
48 current_state=0
49 last_hard_state=0
50 plugin_output=
51 performance_data=
52 last_check=0
53 next_check=0
54 current_attempt=1
55 max_attempts=3
56 state_type=1
57 last_state_change=0
58 last_hard_state_change=0
59 last_time_up=0
60 last_time_down=0
61 last_time_unreachable=0
62 last_notification=0
63 next_notification=0
64 no_more_notifications=0
65 current_notification_number=0
66 notifications_enabled=1
67 problem_has_been_acknowledged=0
68 acknowledgement_type=0
69 active_checks_enabled=1
70 passive_checks_enabled=1
71 event_handler_enabled=1
72 flap_detection_enabled=1
73 failure_prediction_enabled=1
74 process_performance_data=1
75 obsess_over_host=1
76 last_update=1133537302
77 is_flapping=0
78 percent_state_change=0.00
79 scheduled_downtime_depth=0
80 }
81
82service {
83 host_name=ADSL-derby-office
84 service_description=TCP/IP
85 modified_attributes=0
86 check_command=host5_service23_check_ping
87 event_handler=
88 has_been_checked=0
89 should_be_scheduled=1
90 check_execution_time=0.000
91 check_latency=0.000
92 check_type=0
93 current_state=0
94 last_hard_state=0
95 current_attempt=1
96 max_attempts=3
97 state_type=1
98 last_state_change=0
99 last_hard_state_change=0
100 last_time_ok=0
101 last_time_warning=0
102 last_time_unknown=0
103 last_time_critical=0
104 plugin_output=(Service assumed to be ok)
105 performance_data=
106 last_check=0
107 next_check=1133537317
108 current_notification_number=0
109 last_notification=0
110 next_notification=0
111 no_more_notifications=0
112 notifications_enabled=0
113 active_checks_enabled=1
114 passive_checks_enabled=1
115 event_handler_enabled=1
116 problem_has_been_acknowledged=0
117 acknowledgement_type=0
118 flap_detection_enabled=1
119 failure_prediction_enabled=1
120 process_performance_data=1
121 obsess_over_service=0
122 last_update=1133537302
123 is_flapping=0
124 percent_state_change=0.00
125 scheduled_downtime_depth=0
126 }
127
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 @@
1#! /usr/bin/perl -w -I ..
2#
3# check_nagios tests
4#
5# $Id$
6#
7
8use strict;
9use Test::More tests => 13;
10use NPTest;
11
12my $successOutput = '/^NAGIOS OK: /';
13my $warningOutput = '/^NAGIOS WARNING: /';
14my $failureOutput = '/^NAGIOS CRITICAL: /';
15
16my $nagios1 = "t/check_nagios.nagios1.status.log";
17my $nagios2 = "t/check_nagios.nagios2.status.dat";
18
19my $result;
20
21$result = NPTest->testCmd(
22 "./check_nagios -F $nagios1 -e 5 -C init"
23 );
24cmp_ok( $result->return_code, '==', 1, "Log over 5 minutes old" );
25like ( $result->output, $warningOutput, "Output for warning correct" );
26
27my $now = time;
28# This substitution is dependant on the testcase
29system( "perl -pe 's/1133537544/$now/' $nagios1 > $nagios1.tmp" ) == 0 or die "Problem with munging $nagios1";
30
31$result = NPTest->testCmd(
32 "./check_nagios -F $nagios1.tmp -e 1 -C init"
33 );
34cmp_ok( $result->return_code, "==", 0, "Log up to date" );
35like ( $result->output, $successOutput, "Output for success correct" );
36
37my $later = $now - 61;
38system( "perl -pe 's/1133537544/$later/' $nagios1 > $nagios1.tmp" ) == 0 or die "Problem with munging $nagios1";
39
40$result = NPTest->testCmd(
41 "./check_nagios -F $nagios1.tmp -e 1 -C init"
42 );
43cmp_ok( $result->return_code, "==", 1, "Log correctly seen as over 1 minute old" );
44my ($age) = ($_ = $result->output) =~ /status log updated (\d+) seconds ago/;
45like( $age, '/^6[0-9]$/', "Log correctly seen as between 60-69 seconds old" );
46
47$result = NPTest->testCmd(
48 "./check_nagios -F $nagios1.tmp -e 5 -C unlikely_command_string"
49 );
50cmp_ok( $result->return_code, "==", 2, "Nagios command not found" );
51like ( $result->output, $failureOutput, "Output for failure correct" );
52
53$result = NPTest->testCmd(
54 "./check_nagios -F $nagios2 -e 5 -C init"
55 );
56cmp_ok( $result->return_code, "==", 1, "Nagios2 for logfile over 5 mins old" );
57
58$now = time;
59system( "perl -pe 's/1133537302/$now/' $nagios2 > $nagios2.tmp" ) == 0 or die "Problem with munging $nagios2";
60
61$result = NPTest->testCmd(
62 "./check_nagios -F $nagios2.tmp -e 1 -C init"
63 );
64cmp_ok( $result->return_code, "==", 0, "Nagios2 log up to date" );
65
66$later = $now - 61;
67system( "perl -pe 's/1133537302/$later/' $nagios2 > $nagios2.tmp" ) == 0 or die "Problem with munging $nagios2";
68
69$result = NPTest->testCmd(
70 "./check_nagios -F $nagios2.tmp -e 1 -C init"
71 );
72cmp_ok( $result->return_code, "==", 1, "Nagios2 log correctly seen as over 1 minute old" );
73($age) = ($_ = $result->output) =~ /status log updated (\d+) seconds ago/;
74like( $age, '/^6[0-9]$/', "Log correctly seen as between 60-69 seconds old" );
75
76$result = NPTest->testCmd(
77 "./check_nagios -F t/check_nagios.t -e 1 -C init"
78 );
79cmp_ok( $result->return_code, "==", 2, "Invalid log file" );
80
81