summaryrefslogtreecommitdiffstats
path: root/plugins/check_ping.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_ping.c')
-rw-r--r--plugins/check_ping.c105
1 files changed, 60 insertions, 45 deletions
diff --git a/plugins/check_ping.c b/plugins/check_ping.c
index 0c0f2e44..c75ad587 100644
--- a/plugins/check_ping.c
+++ b/plugins/check_ping.c
@@ -58,7 +58,7 @@ the contrib area of the downloads section at http://www.nagios.org\n\n"
58int process_arguments (int, char **); 58int process_arguments (int, char **);
59int get_threshold (char *, float *, int *); 59int get_threshold (char *, float *, int *);
60int validate_arguments (void); 60int validate_arguments (void);
61int run_ping (char *); 61int run_ping (char *, char *);
62void print_usage (void); 62void print_usage (void);
63void print_help (void); 63void print_help (void);
64 64
@@ -67,7 +67,9 @@ int wpl = UNKNOWN_PACKET_LOSS;
67int cpl = UNKNOWN_PACKET_LOSS; 67int cpl = UNKNOWN_PACKET_LOSS;
68float wrta = UNKNOWN_TRIP_TIME; 68float wrta = UNKNOWN_TRIP_TIME;
69float crta = UNKNOWN_TRIP_TIME; 69float crta = UNKNOWN_TRIP_TIME;
70char *server_address = NULL; 70char **addresses = NULL;
71int n_addresses;
72int max_addr = 1;
71int max_packets = -1; 73int max_packets = -1;
72int verbose = FALSE; 74int verbose = FALSE;
73 75
@@ -81,18 +83,14 @@ main (int argc, char **argv)
81{ 83{
82 char *command_line = NULL; 84 char *command_line = NULL;
83 int result = STATE_UNKNOWN; 85 int result = STATE_UNKNOWN;
86 int i;
87
88 addresses = malloc (max_addr);
84 89
85 if (process_arguments (argc, argv) == ERROR) 90 if (process_arguments (argc, argv) == ERROR)
86 usage ("Could not parse arguments"); 91 usage ("Could not parse arguments");
87 exit; 92 exit;
88 93
89 /* does the host address of number of packets argument come first? */
90#ifdef PING_PACKETS_FIRST
91 asprintf (&command_line, PING_COMMAND, max_packets, server_address);
92#else
93 asprintf (&command_line, PING_COMMAND, server_address, max_packets);
94#endif
95
96 /* Set signal handling and alarm */ 94 /* Set signal handling and alarm */
97 if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) { 95 if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
98 printf ("Cannot catch SIGALRM"); 96 printf ("Cannot catch SIGALRM");
@@ -102,43 +100,53 @@ main (int argc, char **argv)
102 /* handle timeouts gracefully */ 100 /* handle timeouts gracefully */
103 alarm (timeout_interval); 101 alarm (timeout_interval);
104 102
105 if (verbose) 103 for (i = 0 ; i < n_addresses ; i++) {
106 printf ("%s ==> ", command_line);
107 104
108 /* run the command */ 105 /* does the host address of number of packets argument come first? */
109 run_ping (command_line); 106#ifdef PING_PACKETS_FIRST
107 asprintf (&command_line, PING_COMMAND, max_packets, addresses[i]);
108#else
109 asprintf (&command_line, PING_COMMAND, addresses[i], max_packets);
110#endif
110 111
111 if (pl == UNKNOWN_PACKET_LOSS || rta == UNKNOWN_TRIP_TIME) { 112 if (verbose)
112 printf ("%s\n", command_line); 113 printf ("%s ==> ", command_line);
113 terminate (STATE_UNKNOWN,
114 "Error: Could not interpret output from ping command\n");
115 }
116 114
117 if (pl >= cpl || rta >= crta || rta < 0) 115 /* run the command */
118 result = STATE_CRITICAL; 116 run_ping (command_line, addresses[i]);
119 else if (pl >= wpl || rta >= wrta) 117
120 result = STATE_WARNING; 118 if (pl == UNKNOWN_PACKET_LOSS || rta == UNKNOWN_TRIP_TIME) {
121 else if (pl < wpl && rta < wrta && pl >= 0 && rta >= 0) 119 printf ("%s\n", command_line);
122 /* cannot use the max function because STATE_UNKNOWN is now 3 gt STATE_OK 120 terminate (STATE_UNKNOWN,
123 result = max (result, STATE_OK); */ 121 "Error: Could not interpret output from ping command\n");
124 if( !( (result == STATE_WARNING) || (result == STATE_CRITICAL) ) ) {
125 result = STATE_OK;
126 } 122 }
127
128 if (display_html == TRUE)
129 printf ("<A HREF='%s/traceroute.cgi?%s'>", CGIURL, server_address);
130 if (pl == 100)
131 printf ("PING %s - %sPacket loss = %d%%", state_text (result), warn_text,
132 pl);
133 else
134 printf ("PING %s - %sPacket loss = %d%%, RTA = %2.2f ms",
135 state_text (result), warn_text, pl, rta);
136 if (display_html == TRUE)
137 printf ("</A>");
138 printf ("\n");
139 123
140 if (verbose) 124 if (pl >= cpl || rta >= crta || rta < 0)
141 printf ("%f:%d%% %f:%d%%\n", wrta, wpl, crta, cpl); 125 result = STATE_CRITICAL;
126 else if (pl >= wpl || rta >= wrta)
127 result = STATE_WARNING;
128 else if (pl < wpl && rta < wrta && pl >= 0 && rta >= 0)
129 /* cannot use the max function because STATE_UNKNOWN is now 3 gt STATE_OK
130 result = max (result, STATE_OK); */
131 if( !( (result == STATE_WARNING) || (result == STATE_CRITICAL) ) ) {
132 result = STATE_OK;
133 }
134
135 if (display_html == TRUE)
136 printf ("<A HREF='%s/traceroute.cgi?%s'>", CGIURL, addresses[i]);
137 if (pl == 100)
138 printf ("PING %s - %sPacket loss = %d%%", state_text (result), warn_text,
139 pl);
140 else
141 printf ("PING %s - %sPacket loss = %d%%, RTA = %2.2f ms",
142 state_text (result), warn_text, pl, rta);
143 if (display_html == TRUE)
144 printf ("</A>");
145 printf ("\n");
146
147 if (verbose)
148 printf ("%f:%d%% %f:%d%%\n", wrta, wpl, crta, cpl);
149 }
142 150
143 return result; 151 return result;
144} 152}
@@ -200,7 +208,14 @@ process_arguments (int argc, char **argv)
200 case 'H': /* hostname */ 208 case 'H': /* hostname */
201 if (is_host (optarg) == FALSE) 209 if (is_host (optarg) == FALSE)
202 usage2 ("Invalid host name/address", optarg); 210 usage2 ("Invalid host name/address", optarg);
203 server_address = optarg; 211 n_addresses++;
212 if (n_addresses > max_addr) {
213 max_addr *= 2;
214 addresses = realloc (addresses, max_addr);
215 if (addresses == NULL)
216 terminate (STATE_UNKNOWN, "Could not realloc() addresses\n");
217 }
218 addresses[n_addresses-1] = optarg;
204 break; 219 break;
205 case 'p': /* number of packets to send */ 220 case 'p': /* number of packets to send */
206 if (is_intnonneg (optarg)) 221 if (is_intnonneg (optarg))
@@ -227,12 +242,12 @@ process_arguments (int argc, char **argv)
227 if (c == argc) 242 if (c == argc)
228 return validate_arguments (); 243 return validate_arguments ();
229 244
230 if (server_address == NULL) { 245 if (addresses[0] == NULL) {
231 if (is_host (argv[c]) == FALSE) { 246 if (is_host (argv[c]) == FALSE) {
232 printf ("Invalid host name/address: %s\n\n", argv[c]); 247 printf ("Invalid host name/address: %s\n\n", argv[c]);
233 return ERROR; 248 return ERROR;
234 } else { 249 } else {
235 server_address = argv[c++]; 250 addresses[0] = argv[c++];
236 if (c == argc) 251 if (c == argc)
237 return validate_arguments (); 252 return validate_arguments ();
238 } 253 }
@@ -349,7 +364,7 @@ validate_arguments ()
349 364
350 365
351int 366int
352run_ping (char *command_line) 367run_ping (char *command_line, char *server_address)
353{ 368{
354 char input_buffer[MAX_INPUT_BUFFER]; 369 char input_buffer[MAX_INPUT_BUFFER];
355 int result = STATE_UNKNOWN; 370 int result = STATE_UNKNOWN;