From 548083b2ea865474915fc8a9ddd361e997585a02 Mon Sep 17 00:00:00 2001 From: Ton Voon Date: Thu, 13 Jul 2006 12:50:23 +0000 Subject: [PATCH] Move new util_* functions to lib/ git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1451 f882894a-f735-0410-b71e-b25c423dba1c --- Makefile.am | 1 + configure.in | 2 +- lib/Makefile.am | 9 +- {plugins => lib}/tests/.cvsignore | 0 {plugins => lib}/tests/Makefile.am | 4 +- {plugins => lib}/tests/README | 0 {plugins => lib}/tests/test_disk.c | 0 {plugins => lib}/tests/test_disk.t | 0 {plugins => lib}/tests/test_utils.c | 9 +- {plugins => lib}/tests/test_utils.t | 0 plugins/Makefile.am | 7 +- plugins/check_disk.c | 36 +++--- plugins/check_dns.c | 1 + plugins/check_mysql.c | 1 + plugins/check_mysql_query.c | 1 + plugins/utils.c | 191 +--------------------------- plugins/utils.h | 25 ---- plugins/utils_disk.c | 141 -------------------- plugins/utils_disk.h | 30 ----- 19 files changed, 37 insertions(+), 421 deletions(-) rename {plugins => lib}/tests/.cvsignore (100%) rename {plugins => lib}/tests/Makefile.am (84%) rename {plugins => lib}/tests/README (100%) rename {plugins => lib}/tests/test_disk.c (100%) rename {plugins => lib}/tests/test_disk.t (100%) rename {plugins => lib}/tests/test_utils.c (98%) rename {plugins => lib}/tests/test_utils.t (100%) delete mode 100644 plugins/utils_disk.c delete mode 100644 plugins/utils_disk.h diff --git a/Makefile.am b/Makefile.am index 28367a9..a45758a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -21,6 +21,7 @@ install-root: cd plugins-root && $(MAKE) $@ test: + cd lib && $(MAKE) test cd plugins; $(MAKE) test cd plugins-scripts; $(MAKE) test diff --git a/configure.in b/configure.in index 54d5700..32ab396 100644 --- a/configure.in +++ b/configure.in @@ -1699,7 +1699,7 @@ AC_OUTPUT( lib/Makefile m4/Makefile plugins/Makefile - plugins/tests/Makefile + lib/tests/Makefile plugins-root/Makefile plugins-scripts/Makefile plugins-scripts/subst diff --git a/lib/Makefile.am b/lib/Makefile.am index 9a8991e..0f40bf1 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -1,5 +1,7 @@ ## Process this file with automake to produce Makefile.in +SUBDIRS = tests + noinst_LIBRARIES = libcoreutils.a libnagiosplug.a # Will auto pick up fsusage.c mountlist.c @@ -39,13 +41,16 @@ other_coreutils_files = \ libcoreutils_a_LIBADD = $(LIBOBJS) libcoreutils_a_DEPENDENCIES = $(libcoreutils_a_LIBADD) -libnagiosplug_a_SOURCES = snprintf.c +libnagiosplug_a_SOURCES = snprintf.c utils_base.c utils_disk.c -INCLUDES = -I$(srcdir) -I$(top_srcdir)/intl +INCLUDES = -I$(srcdir) -I$(top_srcdir)/intl -I$(top_srcdir)/plugins print_coreutil_files: @echo $(libcoreutils_a_SOURCES) $(other_coreutils_files) $(EXTRA_DIST) +test: + cd tests && make test + # Below are from coreutil's lib/Makefile.am BUILT_SOURCES = $(STDBOOL_H) EXTRA_DIST += stdbool_.h diff --git a/plugins/tests/.cvsignore b/lib/tests/.cvsignore similarity index 100% rename from plugins/tests/.cvsignore rename to lib/tests/.cvsignore diff --git a/plugins/tests/Makefile.am b/lib/tests/Makefile.am similarity index 84% rename from plugins/tests/Makefile.am rename to lib/tests/Makefile.am index 741499d..63dee76 100644 --- a/plugins/tests/Makefile.am +++ b/lib/tests/Makefile.am @@ -5,7 +5,7 @@ noinst_PROGRAMS = @EXTRA_TEST@ TESTS = @EXTRA_TEST@ check_PROGRAMS = @EXTRA_TEST@ -INCLUDES = -I$(top_srcdir)/lib -I$(top_srcdir)/intl +INCLUDES = -I$(top_srcdir)/lib -I$(top_srcdir)/intl -I$(top_srcdir)/plugins EXTRA_PROGRAMS = test_utils test_disk @@ -16,7 +16,7 @@ LIBS = @LIBINTL@ test_utils_SOURCES = test_utils.c test_utils_CFLAGS = -g -I.. test_utils_LDFLAGS = -L/usr/local/lib -ltap -test_utils_LDADD = ../utils.o +test_utils_LDADD = ../utils_base.o test_disk_SOURCES = test_disk.c test_disk_CFLAGS = -g -I.. diff --git a/plugins/tests/README b/lib/tests/README similarity index 100% rename from plugins/tests/README rename to lib/tests/README diff --git a/plugins/tests/test_disk.c b/lib/tests/test_disk.c similarity index 100% rename from plugins/tests/test_disk.c rename to lib/tests/test_disk.c diff --git a/plugins/tests/test_disk.t b/lib/tests/test_disk.t similarity index 100% rename from plugins/tests/test_disk.t rename to lib/tests/test_disk.t diff --git a/plugins/tests/test_utils.c b/lib/tests/test_utils.c similarity index 98% rename from plugins/tests/test_utils.c rename to lib/tests/test_utils.c index 67c304a..aedc2a5 100644 --- a/plugins/tests/test_utils.c +++ b/lib/tests/test_utils.c @@ -18,11 +18,8 @@ ******************************************************************************/ -const char *progname = "utils"; - #include "common.h" -#include "utils.h" -#include "popen.h" +#include "utils_base.h" #include "tap.h" @@ -170,7 +167,3 @@ main (int argc, char **argv) return exit_status(); } - -void print_usage() { - printf("Dummy"); -} diff --git a/plugins/tests/test_utils.t b/lib/tests/test_utils.t similarity index 100% rename from plugins/tests/test_utils.t rename to lib/tests/test_utils.t diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 28793cd..9dd7184 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -1,7 +1,5 @@ ## Process this file with automake to produce Makefile.in -SUBDIRS = tests - VPATH = $(top_srcdir) $(top_srcdir)/lib $(top_srcdir)/plugins $(top_srcdir)/plugins/t INCLUDES = -I.. -I$(top_srcdir)/lib -I$(top_srcdir)/intl @LDAPINCLUDE@ @PGINCLUDE@ @SSLINCLUDE@ @@ -45,7 +43,6 @@ TESTS_ENVIRONMENT = perl -I $(top_builddir) -I $(top_srcdir) TESTS = @PLUGIN_TEST@ test: - cd tests && make test perl -I $(top_builddir) -I $(top_srcdir) ../test.pl AM_INSTALL_PROGRAM_FLAGS = @INSTALL_OPTS@ @@ -55,7 +52,7 @@ AM_INSTALL_PROGRAM_FLAGS = @INSTALL_OPTS@ check_apt_LDADD = $(BASEOBJS) runcmd.o check_dig_LDADD = $(NETLIBS) runcmd.o -check_disk_LDADD = $(BASEOBJS) popen.o utils_disk.o +check_disk_LDADD = $(BASEOBJS) popen.o check_dns_LDADD = $(NETLIBS) runcmd.o check_dummy_LDADD = $(BASEOBJS) check_fping_LDADD = $(NETLIBS) popen.o @@ -98,7 +95,7 @@ urlize_LDADD = $(BASEOBJS) popen.o check_apt_DEPENDENCIES = check_apt.c $(BASEOBJS) runcmd.o $(DEPLIBS) check_dig_DEPENDENCIES = check_dig.c $(NETOBJS) runcmd.o $(DEPLIBS) -check_disk_DEPENDENCIES = check_disk.c $(BASEOBJS) popen.o utils_disk.o $(DEPLIBS) +check_disk_DEPENDENCIES = check_disk.c $(BASEOBJS) popen.o $(DEPLIBS) check_dns_DEPENDENCIES = check_dns.c $(NETOBJS) runcmd.o $(DEPLIBS) check_dummy_DEPENDENCIES = check_dummy.c $(DEPLIBS) check_fping_DEPENDENCIES = check_fping.c $(NETOBJS) popen.o $(DEPLIBS) diff --git a/plugins/check_disk.c b/plugins/check_disk.c index 1423448..6beaf86 100644 --- a/plugins/check_disk.c +++ b/plugins/check_disk.c @@ -45,6 +45,7 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net"; #include #include "popen.h" #include "utils.h" +#include "utils_disk.h" #include #include "fsusage.h" #include "mountlist.h" @@ -52,7 +53,6 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net"; # include #endif -#include "utils_disk.h" /* If nonzero, show inode information. */ static int inode_format; @@ -94,7 +94,7 @@ static struct name_list *fs_exclude_list; static struct name_list *dp_exclude_list; -static struct parameter_list *path_select_list; +static struct parameter_list *path_select_list = NULL; /* Linked list of mounted filesystems. */ static struct mount_entry *mount_list; @@ -296,12 +296,17 @@ process_arguments (int argc, char **argv) { int c; struct parameter_list *se; - struct parameter_list **pathtail = &path_select_list; struct parameter_list *temp_list; int result = OK; struct stat *stat_buf; + char *warn_freespace = NULL; + char *crit_freespace = NULL; + char *warn_freespace_percent = NULL; + char *crit_freespace_percent = NULL; + char temp_string[MAX_INPUT_BUFFER]; unsigned long l; + double f; int option = 0; static struct option longopts[] = { @@ -355,6 +360,15 @@ process_arguments (int argc, char **argv) usage2 (_("Timeout interval must be a positive integer"), optarg); } case 'w': /* warning threshold */ + /* + if (strstr(optarg, "%")) { + printf("Got percent with optarg=%s\n", optarg); + warn_freespace_percent = optarg; + } else { + warn_freespace = optarg; + } + break; + */ if (is_intnonneg (optarg)) { w_df = atoi (optarg); break; @@ -444,19 +458,13 @@ process_arguments (int argc, char **argv) show_local_fs = 1; break; case 'p': /* select path */ - se = (struct parameter_list *) malloc (sizeof (struct parameter_list)); - se->name = optarg; - se->name_next = NULL; + se = np_add_parameter(&path_select_list, optarg); se->w_df = w_df; se->c_df = c_df; se->w_dfp = w_dfp; se->c_dfp = c_dfp; se->w_idfp = w_idfp; se->c_idfp = c_idfp; - se->found = 0; - se->found_len = 0; - *pathtail = se; - pathtail = &se->name_next; break; case 'x': /* exclude path or partition */ np_add_name(&dp_exclude_list, optarg); @@ -507,18 +515,13 @@ process_arguments (int argc, char **argv) c_dfp = (100.0 - atof (argv[c++])); if (argc > c && path == NULL) { - se = (struct parameter_list *) malloc (sizeof (struct parameter_list)); - se->name = strdup (argv[c++]); - se->name_next = NULL; + se = np_add_parameter(&path_select_list, strdup(argv[c++])); se->w_df = w_df; se->c_df = c_df; se->w_dfp = w_dfp; se->c_dfp = c_dfp; se->w_idfp = w_idfp; se->c_idfp = c_idfp; - se->found =0; - se->found_len = 0; - *pathtail = se; } if (path_select_list) { @@ -604,7 +607,6 @@ INPUT ERROR: C_DF (%lu) should be less than W_DF (%lu) and both should be greate int - check_disk (double usp, uintmax_t free_disk, double uisp) { int result = STATE_UNKNOWN; diff --git a/plugins/check_dns.c b/plugins/check_dns.c index 081741a..6563e10 100644 --- a/plugins/check_dns.c +++ b/plugins/check_dns.c @@ -28,6 +28,7 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net"; #include "common.h" #include "utils.h" +#include "utils_base.h" #include "netutils.h" #include "runcmd.h" diff --git a/plugins/check_mysql.c b/plugins/check_mysql.c index a47572d..8c7e4ef 100644 --- a/plugins/check_mysql.c +++ b/plugins/check_mysql.c @@ -23,6 +23,7 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net"; #include "common.h" #include "utils.h" +#include "utils_base.h" #include "netutils.h" #include diff --git a/plugins/check_mysql_query.c b/plugins/check_mysql_query.c index 404ca79..e129366 100644 --- a/plugins/check_mysql_query.c +++ b/plugins/check_mysql_query.c @@ -20,6 +20,7 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net"; #include "common.h" #include "utils.h" +#include "utils_base.h" #include "netutils.h" #include diff --git a/plugins/utils.c b/plugins/utils.c index cb01341..2b3acce 100644 --- a/plugins/utils.c +++ b/plugins/utils.c @@ -16,6 +16,7 @@ #include "common.h" #include "utils.h" +#include "utils_base.h" #include #include @@ -131,16 +132,6 @@ state_text (int result) } } -void -die (int result, const char *fmt, ...) -{ - va_list ap; - va_start (ap, fmt); - vprintf (fmt, ap); - va_end (ap); - exit (result); -} - void timeout_alarm_handler (int signo) { @@ -266,156 +257,6 @@ is_option (char *str) return FALSE; } -void set_range_start (range *this, double value) { - this->start = value; - this->start_infinity = FALSE; -} - -void set_range_end (range *this, double value) { - this->end = value; - this->end_infinity = FALSE; -} - -range -*parse_range_string (char *str) { - range *temp_range; - double start; - double end; - char *end_str; - - temp_range = (range *) malloc(sizeof(range)); - - /* Set defaults */ - temp_range->start = 0; - temp_range->start_infinity = FALSE; - temp_range->end = 0; - temp_range->end_infinity = TRUE; - temp_range->alert_on = OUTSIDE; - - if (str[0] == '@') { - temp_range->alert_on = INSIDE; - str++; - } - - end_str = index(str, ':'); - if (end_str != NULL) { - if (str[0] == '~') { - temp_range->start_infinity = TRUE; - } else { - start = strtod(str, NULL); /* Will stop at the ':' */ - set_range_start(temp_range, start); - } - end_str++; /* Move past the ':' */ - } else { - end_str = str; - } - end = strtod(end_str, NULL); - if (strcmp(end_str, "") != 0) { - set_range_end(temp_range, end); - } - - if (temp_range->start_infinity == TRUE || - temp_range->end_infinity == TRUE || - temp_range->start <= temp_range->end) { - return temp_range; - } - free(temp_range); - return NULL; -} - -/* returns 0 if okay, otherwise 1 */ -int -_set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_string) -{ - thresholds *temp_thresholds = NULL; - - temp_thresholds = malloc(sizeof(temp_thresholds)); - - temp_thresholds->warning = NULL; - temp_thresholds->critical = NULL; - - if (warn_string != NULL) { - if ((temp_thresholds->warning = parse_range_string(warn_string)) == NULL) { - return 1; - } - } - if (critical_string != NULL) { - if ((temp_thresholds->critical = parse_range_string(critical_string)) == NULL) { - return 1; - } - } - - if (*my_thresholds != 0) { - /* printf("Freeing here: %d\n", *my_thresholds); */ - free(*my_thresholds); - } - *my_thresholds = temp_thresholds; - - return 0; -} - -void -set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_string) -{ - if (_set_thresholds(my_thresholds, warn_string, critical_string) == 0) { - return; - } else { - usage(_("Range format incorrect")); - } -} - -/* Returns TRUE if alert should be raised based on the range */ -int -check_range(double value, range *my_range) -{ - int false = FALSE; - int true = TRUE; - - if (my_range->alert_on == INSIDE) { - false = TRUE; - true = FALSE; - } - - if (my_range->end_infinity == FALSE && my_range->start_infinity == FALSE) { - if ((my_range->start <= value) && (value <= my_range->end)) { - return false; - } else { - return true; - } - } else if (my_range->start_infinity == FALSE && my_range->end_infinity == TRUE) { - if (my_range->start <= value) { - return false; - } else { - return true; - } - } else if (my_range->start_infinity == TRUE && my_range->end_infinity == FALSE) { - if (value <= my_range->end) { - return false; - } else { - return true; - } - } else { - return false; - } -} - -/* Returns status */ -int -get_status(double value, thresholds *my_thresholds) -{ - if (my_thresholds->critical != NULL) { - if (check_range(value, my_thresholds->critical) == TRUE) { - return STATE_CRITICAL; - } - } - if (my_thresholds->warning != NULL) { - if (check_range(value, my_thresholds->warning) == TRUE) { - return STATE_WARNING; - } - } - return STATE_OK; -} - #ifdef NEED_GETTIMEOFDAY int gettimeofday (struct timeval *tv, struct timezone *tz) @@ -727,33 +568,3 @@ char *fperfdata (const char *label, return data; } - -char *np_escaped_string (const char *string) { - char *data; - int i, j=0; - data = strdup(string); - for (i=0; data[i]; i++) { - if (data[i] == '\\') { - switch(data[++i]) { - case 'n': - data[j++] = '\n'; - break; - case 'r': - data[j++] = '\r'; - break; - case 't': - data[j++] = '\t'; - break; - case '\\': - data[j++] = '\\'; - break; - default: - data[j++] = data[i]; - } - } else { - data[j++] = data[i]; - } - } - data[j] = '\0'; - return data; -} diff --git a/plugins/utils.h b/plugins/utils.h index 4bbe33d..1f53aad 100644 --- a/plugins/utils.h +++ b/plugins/utils.h @@ -20,7 +20,6 @@ suite of plugins. */ void support (void); char *clean_revstring (const char *); void print_revision (const char *, const char *); -void die (int, const char *, ...) __attribute__((noreturn,format(printf, 2, 3))); /* Handle timeouts */ @@ -58,28 +57,6 @@ struct timeval { }; #endif -#define OUTSIDE 0 -#define INSIDE 1 - -typedef struct range_struct { - double start; - int start_infinity; /* FALSE (default) or TRUE */ - double end; - int end_infinity; - int alert_on; /* OUTSIDE (default) or INSIDE */ - } range; - -typedef struct thresholds_struct { - range *warning; - range *critical; - } thresholds; - -range *parse_range_string (char *); -int _set_thresholds(thresholds **, char *, char *); -void set_thresholds(thresholds **, char *, char *); -int check_range(double, range *); -int get_status(double, thresholds *); - #ifndef HAVE_GETTIMEOFDAY int gettimeofday(struct timeval *, struct timezone *); #endif @@ -132,8 +109,6 @@ char *fperfdata (const char *, int, double); -char *np_escaped_string (const char *); - /* The idea here is that, although not every plugin will use all of these, most will or should. Therefore, for consistency, these very common options should have only these meanings throughout the overall suite */ diff --git a/plugins/utils_disk.c b/plugins/utils_disk.c deleted file mode 100644 index 31284d5..0000000 --- a/plugins/utils_disk.c +++ /dev/null @@ -1,141 +0,0 @@ -/**************************************************************************** -* Utils for check_disk -* -* License: GPL -* Copyright (c) 1999-2006 nagios-plugins team -* -* Last Modified: $Date$ -* -* Description: -* -* This file contains utilities for check_disk. These are tested by libtap -* -* License Information: -* -* 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 2 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, write to the Free Software -* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -* -* $Id$ -* -*****************************************************************************/ - -#include "common.h" -#include "utils_disk.h" - -void -np_add_name (struct name_list **list, const char *name) -{ - struct name_list *new_entry; - new_entry = (struct name_list *) malloc (sizeof *new_entry); - new_entry->name = (char *) name; - new_entry->next = *list; - *list = new_entry; -} - -/* Initialises a new parameter at the end of list */ -struct parameter_list * -np_add_parameter(struct parameter_list **list, const char *name) -{ - struct parameter_list *current = *list; - struct parameter_list *new_path; - new_path = (struct parameter_list *) malloc (sizeof *new_path); - new_path->name = (char *) name; - new_path->found = 0; - new_path->found_len = 0; - new_path->w_df = 0; - new_path->c_df = 0; - new_path->w_dfp = -1.0; - new_path->c_dfp = -1.0; - new_path->w_idfp = -1.0; - new_path->c_idfp = -1.0; - new_path->best_match = NULL; - - if (current == NULL) { - *list = new_path; - } else { - while (current->name_next) { - current = current->name_next; - } - current->name_next = new_path; - } - return new_path; -} - -void -np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list, int exact) -{ - struct parameter_list *d; - for (d = desired; d; d= d->name_next) { - struct mount_entry *me; - size_t name_len = strlen(d->name); - size_t best_match_len = 0; - struct mount_entry *best_match = NULL; - - for (me = mount_list; me; me = me->me_next) { - size_t len = strlen (me->me_mountdir); - if ((exact == FALSE && (best_match_len <= len && len <= name_len && - (len == 1 || strncmp (me->me_mountdir, d->name, len) == 0))) - || (exact == TRUE && strcmp(me->me_mountdir, d->name)==0)) - { - best_match = me; - best_match_len = len; - } else { - len = strlen (me->me_devname); - if ((exact == FALSE && (best_match_len <= len && len <= name_len && - (len == 1 || strncmp (me->me_devname, d->name, len) == 0))) - || (exact == TRUE && strcmp(me->me_devname, d->name)==0)) - { - best_match = me; - best_match_len = len; - } - } - } - if (best_match) { - d->best_match = best_match; - d->found = TRUE; - } else { - d->best_match = NULL; /* Not sure why this is needed as it should be null on initialisation */ - } - } -} - -/* Returns TRUE if name is in list */ -int -np_find_name (struct name_list *list, const char *name) -{ - const struct name_list *n; - - if (list == NULL || name == NULL) { - return FALSE; - } - for (n = list; n; n = n->next) { - if (!strcmp(name, n->name)) { - return TRUE; - } - } - return FALSE; -} - -int -np_seen_name(struct name_list *list, const char *name) -{ - const struct name_list *s; - for (s = list; s; s=s->next) { - if (!strcmp(s->name, name)) { - return TRUE; - } - } - return FALSE; -} - diff --git a/plugins/utils_disk.h b/plugins/utils_disk.h deleted file mode 100644 index 676ca09..0000000 --- a/plugins/utils_disk.h +++ /dev/null @@ -1,30 +0,0 @@ -/* Header file for utils_disk */ - -#include "mountlist.h" - -struct name_list -{ - char *name; - struct name_list *next; -}; - -struct parameter_list -{ - char *name; - int found; - int found_len; - uintmax_t w_df; - uintmax_t c_df; - double w_dfp; - double c_dfp; - double w_idfp; - double c_idfp; - struct mount_entry *best_match; - struct parameter_list *name_next; -}; - -void np_add_name (struct name_list **list, const char *name); -int np_find_name (struct name_list *list, const char *name); -int np_seen_name (struct name_list *list, const char *name); -struct parameter_list *np_add_parameter(struct parameter_list **list, const char *name); -int search_parameter_list (struct parameter_list *list, const char *name); -- 2.30.2