static int do_check_con_percentage (size_t values_num, double *values, char **values_names) { size_t i; double sum = 0.0; double percentage; const char *status_str = "UNKNOWN"; int status_code = RET_UNKNOWN; if ((values_num < 1) || (isnan (values[0]))) { printf ("WARNING: The first value is not defined\n"); return (RET_WARNING); } for (i = 0; i < values_num; i++) { if (isnan (values[i])) { if (!nan_is_error_g) continue; printf ("CRITICAL: Data source \"%s\" is NaN\n", values_names[i]); return (RET_CRITICAL); } sum += values[i]; } if (sum == 0.0) { printf ("WARNING: Values sum up to zero\n"); return (RET_WARNING); } percentage = 100.0 * values[0] / sum; if (match_range (&range_critical_g, percentage) != 0) { status_str = "CRITICAL"; status_code = RET_CRITICAL; } else if (match_range (&range_warning_g, percentage) != 0) { status_str = "WARNING"; status_code = RET_WARNING; } else { status_str = "OKAY"; status_code = RET_OKAY; } printf ("%s: %lf percent |", status_str, percentage); for (i = 0; i < values_num; i++) printf (" %s=%lf;;;;", values_names[i], values[i]); return (status_code); } /* int do_check_con_percentage */
static int do_check_con_none (size_t values_num, double *values, char **values_names) { int num_critical = 0; int num_warning = 0; int num_okay = 0; const char *status_str = "UNKNOWN"; int status_code = RET_UNKNOWN; size_t i; for (i = 0; i < values_num; i++) { if (isnan (values[i])) { if (nan_is_error_g) num_critical++; else num_warning++; } else if (match_range (&range_critical_g, values[i]) != 0) num_critical++; else if (match_range (&range_warning_g, values[i]) != 0) num_warning++; else num_okay++; } if ((num_critical == 0) && (num_warning == 0) && (num_okay == 0)) { printf ("WARNING: No defined values found\n"); return (RET_WARNING); } else if ((num_critical == 0) && (num_warning == 0)) { status_str = "OKAY"; status_code = RET_OKAY; } else if (num_critical == 0) { status_str = "WARNING"; status_code = RET_WARNING; } else { status_str = "CRITICAL"; status_code = RET_CRITICAL; } printf ("%s: %i critical, %i warning, %i okay", status_str, num_critical, num_warning, num_okay); if (values_num > 0) { printf (" |"); for (i = 0; i < values_num; i++) printf (" %s=%f;;;;", values_names[i], values[i]); } printf ("\n"); return (status_code); } /* int do_check_con_none */
static int do_check_con_average (size_t values_num, double *values, char **values_names) { size_t i; double total; int total_num; double average; const char *status_str = "UNKNOWN"; int status_code = RET_UNKNOWN; total = 0.0; total_num = 0; for (i = 0; i < values_num; i++) { if (isnan (values[i])) { if (!nan_is_error_g) continue; printf ("CRITICAL: Data source \"%s\" is NaN\n", values_names[i]); return (RET_CRITICAL); } total += values[i]; total_num++; } if (total_num == 0) { printf ("WARNING: No defined values found\n"); return (RET_WARNING); } average = total / total_num; if (match_range (&range_critical_g, average) != 0) { status_str = "CRITICAL"; status_code = RET_CRITICAL; } else if (match_range (&range_warning_g, average) != 0) { status_str = "WARNING"; status_code = RET_WARNING; } else { status_str = "OKAY"; status_code = RET_OKAY; } printf ("%s: %g average |", status_str, average); for (i = 0; i < values_num; i++) printf (" %s=%f;;;;", values_names[i], values[i]); printf ("\n"); return (status_code); } /* int do_check_con_average */
bool match(char* lpStr, char* lpPattern) { if (!lpStr || !lpPattern) return !lpStr && !lpPattern; while (*lpPattern == '%' && *(lpPattern+1) == '%') lpPattern++; // Remove duplicate '%' if (*lpStr == '\0') return (*lpPattern == '\0' || (*lpPattern == '%' && *(lpPattern+1)=='\0')); if (*lpPattern == '%') return (*(lpPattern+1)=='\0') ? true : match(lpStr, lpPattern +1) || match(lpStr+1, lpPattern); else if (*lpPattern == '_') return match(lpStr + 1, lpPattern+1); else if (*lpPattern == '[') { char* pEnd = lpPattern + 1;; for (; *pEnd != ']' && *pEnd != '\0'; pEnd++) { if (*pEnd == '\\' && !(*(pEnd++))) return false; } return (*pEnd == ']') ? match_range(*lpStr, lpPattern+1, pEnd - 1) && match(lpStr+1, pEnd+1) : false; } else if (*lpPattern == '\\') return (charEqIgnoreCase(*(lpPattern+1), *lpStr) && match(lpStr+1, lpPattern+2)); else return (charEqIgnoreCase(*lpPattern, *lpStr) && match(lpStr+1, lpPattern+1)); }