Code

Fixed different thresholds if using exclude lists
[nagiosplug.git] / plugins / check_disk.c
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 *****************************************************************************/
19 const char *progname = "check_disk";
20 const char *revision = "$Revision$";
21 const char *copyright = "1999-2003";
22 const char *authors = "Nagios Plugin Development Team";
23 const char *email = "nagiosplug-devel@lists.sourceforge.net";
25 const char *summary = "\
26 This plugin checks the amount of used disk space on a mounted file system\n\
27 and generates an alert if free space is less than one of the threshold values.";
29 const char *option_summary = "\
30 -w limit -c limit [-p path | -x device] [-t timeout] [-m] [-e]\n\
31         [-v] [-q]";
33 const char *options = "\
34  -w, --warning=INTEGER\n\
35    Exit with WARNING status if less than INTEGER kilobytes of disk are free\n\
36  -w, --warning=PERCENT%%\n\
37    Exit with WARNING status if less than PERCENT of disk space is free\n\
38  -c, --critical=INTEGER\n\
39    Exit with CRITICAL status if less than INTEGER kilobytes of disk are free\n\
40  -c, --critical=PERCENT%%\n\
41    Exit with CRITCAL status if less than PERCENT of disk space is free\n\
42  -u, --units=STRING\n\
43     Choose bytes, kB, MB, GB, TB (default: MB)\n\
44  -k, --kilobytes\n\
45     Same as '--units kB'\n\
46  -m, --megabytes\n\
47     Same as '--units MB'\n\
48  -l, --local\n\
49     Only check local filesystems\n\
50  -p, --path=PATH, --partition=PARTITION\n\
51     Path or partition (may be repeated)\n\
52  -x, --exclude_device=PATH <STRING>\n\
53     Ignore device (only works if -p unspecified)\n\
54  -X, --exclude-type=TYPE <STRING>\n\
55     Ignore all filesystems of indicated type (may be repeated)\n\
56  -M, --mountpoint\n\
57     Display the mountpoint instead of the partition\n\
58  -e, --errors-only\n\
59     Display only devices/mountpoints with errors\n\
60  -C, --clear\n\
61     Clear thresholds\n\
62  -v, --verbose\n\
63     Show details for command-line debugging (do not use with nagios server)\n\
64  -h, --help\n\
65     Print detailed help screen\n\
66  -V, --version\n\
67     Print version information\n";
69 const char *notes = "\
70 \n";
72 const char *examples = "\
73  check_disk -w 10% -c 5% -p /tmp -p /var -C -w 100000 -c 50000 -p /\n\
74    Checks /tmp and /var at 10%,5% and / at 100MB, 50MB\n\
75 \n";
77 #include "common.h"
78 #if HAVE_INTTYPES_H
79 # include <inttypes.h>
80 #endif
81 #include <assert.h>
82 #include "popen.h"
83 #include "utils.h"
84 #include <stdarg.h>
85 #include "../lib/fsusage.h"
86 #include "../lib/mountlist.h"
87 #if HAVE_LIMITS_H
88 # include <limits.h>
89 #endif
91 /* If nonzero, show inode information. */
92 static int inode_format;
94 /* If nonzero, show even filesystems with zero size or
95    uninteresting types. */
96 static int show_all_fs = 1;
98 /* If nonzero, show only local filesystems.  */
99 static int show_local_fs = 0;
101 /* If positive, the units to use when printing sizes;
102    if negative, the human-readable base.  */
103 static int output_block_size;
105 /* If nonzero, invoke the `sync' system call before getting any usage data.
106    Using this option can make df very slow, especially with many or very
107    busy disks.  Note that this may make a difference on some systems --
108    SunOs4.1.3, for one.  It is *not* necessary on Linux.  */
109 static int require_sync = 0;
111 /* A filesystem type to display. */
113 struct name_list
115   char *name;
116   int found;
117   int w_df;
118   int c_df;
119   float w_dfp;
120   float c_dfp;
121   struct name_list *name_next;
122 };
124 /* Linked list of filesystem types to display.
125    If `fs_select_list' is NULL, list all types.
126    This table is generated dynamically from command-line options,
127    rather than hardcoding into the program what it thinks are the
128    valid filesystem types; let the user specify any filesystem type
129    they want to, and if there are any filesystems of that type, they
130    will be shown.
132    Some filesystem types:
133    4.2 4.3 ufs nfs swap ignore io vm efs dbg */
135 static struct name_list *fs_select_list;
137 /* Linked list of filesystem types to omit.
138    If the list is empty, don't exclude any types.  */
140 static struct name_list *fs_exclude_list;
142 static struct name_list *dp_exclude_list;
144 static struct name_list *path_select_list;
146 static struct name_list *dev_select_list;
148 /* Linked list of mounted filesystems. */
149 static struct mount_entry *mount_list;
151 /* For long options that have no equivalent short option, use a
152    non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
153 enum
155   SYNC_OPTION = CHAR_MAX + 1,
156   NO_SYNC_OPTION,
157   BLOCK_SIZE_OPTION
158 };
160 #ifdef _AIX
161  #pragma alloca
162 #endif
164 int process_arguments (int, char **);
165 int validate_arguments (int, int, float, float, char *);
166 int check_disk (int usp, int free_disk);
167 int walk_name_list (struct name_list *list, const char *name);
168 void print_help (void);
169 void print_usage (void);
171 int w_df = -1;
172 int c_df = -1;
173 float w_dfp = -1.0;
174 float c_dfp = -1.0;
175 char *path = "";
176 char *exclude_device = "";
177 char *units = "MB";
178 unsigned long mult = 1024 * 1024;
179 int verbose = 0;
180 int erronly = FALSE;
181 int display_mntp = FALSE;
183 /* Linked list of mounted filesystems. */
184 static struct mount_entry *mount_list;
186 int
187 main (int argc, char **argv)
189         int usp = -1;
190         int total_disk = -1;
191         int used_disk = -1;
192         int free_disk = -1;
193         int result = STATE_UNKNOWN;
194         int disk_result = STATE_UNKNOWN;
195         char *command_line = "";
196         char input_buffer[MAX_INPUT_BUFFER];
197         char file_system[MAX_INPUT_BUFFER];
198         char mntp[MAX_INPUT_BUFFER];
199         char *output = "";
200         char *details = "";
201         float free_space, free_space_pct, total_space;
203         struct mount_entry *me;
204         struct fs_usage fsp;
205         struct name_list *temp_list;
206         char *disk;
208         mount_list = read_filesystem_list (0);
210         if (process_arguments (argc, argv) != OK)
211                 usage ("Could not parse arguments\n");
213         for (me = mount_list; me; me = me->me_next) {
215                 if (path_select_list &&
216                      (walk_name_list (path_select_list, me->me_mountdir) ||
217                       walk_name_list (path_select_list, me->me_devname) ) )
218                         get_fs_usage (me->me_mountdir, me->me_devname, &fsp);
219                 else if (dev_select_list || path_select_list)
220                         continue;
221                 else if (me->me_remote && show_local_fs)
222                         continue;
223                 else if (me->me_dummy && !show_all_fs)
224                         continue;
225                 else if (fs_exclude_list && walk_name_list (fs_exclude_list, me->me_type))
226                         continue;
227                 else if (dp_exclude_list && 
228                          walk_name_list (dp_exclude_list, me->me_devname) ||
229                          walk_name_list (dp_exclude_list, me->me_mountdir))
230                         continue;
231                 else
232                         get_fs_usage (me->me_mountdir, me->me_devname, &fsp);
234                 if (fsp.fsu_blocks && strcmp ("none", me->me_mountdir)) {
235                         usp = (fsp.fsu_blocks - fsp.fsu_bavail) * 100 / fsp.fsu_blocks;
236                         disk_result = check_disk (usp, fsp.fsu_bavail);
237                         result = max_state (disk_result, result);
238                         if (disk_result==STATE_OK && erronly && !verbose)
239                                 continue;
241                         free_space = (float)fsp.fsu_bavail*fsp.fsu_blocksize/mult;
242                         free_space_pct = (float)fsp.fsu_bavail*100/fsp.fsu_blocks;
243                         total_space = (float)fsp.fsu_blocks*fsp.fsu_blocksize/mult;
244                         if (disk_result!=STATE_OK || verbose>=0)
245                                 asprintf (&output, "%s [%.0f %s (%.0f%%) free on %s]",
246                                           output,
247                                           free_space,
248                                           units,
249                                           free_space_pct,
250                                           (!strcmp(file_system, "none") || display_mntp) ? me->me_devname : me->me_mountdir);
251                         asprintf (&details, "%s\n%.0f of %.0f %s (%.0f%%) free on %s (type %s mounted on %s) warn:%d crit:%d warn%%:%.0f%% crit%%:%.0f%%",
252                                   details,
253                                   free_space,
254                                   total_space,
255                                   units,
256                                   free_space_pct,
257                                   me->me_devname,
258                                   me->me_type,
259                                   me->me_mountdir,
260                                   w_df, c_df, w_dfp, c_dfp);
261                 }
263         }
265         if (verbose > 2)
266                 asprintf (&output, "%s%s", output, details);
268         /* Override result if paths specified and not found */
269         temp_list = path_select_list;
270         while (temp_list) {
271                 if (temp_list->found != TRUE) {
272                         asprintf (&output, "%s [%s not found]", output, temp_list->name);
273                         result = STATE_CRITICAL;
274                 }
275                 temp_list = temp_list->name_next;
276         }
278         terminate (result, "DISK %s%s\n", state_text (result), output, details);
283 \f
284 /* process command-line arguments */
285 int
286 process_arguments (int argc, char **argv)
288         int c;
289         struct name_list *se;
290         struct name_list **pathtail = &path_select_list;
291         struct name_list **devtail = &dev_select_list;
292         struct name_list **fstail = &fs_exclude_list;
293         struct name_list **dptail = &dp_exclude_list;
294         struct name_list *temp_list;
295         int result = OK;
297         int option_index = 0;
298         static struct option long_options[] = {
299                 {"timeout", required_argument, 0, 't'},
300                 {"warning", required_argument, 0, 'w'},
301                 {"critical", required_argument, 0, 'c'},
302                 {"local", required_argument, 0, 'l'},
303                 {"kilobytes", required_argument, 0, 'k'},
304                 {"megabytes", required_argument, 0, 'm'},
305                 {"units", required_argument, 0, 'u'},
306                 {"path", required_argument, 0, 'p'},
307                 {"partition", required_argument, 0, 'p'},
308                 {"exclude_device", required_argument, 0, 'x'},
309                 {"exclude-type", required_argument, 0, 'X'},
310                 {"mountpoint", no_argument, 0, 'M'},
311                 {"errors-only", no_argument, 0, 'e'},
312                 {"verbose", no_argument, 0, 'v'},
313                 {"quiet", no_argument, 0, 'q'},
314                 {"clear", no_argument, 0, 'C'},
315                 {"version", no_argument, 0, 'V'},
316                 {"help", no_argument, 0, 'h'},
317                 {0, 0, 0, 0}
318         };
320         if (argc < 2)
321                 return ERROR;
323         se = (struct name_list *) malloc (sizeof (struct name_list));
324         se->name = strdup ("iso9660");
325         se->name_next = NULL;
326         *fstail = se;
327         fstail = &se->name_next;
329         for (c = 1; c < argc; c++)
330                 if (strcmp ("-to", argv[c]) == 0)
331                         strcpy (argv[c], "-t");
333         while (1) {
334                 c = getopt_long (argc, argv, "+?VqhveCt:c:w:u:p:x:X:mklM", long_options, &option_index);
336                 if (c == -1 || c == EOF)
337                         break;
339                 switch (c) {
340                 case 't':                                                                       /* timeout period */
341                         if (is_integer (optarg)) {
342                                 timeout_interval = atoi (optarg);
343                                 break;
344                         }
345                         else {
346                                 usage ("Timeout Interval must be an integer!\n");
347                         }
348                 case 'w':                                                                       /* warning time threshold */
349                         if (is_intnonneg (optarg)) {
350                                 w_df = atoi (optarg);
351                                 break;
352                         }
353                         else if (strpbrk (optarg, ",:") &&
354                                                          strstr (optarg, "%") &&
355                                                          sscanf (optarg, "%d%*[:,]%f%%", &w_df, &w_dfp) == 2) {
356                                 break;
357                         }
358                         else if (strstr (optarg, "%") && sscanf (optarg, "%f%%", &w_dfp) == 1) {
359                                 break;
360                         }
361                         else {
362                                 usage ("Warning threshold must be integer or percentage!\n");
363                         }
364                 case 'c':                                                                       /* critical time threshold */
365                         if (is_intnonneg (optarg)) {
366                                 c_df = atoi (optarg);
367                                 break;
368                         }
369                         else if (strpbrk (optarg, ",:") &&
370                                                          strstr (optarg, "%") &&
371                                                          sscanf (optarg, "%d%*[,:]%f%%", &c_df, &c_dfp) == 2) {
372                                 break;
373                         }
374                         else if (strstr (optarg, "%") && sscanf (optarg, "%f%%", &c_dfp) == 1) {
375                                 break;
376                         }
377                         else {
378                                 usage ("Critical threshold must be integer or percentage!\n");
379                         }
380                 case 'u':
381                         if (! strcmp (optarg, "bytes")) {
382                                 mult = 1;
383                                 units = "B";
384                         } else if (! strcmp (optarg, "kB")) {
385                                 mult = 1024;
386                                 units = "kB";
387                         } else if (! strcmp (optarg, "MB")) {
388                                 mult = 1024 * 1024;
389                                 units = "MB";
390                         } else if (! strcmp (optarg, "GB")) {
391                                 mult = 1024 * 1024 * 1024;
392                                 units = "GB";
393                         } else if (! strcmp (optarg, "TB")) {
394                                 mult = (unsigned long)1024 * 1024 * 1024 * 1024;
395                                 units = "TB";
396                         } else {
397                                 terminate (STATE_UNKNOWN, "unit type %s not known\n", optarg);
398                         }
399                         break;
400                 case 'k': /* display mountpoint */
401                         mult = 1024;
402                         units = "kB";
403                         break;
404                 case 'm': /* display mountpoint */
405                         mult = 1024 * 1024;
406                         units = "MB";
407                         break;
408                 case 'l':
409                         show_local_fs = 1;                      
410                         break;
411                 case 'p':                                                                       /* select path */
412                         se = (struct name_list *) malloc (sizeof (struct name_list));
413                         se->name = strdup (optarg);
414                         se->name_next = NULL;
415                         se->w_df = w_df;
416                         se->c_df = c_df;
417                         se->w_dfp = w_dfp;
418                         se->c_dfp = c_dfp;
419                         *pathtail = se;
420                         pathtail = &se->name_next;
421                         break;
422                 case 'x':                                                                       /* exclude path or partition */
423                         se = (struct name_list *) malloc (sizeof (struct name_list));
424                         se->name = strdup (optarg);
425                         se->name_next = NULL;
426                         *dptail = se;
427                         dptail = &se->name_next;
428                         break;
429                         break;
430                 case 'X':                                                                       /* exclude file system type */
431                         se = (struct name_list *) malloc (sizeof (struct name_list));
432                         se->name = strdup (optarg);
433                         se->name_next = NULL;
434                         *fstail = se;
435                         fstail = &se->name_next;
436                         break;
437                 case 'v':                                                                       /* verbose */
438                         verbose++;
439                         break;
440                 case 'q':                                                                       /* verbose */
441                         verbose--;
442                         break;
443                 case 'e':
444                         erronly = TRUE;
445                         break;
446                 case 'M': /* display mountpoint */
447                         display_mntp = TRUE;
448                         break;
449                 case 'C':
450                         w_df = -1;
451                         c_df = -1;
452                         w_dfp = -1.0;
453                         c_dfp = -1.0;
454                         break;
455                 case 'V':                                                                       /* version */
456                         print_revision (progname, revision);
457                         exit (STATE_OK);
458                 case 'h':                                                                       /* help */
459                         print_help ();
460                         exit (STATE_OK);
461                 case '?':                                                                       /* help */
462                         usage ("check_disk: unrecognized option\n");
463                         break;
464                 }
465         }
467         c = optind;
468         if (w_dfp == -1 && argc > c && is_intnonneg (argv[c]))
469                 w_dfp = (100.0 - atof (argv[c++]));
471         if (c_dfp == -1 && argc > c && is_intnonneg (argv[c]))
472                 c_dfp = (100.0 - atof (argv[c++]));
474         if (argc > c && strlen (path) == 0)
475                 path = argv[c++];
477         if (path_select_list) {
478                 temp_list = path_select_list;
479                 while (temp_list) {
480                         if (validate_arguments (temp_list->w_df, temp_list->c_df, temp_list->w_dfp, temp_list->c_dfp, temp_list->name) == ERROR)
481                                 result = ERROR;
482                         temp_list = temp_list->name_next;
483                 }
484                 return result;
485         } else {
486                 return validate_arguments (w_df, c_df, w_dfp, c_dfp, NULL);
487         }
491 void print_path (char *path) 
493         if (path)
494                 printf (" for %s", path);
495         printf ("\n");
498 int
499 validate_arguments (int w, int c, float wp, float cp, char *path)
501         if (w < 0 && c < 0 && wp < 0 && cp < 0) {
502                 printf ("INPUT ERROR: No thresholds specified");
503                 print_path (path);
504                 return ERROR;
505         }
506         else if ((wp >= 0 || cp >= 0)
507                                          && (wp < 0 || cp < 0 || wp > 100 || cp > 100
508                                                          || cp > wp)) {
509                 printf
510                         ("INPUT ERROR: C_DFP (%f) should be less than W_DFP (%.1f) and both should be between zero and 100 percent, inclusive",
511                          cp, wp);
512                 print_path (path);
513                 return ERROR;
514         }
515         else if ((w > 0 || c > 0) && (w < 0 || c < 0 || c > w)) {
516                 printf
517                         ("INPUT ERROR: C_DF (%d) should be less than W_DF (%d) and both should be greater than zero",
518                          c, w);
519                 print_path (path);
520                 return ERROR;
521         }
522         else {
523                 return OK;
524         }
529 \f
530 int
531 check_disk (int usp, int free_disk)
533         int result = STATE_UNKNOWN;
534         /* check the percent used space against thresholds */
535         if (usp >= 0 && usp >= (100.0 - c_dfp))
536                 result = STATE_CRITICAL;
537         else if (c_df >= 0 && free_disk <= c_df)
538                 result = STATE_CRITICAL;
539         else if (usp >= 0 && usp >= (100.0 - w_dfp))
540                 result = STATE_WARNING;
541         else if (w_df >= 0 && free_disk <= w_df)
542                 result = STATE_WARNING;
543         else if (usp >= 0.0)
544                 result = STATE_OK;
545         return result;
550 int
551 walk_name_list (struct name_list *list, const char *name)
553         while (list) {
554                 if (! strcmp(list->name, name)) {
555                         list->found = 1;
556                         /* if required for name_lists that have not saved w_df, etc (eg exclude lists) */
557                         if (list->w_df) w_df = list->w_df;
558                         if (list->c_df) c_df = list->c_df;
559                         if (list->w_dfp) w_dfp = list->w_dfp;
560                         if (list->c_dfp) c_dfp = list->c_dfp;
561                         return TRUE;
562                 }
563                 list = list->name_next;
564         }
565         return FALSE;
570 \f
571 void
572 print_help (void)
574         print_revision (progname, revision);
575         printf ("Copyright (c) %s %s\n\t<%s>\n\n%s\n",
576                  copyright, authors, email, summary);
577         print_usage ();
578         printf ("\nOptions:\n");
579         printf (options);
580         printf (notes);
581         printf ("Examples:\n%s", examples);
582         support ();
585 void
586 print_usage (void)
588         printf
589                 ("Usage: %s %s\n"
590                  "       %s (-h|--help)\n"
591                  "       %s (-V|--version)\n", progname, option_summary, progname, progname);