summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorTon Voon <tonvoon@users.sourceforge.net>2006-07-13 23:58:00 (GMT)
committerTon Voon <tonvoon@users.sourceforge.net>2006-07-13 23:58:00 (GMT)
commit5912398b9723545ecd061650667cbb238be85743 (patch)
treee5d8353cc8d8c278bfc22bd926f613da63f5e83f /lib
parent548083b2ea865474915fc8a9ddd361e997585a02 (diff)
downloadmonitoring-plugins-5912398b9723545ecd061650667cbb238be85743.tar.gz
Major fixes to check_disk. Now should return same data as df
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1452 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'lib')
-rw-r--r--lib/tests/test_utils.c15
-rw-r--r--lib/utils_base.c225
-rw-r--r--lib/utils_base.h37
-rw-r--r--lib/utils_disk.c140
-rw-r--r--lib/utils_disk.h32
5 files changed, 448 insertions, 1 deletions
diff --git a/lib/tests/test_utils.c b/lib/tests/test_utils.c
index aedc2a5..96b5333 100644
--- a/lib/tests/test_utils.c
+++ b/lib/tests/test_utils.c
@@ -31,7 +31,7 @@ main (int argc, char **argv)
31 thresholds *thresholds = NULL; 31 thresholds *thresholds = NULL;
32 int rc; 32 int rc;
33 33
34 plan_tests(74); 34 plan_tests(82);
35 35
36 range = parse_range_string("6"); 36 range = parse_range_string("6");
37 ok( range != NULL, "'6' is valid range"); 37 ok( range != NULL, "'6' is valid range");
@@ -41,6 +41,14 @@ main (int argc, char **argv)
41 ok( range->end_infinity == FALSE, "Not using infinity"); 41 ok( range->end_infinity == FALSE, "Not using infinity");
42 free(range); 42 free(range);
43 43
44 range = parse_range_string("1:12%%");
45 ok( range != NULL, "'1:12%%' is valid - percentages are ignored");
46 ok( range->start == 1, "Start correct");
47 ok( range->start_infinity == FALSE, "Not using negative infinity");
48 ok( range->end == 12, "End correct");
49 ok( range->end_infinity == FALSE, "Not using infinity");
50 free(range);
51
44 range = parse_range_string("-7:23"); 52 range = parse_range_string("-7:23");
45 ok( range != NULL, "'-7:23' is valid range"); 53 ok( range != NULL, "'-7:23' is valid range");
46 ok( range->start == -7, "Start correct"); 54 ok( range->start == -7, "Start correct");
@@ -114,6 +122,11 @@ main (int argc, char **argv)
114 range = parse_range_string("2:1"); 122 range = parse_range_string("2:1");
115 ok( range == NULL, "'2:1' rejected"); 123 ok( range == NULL, "'2:1' rejected");
116 124
125 rc = _set_thresholds(&thresholds, NULL, NULL);
126 ok( rc == 0, "Thresholds (NULL, NULL) set");
127 ok( thresholds->warning == NULL, "Warning not set");
128 ok( thresholds->critical == NULL, "Critical not set");
129
117 rc = _set_thresholds(&thresholds, NULL, "80"); 130 rc = _set_thresholds(&thresholds, NULL, "80");
118 ok( rc == 0, "Thresholds (NULL, '80') set"); 131 ok( rc == 0, "Thresholds (NULL, '80') set");
119 ok( thresholds->warning == NULL, "Warning not set"); 132 ok( thresholds->warning == NULL, "Warning not set");
diff --git a/lib/utils_base.c b/lib/utils_base.c
new file mode 100644
index 0000000..5a45f84
--- /dev/null
+++ b/lib/utils_base.c
@@ -0,0 +1,225 @@
1/*****************************************************************************
2 *
3 * utils_base.c
4 *
5 * Library of useful functions for plugins
6 * These functions are tested with libtap. See tests/ directory
7 *
8 * Copyright (c) 2006 Nagios Plugin Development Team
9 * License: GPL
10 *
11 * $Revision$
12 * $Date$
13 ****************************************************************************/
14
15#include "common.h"
16#include "utils_base.h"
17
18void
19die (int result, const char *fmt, ...)
20{
21 va_list ap;
22 va_start (ap, fmt);
23 vprintf (fmt, ap);
24 va_end (ap);
25 exit (result);
26}
27
28void set_range_start (range *this, double value) {
29 this->start = value;
30 this->start_infinity = FALSE;
31}
32
33void set_range_end (range *this, double value) {
34 this->end = value;
35 this->end_infinity = FALSE;
36}
37
38range
39*parse_range_string (char *str) {
40 range *temp_range;
41 double start;
42 double end;
43 char *end_str;
44
45 temp_range = (range *) malloc(sizeof(range));
46
47 /* Set defaults */
48 temp_range->start = 0;
49 temp_range->start_infinity = FALSE;
50 temp_range->end = 0;
51 temp_range->end_infinity = TRUE;
52 temp_range->alert_on = OUTSIDE;
53
54 if (str[0] == '@') {
55 temp_range->alert_on = INSIDE;
56 str++;
57 }
58
59 end_str = index(str, ':');
60 if (end_str != NULL) {
61 if (str[0] == '~') {
62 temp_range->start_infinity = TRUE;
63 } else {
64 start = strtod(str, NULL); /* Will stop at the ':' */
65 set_range_start(temp_range, start);
66 }
67 end_str++; /* Move past the ':' */
68 } else {
69 end_str = str;
70 }
71 end = strtod(end_str, NULL);
72 if (strcmp(end_str, "") != 0) {
73 set_range_end(temp_range, end);
74 }
75
76 if (temp_range->start_infinity == TRUE ||
77 temp_range->end_infinity == TRUE ||
78 temp_range->start <= temp_range->end) {
79 return temp_range;
80 }
81 free(temp_range);
82 return NULL;
83}
84
85/* returns 0 if okay, otherwise 1 */
86int
87_set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_string)
88{
89 thresholds *temp_thresholds = NULL;
90
91 temp_thresholds = malloc(sizeof(temp_thresholds));
92
93 temp_thresholds->warning = NULL;
94 temp_thresholds->critical = NULL;
95
96 if (warn_string != NULL) {
97 if ((temp_thresholds->warning = parse_range_string(warn_string)) == NULL) {
98 return 1;
99 }
100 }
101 if (critical_string != NULL) {
102 if ((temp_thresholds->critical = parse_range_string(critical_string)) == NULL) {
103 return 1;
104 }
105 }
106
107 if (*my_thresholds > 0) { /* Not sure why, but sometimes could be -1 */
108 /* printf("Freeing here: %d\n", *my_thresholds); */
109 free(*my_thresholds);
110 }
111 *my_thresholds = temp_thresholds;
112
113 return 0;
114}
115
116void
117set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_string)
118{
119 if (_set_thresholds(my_thresholds, warn_string, critical_string) == 0) {
120 return;
121 } else {
122 die(STATE_UNKNOWN, _("Range format incorrect"));
123 }
124}
125
126void print_thresholds(const char *threshold_name, thresholds *my_threshold) {
127 printf("%s - ", threshold_name);
128 if (! my_threshold) {
129 printf("Threshold not set");
130 } else {
131 if (my_threshold->warning) {
132 printf("Warning: start=%g end=%g; ", my_threshold->warning->start, my_threshold->warning->end);
133 } else {
134 printf("Warning not set; ");
135 }
136 if (my_threshold->critical) {
137 printf("Critical: start=%g end=%g", my_threshold->critical->start, my_threshold->critical->end);
138 } else {
139 printf("Critical not set");
140 }
141 }
142 printf("\n");
143}
144
145/* Returns TRUE if alert should be raised based on the range */
146int
147check_range(double value, range *my_range)
148{
149 int false = FALSE;
150 int true = TRUE;
151
152 if (my_range->alert_on == INSIDE) {
153 false = TRUE;
154 true = FALSE;
155 }
156
157 if (my_range->end_infinity == FALSE && my_range->start_infinity == FALSE) {
158 if ((my_range->start <= value) && (value <= my_range->end)) {
159 return false;
160 } else {
161 return true;
162 }
163 } else if (my_range->start_infinity == FALSE && my_range->end_infinity == TRUE) {
164 if (my_range->start <= value) {
165 return false;
166 } else {
167 return true;
168 }
169 } else if (my_range->start_infinity == TRUE && my_range->end_infinity == FALSE) {
170 if (value <= my_range->end) {
171 return false;
172 } else {
173 return true;
174 }
175 } else {
176 return false;
177 }
178}
179
180/* Returns status */
181int
182get_status(double value, thresholds *my_thresholds)
183{
184 if (my_thresholds->critical != NULL) {
185 if (check_range(value, my_thresholds->critical) == TRUE) {
186 return STATE_CRITICAL;
187 }
188 }
189 if (my_thresholds->warning != NULL) {
190 if (check_range(value, my_thresholds->warning) == TRUE) {
191 return STATE_WARNING;
192 }
193 }
194 return STATE_OK;
195}
196
197char *np_escaped_string (const char *string) {
198 char *data;
199 int i, j=0;
200 data = strdup(string);
201 for (i=0; data[i]; i++) {
202 if (data[i] == '\\') {
203 switch(data[++i]) {
204 case 'n':
205 data[j++] = '\n';
206 break;
207 case 'r':
208 data[j++] = '\r';
209 break;
210 case 't':
211 data[j++] = '\t';
212 break;
213 case '\\':
214 data[j++] = '\\';
215 break;
216 default:
217 data[j++] = data[i];
218 }
219 } else {
220 data[j++] = data[i];
221 }
222 }
223 data[j] = '\0';
224 return data;
225}
diff --git a/lib/utils_base.h b/lib/utils_base.h
new file mode 100644
index 0000000..ca27f16
--- /dev/null
+++ b/lib/utils_base.h
@@ -0,0 +1,37 @@
1/* Header file for nagios plugins utils_base.c */
2
3/* This file holds header information for thresholds - use this in preference to
4 individual plugin logic */
5
6/* This has not been merged with utils.h because of problems with
7 timeout_interval when other utils_*.h files use utils.h */
8
9/* Long term, add new functions to utils_base.h for common routines
10 and utils_*.h for specific to plugin routines. If routines are
11 placed in utils_*.h, then these can be tested with libtap */
12
13#define OUTSIDE 0
14#define INSIDE 1
15
16typedef struct range_struct {
17 double start;
18 int start_infinity; /* FALSE (default) or TRUE */
19 double end;
20 int end_infinity;
21 int alert_on; /* OUTSIDE (default) or INSIDE */
22 } range;
23
24typedef struct thresholds_struct {
25 range *warning;
26 range *critical;
27 } thresholds;
28
29range *parse_range_string (char *);
30int _set_thresholds(thresholds **, char *, char *);
31void set_thresholds(thresholds **, char *, char *);
32int check_range(double, range *);
33int get_status(double, thresholds *);
34
35char *np_escaped_string (const char *);
36
37void die (int, const char *, ...) __attribute__((noreturn,format(printf, 2, 3)));
diff --git a/lib/utils_disk.c b/lib/utils_disk.c
new file mode 100644
index 0000000..fdbeaf1
--- /dev/null
+++ b/lib/utils_disk.c
@@ -0,0 +1,140 @@
1/****************************************************************************
2* Utils for check_disk
3*
4* License: GPL
5* Copyright (c) 1999-2006 nagios-plugins team
6*
7* Last Modified: $Date$
8*
9* Description:
10*
11* This file contains utilities for check_disk. These are tested by libtap
12*
13* License Information:
14*
15* This program is free software; you can redistribute it and/or modify
16* it under the terms of the GNU General Public License as published by
17* the Free Software Foundation; either version 2 of the License, or
18* (at your option) any later version.
19*
20* This program is distributed in the hope that it will be useful,
21* but WITHOUT ANY WARRANTY; without even the implied warranty of
22* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23* GNU General Public License for more details.
24*
25* You should have received a copy of the GNU General Public License
26* along with this program; if not, write to the Free Software
27* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28*
29* $Id$
30*
31*****************************************************************************/
32
33#include "common.h"
34#include "utils_disk.h"
35
36void
37np_add_name (struct name_list **list, const char *name)
38{
39 struct name_list *new_entry;
40 new_entry = (struct name_list *) malloc (sizeof *new_entry);
41 new_entry->name = (char *) name;
42 new_entry->next = *list;
43 *list = new_entry;
44}
45
46/* Initialises a new parameter at the end of list */
47struct parameter_list *
48np_add_parameter(struct parameter_list **list, const char *name)
49{
50 struct parameter_list *current = *list;
51 struct parameter_list *new_path;
52 new_path = (struct parameter_list *) malloc (sizeof *new_path);
53 new_path->name = (char *) name;
54 new_path->best_match = NULL;
55 new_path->name_next = NULL;
56 new_path->freespace_bytes = NULL;
57 new_path->freespace_units = NULL;
58 new_path->freespace_percent = NULL;
59 new_path->usedspace_bytes = NULL;
60 new_path->usedspace_units = NULL;
61 new_path->usedspace_percent = NULL;
62 new_path->usedinodes_percent = NULL;
63
64 if (current == NULL) {
65 *list = new_path;
66 } else {
67 while (current->name_next) {
68 current = current->name_next;
69 }
70 current->name_next = new_path;
71 }
72 return new_path;
73}
74
75void
76np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list, int exact)
77{
78 struct parameter_list *d;
79 for (d = desired; d; d= d->name_next) {
80 struct mount_entry *me;
81 size_t name_len = strlen(d->name);
82 size_t best_match_len = 0;
83 struct mount_entry *best_match = NULL;
84
85 for (me = mount_list; me; me = me->me_next) {
86 size_t len = strlen (me->me_mountdir);
87 if ((exact == FALSE && (best_match_len <= len && len <= name_len &&
88 (len == 1 || strncmp (me->me_mountdir, d->name, len) == 0)))
89 || (exact == TRUE && strcmp(me->me_mountdir, d->name)==0))
90 {
91 best_match = me;
92 best_match_len = len;
93 } else {
94 len = strlen (me->me_devname);
95 if ((exact == FALSE && (best_match_len <= len && len <= name_len &&
96 (len == 1 || strncmp (me->me_devname, d->name, len) == 0)))
97 || (exact == TRUE && strcmp(me->me_devname, d->name)==0))
98 {
99 best_match = me;
100 best_match_len = len;
101 }
102 }
103 }
104 if (best_match) {
105 d->best_match = best_match;
106 } else {
107 d->best_match = NULL; /* Not sure why this is needed as it should be null on initialisation */
108 }
109 }
110}
111
112/* Returns TRUE if name is in list */
113int
114np_find_name (struct name_list *list, const char *name)
115{
116 const struct name_list *n;
117
118 if (list == NULL || name == NULL) {
119 return FALSE;
120 }
121 for (n = list; n; n = n->next) {
122 if (!strcmp(name, n->name)) {
123 return TRUE;
124 }
125 }
126 return FALSE;
127}
128
129int
130np_seen_name(struct name_list *list, const char *name)
131{
132 const struct name_list *s;
133 for (s = list; s; s=s->next) {
134 if (!strcmp(s->name, name)) {
135 return TRUE;
136 }
137 }
138 return FALSE;
139}
140
diff --git a/lib/utils_disk.h b/lib/utils_disk.h
new file mode 100644
index 0000000..928fdbe
--- /dev/null
+++ b/lib/utils_disk.h
@@ -0,0 +1,32 @@
1/* Header file for utils_disk */
2
3#include "mountlist.h"
4#include "utils_base.h"
5
6struct name_list
7{
8 char *name;
9 struct name_list *next;
10};
11
12struct parameter_list
13{
14 char *name;
15 int found;
16 thresholds *freespace_bytes;
17 thresholds *freespace_units;
18 thresholds *freespace_percent;
19 thresholds *usedspace_bytes;
20 thresholds *usedspace_units;
21 thresholds *usedspace_percent;
22 thresholds *usedinodes_percent;
23 struct mount_entry *best_match;
24 struct parameter_list *name_next;
25};
26
27void np_add_name (struct name_list **list, const char *name);
28int np_find_name (struct name_list *list, const char *name);
29int np_seen_name (struct name_list *list, const char *name);
30struct parameter_list *np_add_parameter(struct parameter_list **list, const char *name);
31int search_parameter_list (struct parameter_list *list, const char *name);
32void np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list, int exact);