diff --git a/lib/utils_base.c b/lib/utils_base.c
index 3dae32fbf337636a2b953affa9414cb7fb0697c6..4303e1592700bc96cbc752930a157387215f56ad 100644 (file)
--- a/lib/utils_base.c
+++ b/lib/utils_base.c
/*****************************************************************************
- *
- * utils_base.c
- *
- * Library of useful functions for plugins
- * These functions are tested with libtap. See tests/ directory
- *
- * Copyright (c) 2006 Nagios Plugin Development Team
- * License: GPL
- *
- * $Revision$
- * $Date$
- ****************************************************************************/
+*
+* utils_base.c
+*
+* License: GPL
+* Copyright (c) 2006 Nagios Plugins Development Team
+*
+* Library of useful functions for plugins
+*
+*
+* This program is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program. If not, see <http://www.gnu.org/licenses/>.
+*
+*
+*****************************************************************************/
-#include <stdarg.h>
#include "common.h"
+#include <stdarg.h>
#include "utils_base.h"
void
set_range_end(temp_range, end);
}
- if (temp_range->start_infinity == TRUE ||
+ if (temp_range->start_infinity == TRUE ||
temp_range->end_infinity == TRUE ||
temp_range->start <= temp_range->end) {
return temp_range;
@@ -89,26 +101,24 @@ _set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_st
{
thresholds *temp_thresholds = NULL;
- temp_thresholds = malloc(sizeof(temp_thresholds));
+ if ((temp_thresholds = malloc(sizeof(thresholds))) == NULL)
+ die(STATE_UNKNOWN, _("Cannot allocate memory: %s\n"),
+ strerror(errno));
temp_thresholds->warning = NULL;
temp_thresholds->critical = NULL;
if (warn_string != NULL) {
if ((temp_thresholds->warning = parse_range_string(warn_string)) == NULL) {
- return 1;
+ return NP_RANGE_UNPARSEABLE;
}
}
if (critical_string != NULL) {
if ((temp_thresholds->critical = parse_range_string(critical_string)) == NULL) {
- return 1;
+ return NP_RANGE_UNPARSEABLE;
}
}
- if (*my_thresholds > 0) { /* Not sure why, but sometimes could be -1 */
- /* printf("Freeing here: %d\n", *my_thresholds); */
- free(*my_thresholds);
- }
*my_thresholds = temp_thresholds;
return 0;
@@ -117,10 +127,14 @@ _set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_st
void
set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_string)
{
- if (_set_thresholds(my_thresholds, warn_string, critical_string) == 0) {
+ switch (_set_thresholds(my_thresholds, warn_string, critical_string)) {
+ case 0:
return;
- } else {
+ case NP_RANGE_UNPARSEABLE:
die(STATE_UNKNOWN, _("Range format incorrect"));
+ case NP_WARN_WITHIN_CRIT:
+ die(STATE_UNKNOWN, _("Warning level is a subset of critical and will not be alerted"));
+ break;
}
}
int
check_range(double value, range *my_range)
{
- int false = FALSE;
- int true = TRUE;
-
+ int no = FALSE;
+ int yes = TRUE;
+
if (my_range->alert_on == INSIDE) {
- false = TRUE;
- true = FALSE;
+ no = TRUE;
+ yes = FALSE;
}
if (my_range->end_infinity == FALSE && my_range->start_infinity == FALSE) {
if ((my_range->start <= value) && (value <= my_range->end)) {
- return false;
+ return no;
} else {
- return true;
+ return yes;
}
} else if (my_range->start_infinity == FALSE && my_range->end_infinity == TRUE) {
if (my_range->start <= value) {
- return false;
+ return no;
} else {
- return true;
+ return yes;
}
} else if (my_range->start_infinity == TRUE && my_range->end_infinity == FALSE) {
if (value <= my_range->end) {
- return false;
+ return no;
} else {
- return true;
+ return yes;
}
} else {
- return false;
+ return no;
}
}
data[j] = '\0';
return data;
}
+
+int np_check_if_root(void) { return (geteuid() == 0); }
+
+int np_warn_if_not_root(void) {
+ int status = np_check_if_root();
+ if(!status) {
+ printf(_("Warning: "));
+ printf(_("This plugin must be either run as root or setuid root.\n"));
+ printf(_("To run as root, you can use a tool like sudo.\n"));
+ printf(_("To set the setuid permissions, use the command:\n"));
+ /* XXX could we use something like progname? */
+ printf("\tchmod u+s yourpluginfile\n");
+ }
+ return status;
+}
+
+/*
+ * Extract the value from key/value pairs, or return NULL. The value returned
+ * can be free()ed.
+ * This function can be used to parse NTP control packet data and performance
+ * data strings.
+ */
+char *np_extract_value(const char *varlist, const char *name, char sep) {
+ char *tmp=NULL, *value=NULL;
+ int i;
+
+ while (1) {
+ /* Strip any leading space */
+ for (varlist; isspace(varlist[0]); varlist++);
+
+ if (strncmp(name, varlist, strlen(name)) == 0) {
+ varlist += strlen(name);
+ /* strip trailing spaces */
+ for (varlist; isspace(varlist[0]); varlist++);
+
+ if (varlist[0] == '=') {
+ /* We matched the key, go past the = sign */
+ varlist++;
+ /* strip leading spaces */
+ for (varlist; isspace(varlist[0]); varlist++);
+
+ if (tmp = index(varlist, sep)) {
+ /* Value is delimited by a comma */
+ if (tmp-varlist == 0) continue;
+ value = (char *)malloc(tmp-varlist+1);
+ strncpy(value, varlist, tmp-varlist);
+ value[tmp-varlist] = '\0';
+ } else {
+ /* Value is delimited by a \0 */
+ if (strlen(varlist) == 0) continue;
+ value = (char *)malloc(strlen(varlist) + 1);
+ strncpy(value, varlist, strlen(varlist));
+ value[strlen(varlist)] = '\0';
+ }
+ break;
+ }
+ }
+ if (tmp = index(varlist, sep)) {
+ /* More keys, keep going... */
+ varlist = tmp + 1;
+ } else {
+ /* We're done */
+ break;
+ }
+ }
+
+ /* Clean-up trailing spaces/newlines */
+ if (value) for (i=strlen(value)-1; isspace(value[i]); i--) value[i] = '\0';
+
+ return value;
+}
+