/* ** init a pre allocated or static hash structure ** and allocate buckets. */ Hash* hash_init(int type, Hash* h, char* name, int size, HashFunctions fun) { int sz; int ix = 0; h->meta_alloc_type = type; while (h_size_table[ix] != -1 && h_size_table[ix] < size) ix++; if (h_size_table[ix] == -1) return NULL; size = h_size_table[ix]; sz = size*sizeof(HashBucket*); h->bucket = (HashBucket**) fun.meta_alloc(h->meta_alloc_type, sz); sys_memzero(h->bucket, sz); h->is_allocated = 0; h->name = name; h->fun = fun; h->size = size; h->size_ix = ix; h->min_size_ix = ix; h->nobjs = 0; set_thresholds(h); return h; }
int main(int argc, char *argv[]) { int result, offset_result; double offset=0; char *result_line, *perfdata_line; setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); result = offset_result = STATE_OK; /* Parse extra opts if any */ argv=np_extra_opts (&argc, argv, progname); if (process_arguments (argc, argv) == ERROR) usage4 (_("Could not parse arguments")); set_thresholds(&offset_thresholds, owarn, ocrit); /* initialize alarm signal handling */ signal (SIGALRM, socket_timeout_alarm_handler); /* set socket timeout */ alarm (socket_timeout); offset = offset_request(server_address, &offset_result); if (offset_result == STATE_UNKNOWN) { result = (quiet == 1 ? STATE_UNKNOWN : STATE_CRITICAL); } else { result = get_status(fabs(offset), offset_thresholds); } switch (result) { case STATE_CRITICAL : xasprintf(&result_line, _("NTP CRITICAL:")); break; case STATE_WARNING : xasprintf(&result_line, _("NTP WARNING:")); break; case STATE_OK : xasprintf(&result_line, _("NTP OK:")); break; default : xasprintf(&result_line, _("NTP UNKNOWN:")); break; } if(offset_result == STATE_UNKNOWN) { xasprintf(&result_line, "%s %s", result_line, _("Offset unknown")); xasprintf(&perfdata_line, ""); } else { xasprintf(&result_line, "%s %s %.10g secs", result_line, _("Offset"), offset); xasprintf(&perfdata_line, "%s", perfd_offset(offset)); } printf("%s|%s\n", result_line, perfdata_line); if(server_address!=NULL) free(server_address); return result; }
/* 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_UNKNOWN); case 'V': /* version */ print_revision (progname, NP_VERSION); exit (STATE_UNKNOWN); case 'c': /* critical */ critical_range = optarg; break; case 'w': /* warning */ warning_range = optarg; break; } } c = optind; if (warning_range == NULL && argc > c) warning_range = argv[c++]; if (critical_range == NULL && argc > c) critical_range = argv[c++]; /* this will abort in case of invalid ranges */ set_thresholds (&thlds, warning_range, critical_range); if (thlds->warning->end < 0) usage4 (_("Warning threshold must be a positive integer")); if (thlds->critical->end < 0) usage4 (_("Critical threshold must be a positive integer")); return OK; }
static int process_arguments (int argc, char **argv) { int c; int escape = 0; char *temp; int option = 0; static struct option longopts[] = { {"critical", required_argument, 0, 'c'}, {"warning", required_argument, 0, 'w'}, {"timeunit", required_argument, 0, 'u'}, {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} }; while ( 1 ) { c = getopt_long ( argc, argv, "+hvVu:c:w:", longopts, &option ); if ( c == -1 || c == EOF || c == 1 ) break; switch ( c ) { case '?': usage5 (); case 'h': print_help (); exit ( STATE_OK ); case 'v': verbose++; if (verbose >= 3) { printf("Verbose mode enabled\n"); } break; case 'V': print_revision (progname, NP_VERSION); exit (STATE_OK); case 'u': timeunit = optarg; break; case 'c': critical = optarg; break; case 'w': warning = optarg; break; } // end case } // end while c = optind; set_thresholds(&my_thresholds, warning, critical); return validate_arguments (); } // end process_arguments
void __init TAU_init_smp(void * info) { unsigned long cpu = smp_processor_id(); /* */ tau[cpu].low = 5; tau[cpu].high = 120; set_thresholds(cpu); }
void __init TAU_init_smp(void * info) { unsigned long cpu = smp_processor_id(); /* set these to a reasonable value and let the timer shrink the * window */ tau[cpu].low = 5; tau[cpu].high = 120; set_thresholds(cpu); }
static void tau_timeout(void * info) { int cpu; unsigned long flags; int size; int shrink; /* disabling interrupts *should* be okay */ local_irq_save(flags); cpu = smp_processor_id(); #ifndef CONFIG_TAU_INT TAUupdate(cpu); #endif size = tau[cpu].high - tau[cpu].low; if (size > min_window && ! tau[cpu].grew) { /* do an exponential shrink of half the amount currently over size */ shrink = (2 + size - min_window) / 4; if (shrink) { tau[cpu].low += shrink; tau[cpu].high -= shrink; } else { /* size must have been min_window + 1 */ tau[cpu].low += 1; #if 1 /* debug */ if ((tau[cpu].high - tau[cpu].low) != min_window){ printk(KERN_ERR "temp.c: line %d, logic error\n", __LINE__); } #endif } } tau[cpu].grew = 0; set_thresholds(cpu); /* * Do the enable every time, since otherwise a bunch of (relatively) * complex sleep code needs to be added. One mtspr every time * tau_timeout is called is probably not a big deal. * * Enable thermal sensor and set up sample interval timer * need 20 us to do the compare.. until a nice 'cpu_speed' function * call is implemented, just assume a 500 mhz clock. It doesn't really * matter if we take too long for a compare since it's all interrupt * driven anyway. * * use a extra long time.. (60 us @ 500 mhz) */ mtspr(SPRN_THRM3, THRM3_SITV(500*60) | THRM3_E); local_irq_restore(flags); }
static void tau_timeout(void * info) { int cpu; unsigned long flags; int size; int shrink; /* */ local_irq_save(flags); cpu = smp_processor_id(); #ifndef CONFIG_TAU_INT TAUupdate(cpu); #endif size = tau[cpu].high - tau[cpu].low; if (size > min_window && ! tau[cpu].grew) { /* */ shrink = (2 + size - min_window) / 4; if (shrink) { tau[cpu].low += shrink; tau[cpu].high -= shrink; } else { /* */ tau[cpu].low += 1; #if 1 /* */ if ((tau[cpu].high - tau[cpu].low) != min_window){ printk(KERN_ERR "temp.c: line %d, logic error\n", __LINE__); } #endif } } tau[cpu].grew = 0; set_thresholds(cpu); /* */ mtspr(SPRN_THRM3, THRM3_SITV(500*60) | THRM3_E); local_irq_restore(flags); }
static void set_sensor_thresholds(ipmi_sensor_t *sensor, void *cb_data) { struct ohoi_sensor_thresholds *thres_data; SaErrorT rv; thres_data = cb_data; if (ignore_sensor(sensor)) { dbg("sensor is ignored"); thres_data->hyster_done = 1; thres_data->thres_done = 1; thres_data->rvalue = SA_ERR_HPI_NOT_PRESENT; return; } if (ipmi_sensor_get_event_reading_type(sensor) != IPMI_EVENT_READING_TYPE_THRESHOLD) { dbg("Not threshold sensor!"); thres_data->hyster_done = 1; thres_data->thres_done = 1; thres_data->rvalue = SA_ERR_HPI_INVALID_CMD; return; } if ((ipmi_sensor_get_threshold_access(sensor) != IPMI_THRESHOLD_ACCESS_SUPPORT_SETTABLE) || (ipmi_sensor_get_hysteresis_support(sensor) != IPMI_HYSTERESIS_SUPPORT_SETTABLE)) { dbg("sensor doesn't support threshold or histeresis set"); thres_data->hyster_done = 1; thres_data->thres_done = 1; thres_data->rvalue = SA_ERR_HPI_INVALID_CMD; return; } rv = set_thresholds(sensor, thres_data); if (rv != SA_OK) { dbg("Unable to set thresholds"); thres_data->hyster_done = 1; thres_data->thres_done = 1; thres_data->rvalue = rv; return; } rv = set_hysteresis(sensor, thres_data); if (rv != SA_OK) { thres_data->hyster_done = 1; thres_data->thres_done = 1; thres_data->rvalue = rv; dbg("Unable to set hysteresis"); return; } return; }
int validate_arguments () { if (ld_host==NULL || strlen(ld_host)==0) usage4 (_("Please specify the host name\n")); if (ld_base==NULL) usage4 (_("Please specify the LDAP base\n")); if (crit_entries!=NULL || warn_entries!=NULL) { set_thresholds(&entries_thresholds, warn_entries, crit_entries); } return OK; }
bool CapacitiveSensor::connect(ConnectionPtr the_device) { m_impl->m_sensor_device = the_device; m_impl->m_sensor_accumulator.clear(); if(the_device /*&& the_uart_device->is_open()*/) { the_device->drain(); set_thresholds(m_impl->m_thresh_touch, m_impl->m_thresh_release); set_charge_current(m_impl->m_charge_current); m_impl->m_sensor_device->set_receive_cb(std::bind(&CapacitiveSensor::receive_data, this, std::placeholders::_1, std::placeholders::_2)); return true; } return false; }
void TAUupdate(int cpu) { unsigned thrm; #ifdef DEBUG printk("TAUupdate "); #endif /* */ if((thrm = mfspr(SPRN_THRM1)) & THRM1_TIV){ /* */ if(thrm & THRM1_TIN){ /* */ if (tau[cpu].low >= step_size){ tau[cpu].low -= step_size; tau[cpu].high -= (step_size - window_expand); } tau[cpu].grew = 1; #ifdef DEBUG printk("low threshold crossed "); #endif } } if((thrm = mfspr(SPRN_THRM2)) & THRM1_TIV){ /* */ if(thrm & THRM1_TIN){ /* */ if (tau[cpu].high <= 127-step_size){ tau[cpu].low += (step_size - window_expand); tau[cpu].high += step_size; } tau[cpu].grew = 1; #ifdef DEBUG printk("high threshold crossed "); #endif } } #ifdef DEBUG printk("grew = %d\n", tau[cpu].grew); #endif #ifndef CONFIG_TAU_INT /* */ set_thresholds(cpu); #endif }
void TAUupdate(int cpu) { unsigned thrm; #ifdef DEBUG printk("TAUupdate "); #endif /* if both thresholds are crossed, the step_sizes cancel out * and the window winds up getting expanded twice. */ if((thrm = mfspr(SPRN_THRM1)) & THRM1_TIV){ /* is valid? */ if(thrm & THRM1_TIN){ /* crossed low threshold */ if (tau[cpu].low >= step_size){ tau[cpu].low -= step_size; tau[cpu].high -= (step_size - window_expand); } tau[cpu].grew = 1; #ifdef DEBUG printk("low threshold crossed "); #endif } } if((thrm = mfspr(SPRN_THRM2)) & THRM1_TIV){ /* is valid? */ if(thrm & THRM1_TIN){ /* crossed high threshold */ if (tau[cpu].high <= 127-step_size){ tau[cpu].low += (step_size - window_expand); tau[cpu].high += step_size; } tau[cpu].grew = 1; #ifdef DEBUG printk("high threshold crossed "); #endif } } #ifdef DEBUG printk("grew = %d\n", tau[cpu].grew); #endif #ifndef CONFIG_TAU_INT /* tau_timeout will do this if not using interrupts */ set_thresholds(cpu); #endif }
void set_all_thresholds (struct parameter_list *path) { if (path->freespace_units != NULL) free(path->freespace_units); set_thresholds(&path->freespace_units, warn_freespace_units, crit_freespace_units); if (path->freespace_percent != NULL) free (path->freespace_percent); set_thresholds(&path->freespace_percent, warn_freespace_percent, crit_freespace_percent); if (path->usedspace_units != NULL) free (path->usedspace_units); set_thresholds(&path->usedspace_units, warn_usedspace_units, crit_usedspace_units); if (path->usedspace_percent != NULL) free (path->usedspace_percent); set_thresholds(&path->usedspace_percent, warn_usedspace_percent, crit_usedspace_percent); if (path->usedinodes_percent != NULL) free (path->usedinodes_percent); set_thresholds(&path->usedinodes_percent, warn_usedinodes_percent, crit_usedinodes_percent); if (path->freeinodes_percent != NULL) free (path->freeinodes_percent); set_thresholds(&path->freeinodes_percent, warn_freeinodes_percent, crit_freeinodes_percent); }
/* ** Rehash all objects */ static void rehash(Hash* h, int grow) { int sz; int old_size = h->size; HashBucket** new_bucket; int i; if (grow) { if ((h_size_table[h->size_ix+1]) == -1) return; h->size_ix++; } else { if (h->size_ix == 0) return; h->size_ix--; } h->size = h_size_table[h->size_ix]; sz = h->size*sizeof(HashBucket*); new_bucket = (HashBucket **) h->fun.meta_alloc(h->meta_alloc_type, sz); sys_memzero(new_bucket, sz); for (i = 0; i < old_size; i++) { HashBucket* b = h->bucket[i]; while (b != (HashBucket*) 0) { HashBucket* b_next = b->next; int ix = b->hvalue % h->size; b->next = new_bucket[ix]; new_bucket[ix] = b; b = b_next; } } h->fun.meta_free(h->meta_alloc_type, (void *) h->bucket); h->bucket = new_bucket; set_thresholds(h); }
static void set_sensor_thresholds(ipmi_sensor_t *sensor, void *cb_data) { struct ohoi_sensor_thresholds *thres_data; int rv; thres_data = cb_data; if (ignore_sensor(sensor)) { dbg("sensor is ignored"); return; } if (ipmi_sensor_get_event_reading_type(sensor) == IPMI_EVENT_READING_TYPE_THRESHOLD) { if (ipmi_sensor_get_threshold_access(sensor) == IPMI_THRESHOLD_ACCESS_SUPPORT_SETTABLE) { rv = set_thresholds(sensor, thres_data); if (rv < 0) { dbg("Unable to set thresholds"); return; } } else dbg("sensor doesn't support threshold set"); rv = ipmi_sensor_get_hysteresis_support(sensor); if (rv == IPMI_HYSTERESIS_SUPPORT_SETTABLE) { rv = set_hysteresis(sensor, thres_data); if (rv < 0) { dbg("Unable to set hysteresis"); return; } } else dbg("sensor doesn't support hysteresis set"); } else dbg("Not threshold sensor!"); }
int main(int argc, char **argv) { struct ibnd_config config = { 0 }; int resolved = -1; ib_portid_t portid = { 0 }; ib_portid_t self_portid = { 0 }; int rc = 0; ibnd_fabric_t *fabric = NULL; ib_gid_t self_gid; int port = 0; int mgmt_classes[4] = { IB_SMI_CLASS, IB_SMI_DIRECT_CLASS, IB_SA_CLASS, IB_PERFORMANCE_CLASS }; const struct ibdiag_opt opts[] = { {"suppress", 's', 1, "<err1,err2,...>", "suppress errors listed"}, {"suppress-common", 'c', 0, NULL, "suppress some of the common counters"}, {"node-name-map", 1, 1, "<file>", "node name map file"}, {"port-guid", 'G', 1, "<port_guid>", "report the node containing the port specified by <port_guid>"}, {"", 'S', 1, "<port_guid>", "Same as \"-G\" for backward compatibility"}, {"Direct", 'D', 1, "<dr_path>", "report the node containing the port specified by <dr_path>"}, {"skip-sl", 10, 0, NULL,"don't obtain SL to all destinations"}, {"report-port", 'r', 0, NULL, "report port link information"}, {"threshold-file", 8, 1, NULL, "specify an alternate threshold file, default: " DEF_THRES_FILE}, {"GNDN", 'R', 0, NULL, "(This option is obsolete and does nothing)"}, {"data", 2, 0, NULL, "include data counters for ports with errors"}, {"switch", 3, 0, NULL, "print data for switches only"}, {"ca", 4, 0, NULL, "print data for CA's only"}, {"router", 5, 0, NULL, "print data for routers only"}, {"details", 6, 0, NULL, "include transmit discard details"}, {"counters", 9, 0, NULL, "print data counters only"}, {"clear-errors", 'k', 0, NULL, "Clear error counters after read"}, {"clear-counts", 'K', 0, NULL, "Clear data counters after read"}, {"load-cache", 7, 1, "<file>", "filename of ibnetdiscover cache to load"}, {"outstanding_smps", 'o', 1, NULL, "specify the number of outstanding SMP's which should be " "issued during the scan"}, {0} }; char usage_args[] = ""; memset(suppressed_fields, 0, sizeof suppressed_fields); ibdiag_process_opts(argc, argv, &config, "cDGKLnRrSs", opts, process_opt, usage_args, NULL); argc -= optind; argv += optind; if (!node_type_to_print) node_type_to_print = PRINT_ALL; ibmad_port = mad_rpc_open_port(ibd_ca, ibd_ca_port, mgmt_classes, 4); if (!ibmad_port) IBEXIT("Failed to open port; %s:%d\n", ibd_ca, ibd_ca_port); smp_mkey_set(ibmad_port, ibd_mkey); if (ibd_timeout) { mad_rpc_set_timeout(ibmad_port, ibd_timeout); config.timeout_ms = ibd_timeout; } config.flags = ibd_ibnetdisc_flags; config.mkey = ibd_mkey; node_name_map = open_node_name_map(node_name_map_file); if (dr_path && load_cache_file) { fprintf(stderr, "Cannot specify cache and direct route path\n"); exit(-1); } if (resolve_self(ibd_ca, ibd_ca_port, &self_portid, &port, &self_gid.raw) < 0) { IBEXIT("can't resolve self port %s", argv[0]); goto close_port; } /* limit the scan the fabric around the target */ if (dr_path) { if ((resolved = resolve_portid_str(ibd_ca, ibd_ca_port, &portid, dr_path, IB_DEST_DRPATH, NULL, ibmad_port)) < 0) IBWARN("Failed to resolve %s; attempting full scan", dr_path); } else if (port_guid_str) { if ((resolved = resolve_portid_str(ibd_ca, ibd_ca_port, &portid, port_guid_str, IB_DEST_GUID, ibd_sm_id, ibmad_port)) < 0) IBWARN("Failed to resolve %s; attempting full scan", port_guid_str); if(obtain_sl) lid2sl_table[portid.lid] = portid.sl; } if (load_cache_file) { if ((fabric = ibnd_load_fabric(load_cache_file, 0)) == NULL) { fprintf(stderr, "loading cached fabric failed\n"); exit(-1); } } else { if (resolved >= 0) { if (!config.max_hops) config.max_hops = 1; if (!(fabric = ibnd_discover_fabric(ibd_ca, ibd_ca_port, &portid, &config))) IBWARN("Single node discover failed;" " attempting full scan"); } if (!fabric && !(fabric = ibnd_discover_fabric(ibd_ca, ibd_ca_port, NULL, &config))) { fprintf(stderr, "discover failed\n"); rc = -1; goto close_port; } } set_thresholds(threshold_file); if (port_guid_str) { ibnd_port_t *port = ibnd_find_port_guid(fabric, port_guid); if (port) print_node(port->node, NULL); else fprintf(stderr, "Failed to find node: %s\n", port_guid_str); } else if (dr_path) { ibnd_port_t *port = ibnd_find_port_dr(fabric, dr_path); uint8_t ni[IB_SMP_DATA_SIZE] = { 0 }; if (!smp_query_via(ni, &portid, IB_ATTR_NODE_INFO, 0, ibd_timeout, ibmad_port)) { rc = -1; goto destroy_fabric; } mad_decode_field(ni, IB_NODE_PORT_GUID_F, &(port_guid)); port = ibnd_find_port_guid(fabric, port_guid); if (port) { if(obtain_sl) if(path_record_query(self_gid,port->guid)) goto destroy_fabric; print_node(port->node, NULL); } else fprintf(stderr, "Failed to find node: %s\n", dr_path); } else { if(obtain_sl) if(path_record_query(self_gid,0)) goto destroy_fabric; ibnd_iter_nodes(fabric, print_node, NULL); } rc = print_summary(); if (rc) rc = 1; destroy_fabric: ibnd_destroy_fabric(fabric); close_port: mad_rpc_close_port(ibmad_port); close_node_name_map(node_name_map); exit(rc); }
/* 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'}, {"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}, {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, "Vvht:c:w:p:s:u:C:a:z:r:m:P:", 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 */ if (!is_integer (optarg)) usage2 (_("Timeout interval must be a positive integer"), optarg); else timeout_interval = atoi (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 '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 'v': /* command */ verbose++; 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 (); }
int main(int argc, char *argv[]){ int result, offset_result, jitter_result; double offset=0, jitter=0; char *result_line, *perfdata_line; setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); result = offset_result = jitter_result = STATE_OK; /* Parse extra opts if any */ argv=np_extra_opts (&argc, argv, progname); if (process_arguments (argc, argv) == ERROR) usage4 (_("Could not parse arguments")); set_thresholds(&offset_thresholds, owarn, ocrit); set_thresholds(&jitter_thresholds, jwarn, jcrit); /* initialize alarm signal handling */ signal (SIGALRM, socket_timeout_alarm_handler); /* set socket timeout */ alarm (socket_timeout); offset = offset_request(server_address, &offset_result); /* check_ntp used to always return CRITICAL if offset_result == STATE_UNKNOWN. * Now we'll only do that is the offset thresholds were set */ if (do_offset && offset_result == STATE_UNKNOWN) { result = STATE_CRITICAL; } else { result = get_status(fabs(offset), offset_thresholds); } /* If not told to check the jitter, we don't even send packets. * jitter is checked using NTP control packets, which not all * servers recognize. Trying to check the jitter on OpenNTPD * (for example) will result in an error */ if(do_jitter){ jitter=jitter_request(server_address, &jitter_result); result = max_state_alt(result, get_status(jitter, jitter_thresholds)); /* -1 indicates that we couldn't calculate the jitter * Only overrides STATE_OK from the offset */ if(jitter == -1.0 && result == STATE_OK) result = STATE_UNKNOWN; } result = max_state_alt(result, jitter_result); switch (result) { case STATE_CRITICAL : xasprintf(&result_line, _("NTP CRITICAL:")); break; case STATE_WARNING : xasprintf(&result_line, _("NTP WARNING:")); break; case STATE_OK : xasprintf(&result_line, _("NTP OK:")); break; default : xasprintf(&result_line, _("NTP UNKNOWN:")); break; } if(offset_result == STATE_UNKNOWN){ xasprintf(&result_line, "%s %s", result_line, _("Offset unknown")); xasprintf(&perfdata_line, ""); } else { xasprintf(&result_line, "%s %s %.10g secs", result_line, _("Offset"), offset); xasprintf(&perfdata_line, "%s", perfd_offset(offset)); } if (do_jitter) { xasprintf(&result_line, "%s, jitter=%f", result_line, jitter); xasprintf(&perfdata_line, "%s %s", perfdata_line, perfd_jitter(jitter)); } printf("%s|%s\n", result_line, perfdata_line); if(server_address!=NULL) free(server_address); return result; }
/* 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'}, {"expected-address", required_argument, 0, 'a'}, {"expect-authority", no_argument, 0, 'A'}, {"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, "hVvAt:H:s:r:a: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 'A': /* expect authority */ expect_authority = 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 (); }
int main (int argc, char **argv) { int c; bool verbose = false; char *critical = NULL, *warning = NULL; nagstatus status = STATE_OK; thresholds *my_threshold = NULL; unsigned long long nctxt[2]; unsigned int sleep_time = 1, tog = 0; /* toggle switch for cleaner code */ unsigned long i, delay, count; set_program_name (argv[0]); while ((c = getopt_long (argc, argv, "c:w:v" GETOPT_HELP_VERSION_STRING, longopts, NULL)) != -1) { switch (c) { default: usage (stderr); case 'c': critical = optarg; break; case 'w': warning = optarg; break; case 'v': verbose = true; break; case_GETOPT_HELP_CHAR case_GETOPT_VERSION_CHAR } } delay = DELAY_DEFAULT, count = COUNT_DEFAULT; if (optind < argc) { delay = strtol_or_err (argv[optind++], "failed to parse argument"); if (delay < 1) plugin_error (STATE_UNKNOWN, 0, "delay must be positive integer"); else if (UINT_MAX < delay) plugin_error (STATE_UNKNOWN, 0, "too large delay value"); sleep_time = delay; } if (optind < argc) count = strtol_or_err (argv[optind++], "failed to parse argument"); status = set_thresholds (&my_threshold, warning, critical); if (status == NP_RANGE_UNPARSEABLE) usage (stderr); unsigned long long dnctxt = nctxt[0] = cpu_stats_get_cswch (); if (verbose) printf ("ctxt = %Lu\n", dnctxt); for (i = 1; i < count; i++) { sleep (sleep_time); tog = !tog; nctxt[tog] = cpu_stats_get_cswch (); dnctxt = (nctxt[tog] - nctxt[!tog]) / sleep_time; if (verbose) printf ("ctxt = %Lu --> %Lu/s\n", nctxt[tog], dnctxt); } status = get_status (dnctxt, my_threshold); free (my_threshold); char *time_unit = (count > 1) ? "/s" : ""; printf ("%s %s - number of context switches%s %Lu | cswch%s=%Lu\n", program_name_short, state_text (status), time_unit, dnctxt, time_unit, dnctxt); return status; }
/* 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': xasprintf(&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 (); }
int main (int argc, char **argv) { bool vmem_perfdata = false; int c, status, err; int shift = k_shift; char *critical = NULL, *warning = NULL; char *units = NULL; char *status_msg; char *perfdata_swap_msg, *perfdata_vmem_msg = NULL; float percent_used = 0; thresholds *my_threshold = NULL; struct proc_sysmem *sysmem = NULL; unsigned long kb_swap_cached; unsigned long kb_swap_free; unsigned long kb_swap_total; unsigned long kb_swap_used; struct proc_vmem *vmem = NULL; unsigned long dpswpin, dpswpout; unsigned long kb_swap_pageins[2]; unsigned long kb_swap_pageouts[2]; set_program_name (argv[0]); while ((c = getopt_long (argc, argv, "sc:w:bkmg" GETOPT_HELP_VERSION_STRING, longopts, NULL)) != -1) { switch (c) { default: usage (stderr); case 's': vmem_perfdata = true; break; case 'c': critical = optarg; break; case 'w': warning = optarg; break; case 'b': shift = b_shift; units = xstrdup ("B"); break; case 'k': shift = k_shift; units = xstrdup ("kB"); break; case 'm': shift = m_shift; units = xstrdup ("MB"); break; case 'g': shift = g_shift; units = xstrdup ("GB"); break; case_GETOPT_HELP_CHAR case_GETOPT_VERSION_CHAR } } status = set_thresholds (&my_threshold, warning, critical); if (status == NP_RANGE_UNPARSEABLE) usage (stderr); /* output in kilobytes by default */ if (units == NULL) units = xstrdup ("kB"); err = proc_sysmem_new (&sysmem); if (err < 0) plugin_error (STATE_UNKNOWN, err, "memory exhausted"); proc_sysmem_read (sysmem); kb_swap_cached = proc_sysmem_get_swap_cached (sysmem); kb_swap_free = proc_sysmem_get_swap_free (sysmem); kb_swap_total = proc_sysmem_get_swap_total (sysmem); kb_swap_used = proc_sysmem_get_swap_used (sysmem); if (vmem_perfdata) { err = proc_vmem_new (&vmem); if (err < 0) plugin_error (STATE_UNKNOWN, err, "memory exhausted"); proc_vmem_read (vmem); kb_swap_pageins[0] = proc_vmem_get_pswpin (vmem); kb_swap_pageouts[0] = proc_vmem_get_pswpout (vmem); sleep (1); proc_vmem_read (vmem); kb_swap_pageins[1] = proc_vmem_get_pswpin (vmem); kb_swap_pageouts[1] = proc_vmem_get_pswpout (vmem); dpswpin = kb_swap_pageins[1] - kb_swap_pageins[0]; dpswpout = kb_swap_pageouts[1] - kb_swap_pageouts[0]; perfdata_vmem_msg = xasprintf (" swap_pageins/s=%lu swap_pageouts/s=%lu", dpswpin, dpswpout); } if (kb_swap_total != 0) percent_used = (kb_swap_used * 100.0 / kb_swap_total); status = get_status (percent_used, my_threshold); free (my_threshold); status_msg = xasprintf ("%s: %.2f%% (%Lu %s) used", state_text (status), percent_used, SU (kb_swap_used)); perfdata_swap_msg = xasprintf ("swap_total=%Lu%s swap_used=%Lu%s swap_free=%Lu%s " /* The amount of swap, in kB, used as cache memory */ "swap_cached=%Lu%s", SU (kb_swap_total), SU (kb_swap_used), SU (kb_swap_free), SU (kb_swap_cached)); printf ("%s %s | %s%s\n", "SWAP", status_msg, perfdata_swap_msg, vmem_perfdata ? perfdata_vmem_msg : ""); proc_vmem_unref (vmem); proc_sysmem_unref (sysmem); return status; }
int main (int argc, char **argv) { int c, n_ports, n_online; bool verbose = false, summary = false; char *critical = NULL, *warning = NULL; nagstatus status = STATE_OK; thresholds *my_threshold = NULL; unsigned long delay, count; unsigned int sleep_time = 1; set_program_name (argv[0]); while ((c = getopt_long (argc, argv, "c:w:vi" GETOPT_HELP_VERSION_STRING, longopts, NULL)) != -1) { switch (c) { default: usage (stderr); case 'i': summary = true; break; case 'c': critical = optarg; break; case 'w': warning = optarg; break; case 'v': verbose = true; break; case_GETOPT_HELP_CHAR case_GETOPT_VERSION_CHAR } } if (summary) { fc_host_summary (verbose); return STATE_UNKNOWN; } delay = DELAY_DEFAULT, count = COUNT_DEFAULT; if (optind < argc) { delay = strtol_or_err (argv[optind++], "failed to parse argument"); if (delay < 1) plugin_error (STATE_UNKNOWN, 0, "delay must be positive integer"); else if (UINT_MAX < delay) plugin_error (STATE_UNKNOWN, 0, "too large delay value"); sleep_time = delay; } if (optind < argc) count = strtol_or_err (argv[optind++], "failed to parse argument"); status = set_thresholds (&my_threshold, warning, critical); if (status == NP_RANGE_UNPARSEABLE) usage (stderr); fc_host_statistics stats = {0}; fc_host_status (&n_ports, &n_online, &stats, sleep_time, count); status = get_status (n_online, my_threshold); printf ("%s %s - Fiber Channel ports status: %d/%d Online " "| rx_frames=%Lu tx_frames=%Lu" " error_frames=%Lu" " invalid_crc_count=%Lu" " link_failure_count=%Lu" " loss_of_signal_count=%Lu" " loss_of_sync_count=%Lu\n", program_name_short, state_text (status), n_online, n_ports, (unsigned long long) stats.rx_frames, (unsigned long long) stats.tx_frames, (unsigned long long) stats.error_frames, (unsigned long long) stats.invalid_crc_count, (unsigned long long) stats.link_failure_count, (unsigned long long) stats.loss_of_signal_count, (unsigned long long) stats.loss_of_sync_count ); return status; }
int main(int argc, char **argv){ char *ptr; int data_val; int return_code=STATE_OK; thresholds *thresholds = NULL; 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) usage(_("Could not parse arguments")); /* Initialize the thresholds */ set_thresholds(&thresholds, warn_threshold, crit_threshold); if(verbose) print_thresholds("check_cluster", thresholds); /* check the data values */ for(ptr=strtok(data_vals,",");ptr!=NULL;ptr=strtok(NULL,",")){ data_val=atoi(ptr); if(check_type==CHECK_SERVICES){ switch(data_val){ case 0: total_services_ok++; break; case 1: total_services_warning++; break; case 2: total_services_critical++; break; case 3: total_services_unknown++; break; default: break; } } else{ switch(data_val){ case 0: total_hosts_up++; break; case 1: total_hosts_down++; break; case 2: total_hosts_unreachable++; break; default: break; } } } /* return the status of the cluster */ if(check_type==CHECK_SERVICES){ return_code=get_status(total_services_warning+total_services_unknown+total_services_critical, thresholds); printf("CLUSTER %s: %s: %d ok, %d warning, %d unknown, %d critical\n", state_text(return_code), (label==NULL)?"Service cluster":label, total_services_ok,total_services_warning, total_services_unknown,total_services_critical); } else{ return_code=get_status(total_hosts_down+total_hosts_unreachable, thresholds); printf("CLUSTER %s: %s: %d up, %d down, %d unreachable\n", state_text(return_code), (label==NULL)?"Host cluster":label, total_hosts_up,total_hosts_down,total_hosts_unreachable); } return return_code; }
int main (int argc, char **argv) { int c; bool verbose = false; char *critical = NULL, *warning = NULL; nagstatus status = STATE_OK; thresholds *my_threshold = NULL; unsigned long long nintr[2]; unsigned int sleep_time = 1, tog = 0, /* toggle switch for cleaner code */ ncpus0, ncpus1; unsigned long i, delay, count, *vintr[2] = { NULL, NULL }; set_program_name (argv[0]); while ((c = getopt_long (argc, argv, "c:w:v" GETOPT_HELP_VERSION_STRING, longopts, NULL)) != -1) { switch (c) { default: usage (stderr); case 'c': critical = optarg; break; case 'w': warning = optarg; break; case 'v': verbose = true; break; case_GETOPT_HELP_CHAR case_GETOPT_VERSION_CHAR } } delay = DELAY_DEFAULT, count = COUNT_DEFAULT; if (optind < argc) { delay = strtol_or_err (argv[optind++], "failed to parse argument"); if (delay < 1) plugin_error (STATE_UNKNOWN, 0, "delay must be positive integer"); else if (UINT_MAX < delay) plugin_error (STATE_UNKNOWN, 0, "too large delay value"); sleep_time = delay; } if (optind < argc) count = strtol_or_err (argv[optind++], "failed to parse argument"); status = set_thresholds (&my_threshold, warning, critical); if (status == NP_RANGE_UNPARSEABLE) usage (stderr); unsigned long long dnintr = nintr[0] = cpu_stats_get_intr (); if (verbose) printf ("intr = %Lu\n", dnintr); if (count <= 2) vintr[0] = proc_interrupts_get_nintr_per_cpu (&ncpus0); for (i = 1; i < count; i++) { sleep (sleep_time); tog = !tog; nintr[tog] = cpu_stats_get_intr (); dnintr = (nintr[tog] - nintr[!tog]) / sleep_time; if (verbose) printf ("intr = %Lu --> %Lu/s\n", nintr[tog], dnintr); if (count - 2 == i) vintr[0] = proc_interrupts_get_nintr_per_cpu (&ncpus0); else if (count - 1 == i) vintr[1] = proc_interrupts_get_nintr_per_cpu (&ncpus1); } status = get_status (dnintr, my_threshold); free (my_threshold); char *time_unit = (count > 1) ? "/s" : ""; printf ("%s %s - number of interrupts%s %Lu | intr%s=%Lu", program_name_short, state_text (status), time_unit, dnintr, time_unit, dnintr); for (i = 0; i < MIN (ncpus0, ncpus1); i++) printf (" intr_cpu%lu%s=%lu", i, time_unit, (count > 1) ? (vintr[1][i] - vintr[0][i]) / sleep_time : vintr[0][i]); printf ("\n"); free (vintr[1]); free (vintr[0]); return status; }
int main (int argc, char **argv) { int c, err; bool verbose, cpu_model, per_cpu_stats; unsigned long len, i, count, delay; char *critical = NULL, *warning = NULL; char *p = NULL, *cpu_progname; nagstatus currstatus, status; thresholds *my_threshold = NULL; float cpu_perc = 0.0; unsigned int tog = 0; /* toggle switch for cleaner code */ struct cpu_desc *cpudesc = NULL; set_program_name (argv[0]); len = strlen (program_name); if (len > 6 && !strncmp (program_name, "check_", 6)) p = (char *) program_name + 6; else plugin_error (STATE_UNKNOWN, 0, "bug: the plugin does not have a standard name"); if (!strncmp (p, "iowait", 6)) /* check_iowait --> cpu_iowait */ { cpu_progname = xstrdup ("iowait"); program_shorthelp = xstrdup ("This plugin checks I/O wait bottlenecks\n"); } else /* check_cpu --> cpu_user (the default) */ { cpu_progname = xstrdup ("user");; program_shorthelp = xstrdup ("This plugin checks the CPU (user mode) utilization\n"); } err = cpu_desc_new (&cpudesc); if (err < 0) plugin_error (STATE_UNKNOWN, err, "memory exhausted"); /* default values */ verbose = per_cpu_stats = false; cpu_model = true; while ((c = getopt_long ( argc, argv, "c:w:vifmp" GETOPT_HELP_VERSION_STRING, longopts, NULL)) != -1) { switch (c) { default: usage (stderr); case 'i': cpu_desc_read (cpudesc); cpu_desc_summary (cpudesc); return STATE_UNKNOWN; case 'm': cpu_model = false; break; case 'p': per_cpu_stats = true; break; case 'c': critical = optarg; break; case 'w': warning = optarg; break; case 'v': verbose = true; break; case_GETOPT_HELP_CHAR case_GETOPT_VERSION_CHAR } } delay = DELAY_DEFAULT, count = COUNT_DEFAULT; if (optind < argc) { delay = strtol_or_err (argv[optind++], "failed to parse argument"); if (delay < 1) plugin_error (STATE_UNKNOWN, 0, "delay must be positive integer"); else if (DELAY_MAX < delay) plugin_error (STATE_UNKNOWN, 0, "too large delay value (greater than %d)", DELAY_MAX); } if (optind < argc) { count = strtol_or_err (argv[optind++], "failed to parse argument"); if (COUNT_MAX < count) plugin_error (STATE_UNKNOWN, 0, "too large count value (greater than %d)", COUNT_MAX); } status = set_thresholds (&my_threshold, warning, critical); if (status == NP_RANGE_UNPARSEABLE) usage (stderr); int ncpus = per_cpu_stats ? get_processor_number_total () + 1 : 1; jiff duser[ncpus], dsystem[ncpus], didle[ncpus], diowait[ncpus], dsteal[ncpus], ratio[ncpus]; int debt[ncpus]; /* handle idle ticks running backwards */ struct cpu_time cpuv[2][ncpus]; jiff *cpu_value = strncmp (p, "iowait", 6) ? duser : diowait; const char *cpuname; cpu_stats_get_time (cpuv[0], ncpus); for (c = 0; c < ncpus; c++) { duser[c] = cpuv[0][c].user + cpuv[0][c].nice; dsystem[c] = cpuv[0][c].system + cpuv[0][c].irq + cpuv[0][c].softirq; didle[c] = cpuv[0][c].idle; diowait[c] = cpuv[0][c].iowait; dsteal[c] = cpuv[0][c].steal; debt[c] = 0; ratio[c] = duser[c] + dsystem[c] + didle[c] + diowait[c] + dsteal[c]; if (!ratio[c]) ratio[c] = 1, didle[c] = 1; } for (i = 1; i < count; i++) { sleep (delay); tog = !tog; cpu_stats_get_time (cpuv[tog], ncpus); for (c = 0; c < ncpus; c++) { duser[c] = cpuv[tog][c].user - cpuv[!tog][c].user + cpuv[tog][c].nice - cpuv[!tog][c].nice; dsystem[c] = cpuv[tog][c].system - cpuv[!tog][c].system + cpuv[tog][c].irq - cpuv[!tog][c].irq + cpuv[tog][c].softirq - cpuv[!tog][c].softirq; didle[c] = cpuv[tog][c].idle - cpuv[!tog][c].idle; diowait[c] = cpuv[tog][c].iowait - cpuv[!tog][c].iowait; dsteal[c] = cpuv[tog][c].steal - cpuv[!tog][c].steal; /* idle can run backwards for a moment -- kernel "feature" */ if (debt[c]) { didle[c] = (int) didle[c] + debt[c]; debt[c] = 0; } if ((int) didle[c] < 0) { debt[c] = (int) didle[c]; didle[c] = 0; } ratio[c] = duser[c] + dsystem[c] + didle[c] + diowait[c] + dsteal[c]; if (!ratio[c]) ratio[c] = 1, didle[c] = 1; if (NULL == (cpuname = cpuv[0][c].cpuname)) cpuname = "n/a"; if (verbose) printf ("%s_user=%.1f%%, %s_system=%.1f%%, %s_idle=%.1f%%, " "%s_iowait=%.1f%%, %s_steal=%.1f%%\n" , cpuname, 100.0 * duser[c] / ratio[c] , cpuname, 100.0 * dsystem[c] / ratio[c] , cpuname, 100.0 * didle[c] / ratio[c] , cpuname, 100.0 * diowait[c] / ratio[c] , cpuname, 100.0 * dsteal[c] / ratio[c]); dbg ("sum (%s_*) = %.1f%%\n", cpuname, (100.0 * duser[c] / ratio[c]) + (100.0 * dsystem[c] / ratio[c]) + (100.0 * didle[c] / ratio[c]) + (100.0 * diowait[c] / ratio[c]) + (100.0 * dsteal[c] / ratio[c])); } } for (c = 0, status = STATE_OK; c < ncpus; c++) { cpu_perc = (100.0 * (cpu_value[c]) / ratio[c]); currstatus = get_status (cpu_perc, my_threshold); if (currstatus > status) status = currstatus; } cpu_desc_read (cpudesc); char *cpu_model_str = cpu_model ? xasprintf ("(%s) ", cpu_desc_get_model_name (cpudesc)) : NULL; printf ("%s %s%s - cpu %s %.1f%% |" , program_name_short, cpu_model ? cpu_model_str : "" , state_text (status), cpu_progname, cpu_perc); for (c = 0; c < ncpus; c++) { if ((cpuname = cpuv[0][c].cpuname)) printf (" %s_user=%.1f%% %s_system=%.1f%% %s_idle=%.1f%%" " %s_iowait=%.1f%% %s_steal=%.1f%%" , cpuname, 100.0 * duser[c] / ratio[c] , cpuname, 100.0 * dsystem[c] / ratio[c] , cpuname, 100.0 * didle[c] / ratio[c] , cpuname, 100.0 * diowait[c] / ratio[c] , cpuname, 100.0 * dsteal[c] / ratio[c]); } putchar ('\n'); cpu_desc_unref (cpudesc); return status; }
/* 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'}, {"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(); }
int main (int argc, char **argv) { bool show_swapping = false; int c, status, err; char *critical = NULL, *warning = NULL; char *status_msg; char *perfdata_paging_msg, *perfdata_swapping_msg = NULL; unsigned int i, tog = 0, sleep_time = 1; thresholds *my_threshold = NULL; struct proc_vmem *vmem = NULL; unsigned long dpgpgin, dpgpgout, dpgfault, dpgfree, dpgmajfault, dpgscand, dpgscank, dpgsteal; unsigned long nr_vmem_pgpgin[2], nr_vmem_pgpgout[2]; unsigned long nr_vmem_pgfault[2]; unsigned long nr_vmem_pgfree[2]; unsigned long nr_vmem_pgmajfault[2]; unsigned long nr_vmem_pgsteal[2]; unsigned long nr_vmem_pgscand[2]; unsigned long nr_vmem_pgscank[2]; unsigned long dpswpin, dpswpout; unsigned long nr_vmem_dpswpin[2], nr_vmem_dpswpout[2]; unsigned long *tracked_value = &dpgmajfault; set_program_name (argv[0]); while ((c = getopt_long (argc, argv, "psc:w:bkmg" GETOPT_HELP_VERSION_STRING, longopts, NULL)) != -1) { switch (c) { default: usage (stderr); case 'p': /* show_paging = true; */ break; case 's': show_swapping = true; break; case 'c': critical = optarg; break; case 'w': warning = optarg; break; case_GETOPT_HELP_CHAR case_GETOPT_VERSION_CHAR } } status = set_thresholds (&my_threshold, warning, critical); if (status == NP_RANGE_UNPARSEABLE) usage (stderr); err = proc_vmem_new (&vmem); if (err < 0) plugin_error (STATE_UNKNOWN, err, "memory exhausted"); for (i = 0; i < 2; i++) { proc_vmem_read (vmem); nr_vmem_pgpgin[tog] = proc_vmem_get_pgpgin (vmem); nr_vmem_pgpgout[tog] = proc_vmem_get_pgpgout (vmem); nr_vmem_pgfault[tog] = proc_vmem_get_pgfault (vmem); nr_vmem_pgmajfault[tog] = proc_vmem_get_pgmajfault (vmem); nr_vmem_pgfree[tog] = proc_vmem_get_pgfree (vmem); nr_vmem_pgsteal[tog] = proc_vmem_get_pgsteal (vmem); if (show_swapping) { nr_vmem_dpswpin[tog] = proc_vmem_get_pswpin (vmem); nr_vmem_dpswpout[tog] = proc_vmem_get_pswpout (vmem); } nr_vmem_pgscand[tog] = proc_vmem_get_pgscand (vmem); nr_vmem_pgscank[tog] = proc_vmem_get_pgscank (vmem); sleep (sleep_time); tog = !tog; } dpgpgin = nr_vmem_pgpgin[1] - nr_vmem_pgpgin[0]; dpgpgout = nr_vmem_pgpgout[1] - nr_vmem_pgpgout[0]; dpgfault = nr_vmem_pgfault[1] - nr_vmem_pgfault[0]; dpgmajfault = nr_vmem_pgmajfault[1] - nr_vmem_pgmajfault[0]; dpgfree = nr_vmem_pgfree[1] - nr_vmem_pgfree[0]; dpgsteal = nr_vmem_pgsteal[1] - nr_vmem_pgsteal[0]; dpgscand = nr_vmem_pgscand[1] - nr_vmem_pgscand[0]; dpgscank = nr_vmem_pgscank[1] - nr_vmem_pgscank[0]; if (show_swapping) { dpswpin = nr_vmem_dpswpin[1] - nr_vmem_dpswpin[0]; dpswpout = nr_vmem_dpswpout[1] - nr_vmem_dpswpout[0]; perfdata_swapping_msg = xasprintf (" vmem_pswpin/s=%lu vmem_pswpout/s=%lu", dpswpin, dpswpout); } proc_vmem_unref (vmem); status = get_status (*tracked_value, my_threshold); free (my_threshold); status_msg = xasprintf ("%s: %lu majfault/s", state_text (status), *tracked_value); perfdata_paging_msg = xasprintf ("vmem_pgpgin/s=%lu vmem_pgpgout/s=%lu vmem_pgfault/s=%lu " "vmem_pgmajfault/s=%lu vmem_pgfree/s=%lu vmem_pgsteal/s=%lu " "vmem_pgscand/s=%lu vmem_pgscank/s=%lu", dpgpgin, dpgpgout, dpgfault, dpgmajfault, dpgfree, dpgsteal, dpgscand, dpgscank); printf ("%s %s | %s%s\n", "PAGING", status_msg, perfdata_paging_msg, perfdata_swapping_msg ? perfdata_swapping_msg : ""); return status; }