summaryrefslogtreecommitdiffstats
path: root/plugins/check_fping.c
diff options
context:
space:
mode:
authorLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2024-10-31 14:27:01 (GMT)
committerLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2024-10-31 14:27:01 (GMT)
commitfa15fdcf5dc2d40aba2f8520108e552b73b1df2b (patch)
treed51d62db024b317091f42d9ee540da7371e9d170 /plugins/check_fping.c
parent0fd0421052fed1972ecbdfdabecba5a616eaa109 (diff)
parent87eb2bef1ee2a6a42793437b2f5d63f41b1e1806 (diff)
downloadmonitoring-plugins-fa15fdcf5dc2d40aba2f8520108e552b73b1df2b.tar.gz
Merge branch 'master' into fix/check_ssh-variable-stuff
Diffstat (limited to 'plugins/check_fping.c')
-rw-r--r--plugins/check_fping.c921
1 files changed, 441 insertions, 480 deletions
diff --git a/plugins/check_fping.c b/plugins/check_fping.c
index 70d6f9f..49235e2 100644
--- a/plugins/check_fping.c
+++ b/plugins/check_fping.c
@@ -1,36 +1,36 @@
1/***************************************************************************** 1/*****************************************************************************
2* 2 *
3* Monitoring check_fping plugin 3 * Monitoring check_fping plugin
4* 4 *
5* License: GPL 5 * License: GPL
6* Copyright (c) 2000-2007 Monitoring Plugins Development Team 6 * Copyright (c) 2000-2024 Monitoring Plugins Development Team
7* 7 *
8* Description: 8 * Description:
9* 9 *
10* This file contains the check_disk plugin 10 * This file contains the check_disk plugin
11* 11 *
12* This plugin will use the fping command to ping the specified host for a 12 * This plugin will use the fping command to ping the specified host for a
13* fast check 13 * fast check
14* 14 *
15* 15 *
16* This program is free software: you can redistribute it and/or modify 16 * This program is free software: you can redistribute it and/or modify
17* it under the terms of the GNU General Public License as published by 17 * it under the terms of the GNU General Public License as published by
18* the Free Software Foundation, either version 3 of the License, or 18 * the Free Software Foundation, either version 3 of the License, or
19* (at your option) any later version. 19 * (at your option) any later version.
20* 20 *
21* This program is distributed in the hope that it will be useful, 21 * This program is distributed in the hope that it will be useful,
22* but WITHOUT ANY WARRANTY; without even the implied warranty of 22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24* GNU General Public License for more details. 24 * GNU General Public License for more details.
25* 25 *
26* You should have received a copy of the GNU General Public License 26 * You should have received a copy of the GNU General Public License
27* along with this program. If not, see <http://www.gnu.org/licenses/>. 27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28* 28 *
29* 29 *
30*****************************************************************************/ 30 *****************************************************************************/
31 31
32const char *progname = "check_fping"; 32const char *progname = "check_fping";
33const char *copyright = "2000-2007"; 33const char *copyright = "2000-2024";
34const char *email = "devel@monitoring-plugins.org"; 34const char *email = "devel@monitoring-plugins.org";
35 35
36#include "common.h" 36#include "common.h"
@@ -40,488 +40,449 @@ const char *email = "devel@monitoring-plugins.org";
40#include <stdbool.h> 40#include <stdbool.h>
41 41
42enum { 42enum {
43 PACKET_COUNT = 1, 43 PACKET_COUNT = 1,
44 PACKET_SIZE = 56, 44 PACKET_SIZE = 56,
45 PL = 0, 45 PL = 0,
46 RTA = 1 46 RTA = 1
47}; 47};
48 48
49int textscan (char *buf); 49static int textscan(char *buf);
50int process_arguments (int, char **); 50static int process_arguments(int /*argc*/, char ** /*argv*/);
51int get_threshold (char *arg, char *rv[2]); 51static int get_threshold(char *arg, char *rv[2]);
52void print_help (void); 52static void print_help(void);
53void print_usage (void); 53void print_usage(void);
54 54
55char *server_name = NULL; 55static char *server_name = NULL;
56char *sourceip = NULL; 56static char *sourceip = NULL;
57char *sourceif = NULL; 57static char *sourceif = NULL;
58int packet_size = PACKET_SIZE; 58static int packet_size = PACKET_SIZE;
59int packet_count = PACKET_COUNT; 59static int packet_count = PACKET_COUNT;
60int target_timeout = 0; 60static int target_timeout = 0;
61int packet_interval = 0; 61static int packet_interval = 0;
62bool verbose = false; 62static bool verbose = false;
63int cpl; 63static int cpl;
64int wpl; 64static int wpl;
65double crta; 65static double crta;
66double wrta; 66static double wrta;
67bool cpl_p = false; 67static bool cpl_p = false;
68bool wpl_p = false; 68static bool wpl_p = false;
69bool alive_p = false; 69static bool alive_p = false;
70bool crta_p = false; 70static bool crta_p = false;
71bool wrta_p = false; 71static bool wrta_p = false;
72 72
73int 73int main(int argc, char **argv) {
74main (int argc, char **argv) 74 /* normally should be int result = STATE_UNKNOWN; */
75{ 75
76/* normally should be int result = STATE_UNKNOWN; */ 76 int status = STATE_UNKNOWN;
77 77 int result = 0;
78 int status = STATE_UNKNOWN; 78 char *fping_prog = NULL;
79 int result = 0; 79 char *server = NULL;
80 char *fping_prog = NULL; 80 char *command_line = NULL;
81 char *server = NULL; 81 char *input_buffer = NULL;
82 char *command_line = NULL; 82 char *option_string = "";
83 char *input_buffer = NULL; 83 input_buffer = malloc(MAX_INPUT_BUFFER);
84 char *option_string = ""; 84
85 input_buffer = malloc (MAX_INPUT_BUFFER); 85 setlocale(LC_ALL, "");
86 86 bindtextdomain(PACKAGE, LOCALEDIR);
87 setlocale (LC_ALL, ""); 87 textdomain(PACKAGE);
88 bindtextdomain (PACKAGE, LOCALEDIR); 88
89 textdomain (PACKAGE); 89 /* Parse extra opts if any */
90 90 argv = np_extra_opts(&argc, argv, progname);
91 /* Parse extra opts if any */ 91
92 argv=np_extra_opts (&argc, argv, progname); 92 if (process_arguments(argc, argv) == ERROR)
93 93 usage4(_("Could not parse arguments"));
94 if (process_arguments (argc, argv) == ERROR) 94
95 usage4 (_("Could not parse arguments")); 95 server = strscpy(server, server_name);
96 96
97 server = strscpy (server, server_name); 97 /* compose the command */
98 98 if (target_timeout)
99 /* compose the command */ 99 xasprintf(&option_string, "%s-t %d ", option_string, target_timeout);
100 if (target_timeout) 100 if (packet_interval)
101 xasprintf(&option_string, "%s-t %d ", option_string, target_timeout); 101 xasprintf(&option_string, "%s-p %d ", option_string, packet_interval);
102 if (packet_interval) 102 if (sourceip)
103 xasprintf(&option_string, "%s-p %d ", option_string, packet_interval); 103 xasprintf(&option_string, "%s-S %s ", option_string, sourceip);
104 if (sourceip) 104 if (sourceif)
105 xasprintf(&option_string, "%s-S %s ", option_string, sourceip); 105 xasprintf(&option_string, "%s-I %s ", option_string, sourceif);
106 if (sourceif)
107 xasprintf(&option_string, "%s-I %s ", option_string, sourceif);
108 106
109#ifdef PATH_TO_FPING6 107#ifdef PATH_TO_FPING6
110 if (address_family != AF_INET && is_inet6_addr(server)) 108 if (address_family != AF_INET && is_inet6_addr(server))
111 fping_prog = strdup(PATH_TO_FPING6); 109 fping_prog = strdup(PATH_TO_FPING6);
112 else 110 else
113 fping_prog = strdup(PATH_TO_FPING); 111 fping_prog = strdup(PATH_TO_FPING);
114#else 112#else
115 fping_prog = strdup(PATH_TO_FPING); 113 fping_prog = strdup(PATH_TO_FPING);
116#endif 114#endif
117 115
118 xasprintf (&command_line, "%s %s-b %d -c %d %s", fping_prog, 116 xasprintf(&command_line, "%s %s-b %d -c %d %s", fping_prog, option_string, packet_size, packet_count, server);
119 option_string, packet_size, packet_count, server); 117
120 118 if (verbose)
121 if (verbose) 119 printf("%s\n", command_line);
122 printf ("%s\n", command_line); 120
123 121 /* run the command */
124 /* run the command */ 122 child_process = spopen(command_line);
125 child_process = spopen (command_line); 123 if (child_process == NULL) {
126 if (child_process == NULL) { 124 printf(_("Could not open pipe: %s\n"), command_line);
127 printf (_("Could not open pipe: %s\n"), command_line); 125 return STATE_UNKNOWN;
128 return STATE_UNKNOWN; 126 }
129 } 127
130 128 child_stderr = fdopen(child_stderr_array[fileno(child_process)], "r");
131 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); 129 if (child_stderr == NULL) {
132 if (child_stderr == NULL) { 130 printf(_("Could not open stderr for %s\n"), command_line);
133 printf (_("Could not open stderr for %s\n"), command_line); 131 }
134 } 132
135 133 while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
136 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { 134 if (verbose)
137 if (verbose) 135 printf("%s", input_buffer);
138 printf ("%s", input_buffer); 136 status = max_state(status, textscan(input_buffer));
139 status = max_state (status, textscan (input_buffer)); 137 }
140 } 138
141 139 /* If we get anything on STDERR, at least set warning */
142 /* If we get anything on STDERR, at least set warning */ 140 while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
143 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) { 141 status = max_state(status, STATE_WARNING);
144 status = max_state (status, STATE_WARNING); 142 if (verbose)
145 if (verbose) 143 printf("%s", input_buffer);
146 printf ("%s", input_buffer); 144 status = max_state(status, textscan(input_buffer));
147 status = max_state (status, textscan (input_buffer)); 145 }
148 } 146 (void)fclose(child_stderr);
149 (void) fclose (child_stderr); 147
150 148 /* close the pipe */
151 /* close the pipe */ 149 result = spclose(child_process);
152 result = spclose (child_process); 150 if (result) {
153 if (result) { 151 /* need to use max_state not max */
154 /* need to use max_state not max */ 152 status = max_state(status, STATE_WARNING);
155 status = max_state (status, STATE_WARNING); 153 }
156 } 154
157 155 if (result > 1) {
158 if (result > 1 ) { 156 status = max_state(status, STATE_UNKNOWN);
159 status = max_state (status, STATE_UNKNOWN); 157 if (result == 2) {
160 if (result == 2) { 158 die(STATE_UNKNOWN, _("FPING UNKNOWN - IP address not found\n"));
161 die (STATE_UNKNOWN, _("FPING UNKNOWN - IP address not found\n")); 159 }
162 } 160 if (result == 3) {
163 if (result == 3) { 161 die(STATE_UNKNOWN, _("FPING UNKNOWN - invalid commandline argument\n"));
164 die (STATE_UNKNOWN, _("FPING UNKNOWN - invalid commandline argument\n")); 162 }
165 } 163 if (result == 4) {
166 if (result == 4) { 164 die(STATE_UNKNOWN, _("FPING UNKNOWN - failed system call\n"));
167 die (STATE_UNKNOWN, _("FPING UNKNOWN - failed system call\n")); 165 }
168 } 166 }
169 167
170 } 168 printf("FPING %s - %s\n", state_text(status), server_name);
171 169
172 printf ("FPING %s - %s\n", state_text (status), server_name); 170 return status;
173
174 return status;
175} 171}
176 172
177 173int textscan(char *buf) {
178int textscan (char *buf) { 174 char *rtastr = NULL;
179 char *rtastr = NULL; 175 char *losstr = NULL;
180 char *losstr = NULL; 176 char *xmtstr = NULL;
181 char *xmtstr = NULL; 177 double loss;
182 double loss; 178 double rta;
183 double rta; 179 double xmt;
184 double xmt; 180 int status = STATE_UNKNOWN;
185 int status = STATE_UNKNOWN; 181
186 182 /* stops testing after the first successful reply. */
187 /* stops testing after the first successful reply. */ 183 if (alive_p && strstr(buf, "avg, 0% loss)")) {
188 if (alive_p && strstr(buf, "avg, 0% loss)")) { 184 rtastr = strstr(buf, "ms (");
189 rtastr = strstr (buf, "ms ("); 185 rtastr = 1 + index(rtastr, '(');
190 rtastr = 1 + index(rtastr, '('); 186 rta = strtod(rtastr, NULL);
191 rta = strtod(rtastr, NULL); 187 loss = strtod("0", NULL);
192 loss=strtod("0",NULL); 188 die(STATE_OK, _("FPING %s - %s (rta=%f ms)|%s\n"), state_text(STATE_OK), server_name, rta,
193 die (STATE_OK, 189 /* No loss since we only waited for the first reply
194 _("FPING %s - %s (rta=%f ms)|%s\n"), 190 perfdata ("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, true, 0, true, 100), */
195 state_text (STATE_OK), server_name,rta, 191 fperfdata("rta", rta / 1.0e3, "s", wrta_p, wrta / 1.0e3, crta_p, crta / 1.0e3, true, 0, false, 0));
196 /* No loss since we only waited for the first reply 192 }
197 perfdata ("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, true, 0, true, 100), */ 193
198 fperfdata ("rta", rta/1.0e3, "s", wrta_p, wrta/1.0e3, crta_p, crta/1.0e3, true, 0, false, 0)); 194 if (strstr(buf, "not found")) {
199 } 195 die(STATE_CRITICAL, _("FPING UNKNOWN - %s not found\n"), server_name);
200 196
201 if (strstr (buf, "not found")) { 197 } else if (strstr(buf, "is unreachable") || strstr(buf, "Unreachable")) {
202 die (STATE_CRITICAL, _("FPING UNKNOWN - %s not found\n"), server_name); 198 die(STATE_CRITICAL, _("FPING CRITICAL - %s is unreachable\n"), "host");
203 199
204 } 200 } else if (strstr(buf, "Operation not permitted") || strstr(buf, "No such device")) {
205 else if (strstr (buf, "is unreachable") || strstr (buf, "Unreachable")) { 201 die(STATE_UNKNOWN, _("FPING UNKNOWN - %s parameter error\n"), "host");
206 die (STATE_CRITICAL, _("FPING CRITICAL - %s is unreachable\n"), 202 } else if (strstr(buf, "is down")) {
207 "host"); 203 die(STATE_CRITICAL, _("FPING CRITICAL - %s is down\n"), server_name);
208 204
209 } 205 } else if (strstr(buf, "is alive")) {
210 else if (strstr (buf, "Operation not permitted") || strstr (buf, "No such device") ) { 206 status = STATE_OK;
211 die (STATE_UNKNOWN, _("FPING UNKNOWN - %s parameter error\n"), 207
212 "host"); 208 } else if (strstr(buf, "xmt/rcv/%loss") && strstr(buf, "min/avg/max")) {
213 } 209 losstr = strstr(buf, "=");
214 else if (strstr (buf, "is down")) { 210 losstr = 1 + strstr(losstr, "/");
215 die (STATE_CRITICAL, _("FPING CRITICAL - %s is down\n"), server_name); 211 losstr = 1 + strstr(losstr, "/");
216 212 rtastr = strstr(buf, "min/avg/max");
217 } 213 rtastr = strstr(rtastr, "=");
218 else if (strstr (buf, "is alive")) { 214 rtastr = 1 + index(rtastr, '/');
219 status = STATE_OK; 215 loss = strtod(losstr, NULL);
220 216 rta = strtod(rtastr, NULL);
221 } 217 if (cpl_p && loss > cpl)
222 else if (strstr (buf, "xmt/rcv/%loss") && strstr (buf, "min/avg/max")) { 218 status = STATE_CRITICAL;
223 losstr = strstr (buf, "="); 219 else if (crta_p && rta > crta)
224 losstr = 1 + strstr (losstr, "/"); 220 status = STATE_CRITICAL;
225 losstr = 1 + strstr (losstr, "/"); 221 else if (wpl_p && loss > wpl)
226 rtastr = strstr (buf, "min/avg/max"); 222 status = STATE_WARNING;
227 rtastr = strstr (rtastr, "="); 223 else if (wrta_p && rta > wrta)
228 rtastr = 1 + index (rtastr, '/'); 224 status = STATE_WARNING;
229 loss = strtod (losstr, NULL); 225 else
230 rta = strtod (rtastr, NULL); 226 status = STATE_OK;
231 if (cpl_p && loss > cpl) 227 die(status, _("FPING %s - %s (loss=%.0f%%, rta=%f ms)|%s %s\n"), state_text(status), server_name, loss, rta,
232 status = STATE_CRITICAL; 228 perfdata("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, true, 0, true, 100),
233 else if (crta_p && rta > crta) 229 fperfdata("rta", rta / 1.0e3, "s", wrta_p, wrta / 1.0e3, crta_p, crta / 1.0e3, true, 0, false, 0));
234 status = STATE_CRITICAL; 230
235 else if (wpl_p && loss > wpl) 231 } else if (strstr(buf, "xmt/rcv/%loss")) {
236 status = STATE_WARNING; 232 /* no min/max/avg if host was unreachable in fping v2.2.b1 */
237 else if (wrta_p && rta > wrta) 233 /* in v2.4b2: 10.99.0.1 : xmt/rcv/%loss = 0/0/0% */
238 status = STATE_WARNING; 234 losstr = strstr(buf, "=");
239 else 235 xmtstr = 1 + losstr;
240 status = STATE_OK; 236 xmt = strtod(xmtstr, NULL);
241 die (status, 237 if (xmt == 0)
242 _("FPING %s - %s (loss=%.0f%%, rta=%f ms)|%s %s\n"), 238 die(STATE_CRITICAL, _("FPING CRITICAL - %s is down\n"), server_name);
243 state_text (status), server_name, loss, rta, 239 losstr = 1 + strstr(losstr, "/");
244 perfdata ("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, true, 0, true, 100), 240 losstr = 1 + strstr(losstr, "/");
245 fperfdata ("rta", rta/1.0e3, "s", wrta_p, wrta/1.0e3, crta_p, crta/1.0e3, true, 0, false, 0)); 241 loss = strtod(losstr, NULL);
246 242 if (atoi(losstr) == 100)
247 } 243 status = STATE_CRITICAL;
248 else if(strstr (buf, "xmt/rcv/%loss") ) { 244 else if (cpl_p && loss > cpl)
249 /* no min/max/avg if host was unreachable in fping v2.2.b1 */ 245 status = STATE_CRITICAL;
250 /* in v2.4b2: 10.99.0.1 : xmt/rcv/%loss = 0/0/0% */ 246 else if (wpl_p && loss > wpl)
251 losstr = strstr (buf, "="); 247 status = STATE_WARNING;
252 xmtstr = 1 + losstr; 248 else
253 xmt = strtod (xmtstr, NULL); 249 status = STATE_OK;
254 if(xmt == 0) 250 /* loss=%.0f%%;%d;%d;0;100 */
255 die (STATE_CRITICAL, _("FPING CRITICAL - %s is down\n"), server_name); 251 die(status, _("FPING %s - %s (loss=%.0f%% )|%s\n"), state_text(status), server_name, loss,
256 losstr = 1 + strstr (losstr, "/"); 252 perfdata("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, true, 0, true, 100));
257 losstr = 1 + strstr (losstr, "/"); 253
258 loss = strtod (losstr, NULL); 254 } else {
259 if (atoi(losstr) == 100) 255 status = max_state(status, STATE_WARNING);
260 status = STATE_CRITICAL; 256 }
261 else if (cpl_p && loss > cpl) 257
262 status = STATE_CRITICAL; 258 return status;
263 else if (wpl_p && loss > wpl)
264 status = STATE_WARNING;
265 else
266 status = STATE_OK;
267 /* loss=%.0f%%;%d;%d;0;100 */
268 die (status, _("FPING %s - %s (loss=%.0f%% )|%s\n"),
269 state_text (status), server_name, loss ,
270 perfdata ("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, true, 0, true, 100));
271
272 }
273 else {
274 status = max_state (status, STATE_WARNING);
275 }
276
277 return status;
278} 259}
279 260
280
281
282/* process command-line arguments */ 261/* process command-line arguments */
283int 262int process_arguments(int argc, char **argv) {
284process_arguments (int argc, char **argv) 263 int c;
285{ 264 char *rv[2];
286 int c; 265
287 char *rv[2]; 266 int option = 0;
288 267 static struct option longopts[] = {{"hostname", required_argument, 0, 'H'},
289 int option = 0; 268 {"sourceip", required_argument, 0, 'S'},
290 static struct option longopts[] = { 269 {"sourceif", required_argument, 0, 'I'},
291 {"hostname", required_argument, 0, 'H'}, 270 {"critical", required_argument, 0, 'c'},
292 {"sourceip", required_argument, 0, 'S'}, 271 {"warning", required_argument, 0, 'w'},
293 {"sourceif", required_argument, 0, 'I'}, 272 {"alive", no_argument, 0, 'a'},
294 {"critical", required_argument, 0, 'c'}, 273 {"bytes", required_argument, 0, 'b'},
295 {"warning", required_argument, 0, 'w'}, 274 {"number", required_argument, 0, 'n'},
296 {"alive", no_argument, 0, 'a'}, 275 {"target-timeout", required_argument, 0, 'T'},
297 {"bytes", required_argument, 0, 'b'}, 276 {"interval", required_argument, 0, 'i'},
298 {"number", required_argument, 0, 'n'}, 277 {"verbose", no_argument, 0, 'v'},
299 {"target-timeout", required_argument, 0, 'T'}, 278 {"version", no_argument, 0, 'V'},
300 {"interval", required_argument, 0, 'i'}, 279 {"help", no_argument, 0, 'h'},
301 {"verbose", no_argument, 0, 'v'}, 280 {"use-ipv4", no_argument, 0, '4'},
302 {"version", no_argument, 0, 'V'}, 281 {"use-ipv6", no_argument, 0, '6'},
303 {"help", no_argument, 0, 'h'}, 282 {0, 0, 0, 0}};
304 {"use-ipv4", no_argument, 0, '4'}, 283
305 {"use-ipv6", no_argument, 0, '6'}, 284 rv[PL] = NULL;
306 {0, 0, 0, 0} 285 rv[RTA] = NULL;
307 }; 286
308 287 if (argc < 2)
309 rv[PL] = NULL; 288 return ERROR;
310 rv[RTA] = NULL; 289
311 290 if (!is_option(argv[1])) {
312 if (argc < 2) 291 server_name = argv[1];
313 return ERROR; 292 argv[1] = argv[0];
314 293 argv = &argv[1];
315 if (!is_option (argv[1])) { 294 argc--;
316 server_name = argv[1]; 295 }
317 argv[1] = argv[0]; 296
318 argv = &argv[1]; 297 while (1) {
319 argc--; 298 c = getopt_long(argc, argv, "+hVvaH:S:c:w:b:n:T:i:I:46", longopts, &option);
320 } 299
321 300 if (c == -1 || c == EOF || c == 1)
322 while (1) { 301 break;
323 c = getopt_long (argc, argv, "+hVvaH:S:c:w:b:n:T:i:I:46", longopts, &option); 302
324 303 switch (c) {
325 if (c == -1 || c == EOF || c == 1) 304 case '?': /* print short usage statement if args not parsable */
326 break; 305 usage5();
327 306 case 'a': /* host alive mode */
328 switch (c) { 307 alive_p = true;
329 case '?': /* print short usage statement if args not parsable */ 308 break;
330 usage5 (); 309 case 'h': /* help */
331 case 'a': /* host alive mode */ 310 print_help();
332 alive_p = true; 311 exit(STATE_UNKNOWN);
333 break; 312 case 'V': /* version */
334 case 'h': /* help */ 313 print_revision(progname, NP_VERSION);
335 print_help (); 314 exit(STATE_UNKNOWN);
336 exit (STATE_UNKNOWN); 315 case 'v': /* verbose mode */
337 case 'V': /* version */ 316 verbose = true;
338 print_revision (progname, NP_VERSION); 317 break;
339 exit (STATE_UNKNOWN); 318 case 'H': /* hostname */
340 case 'v': /* verbose mode */ 319 if (is_host(optarg) == false) {
341 verbose = true; 320 usage2(_("Invalid hostname/address"), optarg);
342 break; 321 }
343 case 'H': /* hostname */ 322 server_name = strscpy(server_name, optarg);
344 if (is_host (optarg) == false) { 323 break;
345 usage2 (_("Invalid hostname/address"), optarg); 324 case 'S': /* sourceip */
346 } 325 if (is_host(optarg) == false) {
347 server_name = strscpy (server_name, optarg); 326 usage2(_("Invalid hostname/address"), optarg);
348 break; 327 }
349 case 'S': /* sourceip */ 328 sourceip = strscpy(sourceip, optarg);
350 if (is_host (optarg) == false) { 329 break;
351 usage2 (_("Invalid hostname/address"), optarg); 330 case 'I': /* sourceip */
352 } 331 sourceif = strscpy(sourceif, optarg);
353 sourceip = strscpy (sourceip, optarg); 332 break;
354 break; 333 case '4': /* IPv4 only */
355 case 'I': /* sourceip */ 334 address_family = AF_INET;
356 sourceif = strscpy (sourceif, optarg);
357 break; 335 break;
358 case '4': /* IPv4 only */ 336 case '6': /* IPv6 only */
359 address_family = AF_INET;
360 break;
361 case '6': /* IPv6 only */
362#ifdef USE_IPV6 337#ifdef USE_IPV6
363 address_family = AF_INET6; 338 address_family = AF_INET6;
364#else 339#else
365 usage (_("IPv6 support not available\n")); 340 usage(_("IPv6 support not available\n"));
366#endif 341#endif
367 break; 342 break;
368 case 'c': 343 case 'c':
369 get_threshold (optarg, rv); 344 get_threshold(optarg, rv);
370 if (rv[RTA]) { 345 if (rv[RTA]) {
371 crta = strtod (rv[RTA], NULL); 346 crta = strtod(rv[RTA], NULL);
372 crta_p = true; 347 crta_p = true;
373 rv[RTA] = NULL; 348 rv[RTA] = NULL;
374 } 349 }
375 if (rv[PL]) { 350 if (rv[PL]) {
376 cpl = atoi (rv[PL]); 351 cpl = atoi(rv[PL]);
377 cpl_p = true; 352 cpl_p = true;
378 rv[PL] = NULL; 353 rv[PL] = NULL;
379 } 354 }
380 break; 355 break;
381 case 'w': 356 case 'w':
382 get_threshold (optarg, rv); 357 get_threshold(optarg, rv);
383 if (rv[RTA]) { 358 if (rv[RTA]) {
384 wrta = strtod (rv[RTA], NULL); 359 wrta = strtod(rv[RTA], NULL);
385 wrta_p = true; 360 wrta_p = true;
386 rv[RTA] = NULL; 361 rv[RTA] = NULL;
387 } 362 }
388 if (rv[PL]) { 363 if (rv[PL]) {
389 wpl = atoi (rv[PL]); 364 wpl = atoi(rv[PL]);
390 wpl_p = true; 365 wpl_p = true;
391 rv[PL] = NULL; 366 rv[PL] = NULL;
392 } 367 }
393 break; 368 break;
394 case 'b': /* bytes per packet */ 369 case 'b': /* bytes per packet */
395 if (is_intpos (optarg)) 370 if (is_intpos(optarg))
396 packet_size = atoi (optarg); 371 packet_size = atoi(optarg);
397 else 372 else
398 usage (_("Packet size must be a positive integer")); 373 usage(_("Packet size must be a positive integer"));
399 break; 374 break;
400 case 'n': /* number of packets */ 375 case 'n': /* number of packets */
401 if (is_intpos (optarg)) 376 if (is_intpos(optarg))
402 packet_count = atoi (optarg); 377 packet_count = atoi(optarg);
403 else 378 else
404 usage (_("Packet count must be a positive integer")); 379 usage(_("Packet count must be a positive integer"));
405 break; 380 break;
406 case 'T': /* timeout in msec */ 381 case 'T': /* timeout in msec */
407 if (is_intpos (optarg)) 382 if (is_intpos(optarg))
408 target_timeout = atoi (optarg); 383 target_timeout = atoi(optarg);
409 else 384 else
410 usage (_("Target timeout must be a positive integer")); 385 usage(_("Target timeout must be a positive integer"));
411 break; 386 break;
412 case 'i': /* interval in msec */ 387 case 'i': /* interval in msec */
413 if (is_intpos (optarg)) 388 if (is_intpos(optarg))
414 packet_interval = atoi (optarg); 389 packet_interval = atoi(optarg);
415 else 390 else
416 usage (_("Interval must be a positive integer")); 391 usage(_("Interval must be a positive integer"));
417 break; 392 break;
418 } 393 }
419 } 394 }
420
421 if (server_name == NULL)
422 usage4 (_("Hostname was not supplied"));
423
424 return OK;
425}
426 395
396 if (server_name == NULL)
397 usage4(_("Hostname was not supplied"));
427 398
428int 399 return OK;
429get_threshold (char *arg, char *rv[2])
430{
431 char *arg1 = NULL;
432 char *arg2 = NULL;
433
434 arg1 = strscpy (arg1, arg);
435 if (strpbrk (arg1, ",:"))
436 arg2 = 1 + strpbrk (arg1, ",:");
437
438 if (arg2) {
439 arg1[strcspn (arg1, ",:")] = 0;
440 if (strstr (arg1, "%") && strstr (arg2, "%"))
441 die (STATE_UNKNOWN,
442 _("%s: Only one threshold may be packet loss (%s)\n"), progname,
443 arg);
444 if (!strstr (arg1, "%") && !strstr (arg2, "%"))
445 die (STATE_UNKNOWN,
446 _("%s: Only one threshold must be packet loss (%s)\n"),
447 progname, arg);
448 }
449
450 if (arg2 && strstr (arg2, "%")) {
451 rv[PL] = arg2;
452 rv[RTA] = arg1;
453 }
454 else if (arg2) {
455 rv[PL] = arg1;
456 rv[RTA] = arg2;
457 }
458 else if (strstr (arg1, "%")) {
459 rv[PL] = arg1;
460 }
461 else {
462 rv[RTA] = arg1;
463 }
464
465 return OK;
466} 400}
467 401
402int get_threshold(char *arg, char *rv[2]) {
403 char *arg1 = NULL;
404 char *arg2 = NULL;
405
406 arg1 = strscpy(arg1, arg);
407 if (strpbrk(arg1, ",:"))
408 arg2 = 1 + strpbrk(arg1, ",:");
409
410 if (arg2) {
411 arg1[strcspn(arg1, ",:")] = 0;
412 if (strstr(arg1, "%") && strstr(arg2, "%"))
413 die(STATE_UNKNOWN, _("%s: Only one threshold may be packet loss (%s)\n"), progname, arg);
414 if (!strstr(arg1, "%") && !strstr(arg2, "%"))
415 die(STATE_UNKNOWN, _("%s: Only one threshold must be packet loss (%s)\n"), progname, arg);
416 }
417
418 if (arg2 && strstr(arg2, "%")) {
419 rv[PL] = arg2;
420 rv[RTA] = arg1;
421 } else if (arg2) {
422 rv[PL] = arg1;
423 rv[RTA] = arg2;
424 } else if (strstr(arg1, "%")) {
425 rv[PL] = arg1;
426 } else {
427 rv[RTA] = arg1;
428 }
429
430 return OK;
431}
468 432
469void print_help (void) { 433void print_help(void) {
470 434
471 print_revision (progname, NP_VERSION); 435 print_revision(progname, NP_VERSION);
472 436
473 printf ("Copyright (c) 1999 Didi Rieder <adrieder@sbox.tu-graz.ac.at>\n"); 437 printf("Copyright (c) 1999 Didi Rieder <adrieder@sbox.tu-graz.ac.at>\n");
474 printf (COPYRIGHT, copyright, email); 438 printf(COPYRIGHT, copyright, email);
475 439
476 printf ("%s\n", _("This plugin will use the fping command to ping the specified host for a fast check")); 440 printf("%s\n", _("This plugin will use the fping command to ping the specified host for a fast check"));
477 441
478 printf ("%s\n", _("Note that it is necessary to set the suid flag on fping.")); 442 printf("%s\n", _("Note that it is necessary to set the suid flag on fping."));
479 443
480 printf ("\n\n"); 444 printf("\n\n");
481 445
482 print_usage (); 446 print_usage();
483 447
484 printf (UT_HELP_VRSN); 448 printf(UT_HELP_VRSN);
485 printf (UT_EXTRA_OPTS); 449 printf(UT_EXTRA_OPTS);
486 450
487 printf (UT_IPv46); 451 printf(UT_IPv46);
488 452
489 printf (" %s\n", "-H, --hostname=HOST"); 453 printf(" %s\n", "-H, --hostname=HOST");
490 printf (" %s\n", _("name or IP Address of host to ping (IP Address bypasses name lookup, reducing system load)")); 454 printf(" %s\n", _("name or IP Address of host to ping (IP Address bypasses name lookup, reducing system load)"));
491 printf (" %s\n", "-w, --warning=THRESHOLD"); 455 printf(" %s\n", "-w, --warning=THRESHOLD");
492 printf (" %s\n", _("warning threshold pair")); 456 printf(" %s\n", _("warning threshold pair"));
493 printf (" %s\n", "-c, --critical=THRESHOLD"); 457 printf(" %s\n", "-c, --critical=THRESHOLD");
494 printf (" %s\n", _("critical threshold pair")); 458 printf(" %s\n", _("critical threshold pair"));
495 printf (" %s\n", "-a, --alive"); 459 printf(" %s\n", "-a, --alive");
496 printf (" %s\n", _("Return OK after first successful reply")); 460 printf(" %s\n", _("Return OK after first successful reply"));
497 printf (" %s\n", "-b, --bytes=INTEGER"); 461 printf(" %s\n", "-b, --bytes=INTEGER");
498 printf (" %s (default: %d)\n", _("size of ICMP packet"),PACKET_SIZE); 462 printf(" %s (default: %d)\n", _("size of ICMP packet"), PACKET_SIZE);
499 printf (" %s\n", "-n, --number=INTEGER"); 463 printf(" %s\n", "-n, --number=INTEGER");
500 printf (" %s (default: %d)\n", _("number of ICMP packets to send"),PACKET_COUNT); 464 printf(" %s (default: %d)\n", _("number of ICMP packets to send"), PACKET_COUNT);
501 printf (" %s\n", "-T, --target-timeout=INTEGER"); 465 printf(" %s\n", "-T, --target-timeout=INTEGER");
502 printf (" %s (default: fping's default for -t)\n", _("Target timeout (ms)")); 466 printf(" %s (default: fping's default for -t)\n", _("Target timeout (ms)"));
503 printf (" %s\n", "-i, --interval=INTEGER"); 467 printf(" %s\n", "-i, --interval=INTEGER");
504 printf (" %s (default: fping's default for -p)\n", _("Interval (ms) between sending packets")); 468 printf(" %s (default: fping's default for -p)\n", _("Interval (ms) between sending packets"));
505 printf (" %s\n", "-S, --sourceip=HOST"); 469 printf(" %s\n", "-S, --sourceip=HOST");
506 printf (" %s\n", _("name or IP Address of sourceip")); 470 printf(" %s\n", _("name or IP Address of sourceip"));
507 printf (" %s\n", "-I, --sourceif=IF"); 471 printf(" %s\n", "-I, --sourceif=IF");
508 printf (" %s\n", _("source interface name")); 472 printf(" %s\n", _("source interface name"));
509 printf (UT_VERBOSE); 473 printf(UT_VERBOSE);
510 printf ("\n"); 474 printf("\n");
511 printf (" %s\n", _("THRESHOLD is <rta>,<pl>%% where <rta> is the round trip average travel time (ms)")); 475 printf(" %s\n", _("THRESHOLD is <rta>,<pl>%% where <rta> is the round trip average travel time (ms)"));
512 printf (" %s\n", _("which triggers a WARNING or CRITICAL state, and <pl> is the percentage of")); 476 printf(" %s\n", _("which triggers a WARNING or CRITICAL state, and <pl> is the percentage of"));
513 printf (" %s\n", _("packet loss to trigger an alarm state.")); 477 printf(" %s\n", _("packet loss to trigger an alarm state."));
514 478
515 printf ("\n"); 479 printf("\n");
516 printf (" %s\n", _("IPv4 is used by default. Specify -6 to use IPv6.")); 480 printf(" %s\n", _("IPv4 is used by default. Specify -6 to use IPv6."));
517 481
518 printf (UT_SUPPORT); 482 printf(UT_SUPPORT);
519} 483}
520 484
521 485void print_usage(void) {
522void 486 printf("%s\n", _("Usage:"));
523print_usage (void) 487 printf(" %s <host_address> -w limit -c limit [-b size] [-n number] [-T number] [-i number]\n", progname);
524{
525 printf ("%s\n", _("Usage:"));
526 printf (" %s <host_address> -w limit -c limit [-b size] [-n number] [-T number] [-i number]\n", progname);
527} 488}