/* process command-line arguments */ int process_arguments (int argc, char **argv) { int c; char *warning = NULL; char *critical = NULL; int option = 0; static struct option longopts[] = { {"hostname", required_argument, 0, 'H'}, {"socket", required_argument, 0, 's'}, {"database", required_argument, 0, 'd'}, {"username", required_argument, 0, 'u'}, {"password", required_argument, 0, 'p'}, {"file", required_argument, 0, 'f'}, {"group", required_argument, 0, 'g'}, {"port", required_argument, 0, 'P'}, {"critical", required_argument, 0, 'c'}, {"warning", required_argument, 0, 'w'}, {"check-slave", no_argument, 0, 'S'}, {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} }; if (argc < 1) return ERROR; while (1) { c = getopt_long (argc, argv, "hvVSP:p:u:d:f:g:H:s:c:w:", longopts, &option); if (c == -1 || c == EOF) break; switch (c) { case 'H': /* hostname */ if (is_host (optarg)) { db_host = optarg; } else { usage2 (_("Invalid hostname/address"), optarg); } break; case 's': /* socket */ db_socket = optarg; break; case 'd': /* database */ db = optarg; break; case 'u': /* username */ db_user = optarg; break; case 'p': /* authentication information: password */ db_pass = strdup(optarg); /* Delete the password from process list */ while (*optarg != '\0') { *optarg = 'X'; optarg++; } break; case 'f': /* username */ opt_file = optarg; break; case 'g': /* username */ opt_group = optarg; break; case 'P': /* critical time threshold */ db_port = atoi (optarg); break; case 'S': check_slave = 1; /* check-slave */ break; case 'w': warning = optarg; break; case 'c': critical = optarg; break; case 'V': /* version */ print_revision (progname, NP_VERSION); exit (STATE_OK); case 'h': /* help */ print_help (); exit (STATE_OK); case 'v': verbose++; break; case '?': /* help */ usage5 (); } } c = optind; set_thresholds(&my_threshold, warning, critical); while ( argc > c ) { if (db_host == NULL) if (is_host (argv[c])) { db_host = argv[c++]; } else { usage2 (_("Invalid hostname/address"), argv[c]); } else if (db_user == NULL) db_user = argv[c++]; else if (opt_file == NULL) opt_file = argv[c++]; else if (opt_group == NULL) opt_group = argv[c++]; else if (db_pass == NULL) db_pass = argv[c++]; else if (db == NULL) db = argv[c++]; else if (is_intnonneg (argv[c])) db_port = atoi (argv[c++]); else break; } return validate_arguments (); }
/* process command-line arguments */ int process_arguments (int argc, char **argv) { int c; char *warning = NULL; char *critical = NULL; int option = 0; static struct option longopts[] = { {"hostname", required_argument, 0, 'H'}, {"socket", required_argument, 0, 's'}, {"database", required_argument, 0, 'd'}, {"username", required_argument, 0, 'u'}, {"password", required_argument, 0, 'p'}, {"port", required_argument, 0, 'P'}, {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {"query", required_argument, 0, 'q'}, {"warning", required_argument, 0, 'w'}, {"critical", required_argument, 0, 'c'}, {0, 0, 0, 0} }; if (argc < 1) return ERROR; while (1) { c = getopt_long (argc, argv, "hvVP:p:u:d:H:s:q:w:c:", longopts, &option); if (c == -1 || c == EOF) break; switch (c) { case 'H': /* hostname */ if (is_host (optarg)) { db_host = optarg; } else { usage2 (_("Invalid hostname/address"), optarg); } break; case 's': /* socket */ db_socket = optarg; break; case 'd': /* database */ db = optarg; break; case 'u': /* username */ db_user = optarg; break; case 'p': /* authentication information: password */ db_pass = strdup(optarg); /* Delete the password from process list */ while (*optarg != '\0') { *optarg = 'X'; optarg++; } break; case 'P': /* critical time threshold */ db_port = atoi (optarg); break; case 'v': verbose++; break; case 'V': /* version */ print_revision (progname, NP_VERSION); exit (STATE_OK); case 'h': /* help */ print_help (); exit (STATE_OK); case 'q': asprintf(&sql_query, "%s", optarg); break; case 'w': warning = optarg; break; case 'c': critical = optarg; break; case '?': /* help */ usage5 (); } } c = optind; set_thresholds(&my_thresholds, warning, critical); return validate_arguments (); }
/* process command-line arguments */ int process_arguments (int argc, char **argv) { int c; int option = 0; static struct option longopts[] = { {"logfile", required_argument, 0, 'F'}, {"expires", required_argument, 0, 'e'}, {"aggregation", required_argument, 0, 'a'}, {"variable", required_argument, 0, 'v'}, {"critical", required_argument, 0, 'c'}, {"warning", required_argument, 0, 'w'}, {"label", required_argument, 0, 'l'}, {"units", required_argument, 0, 'u'}, {"variable", required_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} }; if (argc < 2) return ERROR; for (c = 1; c < argc; c++) { if (strcmp ("-to", argv[c]) == 0) strcpy (argv[c], "-t"); else if (strcmp ("-wt", argv[c]) == 0) strcpy (argv[c], "-w"); else if (strcmp ("-ct", argv[c]) == 0) strcpy (argv[c], "-c"); } while (1) { c = getopt_long (argc, argv, "hVF:e:a:v:c:w:l:u:", longopts, &option); if (c == -1 || c == EOF) break; switch (c) { case 'F': /* input file */ log_file = optarg; break; case 'e': /* ups name */ expire_minutes = atoi (optarg); break; case 'a': /* port */ if (!strcmp (optarg, "MAX")) use_average = FALSE; else use_average = TRUE; break; case 'v': variable_number = atoi (optarg); if (variable_number < 1 || variable_number > 2) usage4 (_("Invalid variable number")); break; case 'w': /* critical time threshold */ value_warning_threshold = strtoul (optarg, NULL, 10); break; case 'c': /* warning time threshold */ value_critical_threshold = strtoul (optarg, NULL, 10); break; case 'l': /* label */ label = optarg; break; case 'u': /* timeout */ units = optarg; break; case 'V': /* version */ print_revision (progname, NP_VERSION); exit (STATE_OK); case 'h': /* help */ print_help (); exit (STATE_OK); case '?': /* help */ usage5 (); } } c = optind; if (log_file == NULL && argc > c) { log_file = argv[c++]; } if (expire_minutes <= 0 && argc > c) { if (is_intpos (argv[c])) expire_minutes = atoi (argv[c++]); else die (STATE_UNKNOWN, _("%s is not a valid expiration time\nUse '%s -h' for additional help\n"), argv[c], progname); } if (argc > c && strcmp (argv[c], "MAX") == 0) { use_average = FALSE; c++; } else if (argc > c && strcmp (argv[c], "AVG") == 0) { use_average = TRUE; c++; } if (argc > c && variable_number == -1) { variable_number = atoi (argv[c++]); if (variable_number < 1 || variable_number > 2) { printf ("%s :", argv[c]); usage (_("Invalid variable number\n")); } } if (argc > c && value_warning_threshold == 0) { value_warning_threshold = strtoul (argv[c++], NULL, 10); } if (argc > c && value_critical_threshold == 0) { value_critical_threshold = strtoul (argv[c++], NULL, 10); } if (argc > c && strlen (label) == 0) { label = argv[c++]; } if (argc > c && strlen (units) == 0) { units = argv[c++]; } return validate_arguments (); }
/* process command-line arguments */ int process_arguments (int argc, char **argv) { int c; int option = 0; static struct option longopts[] = { {"filename", required_argument, 0, 'F'}, {"expires", required_argument, 0, 'e'}, {"command", required_argument, 0, 'C'}, {"timeout", optional_argument, 0, 't'}, {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {"verbose", no_argument, 0, 'v'}, {0, 0, 0, 0} }; if (argc < 2) return ERROR; if (!is_option (argv[1])) { status_log = argv[1]; if (is_intnonneg (argv[2])) expire_minutes = atoi (argv[2]); else die (STATE_UNKNOWN, _("Expiration time must be an integer (seconds)\n")); process_string = argv[3]; return OK; } while (1) { c = getopt_long (argc, argv, "+hVvF:C:e:t:", longopts, &option); if (c == -1 || c == EOF || c == 1) break; switch (c) { case 'h': /* help */ print_help (); exit (STATE_OK); case 'V': /* version */ print_revision (progname, NP_VERSION); exit (STATE_OK); case 'F': /* status log */ status_log = optarg; break; case 'C': /* command */ process_string = optarg; break; case 'e': /* expiry time */ if (is_intnonneg (optarg)) expire_minutes = atoi (optarg); else die (STATE_UNKNOWN, _("Expiration time must be an integer (seconds)\n")); break; case 't': /* timeout */ timeout_interval = parse_timeout_string (optarg); break; case 'v': verbose++; break; default: /* print short usage_va statement if args not parsable */ usage5(); } } if (status_log == NULL) die (STATE_UNKNOWN, _("You must provide the status_log\n")); if (process_string == NULL) die (STATE_UNKNOWN, _("You must provide a process string\n")); return OK; }
/* process command-line arguments */ int process_arguments (int argc, char **argv) { int c; int option = 0; static struct option longopts[] = { {"hostname", required_argument, 0, 'H'}, {"IPaddress", required_argument, 0, 'I'}, {"expect", required_argument, 0, 'e'}, {"url", required_argument, 0, 'u'}, {"port", required_argument, 0, 'p'}, {"critical", required_argument, 0, 'c'}, {"warning", required_argument, 0, 'w'}, {"timeout", required_argument, 0, 't'}, {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} }; if (argc < 2) return ERROR; for (c = 1; c < argc; c++) { if (strcmp ("-to", argv[c]) == 0) strcpy (argv[c], "-t"); else if (strcmp ("-wt", argv[c]) == 0) strcpy (argv[c], "-w"); else if (strcmp ("-ct", argv[c]) == 0) strcpy (argv[c], "-c"); } while (1) { c = getopt_long (argc, argv, "+hvVI:H:e:u:p:w:c:t:", longopts, &option); if (c == -1 || c == EOF) break; switch (c) { case 'I': /* hostname */ case 'H': /* hostname */ if (server_address) break; else if (is_host (optarg)) server_address = optarg; else usage2 (_("Invalid hostname/address"), optarg); break; case 'e': /* string to expect in response header */ server_expect = optarg; break; case 'u': /* server URL */ server_url = optarg; break; case 'p': /* port */ if (is_intpos (optarg)) { server_port = atoi (optarg); } else { usage4 (_("Port must be a positive integer")); } break; case 'w': /* warning time threshold */ if (is_intnonneg (optarg)) { warning_time = atoi (optarg); check_warning_time = TRUE; } else { usage4 (_("Warning time must be a positive integer")); } break; case 'c': /* critical time threshold */ if (is_intnonneg (optarg)) { critical_time = atoi (optarg); check_critical_time = TRUE; } else { usage4 (_("Critical time must be a positive integer")); } break; case 'v': /* verbose */ verbose = TRUE; break; case 't': /* timeout */ if (is_intnonneg (optarg)) { socket_timeout = atoi (optarg); } else { usage4 (_("Timeout interval must be a positive integer")); } break; case 'V': /* version */ print_revision (progname, revision); exit (STATE_OK); case 'h': /* help */ print_help (); exit (STATE_OK); case '?': /* usage */ usage5 (); } } c = optind; if (server_address==NULL && argc>c) { if (is_host (argv[c])) { server_address = argv[c++]; } else { usage2 (_("Invalid hostname/address"), argv[c]); } } if (server_address==NULL) usage4 (_("You must provide a server to check")); if (host_name==NULL) host_name = strdup (server_address); if (server_expect == NULL) server_expect = strdup(EXPECT); return validate_arguments (); }
/* process command-line arguments */ int process_arguments(int argc, char **argv){ int c; int option = 0; static struct option longopts[] = { {"port", required_argument,0,'p'}, {"timeout", required_argument,0,'t'}, {"critical", required_argument,0,'c'}, {"warning", required_argument,0,'w'}, {"variable", required_argument,0,'v'}, {"hostname", required_argument,0,'H'}, {"params", required_argument,0,'l'}, {"secret", required_argument,0,'s'}, {"display", required_argument,0,'d'}, {"unknown-timeout", no_argument, 0, 'u'}, {"version", no_argument, 0,'V'}, {"help", no_argument, 0,'h'}, {0,0,0,0} }; /* no options were supplied */ if(argc<2) return ERROR; /* backwards compatibility */ if (! is_option(argv[1])) { server_address = strdup(argv[1]); argv[1]=argv[0]; argv=&argv[1]; argc--; } for (c=1;c<argc;c++) { if(strcmp("-to",argv[c])==0) strcpy(argv[c],"-t"); else if (strcmp("-wv",argv[c])==0) strcpy(argv[c],"-w"); else if (strcmp("-cv",argv[c])==0) strcpy(argv[c],"-c"); } while (1) { c = getopt_long(argc,argv,"+hVH:t:c:w:p:v:l:s:d:u",longopts,&option); if (c==-1||c==EOF||c==1) break; switch (c) { case '?': /* print short usage statement if args not parsable */ usage5 (); case 'h': /* help */ print_help(); exit(STATE_OK); case 'V': /* version */ print_revision(progname, NP_VERSION); exit(STATE_OK); case 'H': /* hostname */ server_address = optarg; break; case 's': /* password */ req_password = optarg; break; case 'p': /* port */ if (is_intnonneg(optarg)) server_port=atoi(optarg); else die(STATE_UNKNOWN,_("Server port must be an integer\n")); break; case 'v': if(strlen(optarg)<4) return ERROR; if(!strcmp(optarg,"CLIENTVERSION")) vars_to_check=CHECK_CLIENTVERSION; else if(!strcmp(optarg,"CPULOAD")) vars_to_check=CHECK_CPULOAD; else if(!strcmp(optarg,"UPTIME")) vars_to_check=CHECK_UPTIME; else if(!strcmp(optarg,"USEDDISKSPACE")) vars_to_check=CHECK_USEDDISKSPACE; else if(!strcmp(optarg,"SERVICESTATE")) vars_to_check=CHECK_SERVICESTATE; else if(!strcmp(optarg,"PROCSTATE")) vars_to_check=CHECK_PROCSTATE; else if(!strcmp(optarg,"MEMUSE")) vars_to_check=CHECK_MEMUSE; else if(!strcmp(optarg,"COUNTER")) vars_to_check=CHECK_COUNTER; else if(!strcmp(optarg,"FILEAGE")) vars_to_check=CHECK_FILEAGE; else if(!strcmp(optarg,"INSTANCES")) vars_to_check=CHECK_INSTANCES; else return ERROR; break; case 'l': /* value list */ value_list = optarg; break; case 'w': /* warning threshold */ warning_value=strtoul(optarg,NULL,10); check_warning_value=TRUE; break; case 'c': /* critical threshold */ critical_value=strtoul(optarg,NULL,10); check_critical_value=TRUE; break; case 'd': /* Display select for services */ if (!strcmp(optarg,"SHOWALL")) show_all = TRUE; break; case 'u': socket_timeout_state=STATE_UNKNOWN; break; case 't': /* timeout */ socket_timeout=atoi(optarg); if(socket_timeout<=0) return ERROR; } } if (server_address == NULL) usage4 (_("You must provide a server address or host name")); if (vars_to_check==CHECK_NONE) return ERROR; if (req_password == NULL) req_password = strdup (_("None")); return OK; }
/* process command-line arguments */ int process_arguments (int argc, char **argv) { int c; char *warning = NULL; char *critical = NULL; int option = 0; static struct option longopts[] = { {"hostname" ,required_argument, 0, 'H'}, {"port" ,required_argument, 0, 'P'}, {"expire" ,required_argument, 0, 'E'}, {"verbose" ,no_argument, 0, 'v'}, {"version" ,no_argument, 0, 'V'}, {"help" ,no_argument, 0, 'h'}, {"warning" ,required_argument, 0, 'w'}, {"critical" ,required_argument, 0, 'c'}, {0, 0, 0, 0} }; if (argc < 1) return ERROR; while (1) { c = getopt_long (argc, argv, "H:P:E:vVhw:c:", longopts, &option); if (c == -1 || c == EOF) break; switch (c) { case 'H': if (is_host(optarg)) { mc_host = optarg; } else { usage2(_("Invalid hostname/address"), optarg); } break; case 'P': mc_port = atoi(optarg); break; case 'E': mc_expire = atoi(optarg); break; case 'v': verbose++; break; case 'V': print_revision(progname, revision); exit(STATE_OK); case 'h': print_help(); exit (STATE_OK); case 'w': warning = optarg; break; case 'c': critical = optarg; break; case '?': usage5(); } } c = optind; set_thresholds(&my_thresholds, warning, critical); return validate_arguments(); }
/* process command-line arguments */ int process_arguments (int argc, char **argv) { int c; char *p1, *p2; int option = 0; static struct option longopts[] = { {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {"verbose", no_argument, 0, 'v'}, {"fork", no_argument, 0, 'f'}, {"timeout", required_argument, 0, 't'}, {"host", required_argument, 0, 'H'}, /* backward compatibility */ {"hostname", required_argument, 0, 'H'}, {"port", required_argument,0,'p'}, {"output", required_argument, 0, 'O'}, {"name", required_argument, 0, 'n'}, {"services", required_argument, 0, 's'}, {"identity", required_argument, 0, 'i'}, {"user", required_argument, 0, 'u'}, {"logname", required_argument, 0, 'l'}, {"command", required_argument, 0, 'C'}, {"skip", optional_argument, 0, 'S'}, /* backwards compatibility */ {"skip-stdout", optional_argument, 0, 'S'}, {"skip-stderr", optional_argument, 0, 'E'}, {"proto1", no_argument, 0, '1'}, {"proto2", no_argument, 0, '2'}, {"use-ipv4", no_argument, 0, '4'}, {"use-ipv6", no_argument, 0, '6'}, {"ssh-option", required_argument, 0, 'o'}, {"quiet", no_argument, 0, 'q'}, {"configfile", optional_argument, 0, 'F'}, {0, 0, 0, 0} }; if (argc < 2) return ERROR; for (c = 1; c < argc; c++) if (strcmp ("-to", argv[c]) == 0) strcpy (argv[c], "-t"); while (1) { c = getopt_long (argc, argv, "Vvh1246fqt:H:O:p:i:u:l:C:S::E::n:s:o:F:", longopts, &option); if (c == -1 || c == EOF) break; switch (c) { case 'V': /* version */ print_revision (progname, NP_VERSION); exit (STATE_UNKNOWN); case 'h': /* help */ print_help (); exit (STATE_UNKNOWN); case 'v': /* help */ verbose = TRUE; break; case 't': /* timeout period */ if (!is_integer (optarg)) usage_va(_("Timeout interval must be a positive integer")); else timeout_interval = atoi (optarg); break; case 'H': /* host */ host_or_die(optarg); hostname = optarg; break; case 'p': /* port number */ if (!is_integer (optarg)) usage_va(_("Port must be a positive integer")); comm_append("-p"); comm_append(optarg); break; case 'O': /* output file */ outputfile = optarg; passive = TRUE; break; case 's': /* description of service to check */ p1 = optarg; service = realloc (service, (++services) * sizeof(char *)); while ((p2 = index (p1, ':'))) { *p2 = '\0'; service[services - 1] = p1; service = realloc (service, (++services) * sizeof(char *)); p1 = p2 + 1; } service[services - 1] = p1; break; case 'n': /* short name of host in the monitoring configuration */ host_shortname = optarg; break; case 'u': comm_append("-l"); comm_append(optarg); break; case 'l': /* login name */ comm_append("-l"); comm_append(optarg); break; case 'i': /* identity */ comm_append("-i"); comm_append(optarg); break; case '1': /* Pass these switches directly to ssh */ comm_append("-1"); break; case '2': /* 1 to force version 1, 2 to force version 2 */ comm_append("-2"); break; case '4': /* -4 for IPv4 */ comm_append("-4"); break; case '6': /* -6 for IPv6 */ comm_append("-6"); break; case 'f': /* fork to background */ comm_append("-f"); break; case 'C': /* Command for remote machine */ commands++; if (commands > 1) xasprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd); xasprintf (&remotecmd, "%s%s", remotecmd, optarg); break; case 'S': /* skip n (or all) lines on stdout */ if (optarg == NULL) skip_stdout = -1; /* skip all output on stdout */ else if (!is_integer (optarg)) usage_va(_("skip-stdout argument must be an integer")); else skip_stdout = atoi (optarg); break; case 'E': /* skip n (or all) lines on stderr */ if (optarg == NULL) skip_stderr = -1; /* skip all output on stderr */ else if (!is_integer (optarg)) usage_va(_("skip-stderr argument must be an integer")); else skip_stderr = atoi (optarg); break; case 'o': /* Extra options for the ssh command */ comm_append("-o"); comm_append(optarg); break; case 'q': /* Tell the ssh command to be quiet */ comm_append("-q"); break; case 'F': /* ssh configfile */ comm_append("-F"); comm_append(optarg); break; default: /* help */ usage5(); } } c = optind; if (hostname == NULL) { if (c <= argc) { die (STATE_UNKNOWN, _("%s: You must provide a host name\n"), progname); } host_or_die(argv[c]); hostname = argv[c++]; } if (strlen(remotecmd) == 0) { for (; c < argc; c++) if (strlen(remotecmd) > 0) xasprintf (&remotecmd, "%s %s", remotecmd, argv[c]); else xasprintf (&remotecmd, "%s", argv[c]); } if (commands > 1 || passive) xasprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd); if (remotecmd == NULL || strlen (remotecmd) <= 1) usage_va(_("No remotecmd")); comm_append(hostname); comm_append(remotecmd); return validate_arguments (); }
int main (int argc, char *argv[]) { char *device = NULL; int o, longindex; int retval = 0; thresholds_t thresholds; values_t values; int fd; static struct option longopts[] = { {"device", required_argument, 0, 'd'}, {"immediate", no_argument, 0, 'i'}, {"quiet-check", no_argument, 0, 'q'}, {"auto-on", no_argument, 0, '1'}, {"auto-off", no_argument, 0, '0'}, {"nagios", no_argument, 0, 'n'}, /* DEPRECATED, but we still accept it */ {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'V'}, {0, 0, 0, 0} }; /* Parse extra opts if any */ argv=np_extra_opts (&argc, argv, progname); setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); while (1) { o = getopt_long (argc, argv, "+d:iq10nhVv", longopts, &longindex); if (o == -1 || o == EOF || o == 1) break; switch (o) { case 'd': device = optarg; break; case 'q': fprintf (stderr, "%s\n", _("DEPRECATION WARNING: the -q switch (quiet output) is no longer \"quiet\".")); fprintf (stderr, "%s\n", _("Nagios-compatible output is now always returned.")); break; case 'i': case '1': case '0': printf ("%s\n", _("SMART commands are broken and have been disabled (See Notes in --help).")); return STATE_CRITICAL; break; case 'n': fprintf (stderr, "%s\n", _("DEPRECATION WARNING: the -n switch (Nagios-compatible output) is now the")); fprintf (stderr, "%s\n", _("default and will be removed from future releases.")); break; case 'v': /* verbose */ verbose = TRUE; break; case 'h': print_help (); return STATE_OK; case 'V': print_revision (progname, NP_VERSION); return STATE_OK; default: usage5 (); } } if (optind < argc) { device = argv[optind]; } if (!device) { print_help (); return STATE_OK; } fd = open (device, OPEN_MODE); if (fd < 0) { printf (_("CRITICAL - Couldn't open device %s: %s\n"), device, strerror (errno)); return STATE_CRITICAL; } if (smart_cmd_simple (fd, SMART_CMD_ENABLE, 0, FALSE)) { printf (_("CRITICAL - SMART_CMD_ENABLE\n")); return STATE_CRITICAL; } smart_read_values (fd, &values); smart_read_thresholds (fd, &thresholds); retval = nagios (&values, &thresholds); if (verbose) print_values (&values, &thresholds); close (fd); return retval; }
/* process command-line arguments */ int process_arguments (int argc, char **argv) { int c = 1; char *ptr; int option = 0; static struct option longopts[] = { STD_LONG_OPTS, {"packets", required_argument, 0, 'p'}, {"nohtml", no_argument, 0, 'n'}, {"link", no_argument, 0, 'L'}, {"use-ipv4", no_argument, 0, '4'}, {"use-ipv6", no_argument, 0, '6'}, {0, 0, 0, 0} }; if (argc < 2) return ERROR; for (c = 1; c < argc; c++) { if (strcmp ("-to", argv[c]) == 0) strcpy (argv[c], "-t"); if (strcmp ("-nohtml", argv[c]) == 0) strcpy (argv[c], "-n"); } while (1) { c = getopt_long (argc, argv, "VvhnL46t:c:w:H:p:", longopts, &option); if (c == -1 || c == EOF) break; switch (c) { case '?': /* usage */ usage5 (); case 'h': /* help */ print_help (); exit (STATE_UNKNOWN); break; case 'V': /* version */ print_revision (progname, NP_VERSION); exit (STATE_UNKNOWN); break; case 't': /* timeout period */ timeout_interval = atoi (optarg); break; case 'v': /* verbose mode */ verbose++; break; case '4': /* IPv4 only */ address_family = AF_INET; break; case '6': /* IPv6 only */ #ifdef USE_IPV6 address_family = AF_INET6; #else usage (_("IPv6 support not available\n")); #endif break; case 'H': /* hostname */ ptr=optarg; while (1) { n_addresses++; if (n_addresses > max_addr) { max_addr *= 2; addresses = realloc (addresses, sizeof(char*) * max_addr); if (addresses == NULL) die (STATE_UNKNOWN, _("Could not realloc() addresses\n")); } addresses[n_addresses-1] = ptr; if ((ptr = index (ptr, ','))) { strcpy (ptr, ""); ptr += sizeof(char); } else { break; } } break; case 'p': /* number of packets to send */ if (is_intnonneg (optarg)) max_packets = atoi (optarg); else usage2 (_("<max_packets> (%s) must be a non-negative number\n"), optarg); break; case 'n': /* no HTML */ display_html = FALSE; break; case 'L': /* show HTML */ display_html = TRUE; break; case 'c': get_threshold (optarg, &crta, &cpl); break; case 'w': get_threshold (optarg, &wrta, &wpl); break; } } c = optind; if (c == argc) return validate_arguments (); if (addresses[0] == NULL) { if (is_host (argv[c]) == FALSE) { usage2 (_("Invalid hostname/address"), argv[c]); } else { addresses[0] = argv[c++]; n_addresses++; if (c == argc) return validate_arguments (); } } if (wpl == UNKNOWN_PACKET_LOSS) { if (is_intpercent (argv[c]) == FALSE) { printf (_("<wpl> (%s) must be an integer percentage\n"), argv[c]); return ERROR; } else { wpl = atoi (argv[c++]); if (c == argc) return validate_arguments (); } } if (cpl == UNKNOWN_PACKET_LOSS) { if (is_intpercent (argv[c]) == FALSE) { printf (_("<cpl> (%s) must be an integer percentage\n"), argv[c]); return ERROR; } else { cpl = atoi (argv[c++]); if (c == argc) return validate_arguments (); } } if (wrta < 0.0) { if (is_negative (argv[c])) { printf (_("<wrta> (%s) must be a non-negative number\n"), argv[c]); return ERROR; } else { wrta = atof (argv[c++]); if (c == argc) return validate_arguments (); } } if (crta < 0.0) { if (is_negative (argv[c])) { printf (_("<crta> (%s) must be a non-negative number\n"), argv[c]); return ERROR; } else { crta = atof (argv[c++]); if (c == argc) return validate_arguments (); } } if (max_packets == -1) { if (is_intnonneg (argv[c])) { max_packets = atoi (argv[c++]); } else { printf (_("<max_packets> (%s) must be a non-negative number\n"), argv[c]); return ERROR; } } return validate_arguments (); }
/* process command-line arguments */ int process_arguments (int argc, char **argv) { int c = 1; char *user; struct passwd *pw; int option = 0; int err; int cflags = REG_NOSUB | REG_EXTENDED; char errbuf[MAX_INPUT_BUFFER]; char *temp_string; int i=0; static struct option longopts[] = { {"warning", required_argument, 0, 'w'}, {"critical", required_argument, 0, 'c'}, {"metric", required_argument, 0, 'm'}, {"timeout", required_argument, 0, 't'}, {"status", required_argument, 0, 's'}, {"ppid", required_argument, 0, 'p'}, {"user", required_argument, 0, 'u'}, {"command", required_argument, 0, 'C'}, {"vsz", required_argument, 0, 'z'}, {"rss", required_argument, 0, 'r'}, {"pcpu", required_argument, 0, 'P'}, {"elapsed", required_argument, 0, 'e'}, {"argument-array", required_argument, 0, 'a'}, {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'V'}, {"verbose", no_argument, 0, 'v'}, {"ereg-argument-array", required_argument, 0, CHAR_MAX+1}, {"input-file", required_argument, 0, CHAR_MAX+2}, {"no-kthreads", required_argument, 0, 'k'}, {"traditional-filter", no_argument, 0, 'T'}, {"cgroup-hierarchy", required_argument, 0, 'g'}, {0, 0, 0, 0} }; for (c = 1; c < argc; c++) if (strcmp ("-to", argv[c]) == 0) strcpy (argv[c], "-t"); while (1) { c = getopt_long (argc, argv, "Vvhkt:c:w:p:s:u:C:a:z:r:m:P:Tg:", longopts, &option); if (c == -1 || c == EOF) break; switch (c) { case '?': /* help */ usage5 (); case 'h': /* help */ print_help (); exit (STATE_OK); case 'V': /* version */ print_revision (progname, NP_VERSION); exit (STATE_OK); case 't': /* timeout period */ timeout_interval = parse_timeout_string (optarg); break; case 'c': /* critical threshold */ critical_range = optarg; break; case 'w': /* warning threshold */ warning_range = optarg; break; case 'p': /* process id */ if (sscanf (optarg, "%d%[^0-9]", &ppid, tmp) == 1) { xasprintf (&fmt, "%s%sPPID = %d", (fmt ? fmt : "") , (options ? ", " : ""), ppid); options |= PPID; break; } usage4 (_("Parent Process ID must be an integer!")); case 's': /* status */ if (statopts) break; else statopts = optarg; xasprintf (&fmt, _("%s%sSTATE = %s"), (fmt ? fmt : ""), (options ? ", " : ""), statopts); options |= STAT; break; case 'u': /* user or user id */ if (is_integer (optarg)) { uid = atoi (optarg); pw = getpwuid ((uid_t) uid); /* check to be sure user exists */ if (pw == NULL) usage2 (_("UID was not found"), optarg); } else { pw = getpwnam (optarg); /* check to be sure user exists */ if (pw == NULL) usage2 (_("User name was not found"), optarg); /* then get uid */ uid = pw->pw_uid; } user = pw->pw_name; xasprintf (&fmt, "%s%sUID = %d (%s)", (fmt ? fmt : ""), (options ? ", " : ""), uid, user); options |= USER; break; case 'C': /* command */ /* TODO: allow this to be passed in with --metric */ if (prog) break; else prog = optarg; xasprintf (&fmt, _("%s%scommand name '%s'"), (fmt ? fmt : ""), (options ? ", " : ""), prog); options |= PROG; break; case 'g': /* cgroup hierarchy */ if (cgroup_hierarchy) break; else cgroup_hierarchy = optarg; xasprintf (&fmt, _("%s%scgroup hierarchy '%s'"), (fmt ? fmt : ""), (options ? ", " : ""), cgroup_hierarchy); options |= CGROUP_HIERARCHY; break; case 'a': /* args (full path name with args) */ /* TODO: allow this to be passed in with --metric */ if (args) break; else args = optarg; xasprintf (&fmt, "%s%sargs '%s'", (fmt ? fmt : ""), (options ? ", " : ""), args); options |= ARGS; break; case CHAR_MAX+1: err = regcomp(&re_args, optarg, cflags); if (err != 0) { regerror (err, &re_args, errbuf, MAX_INPUT_BUFFER); die (STATE_UNKNOWN, "PROCS %s: %s - %s\n", _("UNKNOWN"), _("Could not compile regular expression"), errbuf); } /* Strip off any | within the regex optarg */ temp_string = strdup(optarg); while(temp_string[i]!='\0'){ if(temp_string[i]=='|') temp_string[i]=','; i++; } xasprintf (&fmt, "%s%sregex args '%s'", (fmt ? fmt : ""), (options ? ", " : ""), temp_string); options |= EREG_ARGS; break; case 'r': /* RSS */ if (sscanf (optarg, "%d%[^0-9]", &rss, tmp) == 1) { xasprintf (&fmt, "%s%sRSS >= %d", (fmt ? fmt : ""), (options ? ", " : ""), rss); options |= RSS; break; } usage4 (_("RSS must be an integer!")); case 'z': /* VSZ */ if (sscanf (optarg, "%d%[^0-9]", &vsz, tmp) == 1) { xasprintf (&fmt, "%s%sVSZ >= %d", (fmt ? fmt : ""), (options ? ", " : ""), vsz); options |= VSZ; break; } usage4 (_("VSZ must be an integer!")); case 'P': /* PCPU */ /* TODO: -P 1.5.5 is accepted */ if (sscanf (optarg, "%f%[^0-9.]", &pcpu, tmp) == 1) { xasprintf (&fmt, "%s%sPCPU >= %.2f", (fmt ? fmt : ""), (options ? ", " : ""), pcpu); options |= PCPU; break; } usage4 (_("PCPU must be a float!")); case 'm': xasprintf (&metric_name, "%s", optarg); if ( strcmp(optarg, "PROCS") == 0) { metric = METRIC_PROCS; break; } else if ( strcmp(optarg, "VSZ") == 0) { metric = METRIC_VSZ; break; } else if ( strcmp(optarg, "RSS") == 0 ) { metric = METRIC_RSS; break; } else if ( strcmp(optarg, "CPU") == 0 ) { metric = METRIC_CPU; break; } else if ( strcmp(optarg, "ELAPSED") == 0) { metric = METRIC_ELAPSED; break; } usage4 (_("Metric must be one of PROCS, VSZ, RSS, CPU, ELAPSED!")); case 'k': /* linux kernel thread filter */ kthread_filter = 1; break; case 'v': /* command */ verbose++; break; case 'T': usepid = 1; break; case CHAR_MAX+2: input_filename = optarg; break; } } c = optind; if ((! warning_range) && argv[c]) warning_range = argv[c++]; if ((! critical_range) && argv[c]) critical_range = argv[c++]; if (statopts == NULL && argv[c]) { xasprintf (&statopts, "%s", argv[c++]); xasprintf (&fmt, _("%s%sSTATE = %s"), (fmt ? fmt : ""), (options ? ", " : ""), statopts); options |= STAT; } /* this will abort in case of invalid ranges */ set_thresholds (&procs_thresholds, warning_range, critical_range); return validate_arguments (); }
/* process command-line arguments */ int process_arguments (int argc, char **argv) { int c; int option = 0; static struct option longopts[] = { {"critical", required_argument, 0, 'c'}, {"warning", required_argument, 0, 'w'}, {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} }; if (argc < 2) usage ("\n"); while (1) { c = getopt_long (argc, argv, "+hVvc:w:", longopts, &option); if (c == -1 || c == EOF || c == 1) break; switch (c) { case '?': /* print short usage statement if args not parsable */ usage5 (); case 'h': /* help */ print_help (); exit (STATE_OK); case 'V': /* version */ print_revision (progname, revision); exit (STATE_OK); case 'c': /* critical */ if (!is_intnonneg (optarg)) usage4 (_("Critical threshold must be a positive integer")); else cusers = atoi (optarg); break; case 'w': /* warning */ if (!is_intnonneg (optarg)) usage4 (_("Warning threshold must be a positive integer")); else wusers = atoi (optarg); break; } } c = optind; if (wusers == -1 && argc > c) { if (is_intnonneg (argv[c]) == FALSE) usage4 (_("Warning threshold must be a positive integer")); else wusers = atoi (argv[c++]); } if (cusers == -1 && argc > c) { if (is_intnonneg (argv[c]) == FALSE) usage4 (_("Warning threshold must be a positive integer")); else cusers = atoi (argv[c]); } return OK; }
int call_getopt(int argc, char **argv){ extern int optind; int option_index = 0; static struct option long_options[] = { {"serverip", required_argument,0,'s'}, {"requestedip", required_argument,0,'r'}, {"timeout", required_argument,0,'t'}, {"interface", required_argument,0,'i'}, {"mac", required_argument,0,'m'}, {"unicast", no_argument, 0,'u'}, {"verbose", no_argument, 0,'v'}, {"version", no_argument, 0,'V'}, {"help", no_argument, 0,'h'}, {0,0,0,0} }; while(1){ int c=0; c=getopt_long(argc,argv,"+hVvt:s:r:t:i:m:u",long_options,&option_index); if(c==-1||c==EOF||c==1) break; switch(c){ case 's': /* DHCP server address */ resolve_host(optarg,&dhcp_ip); add_requested_server(dhcp_ip); break; case 'r': /* address we are requested from DHCP servers */ resolve_host(optarg,&requested_address); request_specific_address=TRUE; break; case 't': /* timeout */ /* if(is_intnonneg(optarg)) */ if(atoi(optarg)>0) dhcpoffer_timeout=atoi(optarg); /* else usage("Time interval must be a nonnegative integer\n"); */ break; case 'm': /* MAC address */ if((user_specified_mac=mac_aton(optarg)) == NULL) usage("Cannot parse MAC address.\n"); if(verbose) print_hardware_address(user_specified_mac); break; case 'i': /* interface name */ strncpy(network_interface_name,optarg,sizeof(network_interface_name)-1); network_interface_name[sizeof(network_interface_name)-1]='\x0'; break; case 'u': /* unicast testing */ unicast=1; break; case 'V': /* version */ print_revision(progname, NP_VERSION); exit(STATE_OK); case 'h': /* help */ print_help(); exit(STATE_OK); case 'v': /* verbose */ verbose=1; break; case '?': /* help */ usage5 (); break; default: break; } } return optind; }
/* process command-line arguments */ int process_arguments (int argc, char **argv) { int c = 0; /* option character */ int option = 0; static struct option longopts[] = { {"warning", required_argument, 0, 'w'}, {"critical", required_argument, 0, 'c'}, {"allswaps", no_argument, 0, 'a'}, {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} }; if (argc < 2) return ERROR; while (1) { c = getopt_long (argc, argv, "+?Vvhac:w:", longopts, &option); if (c == -1 || c == EOF) break; switch (c) { case 'w': /* warning size threshold */ if (is_intnonneg (optarg)) { warn_size_bytes = (float) atoi (optarg); break; } else if (strstr (optarg, ",") && strstr (optarg, "%") && sscanf (optarg, "%f,%d%%", &warn_size_bytes, &warn_percent) == 2) { warn_size_bytes = floorf(warn_size_bytes); break; } else if (strstr (optarg, "%") && sscanf (optarg, "%d%%", &warn_percent) == 1) { break; } else { usage4 (_("Warning threshold must be integer or percentage!")); } case 'c': /* critical size threshold */ if (is_intnonneg (optarg)) { crit_size_bytes = (float) atoi (optarg); break; } else if (strstr (optarg, ",") && strstr (optarg, "%") && sscanf (optarg, "%f,%d%%", &crit_size_bytes, &crit_percent) == 2) { crit_size_bytes = floorf(crit_size_bytes); break; } else if (strstr (optarg, "%") && sscanf (optarg, "%d%%", &crit_percent) == 1) { break; } else { usage4 (_("Critical threshold must be integer or percentage!")); } case 'a': /* all swap */ allswaps = TRUE; break; case 'v': /* verbose */ verbose++; break; case 'V': /* version */ print_revision (progname, NP_VERSION); exit (STATE_OK); case 'h': /* help */ print_help (); exit (STATE_OK); case '?': /* error */ usage5 (); } } c = optind; if (c == argc) return validate_arguments (); if (warn_percent == 0 && is_intnonneg (argv[c])) warn_percent = atoi (argv[c++]); if (c == argc) return validate_arguments (); if (crit_percent == 0 && is_intnonneg (argv[c])) crit_percent = atoi (argv[c++]); if (c == argc) return validate_arguments (); if (warn_size_bytes == 0 && is_intnonneg (argv[c])) warn_size_bytes = (float) atoi (argv[c++]); if (c == argc) return validate_arguments (); if (crit_size_bytes == 0 && is_intnonneg (argv[c])) crit_size_bytes = (float) atoi (argv[c++]); return validate_arguments (); }
int process_arguments(int argc, char **argv) { int c; int option=0; static struct option longopts[] = { {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {"verbose", no_argument, 0, 'v'}, {"use-ipv4", no_argument, 0, '4'}, {"use-ipv6", no_argument, 0, '6'}, {"quiet", no_argument, 0, 'q'}, {"warning", required_argument, 0, 'w'}, {"critical", required_argument, 0, 'c'}, {"timeout", required_argument, 0, 't'}, {"hostname", required_argument, 0, 'H'}, {"port", required_argument, 0, 'p'}, {0, 0, 0, 0} }; if (argc < 2) usage ("\n"); while (1) { c = getopt_long (argc, argv, "Vhv46qw:c:t:H:p:", longopts, &option); if (c == -1 || c == EOF || c == 1) break; switch (c) { case 'h': print_help(); exit(STATE_OK); break; case 'V': print_revision(progname, NP_VERSION); exit(STATE_OK); break; case 'v': verbose++; break; case 'q': quiet = 1; break; case 'w': owarn = optarg; break; case 'c': ocrit = optarg; break; case 'H': if(is_host(optarg) == FALSE) usage2(_("Invalid hostname/address"), optarg); server_address = strdup(optarg); break; case 'p': port = strdup(optarg); break; case 't': socket_timeout=atoi(optarg); break; case '4': address_family = AF_INET; break; case '6': #ifdef USE_IPV6 address_family = AF_INET6; #else usage4 (_("IPv6 support not available")); #endif break; case '?': /* print short usage statement if args not parsable */ usage5 (); break; } } if(server_address == NULL) { usage4(_("Hostname was not supplied")); } return 0; }
/* process command-line arguments */ int process_arguments (int argc, char **argv) { int c; int option = 0; static struct option longopts[] = { {"hostname", required_argument, 0, 'H'}, {"community", required_argument, 0, 'C'}, /* {"critical", required_argument,0,'c'}, */ /* {"warning", required_argument,0,'w'}, */ /* {"port", required_argument,0,'P'}, */ {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} }; if (argc < 2) return ERROR; while (1) { c = getopt_long (argc, argv, "+hVH:C:", longopts, &option); if (c == -1 || c == EOF || c == 1) break; switch (c) { case 'H': /* hostname */ if (is_host (optarg)) { address = strscpy(address, optarg) ; } else { usage2 (_("Invalid hostname/address"), optarg); } break; case 'C': /* community */ community = strscpy (community, optarg); break; case 'V': /* version */ print_revision (progname, NP_VERSION); exit (STATE_OK); case 'h': /* help */ print_help (); exit (STATE_OK); case '?': /* help */ usage5 (); } } c = optind; if (address == NULL) { if (is_host (argv[c])) { address = argv[c++]; } else { usage2 (_("Invalid hostname/address"), argv[c]); } } if (community == NULL) { if (argv[c] != NULL ) community = argv[c]; else community = strdup (DEFAULT_COMMUNITY); } return validate_arguments (); }
int main (int argc, char *argv[]) { char *device = NULL; int command = -1; int o, longindex; int retval = 0; thresholds_t thresholds; values_t values; int fd; static struct option longopts[] = { {"device", required_argument, 0, 'd'}, {"immediate", no_argument, 0, 'i'}, {"quiet-check", no_argument, 0, 'q'}, {"auto-on", no_argument, 0, '1'}, {"auto-off", no_argument, 0, '0'}, {"nagios", no_argument, 0, 'n'}, {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'V'}, {0, 0, 0, 0} }; /* Parse extra opts if any */ argv=np_extra_opts (&argc, argv, progname); setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); while (1) { o = getopt_long (argc, argv, "+d:iq10nhV", longopts, &longindex); if (o == -1 || o == EOF || o == 1) break; switch (o) { case 'd': device = optarg; break; case 'q': command = 3; break; case 'i': command = 2; break; case '1': command = 1; break; case '0': command = 0; break; case 'n': command = 4; break; case 'h': print_help (); return STATE_OK; case 'V': print_revision (progname, NP_VERSION); return STATE_OK; default: usage5 (); } } if (optind < argc) { device = argv[optind]; } if (!device) { print_help (); return STATE_OK; } fd = open (device, OPEN_MODE); if (fd < 0) { printf (_("CRITICAL - Couldn't open device %s: %s\n"), device, strerror (errno)); return STATE_CRITICAL; } if (smart_cmd_simple (fd, SMART_CMD_ENABLE, 0, TRUE)) { printf (_("CRITICAL - SMART_CMD_ENABLE\n")); return STATE_CRITICAL; } switch (command) { case 0: retval = smart_cmd_simple (fd, SMART_CMD_AUTO_OFFLINE, 0, TRUE); break; case 1: retval = smart_cmd_simple (fd, SMART_CMD_AUTO_OFFLINE, 0xF8, TRUE); break; case 2: retval = smart_cmd_simple (fd, SMART_CMD_IMMEDIATE_OFFLINE, 0, TRUE); break; case 3: smart_read_values (fd, &values); smart_read_thresholds (fd, &thresholds); retval = values_not_passed (&values, &thresholds); break; case 4: smart_read_values (fd, &values); smart_read_thresholds (fd, &thresholds); retval = nagios (&values, &thresholds); break; default: smart_read_values (fd, &values); smart_read_thresholds (fd, &thresholds); print_values (&values, &thresholds); break; } close (fd); return retval; }
/* process command-line arguments */ int process_arguments (int argc, char **argv) { int c; int option = 0; static struct option longopts[] = { {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'V'}, {"timeout", required_argument, 0, 't'}, {"critical", required_argument, 0, 'c'}, {"warning", required_argument, 0, 'w'}, {"hostname", required_argument, 0, 'H'}, {"logname", required_argument, 0, 'l'}, {"password", required_argument, 0, 'p'}, {"authorization", required_argument, 0, 'a'}, {"port", required_argument, 0, 'P'}, {"database", required_argument, 0, 'd'}, {"verbose", no_argument, 0, 'v'}, {0, 0, 0, 0} }; while (1) { c = getopt_long (argc, argv, "hVt:c:w:H:P:d:l:p:a:v", longopts, &option); if (c == EOF) break; switch (c) { case '?': /* usage */ usage5 (); case 'h': /* help */ print_help (); exit (STATE_OK); case 'V': /* version */ print_revision (progname, NP_VERSION); exit (STATE_OK); case 't': /* timeout period */ if (!is_integer (optarg)) usage2 (_("Timeout interval must be a positive integer"), optarg); else timeout_interval = atoi (optarg); break; case 'c': /* critical time threshold */ if (!is_nonnegative (optarg)) usage2 (_("Critical threshold must be a positive integer"), optarg); else tcrit = strtod (optarg, NULL); break; case 'w': /* warning time threshold */ if (!is_nonnegative (optarg)) usage2 (_("Warning threshold must be a positive integer"), optarg); else twarn = strtod (optarg, NULL); break; case 'H': /* host */ if (!is_host (optarg)) usage2 (_("Invalid hostname/address"), optarg); else pghost = optarg; break; case 'P': /* port */ if (!is_integer (optarg)) usage2 (_("Port must be a positive integer"), optarg); else pgport = optarg; break; case 'd': /* database name */ if (!is_pg_dbname (optarg)) /* checks length and valid chars */ usage2 (_("Database name is not valid"), optarg); else /* we know length, and know optarg is terminated, so us strcpy */ strcpy (dbName, optarg); break; case 'l': /* login name */ if (!is_pg_logname (optarg)) usage2 (_("User name is not valid"), optarg); else pguser = optarg; break; case 'p': /* authentication password */ case 'a': pgpasswd = optarg; break; case 'v': verbose++; break; } } return validate_arguments (); }
/* process command-line arguments */ int process_arguments (int argc, char **argv) { int c; char *warning = NULL; char *critical = NULL; int opt_index = 0; static struct option long_opts[] = { {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'V'}, {"verbose", no_argument, 0, 'v'}, {"timeout", required_argument, 0, 't'}, {"hostname", required_argument, 0, 'H'}, {"server", required_argument, 0, 's'}, {"reverse-server", required_argument, 0, 'r'}, {"querytype", required_argument, 0, 'q'}, {"expected-address", required_argument, 0, 'a'}, {"expect-authority", no_argument, 0, 'A'}, {"accept-cname", no_argument, 0, 'n'}, {"warning", required_argument, 0, 'w'}, {"critical", required_argument, 0, 'c'}, {0, 0, 0, 0} }; if (argc < 2) return ERROR; for (c = 1; c < argc; c++) if (strcmp ("-to", argv[c]) == 0) strcpy (argv[c], "-t"); while (1) { c = getopt_long (argc, argv, "hVvAnt:H:s:r:a:q:w:c:", long_opts, &opt_index); if (c == -1 || c == EOF) break; switch (c) { case 'h': /* help */ print_help (); exit (STATE_OK); case 'V': /* version */ print_revision (progname, NP_VERSION); exit (STATE_OK); case 'v': /* version */ verbose = TRUE; break; case 't': /* timeout period */ timeout_interval = atoi (optarg); break; case 'H': /* hostname */ if (strlen (optarg) >= ADDRESS_LENGTH) die (STATE_UNKNOWN, _("Input buffer overflow\n")); strcpy (query_address, optarg); break; case 's': /* server name */ /* TODO: this host_or_die check is probably unnecessary. * Better to confirm nslookup response matches */ host_or_die(optarg); if (strlen (optarg) >= ADDRESS_LENGTH) die (STATE_UNKNOWN, _("Input buffer overflow\n")); strcpy (dns_server, optarg); break; case 'r': /* reverse server name */ /* TODO: Is this host_or_die necessary? */ host_or_die(optarg); if (strlen (optarg) >= ADDRESS_LENGTH) die (STATE_UNKNOWN, _("Input buffer overflow\n")); strcpy (ptr_server, optarg); break; case 'a': /* expected address */ if (strlen (optarg) >= ADDRESS_LENGTH) die (STATE_UNKNOWN, _("Input buffer overflow\n")); expected_address = (char **)realloc(expected_address, (expected_address_cnt+1) * sizeof(char**)); expected_address[expected_address_cnt] = strdup(optarg); expected_address_cnt++; break; case 'q': /* querytype -- A or AAAA or ANY or SRV or TXT, etc. */ if (strlen (optarg) < 1 || strlen (optarg) > 5) die (STATE_UNKNOWN, _("Missing valid querytype parameter. Try using 'A' or 'AAAA' or 'SRV'\n")); strntoupper(optarg, sizeof(optarg)); strcpy(query_type, "-querytype="); strcat(query_type, optarg); break; case 'A': /* expect authority */ expect_authority = TRUE; break; case 'n': /* accept cname responses as a result */ accept_cname = TRUE; break; case 'w': warning = optarg; break; case 'c': critical = optarg; break; default: /* args not parsable */ usage5(); } } c = optind; if (strlen(query_address)==0 && c<argc) { if (strlen(argv[c])>=ADDRESS_LENGTH) die (STATE_UNKNOWN, _("Input buffer overflow\n")); strcpy (query_address, argv[c++]); } if (strlen(dns_server)==0 && c<argc) { /* TODO: See -s option */ host_or_die(argv[c]); if (strlen(argv[c]) >= ADDRESS_LENGTH) die (STATE_UNKNOWN, _("Input buffer overflow\n")); strcpy (dns_server, argv[c++]); } set_thresholds(&time_thresholds, warning, critical); return validate_arguments (); }
/* process command-line arguments */ int process_arguments (int argc, char **argv) { int c; int option = 0; static struct option longopts[] = { {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'V'}, {"host", required_argument, 0, 'H'}, /* backward compatibility */ {"hostname", required_argument, 0, 'H'}, {"port", required_argument, 0, 'p'}, {"use-ipv4", no_argument, 0, '4'}, {"use-ipv6", no_argument, 0, '6'}, {"timeout", required_argument, 0, 't'}, {"verbose", no_argument, 0, 'v'}, {"remote-version", required_argument, 0, 'r'}, {"fingerprint", required_argument, 0, 'f'}, {"remote-openssh-version", required_argument, 0, 'o'}, {0, 0, 0, 0} }; if (argc < 2) return ERROR; for (c = 1; c < argc; c++) if (strcmp ("-to", argv[c]) == 0) strcpy (argv[c], "-t"); while (1) { c = getopt_long (argc, argv, "+Vhv46t:r:f:H:o:p:", longopts, &option); if (c == -1 || c == EOF) break; switch (c) { case '?': /* help */ usage5 (); case 'V': /* version */ print_revision (progname, NP_VERSION); exit (STATE_OK); case 'h': /* help */ print_help (); exit (STATE_OK); case 'v': /* verbose */ verbose = TRUE; break; case 't': /* timeout period */ if (!is_integer (optarg)) usage2 (_("Timeout interval must be a positive integer"), optarg); else socket_timeout = atoi (optarg); break; case '4': address_family = AF_INET; break; case '6': #ifdef USE_IPV6 address_family = AF_INET6; #else usage4 (_("IPv6 support not available")); #endif break; case 'r': /* remote version */ remote_version = optarg; break; case 'o': remote_openssh_version = optarg; break; case 'f': /* remote version */ remote_fingerprint = optarg; break; case 'H': /* host */ if (is_host (optarg) == FALSE) usage2 (_("Invalid hostname/address"), optarg); server_name = optarg; break; case 'p': /* port */ if (is_intpos (optarg)) { port = atoi (optarg); } else { usage2 (_("Port number must be a positive integer"), optarg); } } } c = optind; if (server_name == NULL && c < argc) { if (is_host (argv[c])) { server_name = argv[c++]; } } if (port == -1 && c < argc) { if (is_intpos (argv[c])) { port = atoi (argv[c++]); } else { print_usage (); exit (STATE_UNKNOWN); } } return validate_arguments (); }
/* process command-line arguments */ int process_arguments (int argc, char **argv) { int c; int option = 0; /* initialize the long option struct */ static struct option longopts[] = { {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'V'}, {"timeout", required_argument, 0, 't'}, {"hostname", required_argument, 0, 'H'}, {"base", required_argument, 0, 'b'}, {"attr", required_argument, 0, 'a'}, {"bind", required_argument, 0, 'D'}, {"pass", required_argument, 0, 'P'}, #ifdef HAVE_LDAP_SET_OPTION {"ver2", no_argument, 0, '2'}, {"ver3", no_argument, 0, '3'}, #endif {"starttls", no_argument, 0, 'T'}, {"ssl", no_argument, 0, 'S'}, {"use-ipv4", no_argument, 0, '4'}, {"use-ipv6", no_argument, 0, '6'}, {"port", required_argument, 0, 'p'}, {"warn", required_argument, 0, 'w'}, {"crit", required_argument, 0, 'c'}, {"verbose", no_argument, 0, 'v'}, {0, 0, 0, 0} }; if (argc < 2) return ERROR; for (c = 1; c < argc; c++) { if (strcmp ("-to", argv[c]) == 0) strcpy (argv[c], "-t"); } while (1) { c = getopt_long (argc, argv, "hvV234TS6t:c:w:H:b:p:a:D:P:", longopts, &option); if (c == -1 || c == EOF) break; switch (c) { case 'h': /* help */ print_help (); exit (STATE_OK); case 'V': /* version */ print_revision (progname, NP_VERSION); exit (STATE_OK); case 't': /* timeout period */ if (!is_intnonneg (optarg)) usage2 (_("Timeout interval must be a positive integer"), optarg); else socket_timeout = atoi (optarg); break; case 'H': ld_host = optarg; break; case 'b': ld_base = optarg; break; case 'p': ld_port = atoi (optarg); break; case 'a': ld_attr = optarg; break; case 'D': ld_binddn = optarg; break; case 'P': ld_passwd = optarg; break; case 'w': warn_time = strtod (optarg, NULL); break; case 'c': crit_time = strtod (optarg, NULL); break; #ifdef HAVE_LDAP_SET_OPTION case '2': ld_protocol = 2; break; case '3': ld_protocol = 3; break; #endif case '4': address_family = AF_INET; break; case 'v': verbose++; break; case 'T': if (! ssl_on_connect) starttls = TRUE; else usage_va(_("%s cannot be combined with %s"), "-T/--starttls", "-S/--ssl"); break; case 'S': if (! starttls) { ssl_on_connect = TRUE; if (ld_port == -1) ld_port = LDAPS_PORT; } else usage_va(_("%s cannot be combined with %s"), "-S/--ssl", "-T/--starttls"); break; case '6': #ifdef USE_IPV6 address_family = AF_INET6; #else usage (_("IPv6 support not available\n")); #endif break; default: usage5 (); } } c = optind; if (ld_host == NULL && is_host(argv[c])) ld_host = strdup (argv[c++]); if (ld_base == NULL && argv[c]) ld_base = strdup (argv[c++]); if (ld_port == -1) ld_port = DEFAULT_PORT; return validate_arguments (); }
/* process command-line arguments */ int process_arguments (int argc, char **argv) { int c; int option = 0; static struct option longopts[] = { {"hostname", required_argument, 0, 'H'}, {"query_address", required_argument, 0, 'l'}, {"warning", required_argument, 0, 'w'}, {"critical", required_argument, 0, 'c'}, {"timeout", required_argument, 0, 't'}, {"dig-arguments", required_argument, 0, 'A'}, {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {"record_type", required_argument, 0, 'T'}, {"expected_address", required_argument, 0, 'a'}, {"port", required_argument, 0, 'p'}, {"use-ipv4", no_argument, 0, '4'}, {"use-ipv6", no_argument, 0, '6'}, {0, 0, 0, 0} }; if (argc < 2) return ERROR; while (1) { c = getopt_long (argc, argv, "hVvt:l:H:w:c:T:p:a:A:46", longopts, &option); if (c == -1 || c == EOF) break; switch (c) { case 'h': /* help */ print_help (); exit (STATE_OK); case 'V': /* version */ print_revision (progname, NP_VERSION); exit (STATE_OK); case 'H': /* hostname */ host_or_die(optarg); dns_server = optarg; break; case 'p': /* server port */ if (is_intpos (optarg)) { server_port = atoi (optarg); } else { usage_va(_("Port must be a positive integer - %s"), optarg); } break; case 'l': /* address to lookup */ query_address = optarg; break; case 'w': /* warning */ if (is_nonnegative (optarg)) { warning_interval = strtod (optarg, NULL); } else { usage_va(_("Warning interval must be a positive integer - %s"), optarg); } break; case 'c': /* critical */ if (is_nonnegative (optarg)) { critical_interval = strtod (optarg, NULL); } else { usage_va(_("Critical interval must be a positive integer - %s"), optarg); } break; case 't': /* timeout */ if (is_intnonneg (optarg)) { timeout_interval = atoi (optarg); } else { usage_va(_("Timeout interval must be a positive integer - %s"), optarg); } break; case 'A': /* dig arguments */ dig_args = strdup(optarg); break; case 'v': /* verbose */ verbose = TRUE; break; case 'T': record_type = optarg; break; case 'a': expected_address = optarg; break; case '4': query_transport = "-4"; break; case '6': query_transport = "-6"; break; default: /* usage5 */ usage5(); } } c = optind; if (dns_server == NULL) { if (c < argc) { host_or_die(argv[c]); dns_server = argv[c]; } else { dns_server = strdup ("127.0.0.1"); } } return validate_arguments (); }
int main (int argc, char **argv) { int found = 0, result = STATE_UNKNOWN; char *url = NULL; char *cmd; char *buf; char *nstr; char tstr[MAX_INPUT_BUFFER]; int c; int option = 0; static struct option longopts[] = { {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'V'}, {"url", required_argument, 0, 'u'}, {0, 0, 0, 0} }; setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); /* Need at least 2 args */ if (argc < 3) { print_help(); exit (STATE_UNKNOWN); } while (1) { c = getopt_long (argc, argv, "+hVu:", longopts, &option); if (c == -1 || c == EOF) break; switch (c) { case 'h': /* help */ print_help (); exit (EXIT_SUCCESS); break; case 'V': /* version */ print_revision (progname, NP_VERSION); exit (EXIT_SUCCESS); break; case 'u': url = strdup (argv[optind]); break; case '?': default: usage5 (); } } if (url == NULL) url = strdup (argv[optind++]); cmd = strdup (argv[optind++]); for (c = optind; c < argc; c++) { xasprintf (&cmd, "%s %s", cmd, argv[c]); } child_process = spopen (cmd); if (child_process == NULL) { printf (_("Could not open pipe: %s\n"), cmd); exit (STATE_UNKNOWN); } child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); if (child_stderr == NULL) { printf (_("Could not open stderr for %s\n"), cmd); } bzero(tstr, sizeof(tstr)); buf = malloc(MAX_INPUT_BUFFER); printf ("<A href=\"%s\">", argv[1]); while (fgets (buf, MAX_INPUT_BUFFER - 1, child_process)) { found++; /* Collect the string in temp str so we can tokenize */ strcat(tstr, buf); } if (!found) die (STATE_UNKNOWN, _("%s UNKNOWN - No data received from host\nCMD: %s</A>\n"), argv[0], cmd); /* chop the newline character */ if ((nstr = strchr(tstr, NEWLINE_CHARACTER)) != NULL) *nstr = '\0'; /* tokenize the string for Perfdata if there is some */ nstr = strtok(tstr, PERF_CHARACTER); printf ("%s", nstr); printf ("</A>"); nstr = strtok(NULL, PERF_CHARACTER); if (nstr != NULL) printf (" | %s", nstr); /* close the pipe */ result = spclose (child_process); /* WARNING if output found on stderr */ if (fgets (buf, MAX_INPUT_BUFFER - 1, child_stderr)) result = max_state (result, STATE_WARNING); /* close stderr */ (void) fclose (child_stderr); return result; }
/* process command-line arguments */ int process_arguments (int argc, char **argv) { int c; char* temp; int option = 0; static struct option longopts[] = { {"hostname", required_argument, 0, 'H'}, {"expect", required_argument, 0, 'e'}, {"critical", required_argument, 0, 'c'}, {"warning", required_argument, 0, 'w'}, {"timeout", required_argument, 0, 't'}, {"port", required_argument, 0, 'p'}, {"from", required_argument, 0, 'f'}, {"fqdn", required_argument, 0, 'F'}, {"authtype", required_argument, 0, 'A'}, {"authuser", required_argument, 0, 'U'}, {"authpass", required_argument, 0, 'P'}, {"command", required_argument, 0, 'C'}, {"response", required_argument, 0, 'R'}, {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, {"use-ipv4", no_argument, 0, '4'}, {"use-ipv6", no_argument, 0, '6'}, {"help", no_argument, 0, 'h'}, {"starttls",no_argument,0,'S'}, {"certificate",required_argument,0,'D'}, {"ignore-quit-failure",no_argument,0,'q'}, {0, 0, 0, 0} }; if (argc < 2) return ERROR; for (c = 1; c < argc; c++) { if (strcmp ("-to", argv[c]) == 0) strcpy (argv[c], "-t"); else if (strcmp ("-wt", argv[c]) == 0) strcpy (argv[c], "-w"); else if (strcmp ("-ct", argv[c]) == 0) strcpy (argv[c], "-c"); } while (1) { c = getopt_long (argc, argv, "+hVv46t:p:f:e:c:w:H:C:R:SD:F:A:U:P:q", longopts, &option); if (c == -1 || c == EOF) break; switch (c) { case 'H': /* hostname */ if (is_host (optarg)) { server_address = optarg; } else { usage2 (_("Invalid hostname/address"), optarg); } break; case 'p': /* port */ if (is_intpos (optarg)) server_port = atoi (optarg); else usage4 (_("Port must be a positive integer")); break; case 'F': /* localhostname */ localhostname = strdup(optarg); break; case 'f': /* from argument */ from_arg = optarg + strspn(optarg, "<"); from_arg = strndup(from_arg, strcspn(from_arg, ">")); send_mail_from = 1; break; case 'A': authtype = optarg; use_ehlo = TRUE; break; case 'U': authuser = optarg; break; case 'P': authpass = optarg; break; case 'e': /* server expect string on 220 */ server_expect = optarg; break; case 'C': /* commands */ if (ncommands >= command_size) { command_size+=8; commands = realloc (commands, sizeof(char *) * command_size); if (commands == NULL) die (STATE_UNKNOWN, _("Could not realloc() units [%d]\n"), ncommands); } commands[ncommands] = (char *) malloc (sizeof(char) * 255); strncpy (commands[ncommands], optarg, 255); ncommands++; break; case 'R': /* server responses */ if (nresponses >= response_size) { response_size += 8; responses = realloc (responses, sizeof(char *) * response_size); if (responses == NULL) die (STATE_UNKNOWN, _("Could not realloc() units [%d]\n"), nresponses); } responses[nresponses] = (char *) malloc (sizeof(char) * 255); strncpy (responses[nresponses], optarg, 255); nresponses++; break; case 'c': /* critical time threshold */ if (!is_nonnegative (optarg)) usage4 (_("Critical time must be a positive")); else { critical_time = strtod (optarg, NULL); check_critical_time = TRUE; } break; case 'w': /* warning time threshold */ if (!is_nonnegative (optarg)) usage4 (_("Warning time must be a positive")); else { warning_time = strtod (optarg, NULL); check_warning_time = TRUE; } break; case 'v': /* verbose */ verbose++; break; case 'q': ignore_send_quit_failure++; /* ignore problem sending QUIT */ break; case 't': /* timeout */ if (is_intnonneg (optarg)) { socket_timeout = atoi (optarg); } else { usage4 (_("Timeout interval must be a positive integer")); } break; case 'S': /* starttls */ use_ssl = TRUE; use_ehlo = TRUE; break; case 'D': /* Check SSL cert validity */ #ifdef USE_OPENSSL if ((temp=strchr(optarg,','))!=NULL) { *temp='\0'; if (!is_intnonneg (optarg)) usage2 ("Invalid certificate expiration period", optarg); days_till_exp_warn = atoi(optarg); *temp=','; temp++; if (!is_intnonneg (temp)) usage2 (_("Invalid certificate expiration period"), temp); days_till_exp_crit = atoi (temp); } else { days_till_exp_crit=0; if (!is_intnonneg (optarg)) usage2 ("Invalid certificate expiration period", optarg); days_till_exp_warn = atoi (optarg); } check_cert = TRUE; #else usage (_("SSL support not available - install OpenSSL and recompile")); #endif break; case '4': address_family = AF_INET; break; case '6': #ifdef USE_IPV6 address_family = AF_INET6; #else usage4 (_("IPv6 support not available")); #endif break; case 'V': /* version */ print_revision (progname, NP_VERSION); exit (STATE_OK); case 'h': /* help */ print_help (); exit (STATE_OK); case '?': /* help */ usage5 (); } } c = optind; if (server_address == NULL) { if (argv[c]) { if (is_host (argv[c])) server_address = argv[c]; else usage2 (_("Invalid hostname/address"), argv[c]); } else { xasprintf (&server_address, "127.0.0.1"); } } if (server_expect == NULL) server_expect = strdup (SMTP_EXPECT); if (mail_command == NULL) mail_command = strdup("MAIL "); if (from_arg==NULL) from_arg = strdup(" "); return validate_arguments (); }
/* process command-line arguments */ int process_arguments (int argc, char **argv) { int c; char *rv[2]; int option = 0; static struct option longopts[] = { {"hostname", required_argument, 0, 'H'}, {"sourceip", required_argument, 0, 'S'}, {"sourceif", required_argument, 0, 'I'}, {"critical", required_argument, 0, 'c'}, {"warning", required_argument, 0, 'w'}, {"bytes", required_argument, 0, 'b'}, {"number", required_argument, 0, 'n'}, {"target-timeout", required_argument, 0, 'T'}, {"interval", required_argument, 0, 'i'}, {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {"use-ipv4", no_argument, 0, '4'}, {"use-ipv6", no_argument, 0, '6'}, {0, 0, 0, 0} }; rv[PL] = NULL; rv[RTA] = NULL; if (argc < 2) return ERROR; if (!is_option (argv[1])) { server_name = argv[1]; argv[1] = argv[0]; argv = &argv[1]; argc--; } while (1) { c = getopt_long (argc, argv, "+hVvH:S:c:w:b:n:T:i:I:46", longopts, &option); if (c == -1 || c == EOF || c == 1) break; switch (c) { case '?': /* print short usage statement if args not parsable */ usage5 (); case 'h': /* help */ print_help (); exit (STATE_OK); case 'V': /* version */ print_revision (progname, NP_VERSION); exit (STATE_OK); case 'v': /* verbose mode */ verbose = TRUE; break; case 'H': /* hostname */ if (is_host (optarg) == FALSE) { usage2 (_("Invalid hostname/address"), optarg); } server_name = strscpy (server_name, optarg); break; case 'S': /* sourceip */ if (is_host (optarg) == FALSE) { usage2 (_("Invalid hostname/address"), optarg); } sourceip = strscpy (sourceip, optarg); break; case 'I': /* sourceip */ sourceif = strscpy (sourceif, optarg); case '4': /* IPv4 only */ address_family = AF_INET; break; case '6': /* IPv6 only */ #ifdef USE_IPV6 address_family = AF_INET6; #else usage (_("IPv6 support not available\n")); #endif break; case 'c': get_threshold (optarg, rv); if (rv[RTA]) { crta = strtod (rv[RTA], NULL); crta_p = TRUE; rv[RTA] = NULL; } if (rv[PL]) { cpl = atoi (rv[PL]); cpl_p = TRUE; rv[PL] = NULL; } break; case 'w': get_threshold (optarg, rv); if (rv[RTA]) { wrta = strtod (rv[RTA], NULL); wrta_p = TRUE; rv[RTA] = NULL; } if (rv[PL]) { wpl = atoi (rv[PL]); wpl_p = TRUE; rv[PL] = NULL; } break; case 'b': /* bytes per packet */ if (is_intpos (optarg)) packet_size = atoi (optarg); else usage (_("Packet size must be a positive integer")); break; case 'n': /* number of packets */ if (is_intpos (optarg)) packet_count = atoi (optarg); else usage (_("Packet count must be a positive integer")); break; case 'T': /* timeout in msec */ if (is_intpos (optarg)) target_timeout = atoi (optarg); else usage (_("Target timeout must be a positive integer")); break; case 'i': /* interval in msec */ if (is_intpos (optarg)) packet_interval = atoi (optarg); else usage (_("Interval must be a positive integer")); break; } } if (server_name == NULL) usage4 (_("Hostname was not supplied")); return OK; }
int process_arguments (int argc, char **argv) { int c; int opt_index = 0; static struct option long_opts[] = { {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'V'}, {"verbose", no_argument, 0, 'v'}, {"timeout", required_argument, 0, 't'}, {"hostname", required_argument, 0, 'H'}, {"port", required_argument, 0, 'P'}, {"game-type", required_argument, 0, 'G'}, {"map-field", required_argument, 0, 'm'}, {"ping-field", required_argument, 0, 'p'}, {"game-field", required_argument, 0, 'g'}, {"players-field", required_argument, 0, 129}, {"max-players-field", required_argument, 0, 130}, {0, 0, 0, 0} }; if (argc < 2) return ERROR; for (c = 1; c < argc; c++) { if (strcmp ("-mf", argv[c]) == 0) strcpy (argv[c], "-m"); else if (strcmp ("-pf", argv[c]) == 0) strcpy (argv[c], "-p"); else if (strcmp ("-gf", argv[c]) == 0) strcpy (argv[c], "-g"); } while (1) { c = getopt_long (argc, argv, "hVvt:H:P:G:g:p:m:", long_opts, &opt_index); if (c == -1 || c == EOF) break; switch (c) { case 'h': /* help */ print_help (); exit (STATE_OK); case 'V': /* version */ print_revision (progname, NP_VERSION); exit (STATE_OK); case 'v': /* version */ verbose = TRUE; break; case 't': /* timeout period */ timeout_interval = atoi (optarg); break; case 'H': /* hostname */ if (strlen (optarg) >= MAX_HOST_ADDRESS_LENGTH) die (STATE_UNKNOWN, _("Input buffer overflow\n")); server_ip = optarg; break; case 'P': /* port */ port = atoi (optarg); break; case 'G': /* hostname */ if (strlen (optarg) >= MAX_INPUT_BUFFER) die (STATE_UNKNOWN, _("Input buffer overflow\n")); game_type = optarg; break; case 'p': /* index of ping field */ qstat_ping_field = atoi (optarg); if (qstat_ping_field < 0 || qstat_ping_field > QSTAT_MAX_RETURN_ARGS) return ERROR; break; case 'm': /* index on map field */ qstat_map_field = atoi (optarg); if (qstat_map_field < 0 || qstat_map_field > QSTAT_MAX_RETURN_ARGS) return ERROR; break; case 'g': /* index of game field */ qstat_game_field = atoi (optarg); if (qstat_game_field < 0 || qstat_game_field > QSTAT_MAX_RETURN_ARGS) return ERROR; break; case 129: /* index of player count field */ qstat_game_players = atoi (optarg); if (qstat_game_players_max == 0) qstat_game_players_max = qstat_game_players - 1; if (qstat_game_players < 0 || qstat_game_players > QSTAT_MAX_RETURN_ARGS) return ERROR; break; case 130: /* index of max players field */ qstat_game_players_max = atoi (optarg); if (qstat_game_players_max < 0 || qstat_game_players_max > QSTAT_MAX_RETURN_ARGS) return ERROR; break; default: /* args not parsable */ usage5(); } } c = optind; /* first option is the game type */ if (!game_type && c<argc) game_type = strdup (argv[c++]); /* Second option is the server name */ if (!server_ip && c<argc) server_ip = strdup (argv[c++]); return validate_arguments (); }
/* process command-line arguments */ static int process_arguments (int argc, char **argv) { int c; int escape = 0; char *temp; int option = 0; static struct option longopts[] = { {"hostname", required_argument, 0, 'H'}, {"critical", required_argument, 0, 'c'}, {"warning", required_argument, 0, 'w'}, {"critical-codes", required_argument, 0, 'C'}, {"warning-codes", required_argument, 0, 'W'}, {"timeout", required_argument, 0, 't'}, {"protocol", required_argument, 0, 'P'}, /* FIXME: Unhandled */ {"port", required_argument, 0, 'p'}, {"escape", no_argument, 0, 'E'}, {"all", no_argument, 0, 'A'}, {"send", required_argument, 0, 's'}, {"expect", required_argument, 0, 'e'}, {"maxbytes", required_argument, 0, 'm'}, {"quit", required_argument, 0, 'q'}, {"jail", no_argument, 0, 'j'}, {"delay", required_argument, 0, 'd'}, {"refuse", required_argument, 0, 'r'}, {"mismatch", required_argument, 0, 'M'}, {"use-ipv4", no_argument, 0, '4'}, {"use-ipv6", no_argument, 0, '6'}, {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {"ssl", no_argument, 0, 'S'}, {"certificate", required_argument, 0, 'D'}, {0, 0, 0, 0} }; if (argc < 2) usage4 (_("No arguments found")); /* backwards compatibility */ for (c = 1; c < argc; c++) { if (strcmp ("-to", argv[c]) == 0) strcpy (argv[c], "-t"); else if (strcmp ("-wt", argv[c]) == 0) strcpy (argv[c], "-w"); else if (strcmp ("-ct", argv[c]) == 0) strcpy (argv[c], "-c"); } if (!is_option (argv[1])) { server_address = argv[1]; argv[1] = argv[0]; argv = &argv[1]; argc--; } while (1) { c = getopt_long (argc, argv, "+hVv46EAH:s:e:q:m:c:w:t:p:C:W:d:Sr:jD:M:", longopts, &option); if (c == -1 || c == EOF || c == 1) break; switch (c) { case '?': /* print short usage statement if args not parsable */ usage5 (); case 'h': /* help */ print_help (); exit (STATE_OK); case 'V': /* version */ print_revision (progname, NP_VERSION); exit (STATE_OK); case 'v': /* verbose mode */ flags |= FLAG_VERBOSE; match_flags |= NP_MATCH_VERBOSE; break; case '4': address_family = AF_INET; break; case '6': #ifdef USE_IPV6 address_family = AF_INET6; #else usage4 (_("IPv6 support not available")); #endif break; case 'H': /* hostname */ host_specified = TRUE; server_address = optarg; break; case 'c': /* critical */ critical_time = strtod (optarg, NULL); flags |= FLAG_TIME_CRIT; break; case 'j': /* hide output */ flags |= FLAG_HIDE_OUTPUT; break; case 'w': /* warning */ warning_time = strtod (optarg, NULL); flags |= FLAG_TIME_WARN; break; case 'C': crit_codes = realloc (crit_codes, ++crit_codes_count); crit_codes[crit_codes_count - 1] = optarg; break; case 'W': warn_codes = realloc (warn_codes, ++warn_codes_count); warn_codes[warn_codes_count - 1] = optarg; break; case 't': /* timeout */ if (!is_intpos (optarg)) usage4 (_("Timeout interval must be a positive integer")); else socket_timeout = atoi (optarg); break; case 'p': /* port */ if (!is_intpos (optarg)) usage4 (_("Port must be a positive integer")); else server_port = atoi (optarg); break; case 'E': escape = 1; break; case 's': if (escape) server_send = np_escaped_string(optarg); else xasprintf(&server_send, "%s", optarg); break; case 'e': /* expect string (may be repeated) */ match_flags &= ~NP_MATCH_EXACT; if (server_expect_count == 0) server_expect = malloc (sizeof (char *) * (++server_expect_count)); else server_expect = realloc (server_expect, sizeof (char *) * (++server_expect_count)); server_expect[server_expect_count - 1] = optarg; break; case 'm': if (!is_intpos (optarg)) usage4 (_("Maxbytes must be a positive integer")); else maxbytes = strtol (optarg, NULL, 0); break; case 'q': if (escape) server_quit = np_escaped_string(optarg); else xasprintf(&server_quit, "%s\r\n", optarg); break; case 'r': if (!strncmp(optarg,"ok",2)) econn_refuse_state = STATE_OK; else if (!strncmp(optarg,"warn",4)) econn_refuse_state = STATE_WARNING; else if (!strncmp(optarg,"crit",4)) econn_refuse_state = STATE_CRITICAL; else usage4 (_("Refuse must be one of ok, warn, crit")); break; case 'M': if (!strncmp(optarg,"ok",2)) expect_mismatch_state = STATE_OK; else if (!strncmp(optarg,"warn",4)) expect_mismatch_state = STATE_WARNING; else if (!strncmp(optarg,"crit",4)) expect_mismatch_state = STATE_CRITICAL; else usage4 (_("Mismatch must be one of ok, warn, crit")); break; case 'd': if (is_intpos (optarg)) delay = atoi (optarg); else usage4 (_("Delay must be a positive integer")); break; case 'D': /* Check SSL cert validity - days 'til certificate expiration */ #ifdef HAVE_SSL # ifdef USE_OPENSSL /* XXX */ if ((temp=strchr(optarg,','))!=NULL) { *temp='\0'; if (!is_intnonneg (optarg)) usage2 (_("Invalid certificate expiration period"), optarg); days_till_exp_warn = atoi(optarg); *temp=','; temp++; if (!is_intnonneg (temp)) usage2 (_("Invalid certificate expiration period"), temp); days_till_exp_crit = atoi (temp); } else { days_till_exp_crit=0; if (!is_intnonneg (optarg)) usage2 (_("Invalid certificate expiration period"), optarg); days_till_exp_warn = atoi (optarg); } check_cert = TRUE; flags |= FLAG_SSL; break; # endif /* USE_OPENSSL */ #endif /* fallthrough if we don't have ssl */ case 'S': #ifdef HAVE_SSL flags |= FLAG_SSL; #else die (STATE_UNKNOWN, _("Invalid option - SSL is not available")); #endif break; case 'A': match_flags |= NP_MATCH_ALL; break; } } c = optind; if(host_specified == FALSE && c < argc) server_address = strdup (argv[c++]); if (server_address == NULL) usage4 (_("You must provide a server address")); else if (server_address[0] != '/' && is_host (server_address) == FALSE) die (STATE_CRITICAL, "%s %s - %s: %s\n", SERVICE, state_text(STATE_CRITICAL), _("Invalid hostname, address or socket"), server_address); return TRUE; }
/* process command-line arguments */ int process_arguments (int argc, char **argv) { int c; int option = 0; static struct option longopts[] = { {"logfile", required_argument, 0, 'F'}, {"expires", required_argument, 0, 'e'}, {"aggregation", required_argument, 0, 'a'}, {"variable", required_argument, 0, 'v'}, {"critical", required_argument, 0, 'c'}, {"warning", required_argument, 0, 'w'}, {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} }; if (argc < 2) return ERROR; for (c = 1; c < argc; c++) { if (strcmp ("-to", argv[c]) == 0) strcpy (argv[c], "-t"); else if (strcmp ("-wt", argv[c]) == 0) strcpy (argv[c], "-w"); else if (strcmp ("-ct", argv[c]) == 0) strcpy (argv[c], "-c"); } while (1) { c = getopt_long (argc, argv, "hVF:e:a:c:w:", longopts, &option); if (c == -1 || c == EOF) break; switch (c) { case 'F': /* input file */ log_file = optarg; break; case 'e': /* expiration time */ expire_minutes = atoi (optarg); break; case 'a': /* aggregation (AVE or MAX) */ if (!strcmp (optarg, "MAX")) use_average = FALSE; else use_average = TRUE; break; case 'c': /* warning threshold */ sscanf (optarg, "%lu,%lu", &incoming_critical_threshold, &outgoing_critical_threshold); break; case 'w': /* critical threshold */ sscanf (optarg, "%lu,%lu", &incoming_warning_threshold, &outgoing_warning_threshold); break; case 'V': /* version */ print_revision (progname, revision); exit (STATE_OK); case 'h': /* help */ print_help (); exit (STATE_OK); case '?': /* help */ usage5 (); } } c = optind; if (argc > c && log_file == NULL) { log_file = argv[c++]; } if (argc > c && expire_minutes == -1) { expire_minutes = atoi (argv[c++]); } if (argc > c && strcmp (argv[c], "MAX") == 0) { use_average = FALSE; c++; } else if (argc > c && strcmp (argv[c], "AVG") == 0) { use_average = TRUE; c++; } if (argc > c && incoming_warning_threshold == 0) { incoming_warning_threshold = strtoul (argv[c++], NULL, 10); } if (argc > c && incoming_critical_threshold == 0) { incoming_critical_threshold = strtoul (argv[c++], NULL, 10); } if (argc > c && outgoing_warning_threshold == 0) { outgoing_warning_threshold = strtoul (argv[c++], NULL, 10); } if (argc > c && outgoing_critical_threshold == 0) { outgoing_critical_threshold = strtoul (argv[c++], NULL, 10); } return validate_arguments (); }