Code

7974d1415dd2e26ddb792315a3170f6ffaf12439
[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         float free_space, free_space_pct, total_space;
147         struct mount_entry *me;
148         struct fs_usage fsp;
149         struct name_list *temp_list;
151         output = strdup ("");
152         details = strdup ("");
154         mount_list = read_filesystem_list (0);
156         if (process_arguments (argc, argv) != OK)
157                 usage (_("Could not parse arguments\n"));
159         for (me = mount_list; me; me = me->me_next) {
161                 if (path_select_list &&
162                      (walk_name_list (path_select_list, me->me_mountdir) ||
163                       walk_name_list (path_select_list, me->me_devname) ) )
164                         get_fs_usage (me->me_mountdir, me->me_devname, &fsp);
165                 else if (dev_select_list || path_select_list)
166                         continue;
167                 else if (me->me_remote && show_local_fs)
168                         continue;
169                 else if (me->me_dummy && !show_all_fs)
170                         continue;
171                 else if (fs_exclude_list && walk_name_list (fs_exclude_list, me->me_type))
172                         continue;
173                 else if (dp_exclude_list && 
174                          (walk_name_list (dp_exclude_list, me->me_devname) ||
175                           walk_name_list (dp_exclude_list, me->me_mountdir)))
176                         continue;
177                 else
178                         get_fs_usage (me->me_mountdir, me->me_devname, &fsp);
180                 if (fsp.fsu_blocks && strcmp ("none", me->me_mountdir)) {
181                         usp = (double)(fsp.fsu_blocks - fsp.fsu_bavail) * 100 / fsp.fsu_blocks;
182                         disk_result = check_disk (usp, fsp.fsu_bavail);
183                         result = max_state (disk_result, result);
184                         if (disk_result==STATE_OK && erronly && !verbose)
185                                 continue;
187                         free_space = (float)fsp.fsu_bavail*fsp.fsu_blocksize/mult;
188                         free_space_pct = (float)fsp.fsu_bavail*100/fsp.fsu_blocks;
189                         total_space = (float)fsp.fsu_blocks*fsp.fsu_blocksize/mult;
190                         if (disk_result!=STATE_OK || verbose>=0)
191                                 asprintf (&output, ("%s [%.0f %s (%.0f%%) free on %s]"),
192                                           output,
193                                           free_space,
194                                           units,
195                                           free_space_pct,
196                                           (!strcmp(file_system, "none") || display_mntp) ? me->me_devname : me->me_mountdir);
197                         asprintf (&details, _("%s\n\
198 %.0f of %.0f %s (%.0f%%) free on %s (type %s mounted on %s) warn:%lu crit:%lu warn%%:%.0f%% crit%%:%.0f%%"),
199                                   details, free_space, total_space, units, free_space_pct,
200                                   me->me_devname, me->me_type, me->me_mountdir,
201                                   (unsigned long)w_df, (unsigned long)c_df, w_dfp, c_dfp);
202                 }
204         }
206         if (verbose > 2)
207                 asprintf (&output, "%s%s", output, details);
209         /* Override result if paths specified and not found */
210         temp_list = path_select_list;
211         while (temp_list) {
212                 if (temp_list->found != TRUE) {
213                         asprintf (&output, _("%s [%s not found]"), output, temp_list->name);
214                         result = STATE_CRITICAL;
215                 }
216                 temp_list = temp_list->name_next;
217         }
219         die (result, "DISK %s%s%s\n", state_text (result), output, details);
220         return STATE_UNKNOWN;
225 \f
226 /* process command-line arguments */
227 int
228 process_arguments (int argc, char **argv)
230         int c;
231         struct name_list *se;
232         struct name_list **pathtail = &path_select_list;
233         struct name_list **fstail = &fs_exclude_list;
234         struct name_list **dptail = &dp_exclude_list;
235         struct name_list *temp_list;
236         int result = OK;
238         unsigned long l;
240         int option_index = 0;
241         static struct option long_options[] = {
242                 {"timeout", required_argument, 0, 't'},
243                 {"warning", required_argument, 0, 'w'},
244                 {"critical", required_argument, 0, 'c'},
245                 {"local", required_argument, 0, 'l'},
246                 {"kilobytes", required_argument, 0, 'k'},
247                 {"megabytes", required_argument, 0, 'm'},
248                 {"units", required_argument, 0, 'u'},
249                 {"path", required_argument, 0, 'p'},
250                 {"partition", required_argument, 0, 'p'},
251                 {"exclude_device", required_argument, 0, 'x'},
252                 {"exclude-type", required_argument, 0, 'X'},
253                 {"mountpoint", no_argument, 0, 'M'},
254                 {"errors-only", no_argument, 0, 'e'},
255                 {"verbose", no_argument, 0, 'v'},
256                 {"quiet", no_argument, 0, 'q'},
257                 {"clear", no_argument, 0, 'C'},
258                 {"version", no_argument, 0, 'V'},
259                 {"help", no_argument, 0, 'h'},
260                 {0, 0, 0, 0}
261         };
263         if (argc < 2)
264                 return ERROR;
266         se = (struct name_list *) malloc (sizeof (struct name_list));
267         se->name = strdup ("iso9660");
268         se->name_next = NULL;
269         *fstail = se;
270         fstail = &se->name_next;
272         for (c = 1; c < argc; c++)
273                 if (strcmp ("-to", argv[c]) == 0)
274                         strcpy (argv[c], "-t");
276         while (1) {
277                 c = getopt_long (argc, argv, "+?VqhveCt:c:w:u:p:x:X:mklM", long_options, &option_index);
279                 if (c == -1 || c == EOF)
280                         break;
282                 switch (c) {
283                 case 't':                                                                       /* timeout period */
284                         if (is_integer (optarg)) {
285                                 timeout_interval = atoi (optarg);
286                                 break;
287                         }
288                         else {
289                                 usage (_("Timeout Interval must be an integer!\n"));
290                         }
291                 case 'w':                                                                       /* warning threshold */
292                         if (is_intnonneg (optarg)) {
293                                 w_df = atoi (optarg);
294                                 break;
295                         }
296                         else if (strpbrk (optarg, ",:") &&
297                                                          strstr (optarg, "%") &&
298                                                          sscanf (optarg, "%lu%*[:,]%lf%%", &l, &w_dfp) == 2) {
299                                 w_df = (uintmax_t)l;
300                                 break;
301                         }
302                         else if (strstr (optarg, "%") && sscanf (optarg, "%lf%%", &w_dfp) == 1) {
303                                 break;
304                         }
305                         else {
306                                 usage (_("Warning threshold must be integer or percentage!\n"));
307                         }
308                 case 'c':                                                                       /* critical threshold */
309                         if (is_intnonneg (optarg)) {
310                                 c_df = atoi (optarg);
311                                 break;
312                         }
313                         else if (strpbrk (optarg, ",:") &&
314                                                          strstr (optarg, "%") &&
315                                                          sscanf (optarg, "%lu%*[,:]%lf%%", &l, &c_dfp) == 2) {
316                                 c_df = (uintmax_t)l;
317                                 break;
318                         }
319                         else if (strstr (optarg, "%") && sscanf (optarg, "%lf%%", &c_dfp) == 1) {
320                                 break;
321                         }
322                         else {
323                                 usage (_("Critical threshold must be integer or percentage!\n"));
324                         }
325                 case 'u':
326                         if (units)
327                                 free(units);
328                         if (! strcmp (optarg, "bytes")) {
329                                 mult = (uintmax_t)1;
330                                 units = strdup ("B");
331                         } else if (! strcmp (optarg, "kB")) {
332                                 mult = (uintmax_t)1024;
333                                 units = strdup ("kB");
334                         } else if (! strcmp (optarg, "MB")) {
335                                 mult = (uintmax_t)1024 * 1024;
336                                 units = strdup ("MB");
337                         } else if (! strcmp (optarg, "GB")) {
338                                 mult = (uintmax_t)1024 * 1024 * 1024;
339                                 units = strdup ("GB");
340                         } else if (! strcmp (optarg, "TB")) {
341                                 mult = (uintmax_t)1024 * 1024 * 1024 * 1024;
342                                 units = strdup ("TB");
343                         } else {
344                                 die (STATE_UNKNOWN, _("unit type %s not known\n"), optarg);
345                         }
346                         if (units == NULL)
347                                 die (STATE_UNKNOWN, _("failed allocating storage for '%s'\n"), "units");
348                         break;
349                 case 'k': /* display mountpoint */
350                         mult = 1024;
351                         if (units)
352                                 free(units);
353                         units = strdup ("kB");
354                         break;
355                 case 'm': /* display mountpoint */
356                         mult = 1024 * 1024;
357                         if (units)
358                                 free(units);
359                         units = strdup ("kB");
360                         break;
361                 case 'l':
362                         show_local_fs = 1;                      
363                         break;
364                 case 'p':                                                                       /* select path */
365                         se = (struct name_list *) malloc (sizeof (struct name_list));
366                         se->name = strdup (optarg);
367                         se->name_next = NULL;
368                         se->w_df = w_df;
369                         se->c_df = c_df;
370                         se->w_dfp = w_dfp;
371                         se->c_dfp = c_dfp;
372                         *pathtail = se;
373                         pathtail = &se->name_next;
374                         break;
375                 case 'x':                                                                       /* exclude path or partition */
376                         se = (struct name_list *) malloc (sizeof (struct name_list));
377                         se->name = strdup (optarg);
378                         se->name_next = NULL;
379                         *dptail = se;
380                         dptail = &se->name_next;
381                         break;
382                 case 'X':                                                                       /* exclude file system type */
383                         se = (struct name_list *) malloc (sizeof (struct name_list));
384                         se->name = strdup (optarg);
385                         se->name_next = NULL;
386                         *fstail = se;
387                         fstail = &se->name_next;
388                         break;
389                 case 'v':                                                                       /* verbose */
390                         verbose++;
391                         break;
392                 case 'q':                                                                       /* verbose */
393                         verbose--;
394                         break;
395                 case 'e':
396                         erronly = TRUE;
397                         break;
398                 case 'M': /* display mountpoint */
399                         display_mntp = TRUE;
400                         break;
401                 case 'C':
402                         w_df = 0;
403                         c_df = 0;
404                         w_dfp = -1.0;
405                         c_dfp = -1.0;
406                         break;
407                 case 'V':                                                                       /* version */
408                         print_revision (progname, revision);
409                         exit (STATE_OK);
410                 case 'h':                                                                       /* help */
411                         print_help ();
412                         exit (STATE_OK);
413                 case '?':                                                                       /* help */
414                         usage (_("check_disk: unrecognized option\n"));
415                         break;
416                 }
417         }
419         /* Support for "check_disk warn crit [fs]" with thresholds at used level */
420         c = optind;
421         if (w_dfp < 0 && argc > c && is_intnonneg (argv[c]))
422                 w_dfp = (100.0 - atof (argv[c++]));
424         if (c_dfp < 0 && argc > c && is_intnonneg (argv[c]))
425                 c_dfp = (100.0 - atof (argv[c++]));
427         if (argc > c && path == NULL) {
428                 se = (struct name_list *) malloc (sizeof (struct name_list));
429                 se->name = strdup (argv[c++]);
430                 se->name_next = NULL;
431                 se->w_df = w_df;
432                 se->c_df = c_df;
433                 se->w_dfp = w_dfp;
434                 se->c_dfp = c_dfp;
435                 *pathtail = se;
436         }
438         if (path_select_list) {
439                 temp_list = path_select_list;
440                 while (temp_list) {
441                         if (validate_arguments (temp_list->w_df,
442                                                       temp_list->c_df,
443                                                       temp_list->w_dfp,
444                                                       temp_list->c_dfp,
445                                                       temp_list->name) == ERROR)
446                                 result = ERROR;
447                         temp_list = temp_list->name_next;
448                 }
449                 return result;
450         } else {
451                 return validate_arguments (w_df, c_df, w_dfp, c_dfp, NULL);
452         }
456 void
457 print_path (const char *mypath) 
459         if (mypath == NULL)
460                 printf ("\n");
461         else
462                 printf (" for %s\n", mypath);
464         return;
467 int
468 validate_arguments (uintmax_t w, uintmax_t c, double wp, double cp, char *mypath)
470         if (w == 0 && c == 0 && wp < 0.0 && cp < 0.0) {
471                 printf (_("INPUT ERROR: No thresholds specified"));
472                 print_path (mypath);
473                 return ERROR;
474         }
475         else if ((wp >= 0.0 || cp >= 0.0) &&
476                  (wp < 0.0 || cp < 0.0 || wp > 100.0 || cp > 100.0 || cp > wp)) {
477                 printf (_("\
478 INPUT ERROR: C_DFP (%f) should be less than W_DFP (%.1f) and both should be between zero and 100 percent, inclusive"),
479                         cp, wp);
480                 print_path (mypath);
481                 return ERROR;
482         }
483         else if ((w > 0 || c > 0) && (w == 0 || c == 0 || c > w)) {
484                 printf (_("\
485 INPUT ERROR: C_DF (%lu) should be less than W_DF (%lu) and both should be greater than zero"),
486                         (unsigned long)c, (unsigned long)w);
487                 print_path (mypath);
488                 return ERROR;
489         }
490         
491         if (units == NULL) {
492                 units = strdup ("MB");
493                 mult = (uintmax_t)1024 * 1024;
494         }
495         return OK;
500 \f
501 int
502 check_disk (double usp, uintmax_t free_disk)
504         int result = STATE_UNKNOWN;
505         /* check the percent used space against thresholds */
506         if (usp >= 0.0 && usp >= (100.0 - c_dfp))
507                 result = STATE_CRITICAL;
508         else if (c_df > 0 && free_disk <= c_df)
509                 result = STATE_CRITICAL;
510         else if (usp >= 0.0 && usp >= (100.0 - w_dfp))
511                 result = STATE_WARNING;
512         else if (w_df > 0 && free_disk <= w_df)
513                 result = STATE_WARNING;
514         else if (usp >= 0.0)
515                 result = STATE_OK;
516         return result;
521 int
522 walk_name_list (struct name_list *list, const char *name)
524         while (list) {
525                 if (! strcmp(list->name, name)) {
526                         list->found = 1;
527                         /* if required for name_lists that have not saved w_df, etc (eg exclude lists) */
528                         if (list->w_df) w_df = list->w_df;
529                         if (list->c_df) c_df = list->c_df;
530                         if (list->w_dfp>=0.0) w_dfp = list->w_dfp;
531                         if (list->c_dfp>=0.0) c_dfp = list->c_dfp;
532                         return TRUE;
533                 }
534                 list = list->name_next;
535         }
536         return FALSE;
543 \f
544 void
545 print_help (void)
547         print_revision (progname, revision);
549         printf (_("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n"));
550         printf (_(COPYRIGHT), copyright, email);
552         printf (_("\
553 This plugin checks the amount of used disk space on a mounted file system\n\
554 and generates an alert if free space is less than one of the threshold values."));
556         print_usage ();
558         printf (_(UT_HELP_VRSN));
560         printf (_("\
561  -w, --warning=INTEGER\n\
562    Exit with WARNING status if less than INTEGER kilobytes of disk are free\n\
563  -w, --warning=PERCENT%%\n\
564    Exit with WARNING status if less than PERCENT of disk space is free\n\
565  -c, --critical=INTEGER\n\
566    Exit with CRITICAL status if less than INTEGER kilobytes of disk are free\n\
567  -c, --critical=PERCENT%%\n\
568    Exit with CRITCAL status if less than PERCENT of disk space is free\n\
569  -C, --clear\n\
570     Clear thresholds\n"));
572         printf (_("\
573  -u, --units=STRING\n\
574     Choose bytes, kB, MB, GB, TB (default: MB)\n\
575  -k, --kilobytes\n\
576     Same as '--units kB'\n\
577  -m, --megabytes\n\
578     Same as '--units MB'\n"));
580         printf (_("\
581  -l, --local\n\
582     Only check local filesystems\n\
583  -p, --path=PATH, --partition=PARTITION\n\
584     Path or partition (may be repeated)\n\
585  -x, --exclude_device=PATH <STRING>\n\
586     Ignore device (only works if -p unspecified)\n\
587  -X, --exclude-type=TYPE <STRING>\n\
588     Ignore all filesystems of indicated type (may be repeated)\n\
589  -M, --mountpoint\n\
590     Display the mountpoint instead of the partition\n\
591  -e, --errors-only\n\
592     Display only devices/mountpoints with errors\n"));
594         printf (_(UT_WARN_CRIT));
596         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
598         printf (_(UT_VERBOSE));
600         printf ("%s", _("Examples:\n\
601  check_disk -w 10% -c 5% -p /tmp -p /var -C -w 100000 -c 50000 -p /\n\
602    Checks /tmp and /var at 10%,5% and / at 100MB, 50MB\n"));
604         printf (_(UT_SUPPORT));
610 void
611 print_usage (void)
613         printf (_("\
614 Usage: %s -w limit -c limit [-p path | -x device] [-t timeout] [-m] [-e]\n\
615         [-v] [-q]\n\
616        %s (-h|--help)\n\
617        %s (-V|--version)\n"),
618                 progname,  progname, progname);