Code

Default thresholds not defined for lists in -x parameter (raised by Matt Garrett)
[nagiosplug.git] / plugins / check_disk.c
1 /******************************************************************************
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.
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.
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.
17 *****************************************************************************/
19 const char *progname = "check_disk";
20 const char *revision = "$Revision$";
21 const char *copyright = "1999-2003";
22 const char *email = "nagiosplug-devel@lists.sourceforge.net";
24 #include "common.h"
25 #if HAVE_INTTYPES_H
26 # include <inttypes.h>
27 #endif
28 #include <assert.h>
29 #include "popen.h"
30 #include "utils.h"
31 #include <stdarg.h>
32 #include "../lib/fsusage.h"
33 #include "../lib/mountlist.h"
34 #if HAVE_LIMITS_H
35 # include <limits.h>
36 #endif
38 /* If nonzero, show inode information. */
39 /* static int inode_format; */
41 /* If nonzero, show even filesystems with zero size or
42    uninteresting types. */
43 static int show_all_fs = 1;
45 /* If nonzero, show only local filesystems.  */
46 static int show_local_fs = 0;
48 /* If positive, the units to use when printing sizes;
49    if negative, the human-readable base.  */
50 /* static int output_block_size; */
52 /* If nonzero, invoke the `sync' system call before getting any usage data.
53    Using this option can make df very slow, especially with many or very
54    busy disks.  Note that this may make a difference on some systems --
55    SunOs4.1.3, for one.  It is *not* necessary on Linux.  */
56 /* static int require_sync = 0; */
58 /* A filesystem type to display. */
60 struct name_list
61 {
62   char *name;
63   int found;
64   uintmax_t w_df;
65   uintmax_t c_df;
66   double w_dfp;
67   double c_dfp;
68   struct name_list *name_next;
69 };
71 /* Linked list of filesystem types to display.
72    If `fs_select_list' is NULL, list all types.
73    This table is generated dynamically from command-line options,
74    rather than hardcoding into the program what it thinks are the
75    valid filesystem types; let the user specify any filesystem type
76    they want to, and if there are any filesystems of that type, they
77    will be shown.
79    Some filesystem types:
80    4.2 4.3 ufs nfs swap ignore io vm efs dbg */
82 /* static struct name_list *fs_select_list; */
84 /* Linked list of filesystem types to omit.
85    If the list is empty, don't exclude any types.  */
87 static struct name_list *fs_exclude_list;
89 static struct name_list *dp_exclude_list;
91 static struct name_list *path_select_list;
93 static struct name_list *dev_select_list;
95 /* Linked list of mounted filesystems. */
96 static struct mount_entry *mount_list;
98 /* For long options that have no equivalent short option, use a
99    non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
100 enum
102   SYNC_OPTION = CHAR_MAX + 1,
103   NO_SYNC_OPTION,
104   BLOCK_SIZE_OPTION
105 };
107 #ifdef _AIX
108  #pragma alloca
109 #endif
111 int process_arguments (int, char **);
112 void print_path (const char *mypath);
113 int validate_arguments (uintmax_t, uintmax_t, double, double, char *);
114 int check_disk (double usp, uintmax_t free_disk);
115 int walk_name_list (struct name_list *list, const char *name);
116 void print_help (void);
117 void print_usage (void);
119 uintmax_t w_df = 0;
120 uintmax_t c_df = 0;
121 double w_dfp = -1.0;
122 double c_dfp = -1.0;
123 char *path;
124 char *exclude_device;
125 char *units;
126 uintmax_t mult = 1024 * 1024;
127 int verbose = 0;
128 int erronly = FALSE;
129 int display_mntp = FALSE;
131 /* Linked list of mounted filesystems. */
132 static struct mount_entry *mount_list;
135 \f
136 int
137 main (int argc, char **argv)
139         double usp = -1.0;
140         int result = STATE_UNKNOWN;
141         int disk_result = STATE_UNKNOWN;
142         char file_system[MAX_INPUT_BUFFER];
143         char *output;
144         char *details;
145         char *perf;
146         uintmax_t psize;
147         float free_space, free_space_pct, total_space;
149         struct mount_entry *me;
150         struct fs_usage fsp;
151         struct name_list *temp_list;
153         output = strdup (" - free space:");
154         details = strdup ("");
155         perf = strdup ("");
157         setlocale (LC_ALL, "");
158         bindtextdomain (PACKAGE, LOCALEDIR);
159         textdomain (PACKAGE);
161         mount_list = read_filesystem_list (0);
163         if (process_arguments (argc, argv) != OK)
164                 usage (_("Could not parse arguments\n"));
166         for (me = mount_list; me; me = me->me_next) {
168                 if (path_select_list &&
169                      (walk_name_list (path_select_list, me->me_mountdir) ||
170                       walk_name_list (path_select_list, me->me_devname) ) )
171                         get_fs_usage (me->me_mountdir, me->me_devname, &fsp);
172                 else if (dev_select_list || path_select_list)
173                         continue;
174                 else if (me->me_remote && show_local_fs)
175                         continue;
176                 else if (me->me_dummy && !show_all_fs)
177                         continue;
178                 else if (fs_exclude_list && walk_name_list (fs_exclude_list, me->me_type))
179                         continue;
180                 else if (dp_exclude_list && 
181                          (walk_name_list (dp_exclude_list, me->me_devname) ||
182                           walk_name_list (dp_exclude_list, me->me_mountdir)))
183                         continue;
184                 else
185                         get_fs_usage (me->me_mountdir, me->me_devname, &fsp);
187                 if (fsp.fsu_blocks && strcmp ("none", me->me_mountdir)) {
188                         usp = (double)(fsp.fsu_blocks - fsp.fsu_bavail) * 100 / fsp.fsu_blocks;
189                         disk_result = check_disk (usp, fsp.fsu_bavail);
190                         result = max_state (disk_result, result);
191                         psize = fsp.fsu_blocks*fsp.fsu_blocksize/mult;
192                         asprintf (&perf, "%s %s", perf,
193                                   perfdata ((!strcmp(file_system, "none") || display_mntp) ? me->me_devname : me->me_mountdir,
194                                             fsp.fsu_bavail*fsp.fsu_blocksize/mult, units,
195                                             TRUE, min ((uintmax_t)psize-(uintmax_t)w_df, (uintmax_t)((1.0-w_dfp/100.0)*psize)),
196                                             TRUE, min ((uintmax_t)psize-(uintmax_t)c_df, (uintmax_t)((1.0-c_dfp/100.0)*psize)),
197                                             TRUE, 0,
198                                             TRUE, psize));
199                         if (disk_result==STATE_OK && erronly && !verbose)
200                                 continue;
202                         free_space = (float)fsp.fsu_bavail*fsp.fsu_blocksize/mult;
203                         free_space_pct = (float)fsp.fsu_bavail*100/fsp.fsu_blocks;
204                         total_space = (float)fsp.fsu_blocks*fsp.fsu_blocksize/mult;
205                         if (disk_result!=STATE_OK || verbose>=0)
206                                 asprintf (&output, ("%s %s %.0f %s (%.0f%%);"),
207                                           output,
208                                           (!strcmp(file_system, "none") || display_mntp) ? me->me_devname : me->me_mountdir,
209                                           free_space,
210                                           units,
211                                                                         free_space_pct);
212                         asprintf (&details, _("%s\n\
213 %.0f of %.0f %s (%.0f%%) free on %s (type %s mounted on %s) warn:%lu crit:%lu warn%%:%.0f%% crit%%:%.0f%%"),
214                                   details, free_space, total_space, units, free_space_pct,
215                                   me->me_devname, me->me_type, me->me_mountdir,
216                                   (unsigned long)w_df, (unsigned long)c_df, w_dfp, c_dfp);
217                 }
219         }
221         asprintf (&output, "%s|%s", output, perf);
223         if (verbose > 2)
224                 asprintf (&output, "%s%s", output, details);
226         /* Override result if paths specified and not found */
227         temp_list = path_select_list;
228         while (temp_list) {
229                 if (temp_list->found != TRUE) {
230                         asprintf (&output, _("%s [%s not found]"), output, temp_list->name);
231                         result = STATE_CRITICAL;
232                 }
233                 temp_list = temp_list->name_next;
234         }
236         printf ("DISK %s%s\n", state_text (result), output);
237         return result;
242 \f
243 /* process command-line arguments */
244 int
245 process_arguments (int argc, char **argv)
247         int c;
248         struct name_list *se;
249         struct name_list **pathtail = &path_select_list;
250         struct name_list **fstail = &fs_exclude_list;
251         struct name_list **dptail = &dp_exclude_list;
252         struct name_list *temp_list;
253         int result = OK;
255         unsigned long l;
257         int option = 0;
258         static struct option longopts[] = {
259                 {"timeout", required_argument, 0, 't'},
260                 {"warning", required_argument, 0, 'w'},
261                 {"critical", required_argument, 0, 'c'},
262                 {"local", required_argument, 0, 'l'},
263                 {"kilobytes", required_argument, 0, 'k'},
264                 {"megabytes", required_argument, 0, 'm'},
265                 {"units", required_argument, 0, 'u'},
266                 {"path", required_argument, 0, 'p'},
267                 {"partition", required_argument, 0, 'p'},
268                 {"exclude_device", required_argument, 0, 'x'},
269                 {"exclude-type", required_argument, 0, 'X'},
270                 {"mountpoint", no_argument, 0, 'M'},
271                 {"errors-only", no_argument, 0, 'e'},
272                 {"verbose", no_argument, 0, 'v'},
273                 {"quiet", no_argument, 0, 'q'},
274                 {"clear", no_argument, 0, 'C'},
275                 {"version", no_argument, 0, 'V'},
276                 {"help", no_argument, 0, 'h'},
277                 {0, 0, 0, 0}
278         };
280         if (argc < 2)
281                 return ERROR;
283         se = (struct name_list *) malloc (sizeof (struct name_list));
284         se->name = strdup ("iso9660");
285         se->name_next = NULL;
286         *fstail = se;
287         fstail = &se->name_next;
289         for (c = 1; c < argc; c++)
290                 if (strcmp ("-to", argv[c]) == 0)
291                         strcpy (argv[c], "-t");
293         while (1) {
294                 c = getopt_long (argc, argv, "+?VqhveCt:c:w:u:p:x:X:mklM", longopts, &option);
296                 if (c == -1 || c == EOF)
297                         break;
299                 switch (c) {
300                 case 't':                                                                       /* timeout period */
301                         if (is_integer (optarg)) {
302                                 timeout_interval = atoi (optarg);
303                                 break;
304                         }
305                         else {
306                                 usage (_("Timeout Interval must be an integer!\n"));
307                         }
308                 case 'w':                                                                       /* warning threshold */
309                         if (is_intnonneg (optarg)) {
310                                 w_df = atoi (optarg);
311                                 break;
312                         }
313                         else if (strpbrk (optarg, ",:") &&
314                                                          strstr (optarg, "%") &&
315                                                          sscanf (optarg, "%lu%*[:,]%lf%%", &l, &w_dfp) == 2) {
316                                 w_df = (uintmax_t)l;
317                                 break;
318                         }
319                         else if (strstr (optarg, "%") && sscanf (optarg, "%lf%%", &w_dfp) == 1) {
320                                 break;
321                         }
322                         else {
323                                 usage (_("Warning threshold must be integer or percentage!\n"));
324                         }
325                 case 'c':                                                                       /* critical threshold */
326                         if (is_intnonneg (optarg)) {
327                                 c_df = atoi (optarg);
328                                 break;
329                         }
330                         else if (strpbrk (optarg, ",:") &&
331                                                          strstr (optarg, "%") &&
332                                                          sscanf (optarg, "%lu%*[,:]%lf%%", &l, &c_dfp) == 2) {
333                                 c_df = (uintmax_t)l;
334                                 break;
335                         }
336                         else if (strstr (optarg, "%") && sscanf (optarg, "%lf%%", &c_dfp) == 1) {
337                                 break;
338                         }
339                         else {
340                                 usage (_("Critical threshold must be integer or percentage!\n"));
341                         }
342                 case 'u':
343                         if (units)
344                                 free(units);
345                         if (! strcmp (optarg, "bytes")) {
346                                 mult = (uintmax_t)1;
347                                 units = strdup ("B");
348                         } else if (! strcmp (optarg, "kB")) {
349                                 mult = (uintmax_t)1024;
350                                 units = strdup ("kB");
351                         } else if (! strcmp (optarg, "MB")) {
352                                 mult = (uintmax_t)1024 * 1024;
353                                 units = strdup ("MB");
354                         } else if (! strcmp (optarg, "GB")) {
355                                 mult = (uintmax_t)1024 * 1024 * 1024;
356                                 units = strdup ("GB");
357                         } else if (! strcmp (optarg, "TB")) {
358                                 mult = (uintmax_t)1024 * 1024 * 1024 * 1024;
359                                 units = strdup ("TB");
360                         } else {
361                                 die (STATE_UNKNOWN, _("unit type %s not known\n"), optarg);
362                         }
363                         if (units == NULL)
364                                 die (STATE_UNKNOWN, _("failed allocating storage for '%s'\n"), "units");
365                         break;
366                 case 'k': /* display mountpoint */
367                         mult = 1024;
368                         if (units)
369                                 free(units);
370                         units = strdup ("kB");
371                         break;
372                 case 'm': /* display mountpoint */
373                         mult = 1024 * 1024;
374                         if (units)
375                                 free(units);
376                         units = strdup ("kB");
377                         break;
378                 case 'l':
379                         show_local_fs = 1;                      
380                         break;
381                 case 'p':                                                                       /* select path */
382                         se = (struct name_list *) malloc (sizeof (struct name_list));
383                         se->name = optarg;
384                         se->name_next = NULL;
385                         se->w_df = w_df;
386                         se->c_df = c_df;
387                         se->w_dfp = w_dfp;
388                         se->c_dfp = c_dfp;
389                         *pathtail = se;
390                         pathtail = &se->name_next;
391                         break;
392                 case 'x':                                                                       /* exclude path or partition */
393                         se = (struct name_list *) malloc (sizeof (struct name_list));
394                         se->name = optarg;
395                         se->name_next = NULL;
396                         se->w_df = 0;
397                         se->c_df = 0;
398                         se->w_dfp = -1.0;
399                         se->c_dfp = -1.0;
400                         *dptail = se;
401                         dptail = &se->name_next;
402                         break;
403                 case 'X':                                                                       /* exclude file system type */
404                         se = (struct name_list *) malloc (sizeof (struct name_list));
405                         se->name = optarg;
406                         se->name_next = NULL;
407                         *fstail = se;
408                         fstail = &se->name_next;
409                         break;
410                 case 'v':                                                                       /* verbose */
411                         verbose++;
412                         break;
413                 case 'q':                                                                       /* verbose */
414                         verbose--;
415                         break;
416                 case 'e':
417                         erronly = TRUE;
418                         break;
419                 case 'M': /* display mountpoint */
420                         display_mntp = TRUE;
421                         break;
422                 case 'C':
423                         w_df = 0;
424                         c_df = 0;
425                         w_dfp = -1.0;
426                         c_dfp = -1.0;
427                         break;
428                 case 'V':                                                                       /* version */
429                         print_revision (progname, revision);
430                         exit (STATE_OK);
431                 case 'h':                                                                       /* help */
432                         print_help ();
433                         exit (STATE_OK);
434                 case '?':                                                                       /* help */
435                         usage (_("check_disk: unrecognized option\n"));
436                         break;
437                 }
438         }
440         /* Support for "check_disk warn crit [fs]" with thresholds at used level */
441         c = optind;
442         if (w_dfp < 0 && argc > c && is_intnonneg (argv[c]))
443                 w_dfp = (100.0 - atof (argv[c++]));
445         if (c_dfp < 0 && argc > c && is_intnonneg (argv[c]))
446                 c_dfp = (100.0 - atof (argv[c++]));
448         if (argc > c && path == NULL) {
449                 se = (struct name_list *) malloc (sizeof (struct name_list));
450                 se->name = strdup (argv[c++]);
451                 se->name_next = NULL;
452                 se->w_df = w_df;
453                 se->c_df = c_df;
454                 se->w_dfp = w_dfp;
455                 se->c_dfp = c_dfp;
456                 *pathtail = se;
457         }
459         if (path_select_list) {
460                 temp_list = path_select_list;
461                 while (temp_list) {
462                         if (validate_arguments (temp_list->w_df,
463                                                       temp_list->c_df,
464                                                       temp_list->w_dfp,
465                                                       temp_list->c_dfp,
466                                                       temp_list->name) == ERROR)
467                                 result = ERROR;
468                         temp_list = temp_list->name_next;
469                 }
470                 return result;
471         } else {
472                 return validate_arguments (w_df, c_df, w_dfp, c_dfp, NULL);
473         }
477 void
478 print_path (const char *mypath) 
480         if (mypath == NULL)
481                 printf ("\n");
482         else
483                 printf (" for %s\n", mypath);
485         return;
488 int
489 validate_arguments (uintmax_t w, uintmax_t c, double wp, double cp, char *mypath)
491         if (w == 0 && c == 0 && wp < 0.0 && cp < 0.0) {
492                 printf (_("INPUT ERROR: No thresholds specified"));
493                 print_path (mypath);
494                 return ERROR;
495         }
496         else if ((wp >= 0.0 || cp >= 0.0) &&
497                  (wp < 0.0 || cp < 0.0 || wp > 100.0 || cp > 100.0 || cp > wp)) {
498                 printf (_("\
499 INPUT ERROR: C_DFP (%f) should be less than W_DFP (%.1f) and both should be between zero and 100 percent, inclusive"),
500                         cp, wp);
501                 print_path (mypath);
502                 return ERROR;
503         }
504         else if ((w > 0 || c > 0) && (w == 0 || c == 0 || c > w)) {
505                 printf (_("\
506 INPUT ERROR: C_DF (%lu) should be less than W_DF (%lu) and both should be greater than zero"),
507                         (unsigned long)c, (unsigned long)w);
508                 print_path (mypath);
509                 return ERROR;
510         }
511         
512         if (units == NULL) {
513                 units = strdup ("MB");
514                 mult = (uintmax_t)1024 * 1024;
515         }
516         return OK;
521 \f
522 int
523 check_disk (double usp, uintmax_t free_disk)
525         int result = STATE_UNKNOWN;
526         /* check the percent used space against thresholds */
527         if (usp >= 0.0 && c_dfp >=0.0 && usp >= (100.0 - c_dfp))
528                 result = STATE_CRITICAL;
529         else if (c_df > 0 && free_disk <= c_df)
530                 result = STATE_CRITICAL;
531         else if (usp >= 0.0 && w_dfp >=0.0 && usp >= (100.0 - w_dfp))
532                 result = STATE_WARNING;
533         else if (w_df > 0 && free_disk <= w_df)
534                 result = STATE_WARNING;
535         else if (usp >= 0.0)
536                 result = STATE_OK;
537         return result;
542 int
543 walk_name_list (struct name_list *list, const char *name)
545         while (list) {
546                 if (! strcmp(list->name, name)) {
547                         list->found = 1;
548                         /* if required for name_lists that have not saved w_df, etc (eg exclude lists) */
549                         if (list->w_df) w_df = list->w_df;
550                         if (list->c_df) c_df = list->c_df;
551                         if (list->w_dfp>=0.0) w_dfp = list->w_dfp;
552                         if (list->c_dfp>=0.0) c_dfp = list->c_dfp;
553                         return TRUE;
554                 }
555                 list = list->name_next;
556         }
557         return FALSE;
564 \f
565 void
566 print_help (void)
568         print_revision (progname, revision);
570         printf (_("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n"));
571         printf (_(COPYRIGHT), copyright, email);
573         printf (_("\
574 This plugin checks the amount of used disk space on a mounted file system\n\
575 and generates an alert if free space is less than one of the threshold values."));
577         print_usage ();
579         printf (_(UT_HELP_VRSN));
581         printf (_("\
582  -w, --warning=INTEGER\n\
583    Exit with WARNING status if less than INTEGER kilobytes of disk are free\n\
584  -w, --warning=PERCENT%%\n\
585    Exit with WARNING status if less than PERCENT of disk space is free\n\
586  -c, --critical=INTEGER\n\
587    Exit with CRITICAL status if less than INTEGER kilobytes of disk are free\n\
588  -c, --critical=PERCENT%%\n\
589    Exit with CRITCAL status if less than PERCENT of disk space is free\n\
590  -C, --clear\n\
591     Clear thresholds\n"));
593         printf (_("\
594  -u, --units=STRING\n\
595     Choose bytes, kB, MB, GB, TB (default: MB)\n\
596  -k, --kilobytes\n\
597     Same as '--units kB'\n\
598  -m, --megabytes\n\
599     Same as '--units MB'\n"));
601         printf (_("\
602  -l, --local\n\
603     Only check local filesystems\n\
604  -p, --path=PATH, --partition=PARTITION\n\
605     Path or partition (may be repeated)\n\
606  -x, --exclude_device=PATH <STRING>\n\
607     Ignore device (only works if -p unspecified)\n\
608  -X, --exclude-type=TYPE <STRING>\n\
609     Ignore all filesystems of indicated type (may be repeated)\n\
610  -M, --mountpoint\n\
611     Display the mountpoint instead of the partition\n\
612  -e, --errors-only\n\
613     Display only devices/mountpoints with errors\n"));
615         printf (_(UT_WARN_CRIT));
617         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
619         printf (_(UT_VERBOSE));
621         printf ("%s", _("Examples:\n\
622  check_disk -w 10% -c 5% -p /tmp -p /var -C -w 100000 -c 50000 -p /\n\
623    Checks /tmp and /var at 10%,5% and / at 100MB, 50MB\n"));
625         printf (_(UT_SUPPORT));
631 void
632 print_usage (void)
634         printf (_("\
635 Usage: %s -w limit -c limit [-p path | -x device] [-t timeout] [-m] [-e]\n\
636         [-v] [-q]\n\
637        %s (-h|--help)\n\
638        %s (-V|--version)\n"),
639                 progname,  progname, progname);