From 0645c9fc2c7f801ba3c7d68a17c137a63ada299f Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Tue, 18 Feb 2025 21:58:34 +0100 Subject: Implement new output functionality --- lib/utils_base.c | 167 +++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 118 insertions(+), 49 deletions(-) (limited to 'lib/utils_base.c') diff --git a/lib/utils_base.c b/lib/utils_base.c index 90a4aaa5..ff9540c7 100644 --- a/lib/utils_base.c +++ b/lib/utils_base.c @@ -55,22 +55,24 @@ void np_init(char *plugin_name, int argc, char **argv) { die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); } this_monitoring_plugin->plugin_name = strdup(plugin_name); - if (this_monitoring_plugin->plugin_name == NULL) + if (this_monitoring_plugin->plugin_name == NULL) { die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); + } this_monitoring_plugin->argc = argc; this_monitoring_plugin->argv = argv; } } void np_set_args(int argc, char **argv) { - if (this_monitoring_plugin == NULL) + if (this_monitoring_plugin == NULL) { die(STATE_UNKNOWN, _("This requires np_init to be called")); + } this_monitoring_plugin->argc = argc; this_monitoring_plugin->argv = argv; } -void np_cleanup() { +void np_cleanup(void) { if (this_monitoring_plugin != NULL) { if (this_monitoring_plugin->state != NULL) { if (this_monitoring_plugin->state->state_data) { @@ -162,8 +164,9 @@ range *parse_range_string(char *str) { int _set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_string) { thresholds *temp_thresholds = NULL; - if ((temp_thresholds = calloc(1, sizeof(thresholds))) == NULL) + if ((temp_thresholds = calloc(1, sizeof(thresholds))) == NULL) { die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); + } temp_thresholds->warning = NULL; temp_thresholds->critical = NULL; @@ -215,6 +218,46 @@ void print_thresholds(const char *threshold_name, thresholds *my_threshold) { printf("\n"); } +/* Returns true if alert should be raised based on the range, false otherwise */ +bool mp_check_range(const mp_perfdata_value value, const mp_range my_range) { + bool is_inside = false; + + if (my_range.end_infinity == false && my_range.start_infinity == false) { + // range: .........|---inside---|........... + // value + if ((cmp_perfdata_value(my_range.start, value) < 1) && (cmp_perfdata_value(value, my_range.end) <= 0)) { + is_inside = true; + } else { + is_inside = false; + } + } else if (my_range.start_infinity == false && my_range.end_infinity == true) { + // range: .........|---inside--------- + // value + if (cmp_perfdata_value(my_range.start, value) < 0) { + is_inside = true; + } else { + is_inside = false; + } + } else if (my_range.start_infinity == true && my_range.end_infinity == false) { + // range: -inside--------|.................... + // value + if (cmp_perfdata_value(value, my_range.end) == -1) { + is_inside = true; + } else { + is_inside = false; + } + } else { + // range from -inf to inf, so always inside + is_inside = true; + } + + if ((is_inside && my_range.alert_on_inside_range == INSIDE) || (!is_inside && my_range.alert_on_inside_range == OUTSIDE)) { + return true; + } + + return false; +} + /* Returns true if alert should be raised based on the range */ bool check_range(double value, range *my_range) { bool no = false; @@ -228,24 +271,24 @@ bool check_range(double value, range *my_range) { if (my_range->end_infinity == false && my_range->start_infinity == false) { if ((my_range->start <= value) && (value <= my_range->end)) { return no; - } else { - return yes; } - } else if (my_range->start_infinity == false && my_range->end_infinity == true) { + return yes; + } + + if (my_range->start_infinity == false && my_range->end_infinity == true) { if (my_range->start <= value) { return no; - } else { - return yes; } - } else if (my_range->start_infinity == true && my_range->end_infinity == false) { + return yes; + } + + if (my_range->start_infinity == true && my_range->end_infinity == false) { if (value <= my_range->end) { return no; - } else { - return yes; } - } else { - return no; + return yes; } + return no; } /* Returns status */ @@ -265,7 +308,8 @@ int get_status(double value, thresholds *my_thresholds) { char *np_escaped_string(const char *string) { char *data; - int i, j = 0; + int i; + int j = 0; data = strdup(string); for (i = 0; data[i]; i++) { if (data[i] == '\\') { @@ -302,7 +346,8 @@ int np_check_if_root(void) { return (geteuid() == 0); } * data strings. */ char *np_extract_value(const char *varlist, const char *name, char sep) { - char *tmp = NULL, *value = NULL; + char *tmp = NULL; + char *value = NULL; int i; while (1) { @@ -325,15 +370,17 @@ char *np_extract_value(const char *varlist, const char *name, char sep) { if ((tmp = index(varlist, sep))) { /* Value is delimited by a comma */ - if (tmp - varlist == 0) + if (tmp - varlist == 0) { continue; + } value = (char *)calloc(1, tmp - varlist + 1); strncpy(value, varlist, tmp - varlist); value[tmp - varlist] = '\0'; } else { /* Value is delimited by a \0 */ - if (strlen(varlist) == 0) + if (strlen(varlist) == 0) { continue; + } value = (char *)calloc(1, strlen(varlist) + 1); strncpy(value, varlist, strlen(varlist)); value[strlen(varlist)] = '\0'; @@ -351,9 +398,11 @@ char *np_extract_value(const char *varlist, const char *name, char sep) { } /* Clean-up trailing spaces/newlines */ - if (value) - for (i = strlen(value) - 1; isspace(value[i]); i--) + if (value) { + for (i = strlen(value) - 1; isspace(value[i]); i--) { value[i] = '\0'; + } + } return value; } @@ -378,14 +427,18 @@ const char *state_text(int result) { * return the corresponding STATE_ value or ERROR) */ int mp_translate_state(char *state_text) { - if (!strcasecmp(state_text, "OK") || !strcmp(state_text, "0")) + if (!strcasecmp(state_text, "OK") || !strcmp(state_text, "0")) { return STATE_OK; - if (!strcasecmp(state_text, "WARNING") || !strcmp(state_text, "1")) + } + if (!strcasecmp(state_text, "WARNING") || !strcmp(state_text, "1")) { return STATE_WARNING; - if (!strcasecmp(state_text, "CRITICAL") || !strcmp(state_text, "2")) + } + if (!strcasecmp(state_text, "CRITICAL") || !strcmp(state_text, "2")) { return STATE_CRITICAL; - if (!strcasecmp(state_text, "UNKNOWN") || !strcmp(state_text, "3")) + } + if (!strcasecmp(state_text, "UNKNOWN") || !strcmp(state_text, "3")) { return STATE_UNKNOWN; + } return ERROR; } @@ -394,7 +447,7 @@ int mp_translate_state(char *state_text) { * hopefully a unique key per service/plugin invocation. Use the extra-opts * parse of argv, so that uniqueness in parameters are reflected there. */ -char *_np_state_generate_key() { +char *_np_state_generate_key(void) { int i; char **argv = this_monitoring_plugin->argv; char keyname[41]; @@ -441,7 +494,7 @@ char *_np_state_generate_key() { return p; } -void _cleanup_state_data() { +void _cleanup_state_data(void) { if (this_monitoring_plugin->state->state_data != NULL) { np_free(this_monitoring_plugin->state->state_data->data); np_free(this_monitoring_plugin->state->state_data); @@ -453,19 +506,21 @@ void _cleanup_state_data() { * envvar NAGIOS_PLUGIN_STATE_DIRECTORY * statically compiled shared state directory */ -char *_np_state_calculate_location_prefix() { +char *_np_state_calculate_location_prefix(void) { char *env_dir; /* Do not allow passing MP_STATE_PATH in setuid plugins * for security reasons */ if (!mp_suid()) { env_dir = getenv("MP_STATE_PATH"); - if (env_dir && env_dir[0] != '\0') + if (env_dir && env_dir[0] != '\0') { return env_dir; + } /* This is the former ENV, for backward-compatibility */ env_dir = getenv("NAGIOS_PLUGIN_STATE_DIRECTORY"); - if (env_dir && env_dir[0] != '\0') + if (env_dir && env_dir[0] != '\0') { return env_dir; + } } return NP_STATE_DIR_PREFIX; @@ -483,19 +538,22 @@ void np_enable_state(char *keyname, int expected_data_version) { char *p = NULL; int ret; - if (this_monitoring_plugin == NULL) + if (this_monitoring_plugin == NULL) { die(STATE_UNKNOWN, _("This requires np_init to be called")); + } this_state = (state_key *)calloc(1, sizeof(state_key)); - if (this_state == NULL) + if (this_state == NULL) { die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); + } if (keyname == NULL) { temp_keyname = _np_state_generate_key(); } else { temp_keyname = strdup(keyname); - if (temp_keyname == NULL) + if (temp_keyname == NULL) { die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); + } } /* Die if invalid characters used for keyname */ p = temp_keyname; @@ -513,8 +571,9 @@ void np_enable_state(char *keyname, int expected_data_version) { /* Calculate filename */ ret = asprintf(&temp_filename, "%s/%lu/%s/%s", _np_state_calculate_location_prefix(), (unsigned long)geteuid(), this_monitoring_plugin->plugin_name, this_state->name); - if (ret < 0) + if (ret < 0) { die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); + } this_state->_filename = temp_filename; @@ -528,21 +587,23 @@ void np_enable_state(char *keyname, int expected_data_version) { * If numerically lower, then return as no previous state. die with UNKNOWN * if exceptional error. */ -state_data *np_state_read() { +state_data *np_state_read(void) { state_data *this_state_data = NULL; FILE *statefile; bool rc = false; - if (this_monitoring_plugin == NULL) + if (this_monitoring_plugin == NULL) { die(STATE_UNKNOWN, _("This requires np_init to be called")); + } /* Open file. If this fails, no previous state found */ statefile = fopen(this_monitoring_plugin->state->_filename, "r"); if (statefile != NULL) { this_state_data = (state_data *)calloc(1, sizeof(state_data)); - if (this_state_data == NULL) + if (this_state_data == NULL) { die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); + } this_state_data->data = NULL; this_monitoring_plugin->state->state_data = this_state_data; @@ -581,8 +642,9 @@ bool _np_state_read_file(FILE *f) { /* Note: This introduces a limit of 1024 bytes in the string data */ line = (char *)calloc(1, 1024); - if (line == NULL) + if (line == NULL) { die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); + } while (!failure && (fgets(line, 1024, f)) != NULL) { pos = strlen(line); @@ -590,38 +652,42 @@ bool _np_state_read_file(FILE *f) { line[pos - 1] = '\0'; } - if (line[0] == '#') + if (line[0] == '#') { continue; + } switch (expected) { case STATE_FILE_VERSION: i = atoi(line); - if (i != NP_STATE_FORMAT_VERSION) + if (i != NP_STATE_FORMAT_VERSION) { failure++; - else + } else { expected = STATE_DATA_VERSION; + } break; case STATE_DATA_VERSION: i = atoi(line); - if (i != this_monitoring_plugin->state->data_version) + if (i != this_monitoring_plugin->state->data_version) { failure++; - else + } else { expected = STATE_DATA_TIME; + } break; case STATE_DATA_TIME: /* If time > now, error */ data_time = strtoul(line, NULL, 10); - if (data_time > current_time) + if (data_time > current_time) { failure++; - else { + } else { this_monitoring_plugin->state->state_data->time = data_time; expected = STATE_DATA_TEXT; } break; case STATE_DATA_TEXT: this_monitoring_plugin->state->state_data->data = strdup(line); - if (this_monitoring_plugin->state->state_data->data == NULL) + if (this_monitoring_plugin->state->state_data->data == NULL) { die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); + } expected = STATE_DATA_END; status = true; break; @@ -648,16 +714,18 @@ void np_state_write_string(time_t data_time, char *data_string) { char *directories = NULL; char *p = NULL; - if (data_time == 0) + if (data_time == 0) { time(¤t_time); - else + } else { current_time = data_time; + } /* If file doesn't currently exist, create directories */ if (access(this_monitoring_plugin->state->_filename, F_OK) != 0) { result = asprintf(&directories, "%s", this_monitoring_plugin->state->_filename); - if (result < 0) + if (result < 0) { die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); + } for (p = directories + 1; *p; p++) { if (*p == '/') { @@ -674,8 +742,9 @@ void np_state_write_string(time_t data_time, char *data_string) { } result = asprintf(&temp_file, "%s.XXXXXX", this_monitoring_plugin->state->_filename); - if (result < 0) + if (result < 0) { die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); + } if ((fd = mkstemp(temp_file)) == -1) { np_free(temp_file); -- cgit v1.2.3-74-g34f1