int main (int argc, char **argv) { int users = -1; int result = STATE_UNKNOWN; char *perf; struct utmpx *putmpx; setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); perf = strdup (""); /* Parse extra opts if any */ argv = np_extra_opts (&argc, argv, progname); if (process_arguments (argc, argv) == ERROR) usage4 (_("Could not parse arguments")); users = 0; /* get currently logged users from utmpx */ setutxent (); while ((putmpx = getutxent ()) != NULL) if (putmpx->ut_type == USER_PROCESS) users++; endutxent (); /* check the user count against warning and critical thresholds */ if (users > cusers) result = STATE_CRITICAL; else if (users > wusers) result = STATE_WARNING; else if (users >= 0) result = STATE_OK; if (result == STATE_UNKNOWN) printf ("%s\n", _("Unable to read output")); else { asprintf (&perf, "%s", perfdata ("users", users, "", TRUE, wusers, TRUE, cusers, TRUE, 0, FALSE, 0)); printf (_("USERS %s - %d users currently logged in |%s\n"), state_text (result), users, perf); } return result; }
int main (int argc, char **argv) { int result = STATE_UNKNOWN; int disk_result = STATE_UNKNOWN; char *output; char *details; char *perf; char *preamble; double inode_space_pct; double warning_high_tide; double critical_high_tide; int temp_result; struct mount_entry *me; struct fs_usage fsp, tmpfsp; struct parameter_list *temp_list, *path; preamble = strdup (" - free space:"); output = strdup (""); details = strdup (""); perf = strdup (""); stat_buf = malloc(sizeof *stat_buf); setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); mount_list = read_file_system_list (0); /* Parse extra opts if any */ argv = np_extra_opts (&argc, argv, progname); if (process_arguments (argc, argv) == ERROR) usage4 (_("Could not parse arguments")); /* If a list of paths has not been selected, find entire mount list and create list of paths */ if (path_selected == FALSE) { for (me = mount_list; me; me = me->me_next) { if (! (path = np_find_parameter(path_select_list, me->me_mountdir))) { path = np_add_parameter(&path_select_list, me->me_mountdir); } path->best_match = me; path->group = group; set_all_thresholds(path); } } np_set_best_match(path_select_list, mount_list, exact_match); /* Error if no match found for specified paths */ temp_list = path_select_list; while (temp_list) { if (! temp_list->best_match) { die (STATE_CRITICAL, _("DISK %s: %s not found\n"), _("CRITICAL"), temp_list->name); } temp_list = temp_list->name_next; } /* Process for every path in list */ for (path = path_select_list; path; path=path->name_next) { if (verbose >= 3 && path->freespace_percent->warning != NULL && path->freespace_percent->critical != NULL) printf("Thresholds(pct) for %s warn: %f crit %f\n",path->name, path->freespace_percent->warning->end, path->freespace_percent->critical->end); if (verbose >= 3 && path->group != NULL) printf("Group of %s: %s\n",path->name,path->group); /* reset disk result */ disk_result = STATE_UNKNOWN; me = path->best_match; /* Filters */ /* Remove filesystems already seen */ if (np_seen_name(seen, me->me_mountdir)) { continue; } np_add_name(&seen, me->me_mountdir); if (path->group == NULL) { /* Skip remote filesystems if we're not interested in them */ if (me->me_remote && show_local_fs) { if (stat_remote_fs) stat_path(path); continue; /* Skip pseudo fs's if we haven't asked for all fs's */ } else if (me->me_dummy && !show_all_fs) { continue; /* Skip excluded fstypes */ } else if (fs_exclude_list && np_find_name (fs_exclude_list, me->me_type)) { continue; /* Skip excluded fs's */ } else if (dp_exclude_list && (np_find_name (dp_exclude_list, me->me_devname) || np_find_name (dp_exclude_list, me->me_mountdir))) { continue; /* Skip not included fstypes */ } else if (fs_include_list && !np_find_name (fs_include_list, me->me_type)) { continue; } } stat_path(path); get_fs_usage (me->me_mountdir, me->me_devname, &fsp); if (fsp.fsu_blocks && strcmp ("none", me->me_mountdir)) { get_stats (path, &fsp); if (verbose >= 3) { printf ("For %s, used_pct=%g free_pct=%g used_units=%g free_units=%g total_units=%g used_inodes_pct=%g free_inodes_pct=%g fsp.fsu_blocksize=%llu mult=%llu\n", me->me_mountdir, path->dused_pct, path->dfree_pct, path->dused_units, path->dfree_units, path->dtotal_units, path->dused_inodes_percent, path->dfree_inodes_percent, fsp.fsu_blocksize, mult); } /* Threshold comparisons */ temp_result = get_status(path->dfree_units, path->freespace_units); if (verbose >=3) printf("Freespace_units result=%d\n", temp_result); disk_result = max_state( disk_result, temp_result ); temp_result = get_status(path->dfree_pct, path->freespace_percent); if (verbose >=3) printf("Freespace%% result=%d\n", temp_result); disk_result = max_state( disk_result, temp_result ); temp_result = get_status(path->dused_units, path->usedspace_units); if (verbose >=3) printf("Usedspace_units result=%d\n", temp_result); disk_result = max_state( disk_result, temp_result ); temp_result = get_status(path->dused_pct, path->usedspace_percent); if (verbose >=3) printf("Usedspace_percent result=%d\n", temp_result); disk_result = max_state( disk_result, temp_result ); temp_result = get_status(path->dused_inodes_percent, path->usedinodes_percent); if (verbose >=3) printf("Usedinodes_percent result=%d\n", temp_result); disk_result = max_state( disk_result, temp_result ); temp_result = get_status(path->dfree_inodes_percent, path->freeinodes_percent); if (verbose >=3) printf("Freeinodes_percent result=%d\n", temp_result); disk_result = max_state( disk_result, temp_result ); result = max_state(result, disk_result); /* What a mess of units. The output shows free space, the perf data shows used space. Yikes! Hack here. Trying to get warn/crit levels from freespace_(units|percent) for perf data. Assumption that start=0. Roll on new syntax... */ /* *_high_tide must be reinitialized at each run */ warning_high_tide = UINT_MAX; critical_high_tide = UINT_MAX; if (path->freespace_units->warning != NULL) { warning_high_tide = path->dtotal_units - path->freespace_units->warning->end; } if (path->freespace_percent->warning != NULL) { warning_high_tide = abs( min( (double) warning_high_tide, (double) (1.0 - path->freespace_percent->warning->end/100)*path->dtotal_units )); } if (path->freespace_units->critical != NULL) { critical_high_tide = path->dtotal_units - path->freespace_units->critical->end; } if (path->freespace_percent->critical != NULL) { critical_high_tide = abs( min( (double) critical_high_tide, (double) (1.0 - path->freespace_percent->critical->end/100)*path->dtotal_units )); } /* Nb: *_high_tide are unset when == UINT_MAX */ xasprintf (&perf, "%s %s", perf, perfdata ((!strcmp(me->me_mountdir, "none") || display_mntp) ? me->me_devname : me->me_mountdir, path->dused_units, units, (warning_high_tide != UINT_MAX ? TRUE : FALSE), warning_high_tide, (critical_high_tide != UINT_MAX ? TRUE : FALSE), critical_high_tide, TRUE, 0, TRUE, path->dtotal_units)); if (disk_result==STATE_OK && erronly && !verbose) continue; xasprintf (&output, "%s %s %.0f %s (%.0f%%", output, (!strcmp(me->me_mountdir, "none") || display_mntp) ? me->me_devname : me->me_mountdir, path->dfree_units, units, path->dfree_pct); /* Whether or not to put all disks on new line */ if (newlines) { if (path->dused_inodes_percent < 0) { xasprintf(&output, "%s inode=-);\n", output); } else { xasprintf(&output, "%s inode=%.0f%%);\n", output, path->dfree_inodes_percent ); } } else { if (path->dused_inodes_percent < 0) { xasprintf(&output, "%s inode=-);", output); } else { xasprintf(&output, "%s inode=%.0f%%);", output, path->dfree_inodes_percent ); } } /* TODO: Need to do a similar debug line xasprintf (&details, _("%s\n\ %.0f of %.0f %s (%.0f%% inode=%.0f%%) free on %s (type %s mounted on %s) warn:%lu crit:%lu warn%%:%.0f%% crit%%:%.0f%%"), details, dfree_units, dtotal_units, units, dfree_pct, inode_space_pct, me->me_devname, me->me_type, me->me_mountdir, (unsigned long)w_df, (unsigned long)c_df, w_dfp, c_dfp); */ } } if (verbose >= 2) xasprintf (&output, "%s%s", output, details); if (newlines) { printf ("DISK %s%s\n%s|%s\n", state_text (result), (erronly && result==STATE_OK) ? "" : preamble, output, perf); } else { printf ("DISK %s%s%s|%s\n", state_text (result), (erronly && result==STATE_OK) ? "" : preamble, output, perf); } return result; }
int main (int argc, char **argv) { char *command_line; int result = STATE_UNKNOWN; char *p, *ret[QSTAT_MAX_RETURN_ARGS]; size_t i = 0; output chld_out; 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_va(_("Could not parse arguments")); result = STATE_OK; /* create the command line to execute */ xasprintf (&command_line, "%s -raw %s -%s %s", PATH_TO_QSTAT, QSTAT_DATA_DELIMITER, game_type, server_ip); if (port) xasprintf (&command_line, "%s:%-d", command_line, port); if (verbose > 0) printf ("%s\n", command_line); /* run the command. historically, this plugin ignores output on stderr, * as well as return status of the qstat program */ (void)np_runcmd(command_line, &chld_out, NULL, 0); /* sanity check */ /* was thinking about running qstat without any options, capturing the -default line, parsing it & making an array of all know server types but thought this would be too much hassle considering this is a tool for intelligent sysadmins (ha). Could put a static array of known server types in a header file but then we'd be limiting ourselves In the end, I figured I'd simply let an error occur & then trap it */ if (!strncmp (chld_out.line[0], "unknown option", 14)) { printf (_("CRITICAL - Host type parameter incorrect!\n")); result = STATE_CRITICAL; return result; } p = (char *) strtok (chld_out.line[0], QSTAT_DATA_DELIMITER); while (p != NULL) { ret[i] = p; p = (char *) strtok (NULL, QSTAT_DATA_DELIMITER); i++; if (i >= QSTAT_MAX_RETURN_ARGS) break; } if (strstr (ret[2], QSTAT_HOST_ERROR)) { printf (_("CRITICAL - Host not found\n")); result = STATE_CRITICAL; } else if (strstr (ret[2], QSTAT_HOST_DOWN)) { printf (_("CRITICAL - Game server down or unavailable\n")); result = STATE_CRITICAL; } else if (strstr (ret[2], QSTAT_HOST_TIMEOUT)) { printf (_("CRITICAL - Game server timeout\n")); result = STATE_CRITICAL; } else { printf ("OK: %s/%s %s (%s), Ping: %s ms|%s %s\n", ret[qstat_game_players], ret[qstat_game_players_max], ret[qstat_game_field], ret[qstat_map_field], ret[qstat_ping_field], perfdata ("players", atol(ret[qstat_game_players]), "", FALSE, 0, FALSE, 0, TRUE, 0, TRUE, atol(ret[qstat_game_players_max])), fperfdata ("ping", strtod(ret[qstat_ping_field], NULL), "", FALSE, 0, FALSE, 0, TRUE, 0, FALSE, 0)); } return result; }
int main (int argc, char **argv) { int sd; int result = STATE_UNKNOWN; time_t conntime; setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); /* Parse extra opts if any */ argv=np_extra_opts (&argc, argv, progname); if (process_arguments (argc, argv) == ERROR) usage4 (_("Could not parse arguments")); /* initialize alarm signal handling */ signal (SIGALRM, socket_timeout_alarm_handler); /* set socket timeout */ alarm (socket_timeout); time (&start_time); /* try to connect to the host at the given port number */ if (use_udp) { result = my_udp_connect (server_address, server_port, &sd); } else { result = my_tcp_connect (server_address, server_port, &sd); } if (result != STATE_OK) { if (check_critical_time == TRUE) result = STATE_CRITICAL; else if (check_warning_time == TRUE) result = STATE_WARNING; else result = STATE_UNKNOWN; die (result, _("TIME UNKNOWN - could not connect to server %s, port %d\n"), server_address, server_port); } if (use_udp) { if (send (sd, "", 0, 0) < 0) { if (check_critical_time == TRUE) result = STATE_CRITICAL; else if (check_warning_time == TRUE) result = STATE_WARNING; else result = STATE_UNKNOWN; die (result, _("TIME UNKNOWN - could not send UDP request to server %s, port %d\n"), server_address, server_port); } } /* watch for the connection string */ result = recv (sd, (void *)&raw_server_time, sizeof (raw_server_time), 0); /* close the connection */ close (sd); /* reset the alarm */ time (&end_time); alarm (0); /* return a WARNING status if we couldn't read any data */ if (result <= 0) { if (check_critical_time == TRUE) result = STATE_CRITICAL; else if (check_warning_time == TRUE) result = STATE_WARNING; else result = STATE_UNKNOWN; die (result, _("TIME UNKNOWN - no data received from server %s, port %d\n"), server_address, server_port); } result = STATE_OK; conntime = (end_time - start_time); if (check_critical_time == TRUE && conntime > critical_time) result = STATE_CRITICAL; else if (check_warning_time == TRUE && conntime > warning_time) result = STATE_WARNING; if (result != STATE_OK) die (result, _("TIME %s - %d second response time|%s\n"), state_text (result), (int)conntime, perfdata ("time", (long)conntime, "s", check_warning_time, (long)warning_time, check_critical_time, (long)critical_time, TRUE, 0, FALSE, 0)); server_time = ntohl (raw_server_time) - UNIX_EPOCH; if (server_time > (unsigned long)end_time) diff_time = server_time - (unsigned long)end_time; else diff_time = (unsigned long)end_time - server_time; if (check_critical_diff == TRUE && diff_time > critical_diff) result = STATE_CRITICAL; else if (check_warning_diff == TRUE && diff_time > warning_diff) result = STATE_WARNING; printf (_("TIME %s - %lu second time difference|%s %s\n"), state_text (result), diff_time, perfdata ("time", (long)conntime, "s", check_warning_time, (long)warning_time, check_critical_time, (long)critical_time, TRUE, 0, FALSE, 0), perfdata ("offset", diff_time, "s", check_warning_diff, warning_diff, check_critical_diff, critical_diff, TRUE, 0, FALSE, 0)); return result; }
int textscan (char *buf) { char *rtastr = NULL; char *losstr = NULL; char *xmtstr = NULL; double loss; double rta; double xmt; int status = STATE_UNKNOWN; if (strstr (buf, "not found")) { die (STATE_CRITICAL, _("FPING UNKNOW - %s not found\n"), server_name); } else if (strstr (buf, "is unreachable") || strstr (buf, "Unreachable")) { die (STATE_CRITICAL, _("FPING CRITICAL - %s is unreachable\n"), "host"); } else if (strstr (buf, "Operation not permitted") || strstr (buf, "No such device") ) { die (STATE_UNKNOWN, _("FPING UNKNOWN - %s parameter error\n"), "host"); } else if (strstr (buf, "is down")) { die (STATE_CRITICAL, _("FPING CRITICAL - %s is down\n"), server_name); } else if (strstr (buf, "is alive")) { status = STATE_OK; } else if (strstr (buf, "xmt/rcv/%loss") && strstr (buf, "min/avg/max")) { losstr = strstr (buf, "="); losstr = 1 + strstr (losstr, "/"); losstr = 1 + strstr (losstr, "/"); rtastr = strstr (buf, "min/avg/max"); rtastr = strstr (rtastr, "="); rtastr = 1 + index (rtastr, '/'); loss = strtod (losstr, NULL); rta = strtod (rtastr, NULL); if (cpl_p == TRUE && loss > cpl) status = STATE_CRITICAL; else if (crta_p == TRUE && rta > crta) status = STATE_CRITICAL; else if (wpl_p == TRUE && loss > wpl) status = STATE_WARNING; else if (wrta_p == TRUE && rta > wrta) status = STATE_WARNING; else status = STATE_OK; die (status, _("FPING %s - %s (loss=%.0f%%, rta=%f ms)|%s %s\n"), state_text (status), server_name, loss, rta, perfdata ("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, TRUE, 0, TRUE, 100), fperfdata ("rta", rta/1.0e3, "s", wrta_p, wrta/1.0e3, crta_p, crta/1.0e3, TRUE, 0, FALSE, 0)); } else if(strstr (buf, "xmt/rcv/%loss") ) { /* no min/max/avg if host was unreachable in fping v2.2.b1 */ /* in v2.4b2: 10.99.0.1 : xmt/rcv/%loss = 0/0/0% */ losstr = strstr (buf, "="); xmtstr = 1 + losstr; xmt = strtod (xmtstr, NULL); if(xmt == 0) die (STATE_CRITICAL, _("FPING CRITICAL - %s is down\n"), server_name); losstr = 1 + strstr (losstr, "/"); losstr = 1 + strstr (losstr, "/"); loss = strtod (losstr, NULL); if (atoi(losstr) == 100) status = STATE_CRITICAL; else if (cpl_p == TRUE && loss > cpl) status = STATE_CRITICAL; else if (wpl_p == TRUE && loss > wpl) status = STATE_WARNING; else status = STATE_OK; /* loss=%.0f%%;%d;%d;0;100 */ die (status, _("FPING %s - %s (loss=%.0f%% )|%s\n"), state_text (status), server_name, loss , perfdata ("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, TRUE, 0, TRUE, 100)); } else { status = max_state (status, STATE_WARNING); } return status; }
int main (int argc, char **argv) { int result = STATE_UNKNOWN; int disk_result = STATE_UNKNOWN; char *output; char *details; char *perf; char *preamble; double inode_space_pct; uintmax_t total, available, available_to_root, used; double dfree_pct = -1, dused_pct = -1; double dused_units, dfree_units, dtotal_units; double dused_inodes_percent, dfree_inodes_percent; double warning_high_tide; double critical_high_tide; int temp_result; struct mount_entry *me; struct fs_usage fsp, tmpfsp; struct parameter_list *temp_list, *path; struct name_list *seen = NULL; preamble = strdup (" - free space:"); output = strdup (""); details = strdup (""); perf = strdup (""); stat_buf = malloc(sizeof *stat_buf); setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); mount_list = read_file_system_list (0); /* Parse extra opts if any */ argv = np_extra_opts (&argc, argv, progname); if (process_arguments (argc, argv) == ERROR) usage4 (_("Could not parse arguments")); /* If a list of paths has not been selected, find entire mount list and create list of paths */ if (path_selected == FALSE) { for (me = mount_list; me; me = me->me_next) { if (! (path = np_find_parameter(path_select_list, me->me_mountdir))) { path = np_add_parameter(&path_select_list, me->me_mountdir); } path->best_match = me; path->group = group; set_all_thresholds(path); } } np_set_best_match(path_select_list, mount_list, exact_match); /* Error if no match found for specified paths */ temp_list = path_select_list; while (temp_list) { if (! temp_list->best_match) { die (STATE_CRITICAL, _("DISK %s: %s not found\n"), _("CRITICAL"), temp_list->name); } temp_list = temp_list->name_next; } /* Process for every path in list */ for (path = path_select_list; path; path=path->name_next) { if (verbose >= 3 && path->freespace_percent->warning != NULL && path->freespace_percent->critical != NULL) printf("Thresholds(pct) for %s warn: %f crit %f\n",path->name, path->freespace_percent->warning->end, path->freespace_percent->critical->end); if (verbose >= 3 && path->group != NULL) printf("Group of %s: %s\n",path->name,path->group); /* reset disk result */ disk_result = STATE_UNKNOWN; me = path->best_match; /* Filters */ /* Remove filesystems already seen */ if (np_seen_name(seen, me->me_mountdir)) { continue; } else { if (path->group != NULL) { /* find all group members */ fsp.fsu_blocksize = 0; fsp.fsu_blocks = 0; fsp.fsu_bfree = 0; fsp.fsu_bavail = 0; fsp.fsu_files = 0; fsp.fsu_ffree = 0; for (temp_list = path_select_list; temp_list; temp_list=temp_list->name_next) { if (temp_list->group && ! (strcmp(temp_list->group, path->group))) { stat_path(path); get_fs_usage (temp_list->best_match->me_mountdir, temp_list->best_match->me_devname, &tmpfsp); /* possibly differing blocksizes if disks are grouped. Calculating average */ fsp.fsu_blocksize = (fsp.fsu_blocksize * fsp.fsu_blocks + tmpfsp.fsu_blocksize * tmpfsp.fsu_blocks) / \ (fsp.fsu_blocks + tmpfsp.fsu_blocks); /* Size of a block. */ fsp.fsu_blocks += tmpfsp.fsu_blocks; /* Total blocks. */ fsp.fsu_bfree += tmpfsp.fsu_bfree; /* Free blocks available to superuser. */ /* Gnulib workaround - see comment about it a few lines below */ fsp.fsu_bavail += (tmpfsp.fsu_bavail > tmpfsp.fsu_bfree ? 0 : tmpfsp.fsu_bavail); /* Free blocks available to non-superuser. */ fsp.fsu_files += tmpfsp.fsu_files; /* Total file nodes. */ fsp.fsu_ffree += tmpfsp.fsu_ffree; /* Free file nodes. */ if (verbose >= 3) printf("Group %s: add %llu blocks (%s) \n", path->group, tmpfsp.fsu_bavail, temp_list->name); /* printf("Group %s: add %u blocks (%s)\n", temp_list->name); *//* path->group, tmpfsp.fsu_bavail, temp_list->name); */ np_add_name(&seen, temp_list->best_match->me_mountdir); } } /* modify devname and mountdir for output */ me->me_mountdir = me->me_devname = path->group; } else np_add_name(&seen, me->me_mountdir); } if (path->group == NULL) { /* Skip remote filesystems if we're not interested in them */ if (me->me_remote && show_local_fs) { if (stat_remote_fs) stat_path(path); continue; /* Skip pseudo fs's if we haven't asked for all fs's */ } else if (me->me_dummy && !show_all_fs) { continue; /* Skip excluded fstypes */ } else if (fs_exclude_list && np_find_name (fs_exclude_list, me->me_type)) { continue; /* Skip excluded fs's */ } else if (dp_exclude_list && (np_find_name (dp_exclude_list, me->me_devname) || np_find_name (dp_exclude_list, me->me_mountdir))) { continue; } stat_path(path); get_fs_usage (me->me_mountdir, me->me_devname, &fsp); } if (fsp.fsu_blocks && strcmp ("none", me->me_mountdir)) { total = fsp.fsu_blocks; /* 2007-12-08 - Workaround for Gnulib reporting insanely high available * space on BSD (the actual value should be negative but fsp.fsu_bavail * is unsigned) */ available = fsp.fsu_bavail > fsp.fsu_bfree ? 0 : fsp.fsu_bavail; available_to_root = fsp.fsu_bfree; used = total - available_to_root; if (verbose >= 3) printf ("For %s, total=%llu, available=%llu, available_to_root=%llu, used=%llu, fsp.fsu_files=%llu, fsp.fsu_ffree=%llu\n", me->me_mountdir, total, available, available_to_root, used, fsp.fsu_files, fsp.fsu_ffree); dused_pct = calculate_percent( used, used + available ); /* used + available can never be > uintmax */ dfree_pct = 100 - dused_pct; dused_units = used*fsp.fsu_blocksize/mult; dfree_units = available*fsp.fsu_blocksize/mult; dtotal_units = total*fsp.fsu_blocksize/mult; dused_inodes_percent = calculate_percent(fsp.fsu_files - fsp.fsu_ffree, fsp.fsu_files); dfree_inodes_percent = 100 - dused_inodes_percent; if (verbose >= 3) { printf ("For %s, used_pct=%g free_pct=%g used_units=%g free_units=%g total_units=%g used_inodes_pct=%g free_inodes_pct=%g fsp.fsu_blocksize=%llu mult=%llu\n", me->me_mountdir, dused_pct, dfree_pct, dused_units, dfree_units, dtotal_units, dused_inodes_percent, dfree_inodes_percent, fsp.fsu_blocksize, mult); } /* Threshold comparisons */ temp_result = get_status(dfree_units, path->freespace_units); if (verbose >=3) printf("Freespace_units result=%d\n", temp_result); disk_result = max_state( disk_result, temp_result ); temp_result = get_status(dfree_pct, path->freespace_percent); if (verbose >=3) printf("Freespace%% result=%d\n", temp_result); disk_result = max_state( disk_result, temp_result ); temp_result = get_status(dused_units, path->usedspace_units); if (verbose >=3) printf("Usedspace_units result=%d\n", temp_result); disk_result = max_state( disk_result, temp_result ); temp_result = get_status(dused_pct, path->usedspace_percent); if (verbose >=3) printf("Usedspace_percent result=%d\n", temp_result); disk_result = max_state( disk_result, temp_result ); temp_result = get_status(dused_inodes_percent, path->usedinodes_percent); if (verbose >=3) printf("Usedinodes_percent result=%d\n", temp_result); disk_result = max_state( disk_result, temp_result ); temp_result = get_status(dfree_inodes_percent, path->freeinodes_percent); if (verbose >=3) printf("Freeinodes_percent result=%d\n", temp_result); disk_result = max_state( disk_result, temp_result ); result = max_state(result, disk_result); /* What a mess of units. The output shows free space, the perf data shows used space. Yikes! Hack here. Trying to get warn/crit levels from freespace_(units|percent) for perf data. Assumption that start=0. Roll on new syntax... */ /* *_high_tide must be reinitialized at each run */ warning_high_tide = UINT_MAX; critical_high_tide = UINT_MAX; if (path->freespace_units->warning != NULL) { warning_high_tide = dtotal_units - path->freespace_units->warning->end; } if (path->freespace_percent->warning != NULL) { warning_high_tide = abs( min( (double) warning_high_tide, (double) (1.0 - path->freespace_percent->warning->end/100)*dtotal_units )); } if (path->freespace_units->critical != NULL) { critical_high_tide = dtotal_units - path->freespace_units->critical->end; } if (path->freespace_percent->critical != NULL) { critical_high_tide = abs( min( (double) critical_high_tide, (double) (1.0 - path->freespace_percent->critical->end/100)*dtotal_units )); } /* Nb: *_high_tide are unset when == UINT_MAX */ asprintf (&perf, "%s %s", perf, perfdata ((!strcmp(me->me_mountdir, "none") || display_mntp) ? me->me_devname : me->me_mountdir, dused_units, units, (warning_high_tide != UINT_MAX ? TRUE : FALSE), warning_high_tide, (critical_high_tide != UINT_MAX ? TRUE : FALSE), critical_high_tide, TRUE, 0, TRUE, dtotal_units)); if (disk_result==STATE_OK && erronly && !verbose) continue; asprintf (&output, "%s %s %.0f %s (%.0f%%", output, (!strcmp(me->me_mountdir, "none") || display_mntp) ? me->me_devname : me->me_mountdir, dfree_units, units, dfree_pct); if (dused_inodes_percent < 0) { asprintf(&output, "%s inode=-);", output); } else { asprintf(&output, "%s inode=%.0f%%);", output, dfree_inodes_percent ); } /* TODO: Need to do a similar debug line asprintf (&details, _("%s\n\ %.0f of %.0f %s (%.0f%% inode=%.0f%%) free on %s (type %s mounted on %s) warn:%lu crit:%lu warn%%:%.0f%% crit%%:%.0f%%"), details, dfree_units, dtotal_units, units, dfree_pct, inode_space_pct, me->me_devname, me->me_type, me->me_mountdir, (unsigned long)w_df, (unsigned long)c_df, w_dfp, c_dfp); */ } } if (verbose >= 2) asprintf (&output, "%s%s", output, details); printf ("DISK %s%s%s|%s\n", state_text (result), (erronly && result==STATE_OK) ? "" : preamble, output, perf); return result; }
int main (int argc, char **argv) { int users = -1; int result = STATE_UNKNOWN; char *perf; #if HAVE_WTSAPI32_H WTS_SESSION_INFO *wtsinfo; DWORD wtscount; DWORD index; #elif HAVE_UTMPX_H struct utmpx *putmpx; #else char input_buffer[MAX_INPUT_BUFFER]; #endif setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); perf = strdup (""); /* Parse extra opts if any */ argv = np_extra_opts (&argc, argv, progname); if (process_arguments (argc, argv) == ERROR) usage4 (_("Could not parse arguments")); users = 0; #if HAVE_WTSAPI32_H if (!WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, 0, 1, &wtsinfo, &wtscount)) { printf(_("Could not enumerate RD sessions: %d\n"), GetLastError()); return STATE_UNKNOWN; } for (index = 0; index < wtscount; index++) { LPTSTR username; DWORD size; int len; if (!WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, wtsinfo[index].SessionId, WTSUserName, &username, &size)) continue; len = lstrlen(username); WTSFreeMemory(username); if (len == 0) continue; if (wtsinfo[index].State == WTSActive || wtsinfo[index].State == WTSDisconnected) users++; } WTSFreeMemory(wtsinfo); #elif HAVE_UTMPX_H /* get currently logged users from utmpx */ setutxent (); while ((putmpx = getutxent ()) != NULL) if (putmpx->ut_type == USER_PROCESS) users++; endutxent (); #else /* run the command */ child_process = spopen (WHO_COMMAND); if (child_process == NULL) { printf (_("Could not open pipe: %s\n"), WHO_COMMAND); return STATE_UNKNOWN; } child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); if (child_stderr == NULL) printf (_("Could not open stderr for %s\n"), WHO_COMMAND); while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { /* increment 'users' on all lines except total user count */ if (input_buffer[0] != '#') { users++; continue; } /* get total logged in users */ if (sscanf (input_buffer, _("# users=%d"), &users) == 1) break; } /* check STDERR */ if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) result = possibly_set (result, STATE_UNKNOWN); (void) fclose (child_stderr); /* close the pipe */ if (spclose (child_process)) result = possibly_set (result, STATE_UNKNOWN); #endif /* check the user count against warning and critical thresholds */ if (users > cusers) result = STATE_CRITICAL; else if (users > wusers) result = STATE_WARNING; else if (users >= 0) result = STATE_OK; if (result == STATE_UNKNOWN) printf ("%s\n", _("Unable to read output")); else { xasprintf (&perf, "%s", perfdata ("users", users, "", TRUE, wusers, TRUE, cusers, TRUE, 0, FALSE, 0)); printf (_("USERS %s - %d users currently logged in |%s\n"), state_text (result), users, perf); } return result; }
int main (int argc, char **argv) { char *cmd = NULL; char *rawcmd = NULL; int result = STATE_UNKNOWN; int this_result = STATE_UNKNOWN; int i; setlocale (LC_ALL, ""); setlocale (LC_NUMERIC, "C"); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); addresses = malloc (sizeof(char*) * max_addr); addresses[0] = NULL; /* Parse extra opts if any */ argv=np_extra_opts (&argc, argv, progname); if (process_arguments (argc, argv) == ERROR) usage4 (_("Could not parse arguments")); /* Set signal handling and alarm */ if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) { usage4 (_("Cannot catch SIGALRM")); } /* If ./configure finds ping has timeout values, set plugin alarm slightly * higher so that we can use response from command line ping */ #if defined(PING_PACKETS_FIRST) && defined(PING_HAS_TIMEOUT) alarm (timeout_interval + 1); #else alarm (timeout_interval); #endif for (i = 0 ; i < n_addresses ; i++) { #ifdef PING6_COMMAND if (address_family != AF_INET && is_inet6_addr(addresses[i])) rawcmd = strdup(PING6_COMMAND); else rawcmd = strdup(PING_COMMAND); #else rawcmd = strdup(PING_COMMAND); #endif /* does the host address of number of packets argument come first? */ #ifdef PING_PACKETS_FIRST # ifdef PING_HAS_TIMEOUT xasprintf (&cmd, rawcmd, timeout_interval, max_packets, addresses[i]); # else xasprintf (&cmd, rawcmd, max_packets, addresses[i]); # endif #else xasprintf (&cmd, rawcmd, addresses[i], max_packets); #endif if (verbose >= 2) printf ("CMD: %s\n", cmd); /* run the command */ this_result = run_ping (cmd, addresses[i]); if (pl == UNKNOWN_PACKET_LOSS || rta < 0.0) { printf ("%s\n", cmd); die (STATE_UNKNOWN, _("CRITICAL - Could not interpret output from ping command\n")); } if (pl >= cpl || rta >= crta || rta < 0) this_result = STATE_CRITICAL; else if (pl >= wpl || rta >= wrta) this_result = STATE_WARNING; else if (pl >= 0 && rta >= 0) this_result = max_state (STATE_OK, this_result); if (n_addresses > 1 && this_result != STATE_UNKNOWN) die (STATE_OK, "%s is alive\n", addresses[i]); if (display_html == TRUE) printf ("<A HREF='%s/traceroute.cgi?%s'>", CGIURL, addresses[i]); if (pl == 100) printf (_("PING %s - %sPacket loss = %d%%"), state_text (this_result), warn_text, pl); else printf (_("PING %s - %sPacket loss = %d%%, RTA = %2.2f ms"), state_text (this_result), warn_text, pl, rta); if (display_html == TRUE) printf ("</A>"); /* Print performance data */ printf("|%s", fperfdata ("rta", (double) rta, "ms", wrta>0?TRUE:FALSE, wrta, crta>0?TRUE:FALSE, crta, TRUE, 0, FALSE, 0)); printf(" %s\n", perfdata ("pl", (long) pl, "%", wpl>0?TRUE:FALSE, wpl, cpl>0?TRUE:FALSE, cpl, TRUE, 0, FALSE, 0)); if (verbose >= 2) printf ("%f:%d%% %f:%d%%\n", wrta, wpl, crta, cpl); result = max_state (result, this_result); free (rawcmd); free (cmd); } return result; }
int main (int argc, char **argv) { int users = -1; int result = STATE_UNKNOWN; char input_buffer[MAX_INPUT_BUFFER]; char *perf; setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); perf = strdup(""); if (process_arguments (argc, argv) == ERROR) usage4 (_("Could not parse arguments")); /* run the command */ child_process = spopen (WHO_COMMAND); if (child_process == NULL) { printf (_("Could not open pipe: %s\n"), WHO_COMMAND); return STATE_UNKNOWN; } child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); if (child_stderr == NULL) printf (_("Could not open stderr for %s\n"), WHO_COMMAND); users = 0; while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { /* increment 'users' on all lines except total user count */ if (input_buffer[0] != '#') { users++; continue; } /* get total logged in users */ if (sscanf (input_buffer, _("# users=%d"), &users) == 1) break; } /* check STDERR */ if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) result = possibly_set (result, STATE_UNKNOWN); (void) fclose (child_stderr); /* close the pipe */ if (spclose (child_process)) result = possibly_set (result, STATE_UNKNOWN); /* else check the user count against warning and critical thresholds */ if (users >= cusers) result = STATE_CRITICAL; else if (users >= wusers) result = STATE_WARNING; else if (users >= 0) result = STATE_OK; if (result == STATE_UNKNOWN) printf ("%s\n", _("Unable to read output")); else { asprintf(&perf, "%s", perfdata ("users", users, "", TRUE, wusers, TRUE, cusers, TRUE, 0, FALSE, 0)); printf (_("USERS %s - %d users currently logged in |%s\n"), state_text (result), users, perf); } return result; }
int main (int argc, char **argv) { MYSQL mysql; MYSQL_RES *res; MYSQL_ROW row; /* should be status */ char *result = NULL; char *error = NULL; char slaveresult[SLAVERESULTSIZE]; char* perf; perf = strdup (""); setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); /* Parse extra opts if any */ argv=np_extra_opts (&argc, argv, progname); if (process_arguments (argc, argv) == ERROR) usage4 (_("Could not parse arguments")); /* initialize mysql */ mysql_init (&mysql); if (opt_file != NULL) mysql_options(&mysql,MYSQL_READ_DEFAULT_FILE,opt_file); if (opt_group != NULL) mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,opt_group); else mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"client"); if (ssl) mysql_ssl_set(&mysql,key,cert,ca_cert,ca_dir,ciphers); /* establish a connection to the server and error checking */ if (!mysql_real_connect(&mysql,db_host,db_user,db_pass,db,db_port,db_socket,0)) { if (mysql_errno (&mysql) == CR_UNKNOWN_HOST) die (STATE_WARNING, "%s\n", mysql_error (&mysql)); else if (mysql_errno (&mysql) == CR_VERSION_ERROR) die (STATE_WARNING, "%s\n", mysql_error (&mysql)); else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY) die (STATE_WARNING, "%s\n", mysql_error (&mysql)); else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR) die (STATE_WARNING, "%s\n", mysql_error (&mysql)); else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR) die (STATE_WARNING, "%s\n", mysql_error (&mysql)); else die (STATE_CRITICAL, "%s\n", mysql_error (&mysql)); } /* get the server stats */ result = strdup (mysql_stat (&mysql)); /* error checking once more */ if (mysql_error (&mysql)) { if (mysql_errno (&mysql) == CR_SERVER_GONE_ERROR) die (STATE_CRITICAL, "%s\n", mysql_error (&mysql)); else if (mysql_errno (&mysql) == CR_SERVER_LOST) die (STATE_CRITICAL, "%s\n", mysql_error (&mysql)); else if (mysql_errno (&mysql) == CR_UNKNOWN_ERROR) die (STATE_CRITICAL, "%s\n", mysql_error (&mysql)); } /* try to fetch some perf data */ if (mysql_query (&mysql, "show global status") == 0) { if ( (res = mysql_store_result (&mysql)) == NULL) { error = strdup(mysql_error(&mysql)); mysql_close (&mysql); die (STATE_CRITICAL, _("status store_result error: %s\n"), error); } while ( (row = mysql_fetch_row (res)) != NULL) { int i; for(i = 0; i < LENGTH_METRIC_UNIT; i++) { if (strcmp(row[0], metric_unit[i]) == 0) { xasprintf(&perf, "%s%s ", perf, perfdata(metric_unit[i], atol(row[1]), "", FALSE, 0, FALSE, 0, FALSE, 0, FALSE, 0)); continue; } } for(i = 0; i < LENGTH_METRIC_COUNTER; i++) { if (strcmp(row[0], metric_counter[i]) == 0) { xasprintf(&perf, "%s%s ", perf, perfdata(metric_counter[i], atol(row[1]), "c", FALSE, 0, FALSE, 0, FALSE, 0, FALSE, 0)); continue; } } } /* remove trailing space */ if (strlen(perf) > 0) perf[strlen(perf) - 1] = '\0'; } if(check_slave) { /* check the slave status */ if (mysql_query (&mysql, "show slave status") != 0) { error = strdup(mysql_error(&mysql)); mysql_close (&mysql); die (STATE_CRITICAL, _("slave query error: %s\n"), error); } /* store the result */ if ( (res = mysql_store_result (&mysql)) == NULL) { error = strdup(mysql_error(&mysql)); mysql_close (&mysql); die (STATE_CRITICAL, _("slave store_result error: %s\n"), error); } /* Check there is some data */ if (mysql_num_rows(res) == 0) { mysql_close(&mysql); die (STATE_WARNING, "%s\n", _("No slaves defined")); } /* fetch the first row */ if ( (row = mysql_fetch_row (res)) == NULL) { error = strdup(mysql_error(&mysql)); mysql_free_result (res); mysql_close (&mysql); die (STATE_CRITICAL, _("slave fetch row error: %s\n"), error); } if (mysql_field_count (&mysql) == 12) { /* mysql 3.23.x */ snprintf (slaveresult, SLAVERESULTSIZE, _("Slave running: %s"), row[6]); if (strcmp (row[6], "Yes") != 0) { mysql_free_result (res); mysql_close (&mysql); die (STATE_CRITICAL, "%s\n", slaveresult); } } else { /* mysql 4.x.x and mysql 5.x.x */ int slave_io_field = -1 , slave_sql_field = -1, seconds_behind_field = -1, i, num_fields; MYSQL_FIELD* fields; num_fields = mysql_num_fields(res); fields = mysql_fetch_fields(res); for(i = 0; i < num_fields; i++) { if (strcmp(fields[i].name, "Slave_IO_Running") == 0) { slave_io_field = i; continue; } if (strcmp(fields[i].name, "Slave_SQL_Running") == 0) { slave_sql_field = i; continue; } if (strcmp(fields[i].name, "Seconds_Behind_Master") == 0) { seconds_behind_field = i; continue; } } /* Check if slave status is available */ if ((slave_io_field < 0) || (slave_sql_field < 0) || (num_fields == 0)) { mysql_free_result (res); mysql_close (&mysql); die (STATE_CRITICAL, "Slave status unavailable\n"); } /* Save slave status in slaveresult */ snprintf (slaveresult, SLAVERESULTSIZE, "Slave IO: %s Slave SQL: %s Seconds Behind Master: %s", row[slave_io_field], row[slave_sql_field], seconds_behind_field!=-1?row[seconds_behind_field]:"Unknown"); /* Raise critical error if SQL THREAD or IO THREAD are stopped */ if (strcmp (row[slave_io_field], "Yes") != 0 || strcmp (row[slave_sql_field], "Yes") != 0) { mysql_free_result (res); mysql_close (&mysql); die (STATE_CRITICAL, "%s\n", slaveresult); } if (verbose >=3) { if (seconds_behind_field == -1) { printf("seconds_behind_field not found\n"); } else { printf ("seconds_behind_field(index %d)=%s\n", seconds_behind_field, row[seconds_behind_field]); } } /* Check Seconds Behind against threshold */ if ((seconds_behind_field != -1) && (strcmp (row[seconds_behind_field], "NULL") != 0)) { double value = atof(row[seconds_behind_field]); int status; status = get_status(value, my_threshold); xasprintf (&perf, "%s %s", perf, fperfdata ("seconds behind master", value, "s", TRUE, (double) warning_time, TRUE, (double) critical_time, FALSE, 0, FALSE, 0)); if (status == STATE_WARNING) { printf("SLOW_SLAVE %s: %s|%s\n", _("WARNING"), slaveresult, perf); exit(STATE_WARNING); } else if (status == STATE_CRITICAL) { printf("SLOW_SLAVE %s: %s|%s\n", _("CRITICAL"), slaveresult, perf); exit(STATE_CRITICAL); } } } /* free the result */ mysql_free_result (res); } /* close the connection */ mysql_close (&mysql); /* print out the result of stats */ if (check_slave) { printf ("%s %s|%s\n", result, slaveresult, perf); } else { printf ("%s|%s\n", result, perf); } return STATE_OK; }
int main (int argc, char **argv) { int percent_used, percent; float total_swap_mb = 0, used_swap_mb = 0, free_swap_mb = 0; float dsktotal_mb = 0, dskused_mb = 0, dskfree_mb = 0, tmp_mb = 0; int result = STATE_UNKNOWN; char input_buffer[MAX_INPUT_BUFFER]; #ifdef HAVE_PROC_MEMINFO FILE *fp; #else int conv_factor = SWAP_CONVERSION; # ifdef HAVE_SWAP char *temp_buffer; char *swap_command; char *swap_format; # else # ifdef HAVE_DECL_SWAPCTL int i=0, nswaps=0, swapctl_res=0; # ifdef CHECK_SWAP_SWAPCTL_SVR4 swaptbl_t *tbl=NULL; swapent_t *ent=NULL; # else # ifdef CHECK_SWAP_SWAPCTL_BSD struct swapent *ent; # endif /* CHECK_SWAP_SWAPCTL_BSD */ # endif /* CHECK_SWAP_SWAPCTL_SVR4 */ # endif /* HAVE_DECL_SWAPCTL */ # endif #endif char str[32]; char *status; setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); status = strdup (""); /* Parse extra opts if any */ argv=np_extra_opts (&argc, argv, progname); if (process_arguments (argc, argv) == ERROR) usage4 (_("Could not parse arguments")); #ifdef HAVE_PROC_MEMINFO if (verbose >= 3) { printf("Reading PROC_MEMINFO at %s\n", PROC_MEMINFO); } fp = fopen (PROC_MEMINFO, "r"); while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) { if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%*[:] %f %f %f", &dsktotal_mb, &dskused_mb, &dskfree_mb) == 3) { dsktotal_mb = dsktotal_mb / 1048576; /* Apply conversion */ dskused_mb = dskused_mb / 1048576; dskfree_mb = dskfree_mb / 1048576; total_swap_mb += dsktotal_mb; used_swap_mb += dskused_mb; free_swap_mb += dskfree_mb; if (allswaps) { if (dsktotal_mb == 0) percent=100.0; else percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb)); result = max_state (result, check_swap (percent, dskfree_mb)); if (verbose) xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent); } } else if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%[TotalFre]%*[:] %f %*[k]%*[B]", str, &tmp_mb)) { if (verbose >= 3) { printf("Got %s with %f\n", str, tmp_mb); } /* I think this part is always in Kb, so convert to mb */ if (strcmp ("Total", str) == 0) { dsktotal_mb = tmp_mb / 1024; } else if (strcmp ("Free", str) == 0) { dskfree_mb = tmp_mb / 1024; } } } fclose(fp); dskused_mb = dsktotal_mb - dskfree_mb; total_swap_mb = dsktotal_mb; used_swap_mb = dskused_mb; free_swap_mb = dskfree_mb; #else # ifdef HAVE_SWAP xasprintf(&swap_command, "%s", SWAP_COMMAND); xasprintf(&swap_format, "%s", SWAP_FORMAT); /* These override the command used if a summary (and thus ! allswaps) is required */ /* The summary flag returns more accurate information about swap usage on these OSes */ # ifdef _AIX if (!allswaps) { xasprintf(&swap_command, "%s", "/usr/sbin/lsps -s"); xasprintf(&swap_format, "%s", "%f%*s %f"); conv_factor = 1; } # endif if (verbose >= 2) printf (_("Command: %s\n"), swap_command); if (verbose >= 3) printf (_("Format: %s\n"), swap_format); child_process = spopen (swap_command); if (child_process == NULL) { printf (_("Could not open pipe: %s\n"), swap_command); return STATE_UNKNOWN; } child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); if (child_stderr == NULL) printf (_("Could not open stderr for %s\n"), swap_command); sprintf (str, "%s", ""); /* read 1st line */ fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process); if (strcmp (swap_format, "") == 0) { temp_buffer = strtok (input_buffer, " \n"); while (temp_buffer) { if (strstr (temp_buffer, "blocks")) sprintf (str, "%s %s", str, "%f"); else if (strstr (temp_buffer, "dskfree")) sprintf (str, "%s %s", str, "%f"); else sprintf (str, "%s %s", str, "%*s"); temp_buffer = strtok (NULL, " \n"); } } /* If different swap command is used for summary switch, need to read format differently */ # ifdef _AIX if (!allswaps) { fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process); /* Ignore first line */ sscanf (input_buffer, swap_format, &total_swap_mb, &used_swap_mb); free_swap_mb = total_swap_mb * (100 - used_swap_mb) /100; used_swap_mb = total_swap_mb - free_swap_mb; if (verbose >= 3) printf (_("total=%.0f, used=%.0f, free=%.0f\n"), total_swap_mb, used_swap_mb, free_swap_mb); } else { # endif while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { sscanf (input_buffer, swap_format, &dsktotal_mb, &dskfree_mb); dsktotal_mb = dsktotal_mb / conv_factor; /* AIX lists percent used, so this converts to dskfree in MBs */ # ifdef _AIX dskfree_mb = dsktotal_mb * (100 - dskfree_mb) / 100; # else dskfree_mb = dskfree_mb / conv_factor; # endif if (verbose >= 3) printf (_("total=%.0f, free=%.0f\n"), dsktotal_mb, dskfree_mb); dskused_mb = dsktotal_mb - dskfree_mb; total_swap_mb += dsktotal_mb; used_swap_mb += dskused_mb; free_swap_mb += dskfree_mb; if (allswaps) { percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb)); result = max_state (result, check_swap (percent, dskfree_mb)); if (verbose) xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent); } } # ifdef _AIX } # endif /* If we get anything on STDERR, at least set warning */ while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) result = max_state (result, STATE_WARNING); /* close stderr */ (void) fclose (child_stderr); /* close the pipe */ if (spclose (child_process)) result = max_state (result, STATE_WARNING); # else # ifdef CHECK_SWAP_SWAPCTL_SVR4 /* get the number of active swap devices */ if((nswaps=swapctl(SC_GETNSWP, NULL))== -1) die(STATE_UNKNOWN, _("Error getting swap devices\n") ); if(nswaps == 0) die(STATE_OK, _("SWAP OK: No swap devices defined\n")); if(verbose >= 3) printf("Found %d swap device(s)\n", nswaps); /* initialize swap table + entries */ tbl=(swaptbl_t*)malloc(sizeof(swaptbl_t)+(sizeof(swapent_t)*nswaps)); if(tbl==NULL) die(STATE_UNKNOWN, _("malloc() failed!\n")); memset(tbl, 0, sizeof(swaptbl_t)+(sizeof(swapent_t)*nswaps)); tbl->swt_n=nswaps; for(i=0;i<nswaps;i++){ if((tbl->swt_ent[i].ste_path=(char*)malloc(sizeof(char)*MAXPATHLEN)) == NULL) die(STATE_UNKNOWN, _("malloc() failed!\n")); } /* and now, tally 'em up */ swapctl_res=swapctl(SC_LIST, tbl); if(swapctl_res < 0){ perror(_("swapctl failed: ")); die(STATE_UNKNOWN, _("Error in swapctl call\n")); } for(i=0;i<nswaps;i++){ dsktotal_mb = (float) tbl->swt_ent[i].ste_pages / SWAP_CONVERSION; dskfree_mb = (float) tbl->swt_ent[i].ste_free / SWAP_CONVERSION; dskused_mb = ( dsktotal_mb - dskfree_mb ); if (verbose >= 3) printf ("dsktotal_mb=%.0f dskfree_mb=%.0f dskused_mb=%.0f\n", dsktotal_mb, dskfree_mb, dskused_mb); if(allswaps && dsktotal_mb > 0){ percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb)); result = max_state (result, check_swap (percent, dskfree_mb)); if (verbose) { xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent); } } total_swap_mb += dsktotal_mb; free_swap_mb += dskfree_mb; used_swap_mb += dskused_mb; } /* and clean up after ourselves */ for(i=0;i<nswaps;i++){ free(tbl->swt_ent[i].ste_path); } free(tbl); # else # ifdef CHECK_SWAP_SWAPCTL_BSD /* get the number of active swap devices */ nswaps=swapctl(SWAP_NSWAP, NULL, 0); /* initialize swap table + entries */ ent=(struct swapent*)malloc(sizeof(struct swapent)*nswaps); /* and now, tally 'em up */ swapctl_res=swapctl(SWAP_STATS, ent, nswaps); if(swapctl_res < 0){ perror(_("swapctl failed: ")); die(STATE_UNKNOWN, _("Error in swapctl call\n")); } for(i=0;i<nswaps;i++){ dsktotal_mb = (float) ent[i].se_nblks / conv_factor; dskused_mb = (float) ent[i].se_inuse / conv_factor; dskfree_mb = ( dsktotal_mb - dskused_mb ); if(allswaps && dsktotal_mb > 0){ percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb)); result = max_state (result, check_swap (percent, dskfree_mb)); if (verbose) { xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent); } } total_swap_mb += dsktotal_mb; free_swap_mb += dskfree_mb; used_swap_mb += dskused_mb; } /* and clean up after ourselves */ free(ent); # endif /* CHECK_SWAP_SWAPCTL_BSD */ # endif /* CHECK_SWAP_SWAPCTL_SVR4 */ # endif /* HAVE_SWAP */ #endif /* HAVE_PROC_MEMINFO */ /* if total_swap_mb == 0, let's not divide by 0 */ if(total_swap_mb) { percent_used = 100 * ((double) used_swap_mb) / ((double) total_swap_mb); } else { percent_used = 0; } result = max_state (result, check_swap (percent_used, free_swap_mb)); printf (_("SWAP %s - %d%% free (%d MB out of %d MB) %s|"), state_text (result), (100 - percent_used), (int) free_swap_mb, (int) total_swap_mb, status); puts (perfdata ("swap", (long) free_swap_mb, "MB", TRUE, (long) max (warn_size_bytes/(1024 * 1024), warn_percent/100.0*total_swap_mb), TRUE, (long) max (crit_size_bytes/(1024 * 1024), crit_percent/100.0*total_swap_mb), TRUE, 0, TRUE, (long) total_swap_mb)); return result; }
int main (int argc, char **argv) { int result = STATE_OK; FILE *fp; int line; char input_buffer[MAX_INPUT_BUFFER]; char *temp_buffer; time_t current_time; time_t timestamp = 0L; unsigned long average_value_rate = 0L; unsigned long maximum_value_rate = 0L; unsigned long rate = 0L; setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); /* Parse extra opts if any */ argv=np_extra_opts (&argc, argv, progname); if (process_arguments (argc, argv) == ERROR) usage4 (_("Could not parse arguments\n")); /* open the MRTG log file for reading */ fp = fopen (log_file, "r"); if (fp == NULL) { printf (_("Unable to open MRTG log file\n")); return STATE_UNKNOWN; } line = 0; while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) { line++; /* skip the first line of the log file */ if (line == 1) continue; /* break out of read loop if we've passed the number of entries we want to read */ if (line > 2) break; /* grab the timestamp */ temp_buffer = strtok (input_buffer, " "); timestamp = strtoul (temp_buffer, NULL, 10); /* grab the average value 1 rate */ temp_buffer = strtok (NULL, " "); if (variable_number == 1) average_value_rate = strtoul (temp_buffer, NULL, 10); /* grab the average value 2 rate */ temp_buffer = strtok (NULL, " "); if (variable_number == 2) average_value_rate = strtoul (temp_buffer, NULL, 10); /* grab the maximum value 1 rate */ temp_buffer = strtok (NULL, " "); if (variable_number == 1) maximum_value_rate = strtoul (temp_buffer, NULL, 10); /* grab the maximum value 2 rate */ temp_buffer = strtok (NULL, " "); if (variable_number == 2) maximum_value_rate = strtoul (temp_buffer, NULL, 10); } /* close the log file */ fclose (fp); /* if we couldn't read enough data, return an unknown error */ if (line <= 2) { printf (_("Unable to process MRTG log file\n")); return STATE_UNKNOWN; } /* make sure the MRTG data isn't too old */ time (¤t_time); if (expire_minutes > 0 && (current_time - timestamp) > (expire_minutes * 60)) { printf (_("MRTG data has expired (%d minutes old)\n"), (int) ((current_time - timestamp) / 60)); return STATE_WARNING; } /* else check the incoming/outgoing rates */ if (use_average == TRUE) rate = average_value_rate; else rate = maximum_value_rate; if (rate > value_critical_threshold) result = STATE_CRITICAL; else if (rate > value_warning_threshold) result = STATE_WARNING; printf("%s. %s = %lu %s|%s\n", (use_average == TRUE) ? _("Avg") : _("Max"), label, rate, units, perfdata(label, (long) rate, units, (int) value_warning_threshold, (long) value_warning_threshold, (int) value_critical_threshold, (long) value_critical_threshold, 0, 0, 0, 0)); return result; }