summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--CHANGES2
-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
-rw-r--r--plugins/check_disk.c329
-rw-r--r--plugins/t/check_disk.t51
8 files changed, 649 insertions, 182 deletions
diff --git a/CHANGES b/CHANGES
index b58135b..f446565 100644
--- a/CHANGES
+++ b/CHANGES
@@ -5,6 +5,8 @@ This file documents the major additions and syntax changes between releases.
5 New check_apt plugin 5 New check_apt plugin
6 Notice: plugins in contrib/ will start to be removed from this distribution. 6 Notice: plugins in contrib/ will start to be removed from this distribution.
7 Please check at http://www.nagiosexchange.org for contributed plugins 7 Please check at http://www.nagiosexchange.org for contributed plugins
8 Major bug fixes to check_disk where values were incorrectly calculated and alerted on.
9 Perf data for warn/crit/max/min have been removed, to be readded soon.
8 10
91.4.3 111.4.3
10 Setuid plugins (check_dhcp, check_icmp) separated into plugins-root/. Run make install as root to install 12 Setuid plugins (check_dhcp, check_icmp) separated into plugins-root/. Run make install as root to install
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);
diff --git a/plugins/check_disk.c b/plugins/check_disk.c
index 6beaf86..b966fab 100644
--- a/plugins/check_disk.c
+++ b/plugins/check_disk.c
@@ -49,13 +49,14 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net";
49#include <stdarg.h> 49#include <stdarg.h>
50#include "fsusage.h" 50#include "fsusage.h"
51#include "mountlist.h" 51#include "mountlist.h"
52#include "intprops.h" /* necessary for TYPE_MAXIMUM */
52#if HAVE_LIMITS_H 53#if HAVE_LIMITS_H
53# include <limits.h> 54# include <limits.h>
54#endif 55#endif
55 56
56 57
57/* If nonzero, show inode information. */ 58/* If nonzero, show inode information. */
58static int inode_format; 59static int inode_format = 1;
59 60
60/* If nonzero, show even filesystems with zero size or 61/* If nonzero, show even filesystems with zero size or
61 uninteresting types. */ 62 uninteresting types. */
@@ -118,16 +119,11 @@ static struct mount_entry *mount_list;
118int process_arguments (int, char **); 119int process_arguments (int, char **);
119void print_path (const char *mypath); 120void print_path (const char *mypath);
120int validate_arguments (uintmax_t, uintmax_t, double, double, double, double, char *); 121int validate_arguments (uintmax_t, uintmax_t, double, double, double, double, char *);
121int check_disk (double usp, uintmax_t free_disk, double uisp);
122void print_help (void); 122void print_help (void);
123void print_usage (void); 123void print_usage (void);
124 124
125uintmax_t w_df = 0;
126uintmax_t c_df = 0;
127double w_dfp = -1.0; 125double w_dfp = -1.0;
128double c_dfp = -1.0; 126double c_dfp = -1.0;
129double w_idfp = -1.0;
130double c_idfp = -1.0;
131char *path; 127char *path;
132char *exclude_device; 128char *exclude_device;
133char *units; 129char *units;
@@ -136,20 +132,32 @@ int verbose = 0;
136int erronly = FALSE; 132int erronly = FALSE;
137int display_mntp = FALSE; 133int display_mntp = FALSE;
138int exact_match = FALSE; 134int exact_match = FALSE;
135char *warn_freespace_units = NULL;
136char *crit_freespace_units = NULL;
137char *warn_freespace_percent = NULL;
138char *crit_freespace_percent = NULL;
139char *warn_usedspace_units = NULL;
140char *crit_usedspace_units = NULL;
141char *warn_usedspace_percent = NULL;
142char *crit_usedspace_percent = NULL;
143char *warn_usedinodes_percent = NULL;
144char *crit_usedinodes_percent = NULL;
139 145
140 146
141int 147int
142main (int argc, char **argv) 148main (int argc, char **argv)
143{ 149{
144 double usp = -1.0, uisp = -1.0;
145 int result = STATE_UNKNOWN; 150 int result = STATE_UNKNOWN;
146 int disk_result = STATE_UNKNOWN; 151 int disk_result = STATE_UNKNOWN;
147 char file_system[MAX_INPUT_BUFFER];
148 char *output; 152 char *output;
149 char *details; 153 char *details;
150 char *perf; 154 char *perf;
151 uintmax_t psize; 155 float inode_space_pct;
152 float free_space, free_space_pct, total_space, inode_space_pct; 156 uintmax_t total, available, available_to_root, used;
157 double dfree_pct = -1, dused_pct = -1;
158 double dused_units, dfree_units, dtotal_units;
159 double dused_inodes_percent;
160 int temp_result;
153 161
154 struct mount_entry *me; 162 struct mount_entry *me;
155 struct fs_usage fsp; 163 struct fs_usage fsp;
@@ -175,13 +183,12 @@ main (int argc, char **argv)
175 if (! path_select_list) { 183 if (! path_select_list) {
176 for (me = mount_list; me; me = me->me_next) { 184 for (me = mount_list; me; me = me->me_next) {
177 path = np_add_parameter(&path_select_list, me->me_mountdir); 185 path = np_add_parameter(&path_select_list, me->me_mountdir);
178 path->w_df = w_df;
179 path->c_df = c_df;
180 path->w_dfp = w_dfp;
181 path->c_dfp = c_dfp;
182 path->w_idfp = w_idfp;
183 path->c_idfp = c_idfp;
184 path->best_match = me; 186 path->best_match = me;
187 set_thresholds(&path->freespace_units, warn_freespace_units, crit_freespace_units);
188 set_thresholds(&path->freespace_percent, warn_freespace_percent, crit_freespace_percent);
189 set_thresholds(&path->usedspace_units, warn_usedspace_units, crit_usedspace_units);
190 set_thresholds(&path->usedspace_percent, warn_usedspace_percent, crit_usedspace_percent);
191 set_thresholds(&path->usedinodes_percent, warn_usedinodes_percent, crit_usedinodes_percent);
185 } 192 }
186 } else { 193 } else {
187 np_set_best_match(path_select_list, mount_list, exact_match); 194 np_set_best_match(path_select_list, mount_list, exact_match);
@@ -199,12 +206,6 @@ main (int argc, char **argv)
199 /* Process for every path in list */ 206 /* Process for every path in list */
200 for (path = path_select_list; path; path=path->name_next) { 207 for (path = path_select_list; path; path=path->name_next) {
201 me = path->best_match; 208 me = path->best_match;
202 w_df = path->w_df;
203 c_df = path->c_df;
204 w_dfp = path->w_dfp;
205 c_dfp = path->c_dfp;
206 w_idfp = path->w_idfp;
207 c_idfp = path->c_idfp;
208 209
209 /* Filters */ 210 /* Filters */
210 211
@@ -233,13 +234,67 @@ main (int argc, char **argv)
233 get_fs_usage (me->me_mountdir, me->me_devname, &fsp); 234 get_fs_usage (me->me_mountdir, me->me_devname, &fsp);
234 235
235 if (fsp.fsu_blocks && strcmp ("none", me->me_mountdir)) { 236 if (fsp.fsu_blocks && strcmp ("none", me->me_mountdir)) {
236 usp = (double)(fsp.fsu_blocks - fsp.fsu_bavail) * 100 / fsp.fsu_blocks; 237 total = fsp.fsu_blocks;
237 uisp = (double)(fsp.fsu_files - fsp.fsu_ffree) * 100 / fsp.fsu_files; 238 available = fsp.fsu_bavail;
238 disk_result = check_disk (usp, fsp.fsu_bavail, uisp); 239 available_to_root = fsp.fsu_bfree;
240 used = total - available_to_root;
241
242 /* I don't understand the below, but it is taken from coreutils' df */
243 /* Is setting dused_pct, in the best possible way */
244 if (used <= TYPE_MAXIMUM(uintmax_t) / 100) {
245 uintmax_t u100 = used * 100;
246 uintmax_t nonroot_total = used + available;
247 dused_pct = u100 / nonroot_total + (u100 % nonroot_total != 0);
248 } else {
249 /* Possible rounding errors - see coreutils' df for more explanation */
250 double u = used;
251 double a = available;
252 double nonroot_total = u + a;
253 if (nonroot_total) {
254 long int lipct = dused_pct = u * 100 / nonroot_total;
255 double ipct = lipct;
256
257 /* Like 'pct = ceil (dpct);', but without ceil - from coreutils again */
258 if (ipct - 1 < dused_pct && dused_pct <= ipct + 1)
259 dused_pct = ipct + (ipct < dused_pct);
260 }
261 }
262
263 dfree_pct = 100 - dused_pct;
264 dused_units = used*fsp.fsu_blocksize/mult;
265 dfree_units = available*fsp.fsu_blocksize/mult;
266 dtotal_units = total*fsp.fsu_blocksize/mult;
267 dused_inodes_percent = (fsp.fsu_files - fsp.fsu_ffree) * 100 / fsp.fsu_files;
268
269 if (verbose >= 3) {
270 printf ("For %s, used_pct=%g free_pct=%g used_units=%g free_units=%g total_units=%g used_inodes_pct=%g\n",
271 me->me_mountdir, dused_pct, dfree_pct, dused_units, dfree_units, dtotal_units, dused_inodes_percent);
272 }
273
274 /* Threshold comparisons */
275
276 temp_result = get_status(dfree_units, path->freespace_units);
277 if (verbose >=3) printf("Freespace_units result=%d\n", temp_result);
278 result = max_state( result, temp_result );
239 279
280 temp_result = get_status(dfree_pct, path->freespace_percent);
281 if (verbose >=3) printf("Freespace%% result=%d\n", temp_result);
282 result = max_state( result, temp_result );
283
284 temp_result = get_status(dused_units, path->usedspace_units);
285 if (verbose >=3) printf("Usedspace_units result=%d\n", temp_result);
286 result = max_state( result, temp_result );
287
288 temp_result = get_status(dused_pct, path->usedspace_percent);
289 if (verbose >=3) printf("Usedspace_percent result=%d\n", temp_result);
290 result = max_state( result, temp_result );
291
292 temp_result = get_status(dused_inodes_percent, path->usedinodes_percent);
293 if (verbose >=3) printf("Usedinodes_percent result=%d\n", temp_result);
294 result = max_state( result, temp_result );
295
296
240 297
241 result = max_state (disk_result, result);
242 psize = fsp.fsu_blocks*fsp.fsu_blocksize/mult;
243 298
244 299
245 /* Moved this computation up here so we can add it 300 /* Moved this computation up here so we can add it
@@ -248,33 +303,33 @@ main (int argc, char **argv)
248 303
249 304
250 asprintf (&perf, "%s %s", perf, 305 asprintf (&perf, "%s %s", perf,
251 perfdata ((!strcmp(file_system, "none") || display_mntp) ? me->me_devname : me->me_mountdir, 306 perfdata ((!strcmp(me->me_mountdir, "none") || display_mntp) ? me->me_devname : me->me_mountdir,
252 psize-(fsp.fsu_bavail*fsp.fsu_blocksize/mult), units, 307 dused_units, units,
253 TRUE, min ((uintmax_t)psize-(uintmax_t)w_df, (uintmax_t)((1.0-w_dfp/100.0)*psize)), 308 FALSE, 0, /* min ((uintmax_t)dtotal_units-(uintmax_t)w_df, (uintmax_t)((1.0-w_dfp/100.0)*dtotal_units)), */
254 TRUE, min ((uintmax_t)psize-(uintmax_t)c_df, (uintmax_t)((1.0-c_dfp/100.0)*psize)), 309 FALSE, 0, /* min ((uintmax_t)dtotal_units-(uintmax_t)c_df, (uintmax_t)((1.0-c_dfp/100.0)*dtotal_units)), */
255 TRUE, inode_space_pct, 310 FALSE, 0, /* inode_space_pct, */
311 FALSE, 0));; /* dtotal_units)); */
256 312
257 TRUE, psize));
258 if (disk_result==STATE_OK && erronly && !verbose) 313 if (disk_result==STATE_OK && erronly && !verbose)
259 continue; 314 continue;
260 315
261 free_space = (float)fsp.fsu_bavail*fsp.fsu_blocksize/mult; 316 if (disk_result!=STATE_OK || verbose>=0) {
262 free_space_pct = (float)fsp.fsu_bavail*100/fsp.fsu_blocks;
263 total_space = (float)fsp.fsu_blocks*fsp.fsu_blocksize/mult;
264 if (disk_result!=STATE_OK || verbose>=0)
265 asprintf (&output, ("%s %s %.0f %s (%.0f%% inode=%.0f%%);"), 317 asprintf (&output, ("%s %s %.0f %s (%.0f%% inode=%.0f%%);"),
266 output, 318 output,
267 (!strcmp(file_system, "none") || display_mntp) ? me->me_devname : me->me_mountdir, 319 (!strcmp(me->me_mountdir, "none") || display_mntp) ? me->me_devname : me->me_mountdir,
268 free_space, 320 dfree_units,
269 units, 321 units,
270 free_space_pct, 322 dfree_pct,
271 inode_space_pct); 323 inode_space_pct);
324 }
272 325
326 /* Need to do a similar one
273 asprintf (&details, _("%s\n\ 327 asprintf (&details, _("%s\n\
274%.0f of %.0f %s (%.0f%% inode=%.0f%%) free on %s (type %s mounted on %s) warn:%lu crit:%lu warn%%:%.0f%% crit%%:%.0f%%"), 328%.0f of %.0f %s (%.0f%% inode=%.0f%%) free on %s (type %s mounted on %s) warn:%lu crit:%lu warn%%:%.0f%% crit%%:%.0f%%"),
275 details, free_space, total_space, units, free_space_pct, inode_space_pct, 329 details, dfree_units, dtotal_units, units, dfree_pct, inode_space_pct,
276 me->me_devname, me->me_type, me->me_mountdir, 330 me->me_devname, me->me_type, me->me_mountdir,
277 (unsigned long)w_df, (unsigned long)c_df, w_dfp, c_dfp); 331 (unsigned long)w_df, (unsigned long)c_df, w_dfp, c_dfp);
332 */
278 333
279 } 334 }
280 335
@@ -299,14 +354,6 @@ process_arguments (int argc, char **argv)
299 struct parameter_list *temp_list; 354 struct parameter_list *temp_list;
300 int result = OK; 355 int result = OK;
301 struct stat *stat_buf; 356 struct stat *stat_buf;
302 char *warn_freespace = NULL;
303 char *crit_freespace = NULL;
304 char *warn_freespace_percent = NULL;
305 char *crit_freespace_percent = NULL;
306 char temp_string[MAX_INPUT_BUFFER];
307
308 unsigned long l;
309 double f;
310 357
311 int option = 0; 358 int option = 0;
312 static struct option longopts[] = { 359 static struct option longopts[] = {
@@ -359,65 +406,51 @@ process_arguments (int argc, char **argv)
359 else { 406 else {
360 usage2 (_("Timeout interval must be a positive integer"), optarg); 407 usage2 (_("Timeout interval must be a positive integer"), optarg);
361 } 408 }
409
410 /* See comments for 'c' */
362 case 'w': /* warning threshold */ 411 case 'w': /* warning threshold */
363 /*
364 if (strstr(optarg, "%")) { 412 if (strstr(optarg, "%")) {
365 printf("Got percent with optarg=%s\n", optarg); 413 if (*optarg == '@') {
366 warn_freespace_percent = optarg; 414 warn_freespace_percent = optarg;
415 } else {
416 asprintf(&warn_freespace_percent, "@%s", optarg);
417 }
367 } else { 418 } else {
368 warn_freespace = optarg; 419 if (*optarg == '@') {
420 warn_freespace_units = optarg;
421 } else {
422 asprintf(&warn_freespace_units, "@%s", optarg);
423 }
369 } 424 }
370 break; 425 break;
371 */ 426
372 if (is_intnonneg (optarg)) { 427 /* Awful mistake where the range values do not make sense. Normally,
373 w_df = atoi (optarg); 428 you alert if the value is within the range, but since we are using
374 break; 429 freespace, we have to alert if outside the range. Thus we artifically
375 } 430 force @ at the beginning of the range, so that it is backwards compatible
376 else if (strpbrk (optarg, ",:") && 431 */
377 strstr (optarg, "%") &&
378 sscanf (optarg, "%lu%*[:,]%lf%%", &l, &w_dfp) == 2) {
379 w_df = (uintmax_t)l;
380 break;
381 }
382 else if (strstr (optarg, "%") && sscanf (optarg, "%lf%%", &w_dfp) == 1) {
383 break;
384 }
385 else {
386 usage4 (_("Warning threshold must be integer or percentage!"));
387 }
388 case 'c': /* critical threshold */ 432 case 'c': /* critical threshold */
389 if (is_intnonneg (optarg)) { 433 if (strstr(optarg, "%")) {
390 c_df = atoi (optarg); 434 if (*optarg == '@') {
391 break; 435 crit_freespace_percent = optarg;
392 } 436 } else {
393 else if (strpbrk (optarg, ",:") && 437 asprintf(&crit_freespace_percent, "@%s", optarg);
394 strstr (optarg, "%") && 438 }
395 sscanf (optarg, "%lu%*[,:]%lf%%", &l, &c_dfp) == 2) { 439 } else {
396 c_df = (uintmax_t)l; 440 if (*optarg == '@') {
397 break; 441 crit_freespace_units = optarg;
398 } 442 } else {
399 else if (strstr (optarg, "%") && sscanf (optarg, "%lf%%", &c_dfp) == 1) { 443 asprintf(&crit_freespace_units, "@%s", optarg);
400 break; 444 }
401 }
402 else {
403 usage4 (_("Critical threshold must be integer or percentage!"));
404 } 445 }
446 break;
405 447
406 448 case 'W': /* warning inode threshold */
407 case 'W': /* warning inode threshold */ 449 warn_usedinodes_percent = optarg;
408 if (strstr (optarg, "%") && sscanf (optarg, "%lf%%", &w_idfp) == 1) { 450 break;
409 break; 451 case 'K': /* critical inode threshold */
410 } 452 crit_usedinodes_percent = optarg;
411 else { 453 break;
412 usage (_("Warning inode threshold must be percentage!\n"));
413 }
414 case 'K': /* kritical inode threshold */
415 if (strstr (optarg, "%") && sscanf (optarg, "%lf%%", &c_idfp) == 1) {
416 break;
417 }
418 else {
419 usage (_("Critical inode threshold must be percentage!\n"));
420 }
421 case 'u': 454 case 'u':
422 if (units) 455 if (units)
423 free(units); 456 free(units);
@@ -458,13 +491,18 @@ process_arguments (int argc, char **argv)
458 show_local_fs = 1; 491 show_local_fs = 1;
459 break; 492 break;
460 case 'p': /* select path */ 493 case 'p': /* select path */
494 if (! (warn_freespace_units || crit_freespace_units || warn_freespace_percent ||
495 crit_freespace_percent || warn_usedspace_units || crit_usedspace_units ||
496 warn_usedspace_percent || crit_usedspace_percent || warn_usedinodes_percent ||
497 crit_usedinodes_percent)) {
498 die (STATE_UNKNOWN, "DISK %s: %s", _("UNKNOWN"), _("Must set a threshold value before using -p\n"));
499 }
461 se = np_add_parameter(&path_select_list, optarg); 500 se = np_add_parameter(&path_select_list, optarg);
462 se->w_df = w_df; 501 set_thresholds(&se->freespace_units, warn_freespace_units, crit_freespace_units);
463 se->c_df = c_df; 502 set_thresholds(&se->freespace_percent, warn_freespace_percent, crit_freespace_percent);
464 se->w_dfp = w_dfp; 503 set_thresholds(&se->usedspace_units, warn_usedspace_units, crit_usedspace_units);
465 se->c_dfp = c_dfp; 504 set_thresholds(&se->usedspace_percent, warn_usedspace_percent, crit_usedspace_percent);
466 se->w_idfp = w_idfp; 505 set_thresholds(&se->usedinodes_percent, warn_usedinodes_percent, crit_usedinodes_percent);
467 se->c_idfp = c_idfp;
468 break; 506 break;
469 case 'x': /* exclude path or partition */ 507 case 'x': /* exclude path or partition */
470 np_add_name(&dp_exclude_list, optarg); 508 np_add_name(&dp_exclude_list, optarg);
@@ -488,12 +526,16 @@ process_arguments (int argc, char **argv)
488 display_mntp = TRUE; 526 display_mntp = TRUE;
489 break; 527 break;
490 case 'C': 528 case 'C':
491 w_df = 0; 529 warn_freespace_units = NULL;
492 c_df = 0; 530 crit_freespace_units = NULL;
493 w_dfp = -1.0; 531 warn_usedspace_units = NULL;
494 c_dfp = -1.0; 532 crit_usedspace_units = NULL;
495 w_idfp = -1.0; 533 warn_freespace_percent = NULL;
496 c_idfp = -1.0; 534 crit_freespace_percent = NULL;
535 warn_usedspace_percent = NULL;
536 crit_usedspace_percent = NULL;
537 warn_usedinodes_percent = NULL;
538 crit_usedinodes_percent = NULL;
497 break; 539 break;
498 case 'V': /* version */ 540 case 'V': /* version */
499 print_revision (progname, revision); 541 print_revision (progname, revision);
@@ -506,22 +548,26 @@ process_arguments (int argc, char **argv)
506 } 548 }
507 } 549 }
508 550
509 /* Support for "check_disk warn crit [fs]" with thresholds at used level */ 551 /* Support for "check_disk warn crit [fs]" with thresholds at used% level */
510 c = optind; 552 c = optind;
511 if (w_dfp < 0 && argc > c && is_intnonneg (argv[c])) 553 if (warn_usedspace_percent == NULL && argc > c && is_intnonneg (argv[c]))
512 w_dfp = (100.0 - atof (argv[c++])); 554 warn_usedspace_percent = argv[c++];
513 555
514 if (c_dfp < 0 && argc > c && is_intnonneg (argv[c])) 556 if (crit_usedspace_percent == NULL && argc > c && is_intnonneg (argv[c]))
515 c_dfp = (100.0 - atof (argv[c++])); 557 crit_usedspace_percent = argv[c++];
516 558
517 if (argc > c && path == NULL) { 559 if (argc > c && path == NULL) {
518 se = np_add_parameter(&path_select_list, strdup(argv[c++])); 560 se = np_add_parameter(&path_select_list, strdup(argv[c++]));
519 se->w_df = w_df; 561 set_thresholds(&se->freespace_units, warn_freespace_units, crit_freespace_units);
520 se->c_df = c_df; 562 set_thresholds(&se->freespace_percent, warn_freespace_percent, crit_freespace_percent);
521 se->w_dfp = w_dfp; 563 set_thresholds(&se->usedspace_units, warn_usedspace_units, crit_usedspace_units);
522 se->c_dfp = c_dfp; 564 set_thresholds(&se->usedspace_percent, warn_usedspace_percent, crit_usedspace_percent);
523 se->w_idfp = w_idfp; 565 set_thresholds(&se->usedinodes_percent, warn_usedinodes_percent, crit_usedinodes_percent);
524 se->c_idfp = c_idfp; 566 }
567
568 if (units == NULL) {
569 units = strdup ("MB");
570 mult = (uintmax_t)1024 * 1024;
525 } 571 }
526 572
527 if (path_select_list) { 573 if (path_select_list) {
@@ -533,7 +579,7 @@ process_arguments (int argc, char **argv)
533 printf("DISK %s - ", _("CRITICAL")); 579 printf("DISK %s - ", _("CRITICAL"));
534 die (STATE_CRITICAL, _("%s does not exist\n"), temp_list->name); 580 die (STATE_CRITICAL, _("%s does not exist\n"), temp_list->name);
535 } 581 }
536 if (validate_arguments (temp_list->w_df, 582 /* if (validate_arguments (temp_list->w_df,
537 temp_list->c_df, 583 temp_list->c_df,
538 temp_list->w_dfp, 584 temp_list->w_dfp,
539 temp_list->c_dfp, 585 temp_list->c_dfp,
@@ -541,12 +587,14 @@ process_arguments (int argc, char **argv)
541 temp_list->c_idfp, 587 temp_list->c_idfp,
542 temp_list->name) == ERROR) 588 temp_list->name) == ERROR)
543 result = ERROR; 589 result = ERROR;
590 */
544 temp_list = temp_list->name_next; 591 temp_list = temp_list->name_next;
545 } 592 }
546 free(stat_buf); 593 free(stat_buf);
547 return result; 594 return result;
548 } else { 595 } else {
549 return validate_arguments (w_df, c_df, w_dfp, c_dfp, w_idfp, c_idfp, NULL); 596 return TRUE;
597 /* return validate_arguments (w_df, c_df, w_dfp, c_dfp, w_idfp, c_idfp, NULL); */
550 } 598 }
551} 599}
552 600
@@ -560,11 +608,13 @@ print_path (const char *mypath)
560 else 608 else
561 printf (_(" for %s\n"), mypath); 609 printf (_(" for %s\n"), mypath);
562 610
563 return; 611 //return;
564} 612}
565 613
566 614
567 615
616/* TODO: Remove?
617
568int 618int
569validate_arguments (uintmax_t w, uintmax_t c, double wp, double cp, double iwp, double icp, char *mypath) 619validate_arguments (uintmax_t w, uintmax_t c, double wp, double cp, double iwp, double icp, char *mypath)
570{ 620{
@@ -597,37 +647,12 @@ INPUT ERROR: C_DF (%lu) should be less than W_DF (%lu) and both should be greate
597 return ERROR; 647 return ERROR;
598 } 648 }
599 649
600 if (units == NULL) {
601 units = strdup ("MB");
602 mult = (uintmax_t)1024 * 1024;
603 }
604 return OK; 650 return OK;
605} 651}
606 652
653*/
607 654
608 655
609int
610check_disk (double usp, uintmax_t free_disk, double uisp)
611{
612 int result = STATE_UNKNOWN;
613 /* check the percent used space against thresholds */
614 if (usp >= 0.0 && c_dfp >=0.0 && usp >= (100.0 - c_dfp))
615 result = STATE_CRITICAL;
616 else if (uisp >= 0.0 && c_idfp >=0.0 && uisp >= (100.0 - c_idfp))
617 result = STATE_CRITICAL;
618 else if (c_df > 0 && free_disk <= c_df)
619 result = STATE_CRITICAL;
620 else if (usp >= 0.0 && w_dfp >=0.0 && usp >= (100.0 - w_dfp))
621 result = STATE_WARNING;
622 else if (uisp >= 0.0 && w_idfp >=0.0 && uisp >= (100.0 - w_idfp))
623 result = STATE_WARNING;
624 else if (w_df > 0 && free_disk <= w_df)
625 result = STATE_WARNING;
626 else if (usp >= 0.0)
627 result = STATE_OK;
628 return result;
629}
630
631 656
632 657
633 658
diff --git a/plugins/t/check_disk.t b/plugins/t/check_disk.t
index 70d8415..6634492 100644
--- a/plugins/t/check_disk.t
+++ b/plugins/t/check_disk.t
@@ -52,8 +52,8 @@ if ($free_on_mp1 > $free_on_mp2) {
52} 52}
53 53
54 54
55$result = NPTest->testCmd( "./check_disk -w 100 -c 100 -p $more_free" ); 55$result = NPTest->testCmd( "./check_disk -w 1 -c 1 -p $more_free" );
56cmp_ok( $result->return_code, '==', 0, "At least 100 bytes available on $more_free"); 56cmp_ok( $result->return_code, '==', 0, "At least 1 MB available on $more_free");
57like ( $result->output, $successOutput, "OK output" ); 57like ( $result->output, $successOutput, "OK output" );
58 58
59$result = NPTest->testCmd( "./check_disk 100 100 $more_free" ); 59$result = NPTest->testCmd( "./check_disk 100 100 $more_free" );
@@ -110,35 +110,28 @@ cmp_ok( $result->return_code, '==', 2, "And reversing arguments should not make
110 110
111 111
112 112
113
114$result = NPTest->testCmd(
115 "./check_disk -w 10% -c 15% -p $mountpoint_valid"
116 );
117cmp_ok( $result->return_code, '==', 3, "Invalid command line options" );
118
119TODO: { 113TODO: {
120 local $TODO = "-p must come after -w and -c"; 114 local $TODO = "Invalid percent figures";
121 $result = NPTest->testCmd( 115 $result = NPTest->testCmd(
116 "./check_disk -w 10% -c 15% -p $mountpoint_valid"
117 );
118 cmp_ok( $result->return_code, '==', 3, "Invalid command line options" );
119}
120
121$result = NPTest->testCmd(
122 "./check_disk -p $mountpoint_valid -w 10% -c 15%" 122 "./check_disk -p $mountpoint_valid -w 10% -c 15%"
123 ); 123 );
124 cmp_ok( $result->return_code, "==", 3, "Invalid options - order unimportant" ); 124cmp_ok( $result->return_code, "==", 3, "Invalid options: -p must come after thresholds" );
125}
126 125
127$result = NPTest->testCmd( "./check_disk -w 100% -c 100% ".${mountpoint_valid} ); # 100% empty 126$result = NPTest->testCmd( "./check_disk -w 100% -c 100% ".${mountpoint_valid} ); # 100% empty
128cmp_ok( $result->return_code, "==", 2, "100% empty" ); 127cmp_ok( $result->return_code, "==", 2, "100% empty" );
129like( $result->output, $failureOutput, "Right output" ); 128like( $result->output, $failureOutput, "Right output" );
130 129
131TODO: { 130$result = NPTest->testCmd( "./check_disk -w 100000 -c 100000 $mountpoint_valid" );
132 local $TODO = "Requesting 100GB free is should be critical"; 131cmp_ok( $result->return_code, '==', 2, "Check for 100GB free" );
133 $result = NPTest->testCmd( "./check_disk -w 100000 -c 100000 $mountpoint_valid" );
134 cmp_ok( $result->return_code, '==', 2, "Check for 100GB free" );
135}
136 132
137TODO: { 133$result = NPTest->testCmd( "./check_disk -w 100 -c 100 -u GB ".${mountpoint_valid} ); # 100 GB empty
138 local $TODO = "-u GB does not work"; 134cmp_ok( $result->return_code, "==", 2, "100 GB empty" );
139 $result = NPTest->testCmd( "./check_disk -w 100 -c 100 -u GB ".${mountpoint_valid} ); # 100 GB empty
140 cmp_ok( $result->return_code, "==", 2, "100 GB empty" );
141}
142 135
143 136
144# Checking old syntax of check_disk warn crit [fs], with warn/crit at USED% thresholds 137# Checking old syntax of check_disk warn crit [fs], with warn/crit at USED% thresholds
@@ -151,17 +144,17 @@ cmp_ok( $result->return_code, '==', 0, "Old syntax: 100% used" );
151$result = NPTest->testCmd( "./check_disk 0 100 $mountpoint_valid" ); 144$result = NPTest->testCmd( "./check_disk 0 100 $mountpoint_valid" );
152cmp_ok( $result->return_code, '==', 1, "Old syntax: warn 0% used" ); 145cmp_ok( $result->return_code, '==', 1, "Old syntax: warn 0% used" );
153 146
154$result = NPTest->testCmd( "./check_disk 0 200 $mountpoint_valid" );
155cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
156
157TODO: { 147TODO: {
158 local $TODO = "Need to properly check input"; 148 local $TODO = "Invalid values";
149 $result = NPTest->testCmd( "./check_disk 0 200 $mountpoint_valid" );
150 cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
151
159 $result = NPTest->testCmd( "./check_disk 200 200 $mountpoint_valid" ); 152 $result = NPTest->testCmd( "./check_disk 200 200 $mountpoint_valid" );
160 cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" ); 153 cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
161}
162 154
163$result = NPTest->testCmd( "./check_disk 200 0 $mountpoint_valid" ); 155 $result = NPTest->testCmd( "./check_disk 200 0 $mountpoint_valid" );
164cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" ); 156 cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
157}
165 158
166$result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /bob" ); 159$result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /bob" );
167cmp_ok( $result->return_code, '==', 2, "Checking /bob - return error because /bob does not exist" ); 160cmp_ok( $result->return_code, '==', 2, "Checking /bob - return error because /bob does not exist" );