int process_arguments (int argc, char **argv) { int c; int option = 0; static struct option longopts[] = { MP_LONGOPTS_DEFAULT, MP_LONGOPTS_WC, {"bonding", required_argument, NULL, (int)'b'}, MP_LONGOPTS_END, }; while (1) { c = mp_getopt(&argc, &argv, MP_OPTSTR_DEFAULT"b:", longopts, &option); if (c == -1 || c == EOF) break; switch (c) { case 'b': mp_array_push(&bond, strdup(optarg), &bonds); break; } } return(OK); }
int process_arguments (int argc, char **argv) { int c; int option = 0; static struct option longopts[] = { MP_LONGOPTS_DEFAULT, MP_LONGOPTS_HOST, {"variable", required_argument, NULL, (int)LONGOPT_VARIABLE}, MYSQL_LONGOPTS, MP_LONGOPTS_END }; while (1) { c = mp_getopt(&argc, &argv, MP_OPTSTR_DEFAULT"H:"MYSQL_OPTSTR, longopts, &option); if (c == -1 || c == EOF) break; getopt_mysql(c); switch (c) { case LONGOPT_VARIABLE: { char *u; u = optarg; optarg = strsep(&u, ":"); mp_array_push(&variable, optarg, &variables); variables--; if (u) mp_array_push(&unit, optarg, &variables); else mp_array_push(&unit, "", &variables); } break; } } /* Apply defaults */ if (variables == 0) { variable = default_variable; unit = default_unit; variables = 13; } return(OK); }
END_TEST // mp_array_push START_TEST (test_array_push) { char **array = NULL; int num = 0; mp_array_push(&array, "I", &num); mp_array_push(&array, "LOVE", &num); mp_array_push(&array, "NALA", &num); fail_unless (num == 3, "mp_array_push failed: num = %d", num); fail_unless (strcmp(array[0], "I") == 0, "mp_array_push failed: Element 0 %s", array[0]); fail_unless (strcmp(array[1], "LOVE") == 0, "mp_array_push failed: Element 1 %s", array[1]); fail_unless (strcmp(array[2], "NALA") == 0, "mp_array_push failed: Element 2 %s", array[2]); }
int process_arguments (int argc, char **argv) { int c; int option = 0; static struct option longopts[] = { MP_LONGOPTS_NOTIFY, {"userkey", required_argument, 0, 'U'}, {"password", required_argument, 0, 'P'}, {"to", required_argument, NULL, (int)'T'}, {"from", required_argument, NULL, (int)'f'}, MP_LONGOPTS_END }; while (1) { c = mp_getopt(&argc, &argv, MP_OPTSTR_NOTIFY, longopts, &option); if (c == -1 || c == EOF) break; getopt_notify(c); switch (c) { case 'U': userkey = optarg; break; case 'P': password = optarg; break; case 'T': mp_array_push(&number, optarg, &numbers); break; case 'f': from = optarg; break; }; } /* Checks */ if (!userkey || !password) usage("Userkey and password are mandatory."); if (numbers == 0) usage("--to is mandatory."); if (!mp_notify_file && !mp_notify_msg) usage("--file or --message is mandatory."); return(OK); }
int process_arguments (int argc, char **argv) { int c; int option = 0; static struct option longopts[] = { MP_LONGOPTS_DEFAULT, // PLUGIN OPTS {"cert", required_argument, NULL, (int)'C'}, MP_LONGOPTS_WC, MP_LONGOPTS_END }; mp_threshold_set_warning_time(&expire_thresholds, "30d:"); mp_threshold_set_critical_time(&expire_thresholds, "10d:"); while (1) { c = mp_getopt(&argc, &argv, MP_OPTSTR_DEFAULT"w:c:C:", longopts, &option); if (c == -1 || c == EOF) break; getopt_wc_time_at(c, optarg, &expire_thresholds); switch (c) { /* Plugin opts */ case 'C': { glob_t globbuf; globbuf.gl_offs = 0; glob(optarg, GLOB_BRACE|GLOB_TILDE|GLOB_NOMAGIC, NULL, &globbuf); int i=0; for (i=0; i < globbuf.gl_pathc; i++) { mp_array_push(&cert_file, globbuf.gl_pathv[i], &cert_files); } break; } } } if (cert_files < 1) usage("At least one cert file is required."); return(OK); }
END_TEST START_TEST (test_array_push_multi) { char *input = NULL; char **array = NULL; int num = 0; input = mp_strdup("I,LOVE,NALA"); mp_array_push(&array, input, &num); fail_unless (num == 3, "mp_array_push failed: num = %d", num); fail_unless (strcmp(array[0], "I") == 0, "mp_array_push failed: Element 0 %s", array[0]); fail_unless (strcmp(array[1], "LOVE") == 0, "mp_array_push failed: Element 1 %s", array[1]); fail_unless (strcmp(array[2], "NALA") == 0, "mp_array_push failed: Element 2 %s", array[2]); }
int main (int argc, char **argv) { /* Local Vars */ int i; int j; char *filename; bonding_info *info; char *buf; char *output = NULL; int status = STATE_OK; /* Set signal handling and alarm */ if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) critical("Setup SIGALRM trap failed!"); /* Process check arguments */ if (process_arguments(argc, argv) != OK) unknown("Parsing arguments failed!"); /* Start plugin timeout */ alarm(mp_timeout); if (bonds==0) { DIR *dir; struct dirent *entry; dir = opendir("/proc/net/bonding/"); if (dir == NULL) critical("Can't open '/proc/net/bonding/'"); while ((entry = readdir(dir)) != NULL) { if (strncmp(entry->d_name, "bond", 4) == 0) { mp_array_push(&bond, strdup(entry->d_name), &bonds); } } closedir(dir); } filename = mp_malloc(sizeof(char)*32); buf = mp_malloc(sizeof(char)*64); for(i=0; i < bonds; i++) { mp_sprintf(filename, "/proc/net/bonding/%s", bond[i]); info = parseBond(filename); if (info == NULL) { status = STATE_CRITICAL; mp_snprintf(buf, 64, "%s not found", bond[i]); mp_strcat_comma(&output, buf); } else if (info->mii_status) { char *up = NULL; char *down = NULL; for(j=0; j < info->slaves; j++) { if (info->slave[j]->mii_status) { mp_strcat_comma(&up, info->slave[j]->interface); } else { mp_strcat_comma(&down, info->slave[j]->interface); } } if (down) { mp_snprintf(buf, 64, "%s up (%s, down: %s)", bond[i], up, down); status = status == STATE_OK ? STATE_WARNING : status; } else { mp_snprintf(buf, 64, "%s up (%s)", bond[i], up); } mp_strcat_comma(&output, buf); } else { mp_snprintf(buf, 64, "%s down (%s)", bond[i], info->mode); mp_strcat_comma(&output, buf); status = STATE_CRITICAL; } } switch (status) { case STATE_OK: ok(output); break; case STATE_WARNING: warning(output); break; case STATE_CRITICAL: critical(output); break; case STATE_UNKNOWN: unknown(output); break; } critical("You should never reach this point."); }