diff options
author | Holger Weiss <holger@zedat.fu-berlin.de> | 2013-09-30 00:03:24 +0200 |
---|---|---|
committer | Holger Weiss <holger@zedat.fu-berlin.de> | 2013-09-30 00:03:24 +0200 |
commit | 0b6423f9c99d9edf8c96fefd0f6c453859395aa1 (patch) | |
tree | 1c2b6b21704a294940f87c7892676998d8371707 /web/attachments/178818-check_disk.c | |
download | site-0b6423f9c99d9edf8c96fefd0f6c453859395aa1.tar.gz |
Import Nagios Plugins site
Import the Nagios Plugins web site, Cronjobs, infrastructure scripts,
and configuration files.
Diffstat (limited to 'web/attachments/178818-check_disk.c')
-rw-r--r-- | web/attachments/178818-check_disk.c | 916 |
1 files changed, 916 insertions, 0 deletions
diff --git a/web/attachments/178818-check_disk.c b/web/attachments/178818-check_disk.c new file mode 100644 index 0000000..bad05fb --- /dev/null +++ b/web/attachments/178818-check_disk.c | |||
@@ -0,0 +1,916 @@ | |||
1 | /****************************************************************************** | ||
2 | |||
3 | This program is free software; you can redistribute it and/or modify | ||
4 | it under the terms of the GNU General Public License as published by | ||
5 | the Free Software Foundation; either version 2 of the License, or | ||
6 | (at your option) any later version. | ||
7 | |||
8 | This program is distributed in the hope that it will be useful, | ||
9 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
11 | GNU General Public License for more details. | ||
12 | |||
13 | You should have received a copy of the GNU General Public License | ||
14 | along with this program; if not, write to the Free Software | ||
15 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
16 | |||
17 | $Id: check_disk.c,v 1.65.4m 2006/05/20 21:34:01 maemigh Exp $ | ||
18 | |||
19 | *****************************************************************************/ | ||
20 | |||
21 | const char *progname = "check_disk"; | ||
22 | const char *program_name = "check_disk"; /* Required for coreutils libs */ | ||
23 | const char *revision = "$Revision: 1.65.4 maemigh $"; | ||
24 | const char *copyright = "1999-2005"; | ||
25 | const char *email = "nagiosplug-devel@lists.sourceforge.net"; | ||
26 | |||
27 | /* | ||
28 | * Additional inode code by Jorgen Lundman <lundman@lundman.net> | ||
29 | */ | ||
30 | |||
31 | |||
32 | #include "common.h" | ||
33 | #if HAVE_INTTYPES_H | ||
34 | # include <inttypes.h> | ||
35 | #endif | ||
36 | #include <assert.h> | ||
37 | #include "popen.h" | ||
38 | #include "utils.h" | ||
39 | #include <stdarg.h> | ||
40 | #include "../lib/fsusage.h" | ||
41 | #include "../lib/mountlist.h" | ||
42 | #if HAVE_LIMITS_H | ||
43 | # include <limits.h> | ||
44 | #endif | ||
45 | |||
46 | /* If nonzero, show even filesystems with zero size or | ||
47 | uninteresting types. */ | ||
48 | static int show_all_fs = 1; | ||
49 | |||
50 | /* If nonzero, show only local filesystems. */ | ||
51 | static int show_local_fs = 0; | ||
52 | |||
53 | /* If nonzero, show output in HTML format. */ | ||
54 | static int show_html = 0; | ||
55 | |||
56 | /* If nonzero, output percent of space and inodes USED rather than percent free */ | ||
57 | static int show_used = 0; | ||
58 | |||
59 | /* If zero, display all mounts even with -p specified */ | ||
60 | static int path_select_exclude_others = 1; | ||
61 | |||
62 | /* If positive, the units to use when printing sizes; | ||
63 | if negative, the human-readable base. */ | ||
64 | /* static int output_block_size; */ | ||
65 | |||
66 | /* If nonzero, invoke the `sync' system call before getting any usage data. | ||
67 | Using this option can make df very slow, especially with many or very | ||
68 | busy disks. Note that this may make a difference on some systems -- | ||
69 | SunOs4.1.3, for one. It is *not* necessary on Linux. */ | ||
70 | /* static int require_sync = 0; */ | ||
71 | |||
72 | /* A filesystem type to display. */ | ||
73 | |||
74 | struct name_list | ||
75 | { | ||
76 | char *name; | ||
77 | int exclude; | ||
78 | int found; | ||
79 | int foundexact; | ||
80 | int found_len; | ||
81 | uintmax_t w_df; | ||
82 | uintmax_t c_df; | ||
83 | double w_dfp; | ||
84 | double c_dfp; | ||
85 | double w_idfp; | ||
86 | double c_idfp; | ||
87 | struct name_list *name_next; | ||
88 | }; | ||
89 | |||
90 | /* Linked list of filesystem types to display. | ||
91 | If `fs_select_list' is NULL, list all types. | ||
92 | This table is generated dynamically from command-line options, | ||
93 | rather than hardcoding into the program what it thinks are the | ||
94 | valid filesystem types; let the user specify any filesystem type | ||
95 | they want to, and if there are any filesystems of that type, they | ||
96 | will be shown. | ||
97 | |||
98 | Some filesystem types: | ||
99 | 4.2 4.3 ufs nfs swap ignore io vm efs dbg */ | ||
100 | |||
101 | /* static struct name_list *fs_select_list; */ | ||
102 | |||
103 | /* Linked list of filesystem types to omit. | ||
104 | If the list is empty, don't exclude any types. */ | ||
105 | |||
106 | static struct name_list *fs_exclude_list; | ||
107 | |||
108 | static struct name_list *dp_exclude_list; | ||
109 | |||
110 | static struct name_list *path_select_list; | ||
111 | |||
112 | static struct name_list *dev_select_list; | ||
113 | |||
114 | /* Linked list of mounted filesystems. */ | ||
115 | static struct mount_entry *mount_list; | ||
116 | |||
117 | /* For long options that have no equivalent short option, use a | ||
118 | non-character as a pseudo short option, starting with CHAR_MAX + 1. */ | ||
119 | enum | ||
120 | { | ||
121 | SYNC_OPTION = CHAR_MAX + 1, | ||
122 | NO_SYNC_OPTION, | ||
123 | BLOCK_SIZE_OPTION | ||
124 | }; | ||
125 | |||
126 | #ifdef _AIX | ||
127 | #pragma alloca | ||
128 | #endif | ||
129 | |||
130 | int process_arguments (int, char **); | ||
131 | void print_path (const char *mypath); | ||
132 | int validate_arguments (uintmax_t, uintmax_t, double, double, double, double, char *); | ||
133 | int check_disk (double usp, uintmax_t free_disk, double uisp); | ||
134 | int walk_name_list (struct name_list *list, const char *name); | ||
135 | void print_help (void); | ||
136 | void print_usage (void); | ||
137 | |||
138 | uintmax_t w_df = 0; | ||
139 | uintmax_t c_df = 0; | ||
140 | double w_dfp = -1.0; | ||
141 | double c_dfp = -1.0; | ||
142 | double w_idfp = -1.0; | ||
143 | double c_idfp = -1.0; | ||
144 | uintmax_t dw_df = 0; | ||
145 | uintmax_t dc_df = 0; | ||
146 | double dw_dfp = -1.0; | ||
147 | double dc_dfp = -1.0; | ||
148 | double dw_idfp = -1.0; | ||
149 | double dc_idfp = -1.0; | ||
150 | |||
151 | char *path; | ||
152 | char *exclude_device; | ||
153 | char *units; | ||
154 | uintmax_t mult = 1024 * 1024; | ||
155 | int verbose = 0; | ||
156 | int erronly = FALSE; | ||
157 | int display_mntp = FALSE; | ||
158 | int longest_length = 0; | ||
159 | /* Linked list of mounted filesystems. */ | ||
160 | static struct mount_entry *mount_list; | ||
161 | |||
162 | |||
163 | |||
164 | int | ||
165 | main (int argc, char **argv) | ||
166 | { | ||
167 | double usp = -1.0, uisp = -1.0; | ||
168 | int result = STATE_UNKNOWN; | ||
169 | int disk_result = STATE_UNKNOWN; | ||
170 | char file_system[MAX_INPUT_BUFFER]; | ||
171 | char *output; | ||
172 | char *details; | ||
173 | char *perf; | ||
174 | uintmax_t psize; | ||
175 | float free_space, free_space_pct, total_space, inode_space_pct; | ||
176 | |||
177 | struct mount_entry *me; | ||
178 | struct fs_usage fsp; | ||
179 | struct name_list *temp_list; | ||
180 | |||
181 | output = strdup (" - free space:"); | ||
182 | details = strdup (""); | ||
183 | perf = strdup (""); | ||
184 | |||
185 | setlocale (LC_ALL, ""); | ||
186 | bindtextdomain (PACKAGE, LOCALEDIR); | ||
187 | textdomain (PACKAGE); | ||
188 | |||
189 | mount_list = read_filesystem_list (0); | ||
190 | |||
191 | if (process_arguments (argc, argv) == ERROR) | ||
192 | usage4 (_("Could not parse arguments")); | ||
193 | |||
194 | /* if a list of paths has been selected, preseed the list with | ||
195 | * the longest matching filesystem name by iterating across | ||
196 | * the mountlist once ahead of time. this will allow a query on | ||
197 | * "/var/log" to return information about "/var" if no "/var/log" | ||
198 | * filesystem exists, etc. this is the default behavior already | ||
199 | * with df-based checks, but for systems with their own space | ||
200 | * checking routines, this should make them more consistent. | ||
201 | * | ||
202 | * Also keep track of the longest path that will be displayed | ||
203 | * in order to format in HTML. | ||
204 | */ | ||
205 | |||
206 | /* The walk_name_list function will keep track of longest_length */ | ||
207 | if(path_select_list){ | ||
208 | for (me = mount_list; me; me = me->me_next) { | ||
209 | walk_name_list(path_select_list, me->me_devname); | ||
210 | walk_name_list(path_select_list, me->me_mountdir); | ||
211 | } | ||
212 | } | ||
213 | /* Longest length is only kept track of when the list passed is NOT | ||
214 | * and exclude list. Thus we must verify the longest path that | ||
215 | * will be displayed in the output. | ||
216 | */ | ||
217 | else { | ||
218 | for (me = mount_list; me; me = me->me_next) { | ||
219 | if(!walk_name_list(fs_exclude_list, me->me_type) && | ||
220 | !walk_name_list(dp_exclude_list, me->me_devname) && | ||
221 | !walk_name_list(dp_exclude_list, me->me_mountdir)) { | ||
222 | get_fs_usage (me->me_mountdir, me->me_devname, &fsp); | ||
223 | if (fsp.fsu_blocks && strcmp ("none", me->me_mountdir)) { | ||
224 | if (!strcmp(file_system, "none") || display_mntp) { | ||
225 | if(strlen(me->me_devname) > longest_length) longest_length = strlen(me->me_devname); | ||
226 | } else { | ||
227 | if(strlen(me->me_mountdir) > longest_length) longest_length = strlen(me->me_mountdir); | ||
228 | } | ||
229 | } | ||
230 | } | ||
231 | } | ||
232 | } | ||
233 | /* now pretend we never saw anything, but keep found_len. | ||
234 | * thus future searches will only match the best match */ | ||
235 | for (temp_list = path_select_list; temp_list; temp_list=temp_list->name_next){ | ||
236 | temp_list->found=0; | ||
237 | } | ||
238 | |||
239 | /* for every mount entry */ | ||
240 | for (me = mount_list; me; me = me->me_next) { | ||
241 | /* if there's a list of paths to select, the current mount | ||
242 | * entry matches in path or device name, get fs usage */ | ||
243 | if (path_select_list && | ||
244 | (walk_name_list(path_select_list, me->me_devname) || | ||
245 | walk_name_list(path_select_list, me->me_mountdir))) { | ||
246 | get_fs_usage (me->me_mountdir, me->me_devname, &fsp); | ||
247 | /* else if there's a list of paths/devices to select (but | ||
248 | * we didn't match above) skip to the next mount entry */ | ||
249 | } else if (dev_select_list || (path_select_list && path_select_exclude_others == 1)) { | ||
250 | continue; | ||
251 | /* skip remote filesystems if we're not interested in them */ | ||
252 | } else if (me->me_remote && show_local_fs) { | ||
253 | continue; | ||
254 | /* skip pseudo fs's if we haven't asked for all fs's */ | ||
255 | } else if (me->me_dummy && !show_all_fs) { | ||
256 | continue; | ||
257 | /* skip excluded fstypes */ | ||
258 | } else if (fs_exclude_list && walk_name_list (fs_exclude_list, me->me_type)) { | ||
259 | continue; | ||
260 | /* skip excluded fs's */ | ||
261 | } else if (dp_exclude_list && | ||
262 | (walk_name_list (dp_exclude_list, me->me_devname) || | ||
263 | walk_name_list (dp_exclude_list, me->me_mountdir))) { | ||
264 | continue; | ||
265 | /* otherwise, get fs usage */ | ||
266 | } else { | ||
267 | /* Reset thresholds to defaults */ | ||
268 | w_df = dw_df; | ||
269 | c_df = dc_df; | ||
270 | w_dfp = dw_dfp; | ||
271 | c_dfp = dc_dfp; | ||
272 | w_idfp = dw_idfp; | ||
273 | c_idfp = dc_idfp; | ||
274 | get_fs_usage (me->me_mountdir, me->me_devname, &fsp); | ||
275 | } | ||
276 | if (fsp.fsu_blocks && strcmp ("none", me->me_mountdir)) { | ||
277 | usp = (double)(fsp.fsu_blocks - fsp.fsu_bavail) * 100 / fsp.fsu_blocks; | ||
278 | uisp = (double)(fsp.fsu_files - fsp.fsu_ffree) * 100 / fsp.fsu_files; | ||
279 | disk_result = check_disk (usp, fsp.fsu_bavail, uisp); | ||
280 | |||
281 | |||
282 | result = max_state (disk_result, result); | ||
283 | psize = fsp.fsu_blocks*fsp.fsu_blocksize/mult; | ||
284 | |||
285 | |||
286 | /* Moved this computation up here so we can add it | ||
287 | * to perf */ | ||
288 | inode_space_pct = (float)fsp.fsu_ffree*100/fsp.fsu_files; | ||
289 | |||
290 | asprintf (&perf, "%s %s", perf, | ||
291 | perfdata ((!strcmp(file_system, "none") || display_mntp) ? me->me_devname : me->me_mountdir, | ||
292 | psize-(fsp.fsu_bavail*fsp.fsu_blocksize/mult), units, | ||
293 | TRUE, min ((uintmax_t)psize-(uintmax_t)w_df, (uintmax_t)((1.0-w_dfp/100.0)*psize)), | ||
294 | TRUE, min ((uintmax_t)psize-(uintmax_t)c_df, (uintmax_t)((1.0-c_dfp/100.0)*psize)), | ||
295 | TRUE, inode_space_pct, | ||
296 | |||
297 | TRUE, psize)); | ||
298 | if (disk_result==STATE_OK && erronly && !verbose) | ||
299 | continue; | ||
300 | |||
301 | free_space = (float)fsp.fsu_bavail*fsp.fsu_blocksize/mult; | ||
302 | free_space_pct = (float)fsp.fsu_bavail*100/fsp.fsu_blocks; | ||
303 | total_space = (float)fsp.fsu_blocks*fsp.fsu_blocksize/mult; | ||
304 | if (disk_result!=STATE_OK || verbose>=0) { | ||
305 | if (show_html == 1) { | ||
306 | int x = 0; | ||
307 | int delta_len = (longest_length - strlen((!strcmp(file_system, "none") || display_mntp) ? me->me_devname : me->me_mountdir)); | ||
308 | for(x=delta_len; x >= 0; x--) { | ||
309 | if((!strcmp(file_system, "none") || display_mntp)) strcat(me->me_devname, " "); | ||
310 | else strcat(me->me_mountdir, " "); | ||
311 | } | ||
312 | asprintf (&output, ("%s <br/>%s%s %6.0f %s (%3.0f%% inode=%3.0f%%);%s"), | ||
313 | output, | ||
314 | (disk_result!=STATE_OK) ? "*" : " ", | ||
315 | (!strcmp(file_system, "none") || display_mntp) ? me->me_devname : me->me_mountdir, | ||
316 | free_space, | ||
317 | units, | ||
318 | (show_used == 1) ? 100-free_space_pct : free_space_pct, | ||
319 | (show_used == 1) ? 100-inode_space_pct : inode_space_pct, | ||
320 | (disk_result!=STATE_OK) ? "*" : " "); | ||
321 | } else { | ||
322 | asprintf (&output, ("%s %s %.0f %s (%.0f%% inode=%.0f%%);"), | ||
323 | output, | ||
324 | (!strcmp(file_system, "none") || display_mntp) ? me->me_devname : me->me_mountdir, | ||
325 | free_space, | ||
326 | units, | ||
327 | (show_used == 1) ? 100-free_space_pct : free_space_pct, | ||
328 | (show_used == 1) ? 100-inode_space_pct : inode_space_pct); | ||
329 | } | ||
330 | } | ||
331 | asprintf (&details, _("%s\n\ | ||
332 | %.0f of %.0f %s (%.0f%% inode=%.0f%%) free on %s (type %s mounted on %s) warn:%lu crit:%lu warn%%:%.0f%% crit%%:%.0f%%"), | ||
333 | details, free_space, total_space, units, free_space_pct, inode_space_pct, | ||
334 | me->me_devname, me->me_type, me->me_mountdir, | ||
335 | (unsigned long)w_df, (unsigned long)c_df, w_dfp, c_dfp); | ||
336 | |||
337 | } | ||
338 | |||
339 | } | ||
340 | |||
341 | if (verbose > 2) | ||
342 | asprintf (&output, "%s%s", output, details); | ||
343 | |||
344 | /* Override result if paths specified and not found */ | ||
345 | temp_list = path_select_list; | ||
346 | while (temp_list) { | ||
347 | if (!temp_list->found) { | ||
348 | asprintf (&output, _("%s [%s not found]"), output, temp_list->name); | ||
349 | result = STATE_CRITICAL; | ||
350 | } | ||
351 | temp_list = temp_list->name_next; | ||
352 | } | ||
353 | if (show_html == 1) | ||
354 | printf ("<pre>DISK %s%s|%s\n", state_text (result), output, perf); | ||
355 | else | ||
356 | printf ("DISK %s%s|%s\n", state_text (result), output, perf); | ||
357 | return result; | ||
358 | } | ||
359 | |||
360 | |||
361 | |||
362 | /* process command-line arguments */ | ||
363 | int | ||
364 | process_arguments (int argc, char **argv) | ||
365 | { | ||
366 | int c; | ||
367 | struct name_list *se; | ||
368 | struct name_list **pathtail = &path_select_list; | ||
369 | struct name_list **fstail = &fs_exclude_list; | ||
370 | struct name_list **dptail = &dp_exclude_list; | ||
371 | struct name_list *temp_list; | ||
372 | int result = OK; | ||
373 | struct stat *stat_buf; | ||
374 | |||
375 | unsigned long l; | ||
376 | |||
377 | int option = 0; | ||
378 | static struct option longopts[] = { | ||
379 | {"timeout", required_argument, 0, 't'}, | ||
380 | {"warning", required_argument, 0, 'w'}, | ||
381 | {"critical", required_argument, 0, 'c'}, | ||
382 | {"iwarning", required_argument, 0, 'W'}, | ||
383 | /* Dang, -C is taken. We might want to reshuffle this. */ | ||
384 | {"icritical", required_argument, 0, 'K'}, | ||
385 | {"local", required_argument, 0, 'l'}, | ||
386 | {"kilobytes", required_argument, 0, 'k'}, | ||
387 | {"megabytes", required_argument, 0, 'm'}, | ||
388 | {"units", required_argument, 0, 'u'}, | ||
389 | {"all", no_argument, 0, 'a'}, | ||
390 | {"path", required_argument, 0, 'p'}, | ||
391 | {"partition", required_argument, 0, 'p'}, | ||
392 | {"exclude_device", required_argument, 0, 'x'}, | ||
393 | {"exclude_path", required_argument, 0, 's'}, | ||
394 | {"exclude-type", required_argument, 0, 'X'}, | ||
395 | {"mountpoint", no_argument, 0, 'M'}, | ||
396 | {"errors-only", no_argument, 0, 'e'}, | ||
397 | {"verbose", no_argument, 0, 'v'}, | ||
398 | {"quiet", no_argument, 0, 'q'}, | ||
399 | {"clear", no_argument, 0, 'C'}, | ||
400 | {"version", no_argument, 0, 'V'}, | ||
401 | {"help", no_argument, 0, 'h'}, | ||
402 | {"html", no_argument, 0, 'H'}, | ||
403 | {"used", no_argument, 0, 'U'}, | ||
404 | {0, 0, 0, 0} | ||
405 | }; | ||
406 | |||
407 | if (argc < 2) | ||
408 | return ERROR; | ||
409 | |||
410 | se = (struct name_list *) malloc (sizeof (struct name_list)); | ||
411 | se->name = strdup ("iso9660"); | ||
412 | se->name_next = NULL; | ||
413 | se->found = 0; | ||
414 | se->foundexact = 0; | ||
415 | se->exclude = 0; | ||
416 | se->found_len = 0; | ||
417 | *fstail = se; | ||
418 | fstail = &se->name_next; | ||
419 | for (c = 1; c < argc; c++) | ||
420 | if (strcmp ("-to", argv[c]) == 0) | ||
421 | strcpy (argv[c], "-t"); | ||
422 | |||
423 | while (1) { | ||
424 | c = getopt_long (argc, argv, "+?VqhHiUveCat:c:w:K:W:u:p:x:s:X:mklM", longopts, &option); | ||
425 | |||
426 | if (c == -1 || c == EOF) | ||
427 | break; | ||
428 | |||
429 | switch (c) { | ||
430 | case 't': /* timeout period */ | ||
431 | if (is_integer (optarg)) { | ||
432 | timeout_interval = atoi (optarg); | ||
433 | break; | ||
434 | } | ||
435 | else { | ||
436 | usage2 (_("Timeout interval must be a positive integer"), optarg); | ||
437 | } | ||
438 | case 'w': /* warning threshold */ | ||
439 | if (is_intnonneg (optarg)) { | ||
440 | w_df = atoi (optarg); | ||
441 | break; | ||
442 | } | ||
443 | else if (strpbrk (optarg, ",:") && | ||
444 | strstr (optarg, "%") && | ||
445 | sscanf (optarg, "%lu%*[:,]%lf%%", &l, &w_dfp) == 2) { | ||
446 | w_df = (uintmax_t)l; | ||
447 | break; | ||
448 | } | ||
449 | else if (strstr (optarg, "%") && sscanf (optarg, "%lf%%", &w_dfp) == 1) { | ||
450 | break; | ||
451 | } | ||
452 | else { | ||
453 | usage4 (_("Warning threshold must be integer or percentage!")); | ||
454 | } | ||
455 | case 'c': /* critical threshold */ | ||
456 | if (is_intnonneg (optarg)) { | ||
457 | c_df = atoi (optarg); | ||
458 | break; | ||
459 | } | ||
460 | else if (strpbrk (optarg, ",:") && | ||
461 | strstr (optarg, "%") && | ||
462 | sscanf (optarg, "%lu%*[,:]%lf%%", &l, &c_dfp) == 2) { | ||
463 | c_df = (uintmax_t)l; | ||
464 | break; | ||
465 | } | ||
466 | else if (strstr (optarg, "%") && sscanf (optarg, "%lf%%", &c_dfp) == 1) { | ||
467 | break; | ||
468 | } | ||
469 | else { | ||
470 | usage4 (_("Critical threshold must be integer or percentage!")); | ||
471 | } | ||
472 | |||
473 | |||
474 | case 'W': /* warning inode threshold */ | ||
475 | if (strstr (optarg, "%") && sscanf (optarg, "%lf%%", &w_idfp) == 1) { | ||
476 | break; | ||
477 | } | ||
478 | else { | ||
479 | usage (_("Warning inode threshold must be percentage!\n")); | ||
480 | } | ||
481 | case 'K': /* kritical inode threshold */ | ||
482 | if (strstr (optarg, "%") && sscanf (optarg, "%lf%%", &c_idfp) == 1) { | ||
483 | break; | ||
484 | } | ||
485 | else { | ||
486 | usage (_("Critical inode threshold must be percentage!\n")); | ||
487 | } | ||
488 | case 'u': | ||
489 | if (units) | ||
490 | free(units); | ||
491 | if (! strcmp (optarg, "bytes")) { | ||
492 | mult = (uintmax_t)1; | ||
493 | units = strdup ("B"); | ||
494 | } else if (! strcmp (optarg, "kB")) { | ||
495 | mult = (uintmax_t)1024; | ||
496 | units = strdup ("kB"); | ||
497 | } else if (! strcmp (optarg, "MB")) { | ||
498 | mult = (uintmax_t)1024 * 1024; | ||
499 | units = strdup ("MB"); | ||
500 | } else if (! strcmp (optarg, "GB")) { | ||
501 | mult = (uintmax_t)1024 * 1024 * 1024; | ||
502 | units = strdup ("GB"); | ||
503 | } else if (! strcmp (optarg, "TB")) { | ||
504 | mult = (uintmax_t)1024 * 1024 * 1024 * 1024; | ||
505 | units = strdup ("TB"); | ||
506 | } else { | ||
507 | die (STATE_UNKNOWN, _("unit type %s not known\n"), optarg); | ||
508 | } | ||
509 | if (units == NULL) | ||
510 | die (STATE_UNKNOWN, _("failed allocating storage for '%s'\n"), "units"); | ||
511 | break; | ||
512 | case 'k': /* display mountpoint */ | ||
513 | mult = 1024; | ||
514 | if (units) | ||
515 | free(units); | ||
516 | units = strdup ("kB"); | ||
517 | break; | ||
518 | case 'm': /* display mountpoint */ | ||
519 | mult = 1024 * 1024; | ||
520 | if (units) | ||
521 | free(units); | ||
522 | units = strdup ("MB"); | ||
523 | break; | ||
524 | case 'l': | ||
525 | show_local_fs = 1; | ||
526 | break; | ||
527 | case 'a': /* Choose all paths */ | ||
528 | /* Remember the values passed in for all paths */ | ||
529 | path_select_exclude_others=0; | ||
530 | dw_df = w_df; | ||
531 | dc_df = c_df; | ||
532 | dw_dfp = w_dfp; | ||
533 | dc_dfp = c_dfp; | ||
534 | dw_idfp = w_idfp; | ||
535 | dc_idfp = c_idfp; | ||
536 | break; | ||
537 | case 'p': /* select path */ | ||
538 | se = (struct name_list *) malloc (sizeof (struct name_list)); | ||
539 | se->name = optarg; | ||
540 | se->name_next = NULL; | ||
541 | se->w_df = w_df; | ||
542 | se->c_df = c_df; | ||
543 | se->w_dfp = w_dfp; | ||
544 | se->c_dfp = c_dfp; | ||
545 | se->w_idfp = w_idfp; | ||
546 | se->c_idfp = c_idfp; | ||
547 | se->found = 0; | ||
548 | se->foundexact = 0; | ||
549 | se->found_len = 0; | ||
550 | se->exclude = 0; | ||
551 | *pathtail = se; | ||
552 | pathtail = &se->name_next; | ||
553 | break; | ||
554 | case 'x': /* exclude path or partition */ | ||
555 | se = (struct name_list *) malloc (sizeof (struct name_list)); | ||
556 | se->name = optarg; | ||
557 | se->name_next = NULL; | ||
558 | |||
559 | /* If you don't clear the w_fd etc values here, they | ||
560 | * get processed when you walk the list and assigned | ||
561 | * to the global w_df! | ||
562 | */ | ||
563 | se->w_df = 0; | ||
564 | se->c_df = 0; | ||
565 | se->w_dfp = 0; | ||
566 | se->c_dfp = 0; | ||
567 | se->w_idfp = 0; | ||
568 | se->c_idfp = 0; | ||
569 | se->found = 0; | ||
570 | se->foundexact = 0; | ||
571 | se->found_len = 0; | ||
572 | se->exclude = 1; | ||
573 | *dptail = se; | ||
574 | dptail = &se->name_next; | ||
575 | break; | ||
576 | case 's': /* exclude path or partition */ | ||
577 | se = (struct name_list *) malloc (sizeof (struct name_list)); | ||
578 | se->name = optarg; | ||
579 | se->name_next = NULL; | ||
580 | |||
581 | /* If you don't clear the w_fd etc values here, they | ||
582 | * get processed when you walk the list and assigned | ||
583 | * to the global w_df! | ||
584 | */ | ||
585 | se->w_df = 0; | ||
586 | se->c_df = 0; | ||
587 | se->w_dfp = 0; | ||
588 | se->c_dfp = 0; | ||
589 | se->w_idfp = 0; | ||
590 | se->c_idfp = 0; | ||
591 | se->found = 0; | ||
592 | se->foundexact = 0; | ||
593 | se->found_len = 0; | ||
594 | se->exclude = 2; | ||
595 | *dptail = se; | ||
596 | dptail = &se->name_next; | ||
597 | break; | ||
598 | case 'X': /* exclude file system type */ | ||
599 | se = (struct name_list *) malloc (sizeof (struct name_list)); | ||
600 | se->name = optarg; | ||
601 | se->name_next = NULL; | ||
602 | /* If you don't clear the w_fd etc values here, they | ||
603 | * get processed when you walk the list and assigned | ||
604 | * to the global w_df! | ||
605 | */ | ||
606 | se->w_df = 0; | ||
607 | se->c_df = 0; | ||
608 | se->w_dfp = 0; | ||
609 | se->c_dfp = 0; | ||
610 | se->w_idfp = 0; | ||
611 | se->c_idfp = 0; | ||
612 | se->found = 0; | ||
613 | se->foundexact = 0; | ||
614 | se->found_len = 0; | ||
615 | *fstail = se; | ||
616 | fstail = &se->name_next; | ||
617 | break; | ||
618 | case 'v': /* verbose */ | ||
619 | verbose++; | ||
620 | break; | ||
621 | case 'q': /* verbose */ | ||
622 | verbose--; | ||
623 | break; | ||
624 | case 'e': | ||
625 | erronly = TRUE; | ||
626 | break; | ||
627 | case 'M': /* display mountpoint */ | ||
628 | display_mntp = TRUE; | ||
629 | break; | ||
630 | case 'C': | ||
631 | w_df = 0; | ||
632 | c_df = 0; | ||
633 | w_dfp = -1.0; | ||
634 | c_dfp = -1.0; | ||
635 | w_idfp = -1.0; | ||
636 | c_idfp = -1.0; | ||
637 | break; | ||
638 | case 'H': /* HTML formatted output */ | ||
639 | show_html = 1; | ||
640 | break; | ||
641 | case 'U': | ||
642 | show_used = 1; | ||
643 | break; | ||
644 | case 'V': /* version */ | ||
645 | print_revision (progname, revision); | ||
646 | exit (STATE_OK); | ||
647 | case 'h': /* help */ | ||
648 | print_help (); | ||
649 | exit (STATE_OK); | ||
650 | case '?': /* help */ | ||
651 | usage (_("Unknown argument")); | ||
652 | } | ||
653 | } | ||
654 | |||
655 | /* Support for "check_disk warn crit [fs]" with thresholds at used level */ | ||
656 | c = optind; | ||
657 | if (w_dfp < 0 && argc > c && is_intnonneg (argv[c])) | ||
658 | w_dfp = (100.0 - atof (argv[c++])); | ||
659 | |||
660 | if (c_dfp < 0 && argc > c && is_intnonneg (argv[c])) | ||
661 | c_dfp = (100.0 - atof (argv[c++])); | ||
662 | |||
663 | if (argc > c && path == NULL) { | ||
664 | se = (struct name_list *) malloc (sizeof (struct name_list)); | ||
665 | se->name = strdup (argv[c++]); | ||
666 | se->name_next = NULL; | ||
667 | se->w_df = w_df; | ||
668 | se->c_df = c_df; | ||
669 | se->w_dfp = w_dfp; | ||
670 | se->c_dfp = c_dfp; | ||
671 | se->w_idfp = w_idfp; | ||
672 | se->c_idfp = c_idfp; | ||
673 | se->found =0; | ||
674 | se->foundexact = 0; | ||
675 | se->found_len = 0; | ||
676 | se->exclude = 0; | ||
677 | *pathtail = se; | ||
678 | } | ||
679 | |||
680 | /* Remember the default values */ | ||
681 | if(path_select_exclude_others == 1) { | ||
682 | dw_df = w_df; | ||
683 | dc_df = c_df; | ||
684 | dw_dfp = w_dfp; | ||
685 | dc_dfp = c_dfp; | ||
686 | dw_idfp = w_idfp; | ||
687 | dc_idfp = c_idfp; | ||
688 | } | ||
689 | |||
690 | if (path_select_list) { | ||
691 | temp_list = path_select_list; | ||
692 | stat_buf = malloc(sizeof *stat_buf); | ||
693 | while (temp_list) { | ||
694 | /* Stat each entry to check that dir exists */ | ||
695 | if (stat (temp_list->name, &stat_buf[0])) { | ||
696 | printf("DISK %s - ", _("CRITICAL")); | ||
697 | die (STATE_CRITICAL, _("%s does not exist\n"), temp_list->name); | ||
698 | } | ||
699 | if (validate_arguments (temp_list->w_df, | ||
700 | temp_list->c_df, | ||
701 | temp_list->w_dfp, | ||
702 | temp_list->c_dfp, | ||
703 | temp_list->w_idfp, | ||
704 | temp_list->c_idfp, | ||
705 | temp_list->name) == ERROR) | ||
706 | result = ERROR; | ||
707 | temp_list = temp_list->name_next; | ||
708 | } | ||
709 | free(stat_buf); | ||
710 | return result; | ||
711 | } else { | ||
712 | return validate_arguments (w_df, c_df, w_dfp, c_dfp, w_idfp, c_idfp, NULL); | ||
713 | } | ||
714 | } | ||
715 | |||
716 | |||
717 | |||
718 | void | ||
719 | print_path (const char *mypath) | ||
720 | { | ||
721 | if (mypath == NULL) | ||
722 | printf ("\n"); | ||
723 | else | ||
724 | printf (_(" for %s\n"), mypath); | ||
725 | |||
726 | return; | ||
727 | } | ||
728 | |||
729 | |||
730 | |||
731 | int | ||
732 | validate_arguments (uintmax_t w, uintmax_t c, double wp, double cp, double iwp, double icp, char *mypath) | ||
733 | { | ||
734 | if (w < 0 && c < 0 && wp < 0.0 && cp < 0.0) { | ||
735 | printf (_("INPUT ERROR: No thresholds specified")); | ||
736 | print_path (mypath); | ||
737 | return ERROR; | ||
738 | } | ||
739 | else if ((wp >= 0.0 || cp >= 0.0) && | ||
740 | (wp < 0.0 || cp < 0.0 || wp > 100.0 || cp > 100.0 || cp > wp)) { | ||
741 | printf (_("\ | ||
742 | INPUT ERROR: C_DFP (%f) should be less than W_DFP (%.1f) and both should be between zero and 100 percent, inclusive"), | ||
743 | cp, wp); | ||
744 | print_path (mypath); | ||
745 | return ERROR; | ||
746 | } | ||
747 | else if ((iwp >= 0.0 || icp >= 0.0) && | ||
748 | (iwp < 0.0 || icp < 0.0 || iwp > 100.0 || icp > 100.0 || icp > iwp)) { | ||
749 | printf (_("\ | ||
750 | INPUT ERROR: C_IDFP (%f) should be less than W_IDFP (%.1f) and both should be between zero and 100 percent, inclusive"), | ||
751 | icp, iwp); | ||
752 | print_path (mypath); | ||
753 | return ERROR; | ||
754 | } | ||
755 | else if ((w > 0 || c > 0) && (w == 0 || c == 0 || c > w)) { | ||
756 | printf (_("\ | ||
757 | INPUT ERROR: C_DF (%lu) should be less than W_DF (%lu) and both should be greater than zero"), | ||
758 | (unsigned long)c, (unsigned long)w); | ||
759 | print_path (mypath); | ||
760 | return ERROR; | ||
761 | } | ||
762 | |||
763 | if (units == NULL) { | ||
764 | units = strdup ("MB"); | ||
765 | mult = (uintmax_t)1024 * 1024; | ||
766 | } | ||
767 | return OK; | ||
768 | } | ||
769 | |||
770 | |||
771 | |||
772 | int | ||
773 | |||
774 | check_disk (double usp, uintmax_t free_disk, double uisp) | ||
775 | { | ||
776 | int result = STATE_UNKNOWN; | ||
777 | /* check the percent used space against thresholds */ | ||
778 | if (usp >= 0.0 && c_dfp >=0.0 && usp >= (100.0 - c_dfp)) | ||
779 | result = STATE_CRITICAL; | ||
780 | else if (uisp >= 0.0 && c_idfp >=0.0 && uisp >= (100.0 - c_idfp)) | ||
781 | result = STATE_CRITICAL; | ||
782 | else if (c_df > 0 && free_disk <= c_df) | ||
783 | result = STATE_CRITICAL; | ||
784 | else if (usp >= 0.0 && w_dfp >=0.0 && usp >= (100.0 - w_dfp)) | ||
785 | result = STATE_WARNING; | ||
786 | else if (uisp >= 0.0 && w_idfp >=0.0 && uisp >= (100.0 - w_idfp)) | ||
787 | result = STATE_WARNING; | ||
788 | else if (w_df > 0 && free_disk <= w_df) | ||
789 | result = STATE_WARNING; | ||
790 | else if (usp >= 0.0) | ||
791 | result = STATE_OK; | ||
792 | return result; | ||
793 | } | ||
794 | |||
795 | |||
796 | |||
797 | int | ||
798 | walk_name_list (struct name_list *list, const char *name) | ||
799 | { | ||
800 | int foundexact = FALSE, foundmatch = FALSE; | ||
801 | int name_len; | ||
802 | name_len = strlen(name); | ||
803 | while (list) { | ||
804 | int list_name_len; | ||
805 | list_name_len = strlen(list->name); | ||
806 | if ((list->exclude == 1 && name_len == list_name_len && strncmp(list->name, name, list_name_len-1) == 0) || | ||
807 | (list->foundexact == 0 && list->exclude == 0 && name_len < list_name_len && strncmp(list->name, name, name_len-1) == 0) || | ||
808 | (list->exclude == 0 && name_len == list_name_len && strncmp(list->name, name, name_len-1) == 0) || | ||
809 | (list->exclude == 2 && name_len >= list_name_len && strncmp(list->name, name, list_name_len-1) == 0)) { | ||
810 | /* Set to -1 for now to note it was a potential match. If an EXACT match is found later, | ||
811 | then reset this found to 0. If not, set it to 1. */ | ||
812 | list->found = -1; | ||
813 | list->found_len = name_len; | ||
814 | /* Grab the longest string length that is not being excluded in order to format HTML properly */ | ||
815 | if (name_len > longest_length && list->exclude == 0) {longest_length = name_len;} | ||
816 | /* if required for name_lists that have not saved w_df, etc (eg exclude lists) */ | ||
817 | if (list->w_df) w_df = list->w_df; | ||
818 | if (list->c_df) c_df = list->c_df; | ||
819 | if (list->w_dfp>=0.0) w_dfp = list->w_dfp; | ||
820 | if (list->c_dfp>=0.0) c_dfp = list->c_dfp; | ||
821 | if (name_len == list_name_len) { | ||
822 | foundexact=TRUE; | ||
823 | list->foundexact = TRUE; | ||
824 | list->found = 1; | ||
825 | } | ||
826 | foundmatch = TRUE; | ||
827 | } | ||
828 | list = list->name_next; | ||
829 | } | ||
830 | /* Traverse the list again to reset the found variable properly */ | ||
831 | while (list) { | ||
832 | if (list->found == -1 && foundexact == TRUE) list->found = 0; | ||
833 | if (list->found == -1 && foundexact == FALSE) list->found =1; | ||
834 | list = list->name_next; | ||
835 | printf("tried "); | ||
836 | } | ||
837 | return foundmatch; | ||
838 | } | ||
839 | |||
840 | |||
841 | |||
842 | void | ||
843 | print_help (void) | ||
844 | { | ||
845 | print_revision (progname, revision); | ||
846 | |||
847 | printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n"); | ||
848 | printf (COPYRIGHT, copyright, email); | ||
849 | |||
850 | printf (_("This plugin checks the amount of used disk space on a mounted file system")); | ||
851 | printf (_("and generates an alert if free space is less than one of the threshold values")); | ||
852 | |||
853 | printf ("\n\n"); | ||
854 | |||
855 | print_usage (); | ||
856 | |||
857 | printf (_(UT_HELP_VRSN)); | ||
858 | |||
859 | printf (" %s\n", "-w, --warning=INTEGER"); | ||
860 | printf (" %s\n", _("Exit with WARNING status if less than INTEGER units of disk are free")); | ||
861 | printf (" %s\n", "-w, --warning=PERCENT%"); | ||
862 | printf (" %s\n", _("Exit with WARNING status if less than PERCENT of disk space is free")); | ||
863 | printf (" %s\n", "-W, --iwarning=PERCENT%"); | ||
864 | printf (" %s\n", _("Exit with WARNING status if less than PERCENT of inode space is free")); | ||
865 | printf (" %s\n", "-K, --icritical=PERCENT%"); | ||
866 | printf (" %s\n", _("Exit with CRITICAL status if less than PERCENT of inode space is free")); | ||
867 | printf (" %s\n", "-c, --critical=INTEGER"); | ||
868 | printf (" %s\n", _("Exit with CRITICAL status if less than INTEGER units of disk are free")); | ||
869 | printf (" %s\n", "-c, --critical=PERCENT%"); | ||
870 | printf (" %s\n", _("Exit with CRITCAL status if less than PERCENT of disk space is free")); | ||
871 | printf (" %s\n", "-C, --clear"); | ||
872 | printf (" %s\n", _("Clear thresholds")); | ||
873 | printf (" %s\n", "-u, --units=STRING"); | ||
874 | printf (" %s\n", _("Choose bytes, kB, MB, GB, TB (default: MB)")); | ||
875 | printf (" %s\n", "-k, --kilobytes"); | ||
876 | printf (" %s\n", _("Same as '--units kB'")); | ||
877 | printf (" %s\n", "-m, --megabytes"); | ||
878 | printf (" %s\n", _("Same as '--units MB'")); | ||
879 | printf (" %s\n", "-l, --local"); | ||
880 | printf (" %s\n", _("Only check local filesystems")); | ||
881 | printf (" %s\n", "-p, --path=PATH, --partition=PARTITION"); | ||
882 | printf (" %s\n", _("Path or partition (may be repeated)")); | ||
883 | printf (" %s\n", "-x, --exclude_device=PATH <STRING>"); | ||
884 | printf (" %s\n", _("Ignore device (only works if -p unspecified)")); | ||
885 | printf (" %s\n", "-s, --exclude_path=PATH <STRING>"); | ||
886 | printf (" %s\n", _("Ignore device (only works if -p unspecified)")); | ||
887 | printf (" %s\n", _("-X, --exclude-type=TYPE <STRING>")); | ||
888 | printf (" %s\n", _("Ignore all filesystems of indicated type (may be repeated)")); | ||
889 | printf (" %s\n", "-m, --mountpoint"); | ||
890 | printf (" %s\n", _("Display the mountpoint instead of the partition")); | ||
891 | printf (" %s\n", "-e, --errors-only"); | ||
892 | printf (" %s\n", _("Display only devices/mountpoints with errors")); | ||
893 | printf (" %s\n", "-H, --html"); | ||
894 | printf (" %s\n", _("Output in HTML format")); | ||
895 | printf (" %s\n", "-U, --used"); | ||
896 | printf (" %s\n", _("Output percentage of space/inodes used rather than free")); | ||
897 | printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT); | ||
898 | printf (_(UT_VERBOSE)); | ||
899 | printf ("\n"); | ||
900 | printf ("%s\n", _("Examples:")); | ||
901 | printf (" %s\n", "check_disk -w 10% -c 5% -p /tmp -p /var -C -w 100000 -c 50000 -p /"); | ||
902 | printf (" %s\n", _("Checks /tmp and /var at 10% and 5%, and / at 100MB and 50MB")); | ||
903 | printf (" %s\n", "check_disk -w 10% -c 5% -a -C -w 5% -c 3% -p /"); | ||
904 | printf (" %s\n", _("Checks / at 5% and 3% and every other mount at 10% and 5%")); | ||
905 | printf (_(UT_SUPPORT)); | ||
906 | } | ||
907 | |||
908 | |||
909 | |||
910 | void | ||
911 | print_usage (void) | ||
912 | { | ||
913 | printf (_("Usage:")); | ||
914 | printf (" %s -w limit -c limit [-a | -p path | -x device | -s path] [-t timeout]", progname); | ||
915 | printf ("[-m] [-e] [-W limit] [-K limit] [-H] [-U] [-v] [-q]\n"); | ||
916 | } | ||