Skip to content

Commit

Permalink
add warning_string/critical_string to the treshold objects and implem…
Browse files Browse the repository at this point in the history
…ented the printing in check_snmp. also cleaned up the perfdata section of check_snmp with much needed whitespace and comments
  • Loading branch information
hedenface committed Sep 16, 2017
1 parent 7989ae8 commit f838eaa
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 32 deletions.
10 changes: 10 additions & 0 deletions lib/utils_base.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,22 @@ _set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_st

temp_thresholds->warning = NULL;
temp_thresholds->critical = NULL;
temp_thresholds->warning_string = NULL;
temp_thresholds->critical_string = NULL;

if (warn_string) {
if (!(temp_thresholds->warning = parse_range_string(warn_string))) {
free(temp_thresholds);
return NP_RANGE_UNPARSEABLE;
}
temp_thresholds->warning_string = strdup(warn_string);
}
if (critical_string) {
if (!(temp_thresholds->critical = parse_range_string(critical_string))) {
free(temp_thresholds);
return NP_RANGE_UNPARSEABLE;
}
temp_thresholds->critical_string = strdup(critical_string);
}

*my_thresholds = temp_thresholds;
Expand Down Expand Up @@ -204,11 +208,17 @@ void print_thresholds(const char *threshold_name, thresholds *my_threshold) {
} else {
if (my_threshold->warning) {
printf("Warning: start=%g end=%g; ", my_threshold->warning->start, my_threshold->warning->end);
if (my_threshold->warning_string) {
printf("Warning String: %s; ", my_threshold->warning_string);
}
} else {
printf("Warning not set; ");
}
if (my_threshold->critical) {
printf("Critical: start=%g end=%g", my_threshold->critical->start, my_threshold->critical->end);
if (my_threshold->critical_string) {
printf("Critical String: %s; ", my_threshold->critical_string);
}
} else {
printf("Critical not set");
}
Expand Down
2 changes: 2 additions & 0 deletions lib/utils_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ typedef struct range_struct {
typedef struct thresholds_struct {
range *warning;
range *critical;
char *warning_string;
char *critical_string;
} thresholds;

#define NP_STATE_FORMAT_VERSION 1
Expand Down
103 changes: 71 additions & 32 deletions plugins/check_snmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ main (int argc, char **argv)
set_thresholds(&thlds[i],
w ? strpbrk(w, NP_THRESHOLDS_CHARS) : NULL,
c ? strpbrk(c, NP_THRESHOLDS_CHARS) : NULL);

if (w) {
th_warn=strchr(th_warn, ',');
if (th_warn) th_warn++;
Expand Down Expand Up @@ -575,58 +576,96 @@ main (int argc, char **argv)
ptr = NULL;
strtod(show, &ptr);
if (ptr > show) {
if (perf_labels && nlabels >= (size_t)1 && (size_t)i < nlabels && labels[i] != NULL)
temp_string=labels[i];
else
temp_string=oidname;
if (strpbrk (temp_string, " ='\"") == NULL) {
strncat(perfstr, temp_string, sizeof(perfstr)-strlen(perfstr)-1);

/* use either specified label or oid as label */
if (perf_labels
&& (nlabels >= (size_t)1)
&& ((size_t)i < nlabels)
&& labels[i] != NULL) {

temp_string=labels[i];
}
else {
temp_string = oidname;
}

/* check the label for space, equal, singlequote or doublequote */
if (strpbrk(temp_string, " ='\"") == NULL) {

/* if it doesn't have any - we can just use it as the label */
strncat(perfstr, temp_string, sizeof(perfstr) - strlen(perfstr) - 1);

} else {
if (strpbrk (temp_string, "'") == NULL) {

/* if it does have one of those characters, we need
to find a way to adequately quote it */
if (strpbrk(temp_string, "'") == NULL) {
quote_string="'";
} else {
quote_string="\"";
}
strncat(perfstr, quote_string, sizeof(perfstr)-strlen(perfstr)-1);
strncat(perfstr, temp_string, sizeof(perfstr)-strlen(perfstr)-1);
strncat(perfstr, quote_string, sizeof(perfstr)-strlen(perfstr)-1);

strncat(perfstr, quote_string, sizeof(perfstr) - strlen(perfstr) - 1);
strncat(perfstr, temp_string, sizeof(perfstr) - strlen(perfstr) - 1);
strncat(perfstr, quote_string, sizeof(perfstr) - strlen(perfstr) - 1);
}
strncat(perfstr, "=", sizeof(perfstr)-strlen(perfstr)-1);
len = sizeof(perfstr)-strlen(perfstr)-1;
strncat(perfstr, show, len>ptr-show ? ptr-show : len);

if (nunits > (size_t)0 && (size_t)i < nunits && unitv[i] != NULL) {
xasprintf (&temp_string, "%s", unitv[i]);
strncat(perfstr, temp_string, sizeof(perfstr)-strlen(perfstr)-1);
}
/* append the equal */
strncat(perfstr, "=", sizeof(perfstr) - strlen(perfstr) - 1);
len = sizeof(perfstr) - strlen(perfstr) - 1;

/* and then the data itself from the response */
strncat(perfstr, show, (len > ptr - show) ? ptr - show : len);

if (type)
strncat(perfstr, type, sizeof(perfstr)-strlen(perfstr)-1);
/* now append the unit of measurement */
if ((nunits > (size_t)0)
&& ((size_t)i < nunits)
&& (unitv[i] != NULL)) {

xasprintf(&temp_string, "%s", unitv[i]);
strncat(perfstr, temp_string, sizeof(perfstr) - strlen(perfstr) - 1);
}

/* and the type, if any */
if (type) {
strncat(perfstr, type, sizeof(perfstr) - strlen(perfstr) - 1);
}

/* add warn/crit to perfdata */
if (thlds[i]->warning || thlds[i]->critical) {
strncat(perfstr, ";", sizeof(perfstr)-strlen(perfstr)-1);
if (thlds[i]->warning) {
xasprintf (&temp_string, "%.0f", thlds[i]->warning->end);
strncat(perfstr, temp_string, sizeof(perfstr)-strlen(perfstr)-1);

strncat(perfstr, ";", sizeof(perfstr) - strlen(perfstr) - 1);

/* print the warning string if it exists */
if (thlds[i]->warning_string) {

xasprintf(&temp_string, "%s", thlds[i]->warning_string);
strncat(perfstr, temp_string, sizeof(perfstr) - strlen(perfstr) - 1);
}
strncat(perfstr, ";", sizeof(perfstr)-strlen(perfstr)-1);
if (thlds[i]->critical) {
xasprintf (&temp_string, "%.0f", thlds[i]->critical->end);
strncat(perfstr, temp_string, sizeof(perfstr)-strlen(perfstr)-1);

/* print the critical string if it exists */
if (thlds[i]->critical_string) {

xasprintf(&temp_string, "%s", thlds[i]->critical_string);
strncat(perfstr, temp_string, sizeof(perfstr) - strlen(perfstr) - 1);
}
strncat(perfstr, ";", sizeof(perfstr)-strlen(perfstr)-1);
strncat(perfstr, ";", sizeof(perfstr) - strlen(perfstr) - 1);
}

/* remove trailing semi-colons for guideline adherence */
if (perfstr[strlen(perfstr) - 1] == ';') {
perfstr[strlen(perfstr) - 1] = '\0';
len = strlen(perfstr) - 1;
if (perfstr[len] == ';') {
perfstr[len] = '\0';
}

strncat(perfstr, " ", sizeof(perfstr)-strlen(perfstr)-1);
/* we do not add any min/max value */

strncat(perfstr, " ", sizeof(perfstr) - strlen(perfstr) - 1);
}

}
/* for (line=0, i=0; line < chld_out.lines; line++, i++) */
} /* for (line=0, i=0; line < chld_out.lines; line++, i++) */

total_oids=i;

/* Save state data, as all data collected now */
Expand Down

0 comments on commit f838eaa

Please sign in to comment.