summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--plugins/Makefile.am3
-rw-r--r--plugins/check_fping.c182
-rw-r--r--plugins/check_fping.d/config.h58
3 files changed, 147 insertions, 96 deletions
diff --git a/plugins/Makefile.am b/plugins/Makefile.am
index 41487131..25a286c1 100644
--- a/plugins/Makefile.am
+++ b/plugins/Makefile.am
@@ -57,7 +57,8 @@ EXTRA_DIST = t \
57 check_apt.d \ 57 check_apt.d \
58 check_by_ssh.d \ 58 check_by_ssh.d \
59 check_smtp.d \ 59 check_smtp.d \
60 check_dig.d 60 check_dig.d \
61 check_fping.d
61 62
62PLUGINHDRS = common.h 63PLUGINHDRS = common.h
63 64
diff --git a/plugins/check_fping.c b/plugins/check_fping.c
index b85397d5..ec7abb67 100644
--- a/plugins/check_fping.c
+++ b/plugins/check_fping.c
@@ -38,52 +38,29 @@ const char *email = "devel@monitoring-plugins.org";
38#include "netutils.h" 38#include "netutils.h"
39#include "utils.h" 39#include "utils.h"
40#include <stdbool.h> 40#include <stdbool.h>
41#include "check_fping.d/config.h"
42#include "states.h"
41 43
42enum { 44enum {
43 PACKET_COUNT = 1,
44 PACKET_SIZE = 56,
45 PL = 0, 45 PL = 0,
46 RTA = 1 46 RTA = 1
47}; 47};
48 48
49static int textscan(char *buf); 49static mp_state_enum textscan(char *buf, const char * /*server_name*/, bool /*crta_p*/, double /*crta*/, bool /*wrta_p*/, double /*wrta*/,
50static int process_arguments(int /*argc*/, char ** /*argv*/); 50 bool /*cpl_p*/, int /*cpl*/, bool /*wpl_p*/, int /*wpl*/, bool /*alive_p*/);
51
52typedef struct {
53 int errorcode;
54 check_fping_config config;
55} check_fping_config_wrapper;
56static check_fping_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/);
51static int get_threshold(char *arg, char *rv[2]); 57static int get_threshold(char *arg, char *rv[2]);
52static void print_help(void); 58static void print_help(void);
53void print_usage(void); 59void print_usage(void);
54 60
55static char *server_name = NULL;
56static char *sourceip = NULL;
57static char *sourceif = NULL;
58static int packet_size = PACKET_SIZE;
59static int packet_count = PACKET_COUNT;
60static int target_timeout = 0;
61static int packet_interval = 0;
62static bool verbose = false; 61static bool verbose = false;
63static bool dontfrag = false;
64static bool randomize_packet_data = false;
65static int cpl;
66static int wpl;
67static double crta;
68static double wrta;
69static bool cpl_p = false;
70static bool wpl_p = false;
71static bool alive_p = false;
72static bool crta_p = false;
73static bool wrta_p = false;
74 62
75int main(int argc, char **argv) { 63int main(int argc, char **argv) {
76 /* normally should be int result = STATE_UNKNOWN; */
77
78 int status = STATE_UNKNOWN;
79 int result = 0;
80 char *fping_prog = NULL;
81 char *server = NULL;
82 char *command_line = NULL;
83 char *input_buffer = NULL;
84 char *option_string = "";
85 input_buffer = malloc(MAX_INPUT_BUFFER);
86
87 setlocale(LC_ALL, ""); 64 setlocale(LC_ALL, "");
88 bindtextdomain(PACKAGE, LOCALEDIR); 65 bindtextdomain(PACKAGE, LOCALEDIR);
89 textdomain(PACKAGE); 66 textdomain(PACKAGE);
@@ -91,32 +68,38 @@ int main(int argc, char **argv) {
91 /* Parse extra opts if any */ 68 /* Parse extra opts if any */
92 argv = np_extra_opts(&argc, argv, progname); 69 argv = np_extra_opts(&argc, argv, progname);
93 70
94 if (process_arguments(argc, argv) == ERROR) { 71 check_fping_config_wrapper tmp_config = process_arguments(argc, argv);
72 if (tmp_config.errorcode == ERROR) {
95 usage4(_("Could not parse arguments")); 73 usage4(_("Could not parse arguments"));
96 } 74 }
97 75
98 server = strscpy(server, server_name); 76 const check_fping_config config = tmp_config.config;
77
78 char *server = NULL;
79 server = strscpy(server, config.server_name);
99 80
81 char *option_string = "";
100 /* compose the command */ 82 /* compose the command */
101 if (target_timeout) { 83 if (config.target_timeout) {
102 xasprintf(&option_string, "%s-t %d ", option_string, target_timeout); 84 xasprintf(&option_string, "%s-t %d ", option_string, config.target_timeout);
103 } 85 }
104 if (packet_interval) { 86 if (config.packet_interval) {
105 xasprintf(&option_string, "%s-p %d ", option_string, packet_interval); 87 xasprintf(&option_string, "%s-p %d ", option_string, config.packet_interval);
106 } 88 }
107 if (sourceip) { 89 if (config.sourceip) {
108 xasprintf(&option_string, "%s-S %s ", option_string, sourceip); 90 xasprintf(&option_string, "%s-S %s ", option_string, config.sourceip);
109 } 91 }
110 if (sourceif) { 92 if (config.sourceif) {
111 xasprintf(&option_string, "%s-I %s ", option_string, sourceif); 93 xasprintf(&option_string, "%s-I %s ", option_string, config.sourceif);
112 } 94 }
113 if (dontfrag) { 95 if (config.dontfrag) {
114 xasprintf(&option_string, "%s-M ", option_string); 96 xasprintf(&option_string, "%s-M ", option_string);
115 } 97 }
116 if (randomize_packet_data) { 98 if (config.randomize_packet_data) {
117 xasprintf(&option_string, "%s-R ", option_string); 99 xasprintf(&option_string, "%s-R ", option_string);
118 } 100 }
119 101
102 char *fping_prog = NULL;
120#ifdef PATH_TO_FPING6 103#ifdef PATH_TO_FPING6
121 if (address_family != AF_INET && is_inet6_addr(server)) { 104 if (address_family != AF_INET && is_inet6_addr(server)) {
122 fping_prog = strdup(PATH_TO_FPING6); 105 fping_prog = strdup(PATH_TO_FPING6);
@@ -127,7 +110,8 @@ int main(int argc, char **argv) {
127 fping_prog = strdup(PATH_TO_FPING); 110 fping_prog = strdup(PATH_TO_FPING);
128#endif 111#endif
129 112
130 xasprintf(&command_line, "%s %s-b %d -c %d %s", fping_prog, option_string, packet_size, packet_count, server); 113 char *command_line = NULL;
114 xasprintf(&command_line, "%s %s-b %d -c %d %s", fping_prog, option_string, config.packet_size, config.packet_count, server);
131 115
132 if (verbose) { 116 if (verbose) {
133 printf("%s\n", command_line); 117 printf("%s\n", command_line);
@@ -145,11 +129,14 @@ int main(int argc, char **argv) {
145 printf(_("Could not open stderr for %s\n"), command_line); 129 printf(_("Could not open stderr for %s\n"), command_line);
146 } 130 }
147 131
132 char *input_buffer = malloc(MAX_INPUT_BUFFER);
133 mp_state_enum status = STATE_UNKNOWN;
148 while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { 134 while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
149 if (verbose) { 135 if (verbose) {
150 printf("%s", input_buffer); 136 printf("%s", input_buffer);
151 } 137 }
152 status = max_state(status, textscan(input_buffer)); 138 status = max_state(status, textscan(input_buffer, config.server_name, config.crta_p, config.crta, config.wrta_p, config.wrta,
139 config.cpl_p, config.cpl, config.wpl_p, config.wpl, config.alive_p));
153 } 140 }
154 141
155 /* If we get anything on STDERR, at least set warning */ 142 /* If we get anything on STDERR, at least set warning */
@@ -158,12 +145,13 @@ int main(int argc, char **argv) {
158 if (verbose) { 145 if (verbose) {
159 printf("%s", input_buffer); 146 printf("%s", input_buffer);
160 } 147 }
161 status = max_state(status, textscan(input_buffer)); 148 status = max_state(status, textscan(input_buffer, config.server_name, config.crta_p, config.crta, config.wrta_p, config.wrta,
149 config.cpl_p, config.cpl, config.wpl_p, config.wpl, config.alive_p));
162 } 150 }
163 (void)fclose(child_stderr); 151 (void)fclose(child_stderr);
164 152
165 /* close the pipe */ 153 /* close the pipe */
166 result = spclose(child_process); 154 int result = spclose(child_process);
167 if (result) { 155 if (result) {
168 /* need to use max_state not max */ 156 /* need to use max_state not max */
169 status = max_state(status, STATE_WARNING); 157 status = max_state(status, STATE_WARNING);
@@ -182,21 +170,17 @@ int main(int argc, char **argv) {
182 } 170 }
183 } 171 }
184 172
185 printf("FPING %s - %s\n", state_text(status), server_name); 173 printf("FPING %s - %s\n", state_text(status), config.server_name);
186 174
187 return status; 175 return status;
188} 176}
189 177
190int textscan(char *buf) { 178mp_state_enum textscan(char *buf, const char *server_name, bool crta_p, double crta, bool wrta_p, double wrta, bool cpl_p, int cpl,
191 char *rtastr = NULL; 179 bool wpl_p, int wpl, bool alive_p) {
192 char *losstr = NULL;
193 char *xmtstr = NULL;
194 double loss;
195 double rta;
196 double xmt;
197 int status = STATE_UNKNOWN;
198
199 /* stops testing after the first successful reply. */ 180 /* stops testing after the first successful reply. */
181 double rta;
182 double loss;
183 char *rtastr = NULL;
200 if (alive_p && strstr(buf, "avg, 0% loss)")) { 184 if (alive_p && strstr(buf, "avg, 0% loss)")) {
201 rtastr = strstr(buf, "ms ("); 185 rtastr = strstr(buf, "ms (");
202 rtastr = 1 + index(rtastr, '('); 186 rtastr = 1 + index(rtastr, '(');
@@ -208,6 +192,10 @@ int textscan(char *buf) {
208 fperfdata("rta", rta / 1.0e3, "s", wrta_p, wrta / 1.0e3, crta_p, crta / 1.0e3, true, 0, false, 0)); 192 fperfdata("rta", rta / 1.0e3, "s", wrta_p, wrta / 1.0e3, crta_p, crta / 1.0e3, true, 0, false, 0));
209 } 193 }
210 194
195 mp_state_enum status = STATE_UNKNOWN;
196 char *xmtstr = NULL;
197 double xmt;
198 char *losstr = NULL;
211 if (strstr(buf, "not found")) { 199 if (strstr(buf, "not found")) {
212 die(STATE_CRITICAL, _("FPING UNKNOWN - %s not found\n"), server_name); 200 die(STATE_CRITICAL, _("FPING UNKNOWN - %s not found\n"), server_name);
213 201
@@ -243,7 +231,7 @@ int textscan(char *buf) {
243 status = STATE_OK; 231 status = STATE_OK;
244 } 232 }
245 die(status, _("FPING %s - %s (loss=%.0f%%, rta=%f ms)|%s %s\n"), state_text(status), server_name, loss, rta, 233 die(status, _("FPING %s - %s (loss=%.0f%%, rta=%f ms)|%s %s\n"), state_text(status), server_name, loss, rta,
246 perfdata("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, true, 0, true, 100), 234 perfdata("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, false, 0, false, 0),
247 fperfdata("rta", rta / 1.0e3, "s", wrta_p, wrta / 1.0e3, crta_p, crta / 1.0e3, true, 0, false, 0)); 235 fperfdata("rta", rta / 1.0e3, "s", wrta_p, wrta / 1.0e3, crta_p, crta / 1.0e3, true, 0, false, 0));
248 236
249 } else if (strstr(buf, "xmt/rcv/%loss")) { 237 } else if (strstr(buf, "xmt/rcv/%loss")) {
@@ -269,7 +257,7 @@ int textscan(char *buf) {
269 } 257 }
270 /* loss=%.0f%%;%d;%d;0;100 */ 258 /* loss=%.0f%%;%d;%d;0;100 */
271 die(status, _("FPING %s - %s (loss=%.0f%% )|%s\n"), state_text(status), server_name, loss, 259 die(status, _("FPING %s - %s (loss=%.0f%% )|%s\n"), state_text(status), server_name, loss,
272 perfdata("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, true, 0, true, 100)); 260 perfdata("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, false, 0, false, 0));
273 261
274 } else { 262 } else {
275 status = max_state(status, STATE_WARNING); 263 status = max_state(status, STATE_WARNING);
@@ -279,11 +267,7 @@ int textscan(char *buf) {
279} 267}
280 268
281/* process command-line arguments */ 269/* process command-line arguments */
282int process_arguments(int argc, char **argv) { 270check_fping_config_wrapper process_arguments(int argc, char **argv) {
283 int c;
284 char *rv[2];
285
286 int option = 0;
287 static struct option longopts[] = { 271 static struct option longopts[] = {
288 {"hostname", required_argument, 0, 'H'}, {"sourceip", required_argument, 0, 'S'}, {"sourceif", required_argument, 0, 'I'}, 272 {"hostname", required_argument, 0, 'H'}, {"sourceip", required_argument, 0, 'S'}, {"sourceif", required_argument, 0, 'I'},
289 {"critical", required_argument, 0, 'c'}, {"warning", required_argument, 0, 'w'}, {"alive", no_argument, 0, 'a'}, 273 {"critical", required_argument, 0, 'c'}, {"warning", required_argument, 0, 'w'}, {"alive", no_argument, 0, 'a'},
@@ -292,32 +276,41 @@ int process_arguments(int argc, char **argv) {
292 {"help", no_argument, 0, 'h'}, {"use-ipv4", no_argument, 0, '4'}, {"use-ipv6", no_argument, 0, '6'}, 276 {"help", no_argument, 0, 'h'}, {"use-ipv4", no_argument, 0, '4'}, {"use-ipv6", no_argument, 0, '6'},
293 {"dontfrag", no_argument, 0, 'M'}, {"random", no_argument, 0, 'R'}, {0, 0, 0, 0}}; 277 {"dontfrag", no_argument, 0, 'M'}, {"random", no_argument, 0, 'R'}, {0, 0, 0, 0}};
294 278
279 char *rv[2];
295 rv[PL] = NULL; 280 rv[PL] = NULL;
296 rv[RTA] = NULL; 281 rv[RTA] = NULL;
297 282
283 int option = 0;
284
285 check_fping_config_wrapper result = {
286 .errorcode = OK,
287 .config = check_fping_config_init(),
288 };
289
298 if (argc < 2) { 290 if (argc < 2) {
299 return ERROR; 291 result.errorcode = ERROR;
292 return result;
300 } 293 }
301 294
302 if (!is_option(argv[1])) { 295 if (!is_option(argv[1])) {
303 server_name = argv[1]; 296 result.config.server_name = argv[1];
304 argv[1] = argv[0]; 297 argv[1] = argv[0];
305 argv = &argv[1]; 298 argv = &argv[1];
306 argc--; 299 argc--;
307 } 300 }
308 301
309 while (1) { 302 while (1) {
310 c = getopt_long(argc, argv, "+hVvaH:S:c:w:b:n:T:i:I:M:R:46", longopts, &option); 303 int option_index = getopt_long(argc, argv, "+hVvaH:S:c:w:b:n:T:i:I:M:R:46", longopts, &option);
311 304
312 if (c == -1 || c == EOF || c == 1) { 305 if (option_index == -1 || option_index == EOF || option_index == 1) {
313 break; 306 break;
314 } 307 }
315 308
316 switch (c) { 309 switch (option_index) {
317 case '?': /* print short usage statement if args not parsable */ 310 case '?': /* print short usage statement if args not parsable */
318 usage5(); 311 usage5();
319 case 'a': /* host alive mode */ 312 case 'a': /* host alive mode */
320 alive_p = true; 313 result.config.alive_p = true;
321 break; 314 break;
322 case 'h': /* help */ 315 case 'h': /* help */
323 print_help(); 316 print_help();
@@ -329,19 +322,19 @@ int process_arguments(int argc, char **argv) {
329 verbose = true; 322 verbose = true;
330 break; 323 break;
331 case 'H': /* hostname */ 324 case 'H': /* hostname */
332 if (is_host(optarg) == false) { 325 if (!is_host(optarg)) {
333 usage2(_("Invalid hostname/address"), optarg); 326 usage2(_("Invalid hostname/address"), optarg);
334 } 327 }
335 server_name = strscpy(server_name, optarg); 328 result.config.server_name = optarg;
336 break; 329 break;
337 case 'S': /* sourceip */ 330 case 'S': /* sourceip */
338 if (is_host(optarg) == false) { 331 if (!is_host(optarg)) {
339 usage2(_("Invalid hostname/address"), optarg); 332 usage2(_("Invalid hostname/address"), optarg);
340 } 333 }
341 sourceip = strscpy(sourceip, optarg); 334 result.config.sourceip = optarg;
342 break; 335 break;
343 case 'I': /* sourceip */ 336 case 'I': /* sourceip */
344 sourceif = strscpy(sourceif, optarg); 337 result.config.sourceif = optarg;
345 break; 338 break;
346 case '4': /* IPv4 only */ 339 case '4': /* IPv4 only */
347 address_family = AF_INET; 340 address_family = AF_INET;
@@ -356,78 +349,77 @@ int process_arguments(int argc, char **argv) {
356 case 'c': 349 case 'c':
357 get_threshold(optarg, rv); 350 get_threshold(optarg, rv);
358 if (rv[RTA]) { 351 if (rv[RTA]) {
359 crta = strtod(rv[RTA], NULL); 352 result.config.crta = strtod(rv[RTA], NULL);
360 crta_p = true; 353 result.config.crta_p = true;
361 rv[RTA] = NULL; 354 rv[RTA] = NULL;
362 } 355 }
363 if (rv[PL]) { 356 if (rv[PL]) {
364 cpl = atoi(rv[PL]); 357 result.config.cpl = atoi(rv[PL]);
365 cpl_p = true; 358 result.config.cpl_p = true;
366 rv[PL] = NULL; 359 rv[PL] = NULL;
367 } 360 }
368 break; 361 break;
369 case 'w': 362 case 'w':
370 get_threshold(optarg, rv); 363 get_threshold(optarg, rv);
371 if (rv[RTA]) { 364 if (rv[RTA]) {
372 wrta = strtod(rv[RTA], NULL); 365 result.config.wrta = strtod(rv[RTA], NULL);
373 wrta_p = true; 366 result.config.wrta_p = true;
374 rv[RTA] = NULL; 367 rv[RTA] = NULL;
375 } 368 }
376 if (rv[PL]) { 369 if (rv[PL]) {
377 wpl = atoi(rv[PL]); 370 result.config.wpl = atoi(rv[PL]);
378 wpl_p = true; 371 result.config.wpl_p = true;
379 rv[PL] = NULL; 372 rv[PL] = NULL;
380 } 373 }
381 break; 374 break;
382 case 'b': /* bytes per packet */ 375 case 'b': /* bytes per packet */
383 if (is_intpos(optarg)) { 376 if (is_intpos(optarg)) {
384 packet_size = atoi(optarg); 377 result.config.packet_size = atoi(optarg);
385 } else { 378 } else {
386 usage(_("Packet size must be a positive integer")); 379 usage(_("Packet size must be a positive integer"));
387 } 380 }
388 break; 381 break;
389 case 'n': /* number of packets */ 382 case 'n': /* number of packets */
390 if (is_intpos(optarg)) { 383 if (is_intpos(optarg)) {
391 packet_count = atoi(optarg); 384 result.config.packet_count = atoi(optarg);
392 } else { 385 } else {
393 usage(_("Packet count must be a positive integer")); 386 usage(_("Packet count must be a positive integer"));
394 } 387 }
395 break; 388 break;
396 case 'T': /* timeout in msec */ 389 case 'T': /* timeout in msec */
397 if (is_intpos(optarg)) { 390 if (is_intpos(optarg)) {
398 target_timeout = atoi(optarg); 391 result.config.target_timeout = atoi(optarg);
399 } else { 392 } else {
400 usage(_("Target timeout must be a positive integer")); 393 usage(_("Target timeout must be a positive integer"));
401 } 394 }
402 break; 395 break;
403 case 'i': /* interval in msec */ 396 case 'i': /* interval in msec */
404 if (is_intpos(optarg)) { 397 if (is_intpos(optarg)) {
405 packet_interval = atoi(optarg); 398 result.config.packet_interval = atoi(optarg);
406 } else { 399 } else {
407 usage(_("Interval must be a positive integer")); 400 usage(_("Interval must be a positive integer"));
408 } 401 }
409 break; 402 break;
410 case 'R': 403 case 'R':
411 randomize_packet_data = true; 404 result.config.randomize_packet_data = true;
412 break; 405 break;
413 case 'M': 406 case 'M':
414 dontfrag = true; 407 result.config.dontfrag = true;
415 break; 408 break;
416 } 409 }
417 } 410 }
418 411
419 if (server_name == NULL) { 412 if (result.config.server_name == NULL) {
420 usage4(_("Hostname was not supplied")); 413 usage4(_("Hostname was not supplied"));
421 } 414 }
422 415
423 return OK; 416 return result;
424} 417}
425 418
426int get_threshold(char *arg, char *rv[2]) { 419int get_threshold(char *arg, char *rv[2]) {
427 char *arg1 = NULL;
428 char *arg2 = NULL; 420 char *arg2 = NULL;
429 421
430 arg1 = strscpy(arg1, arg); 422 char *arg1 = strdup(arg);
431 if (strpbrk(arg1, ",:")) { 423 if (strpbrk(arg1, ",:")) {
432 arg2 = 1 + strpbrk(arg1, ",:"); 424 arg2 = 1 + strpbrk(arg1, ",:");
433 } 425 }
diff --git a/plugins/check_fping.d/config.h b/plugins/check_fping.d/config.h
new file mode 100644
index 00000000..a0697bf3
--- /dev/null
+++ b/plugins/check_fping.d/config.h
@@ -0,0 +1,58 @@
1#pragma once
2
3#include "../../config.h"
4#include <stddef.h>
5
6enum {
7 PACKET_SIZE = 56,
8 PACKET_COUNT = 1,
9};
10
11typedef struct {
12 char *server_name;
13 char *sourceip;
14 char *sourceif;
15 int packet_size;
16 int packet_count;
17 int target_timeout;
18 int packet_interval;
19 bool randomize_packet_data;
20 bool dontfrag;
21 bool alive_p;
22
23 double crta;
24 bool crta_p;
25 double wrta;
26 bool wrta_p;
27
28 int cpl;
29 bool cpl_p;
30 int wpl;
31 bool wpl_p;
32} check_fping_config;
33
34check_fping_config check_fping_config_init() {
35 check_fping_config tmp = {
36 .server_name = NULL,
37 .sourceip = NULL,
38 .sourceif = NULL,
39 .packet_size = PACKET_SIZE,
40 .packet_count = PACKET_COUNT,
41 .target_timeout = 0,
42 .packet_interval = 0,
43 .randomize_packet_data = false,
44 .dontfrag = false,
45 .alive_p = false,
46
47 .crta = 0,
48 .crta_p = false,
49 .wrta = 0,
50 .wrta_p = false,
51
52 .cpl = 0,
53 .cpl_p = false,
54 .wpl = 0,
55 .wpl_p = false,
56 };
57 return tmp;
58}