int main (int argc, char **argv) { /* normaly should be int result = STATE_UNKNOWN; */ int status = STATE_UNKNOWN; int result = 0; char *fping_prog = NULL; char *server = NULL; char *command_line = NULL; char *input_buffer = NULL; char *option_string = ""; input_buffer = malloc (MAX_INPUT_BUFFER); setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); /* Parse extra opts if any */ argv=np_extra_opts (&argc, argv, progname); if (process_arguments (argc, argv) == ERROR) usage4 (_("Could not parse arguments")); server = strscpy (server, server_name); /* compose the command */ if (target_timeout) xasprintf(&option_string, "%s-t %d ", option_string, target_timeout); if (packet_interval) xasprintf(&option_string, "%s-p %d ", option_string, packet_interval); if (sourceip) xasprintf(&option_string, "%s-S %s ", option_string, sourceip); if (sourceif) xasprintf(&option_string, "%s-I %s ", option_string, sourceif); #ifdef PATH_TO_FPING6 if (address_family == AF_INET6) fping_prog = strdup(PATH_TO_FPING6); else fping_prog = strdup(PATH_TO_FPING); #else fping_prog = strdup(PATH_TO_FPING); #endif xasprintf (&command_line, "%s %s-b %d -c %d %s", fping_prog, option_string, packet_size, packet_count, server); if (verbose) printf ("%s\n", command_line); /* run the command */ child_process = spopen (command_line); if (child_process == NULL) { printf (_("Could not open pipe: %s\n"), command_line); return STATE_UNKNOWN; } child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); if (child_stderr == NULL) { printf (_("Could not open stderr for %s\n"), command_line); } while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { if (verbose) printf ("%s", input_buffer); status = max_state (status, textscan (input_buffer)); } /* If we get anything on STDERR, at least set warning */ while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) { status = max_state (status, STATE_WARNING); if (verbose) printf ("%s", input_buffer); status = max_state (status, textscan (input_buffer)); } (void) fclose (child_stderr); /* close the pipe */ if (result = spclose (child_process)) /* need to use max_state not max */ status = max_state (status, STATE_WARNING); if (result > 1 ) { status = max_state (status, STATE_UNKNOWN); if (result == 2) { die (STATE_UNKNOWN, _("FPING UNKNOWN - IP address not found\n")); } if (result == 3) { die (STATE_UNKNOWN, _("FPING UNKNOWN - invalid commandline argument\n")); } if (result == 4) { die (STATE_UNKNOWN, _("FPING UNKNOWN - failed system call\n")); } } printf ("FPING %s - %s\n", state_text (status), server_name); return status; }
int main (int argc, char **argv) { /* normaly should be int result = STATE_UNKNOWN; */ int status = STATE_UNKNOWN; char *server = NULL; char *command_line = NULL; char *input_buffer = NULL; char *option_string = ""; input_buffer = malloc (MAX_INPUT_BUFFER); setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); /* Parse extra opts if any */ argv=np_extra_opts (&argc, argv, progname); if (process_arguments (argc, argv) == ERROR) usage4 (_("Could not parse arguments")); server = strscpy (server, server_name); /* compose the command */ if (target_timeout) asprintf(&option_string, "%s-t %d ", option_string, target_timeout); if (packet_interval) asprintf(&option_string, "%s-p %d ", option_string, packet_interval); asprintf (&command_line, "%s %s-b %d -c %d %s", PATH_TO_FPING, option_string, packet_size, packet_count, server); if (verbose) printf ("%s\n", command_line); /* run the command */ child_process = spopen (command_line); if (child_process == NULL) { printf (_("Could not open pipe: %s\n"), command_line); return STATE_UNKNOWN; } child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); if (child_stderr == NULL) { printf (_("Could not open stderr for %s\n"), command_line); } while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { if (verbose) printf ("%s", input_buffer); status = max_state (status, textscan (input_buffer)); } /* If we get anything on STDERR, at least set warning */ while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) { status = max_state (status, STATE_WARNING); if (verbose) printf ("%s", input_buffer); status = max_state (status, textscan (input_buffer)); } (void) fclose (child_stderr); /* close the pipe */ if (spclose (child_process)) /* need to use max_state not max */ status = max_state (status, STATE_WARNING); printf ("FPING %s - %s\n", state_text (status), server_name); return status; }