Code

cb5e49e6da2daf686f4a8dfd5670121a034b4ee0
[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 * fsp.fsu_blocksize);
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                         se->w_df = 0;
408                         se->c_df = 0;
409                         se->w_dfp = -1.0;
410                         se->c_dfp = -1.0;
411                         *fstail = se;
412                         fstail = &se->name_next;
413                         break;
414                 case 'v':                                                                       /* verbose */
415                         verbose++;
416                         break;
417                 case 'q':                                                                       /* verbose */
418                         verbose--;
419                         break;
420                 case 'e':
421                         erronly = TRUE;
422                         break;
423                 case 'M': /* display mountpoint */
424                         display_mntp = TRUE;
425                         break;
426                 case 'C':
427                         w_df = 0;
428                         c_df = 0;
429                         w_dfp = -1.0;
430                         c_dfp = -1.0;
431                         break;
432                 case 'V':                                                                       /* version */
433                         print_revision (progname, revision);
434                         exit (STATE_OK);
435                 case 'h':                                                                       /* help */
436                         print_help ();
437                         exit (STATE_OK);
438                 case '?':                                                                       /* help */
439                         usage (_("check_disk: unrecognized option\n"));
440                         break;
441                 }
442         }
444         /* Support for "check_disk warn crit [fs]" with thresholds at used level */
445         c = optind;
446         if (w_dfp < 0 && argc > c && is_intnonneg (argv[c]))
447                 w_dfp = (100.0 - atof (argv[c++]));
449         if (c_dfp < 0 && argc > c && is_intnonneg (argv[c]))
450                 c_dfp = (100.0 - atof (argv[c++]));
452         if (argc > c && path == NULL) {
453                 se = (struct name_list *) malloc (sizeof (struct name_list));
454                 se->name = strdup (argv[c++]);
455                 se->name_next = NULL;
456                 se->w_df = w_df;
457                 se->c_df = c_df;
458                 se->w_dfp = w_dfp;
459                 se->c_dfp = c_dfp;
460                 *pathtail = se;
461         }
463         if (path_select_list) {
464                 temp_list = path_select_list;
465                 while (temp_list) {
466                         if (validate_arguments (temp_list->w_df,
467                                                       temp_list->c_df,
468                                                       temp_list->w_dfp,
469                                                       temp_list->c_dfp,
470                                                       temp_list->name) == ERROR)
471                                 result = ERROR;
472                         temp_list = temp_list->name_next;
473                 }
474                 return result;
475         } else {
476                 return validate_arguments (w_df, c_df, w_dfp, c_dfp, NULL);
477         }
481 void
482 print_path (const char *mypath) 
484         if (mypath == NULL)
485                 printf ("\n");
486         else
487                 printf (" for %s\n", mypath);
489         return;
492 int
493 validate_arguments (uintmax_t w, uintmax_t c, double wp, double cp, char *mypath)
495         if (w == 0 && c == 0 && wp < 0.0 && cp < 0.0) {
496                 printf (_("INPUT ERROR: No thresholds specified"));
497                 print_path (mypath);
498                 return ERROR;
499         }
500         else if ((wp >= 0.0 || cp >= 0.0) &&
501                  (wp < 0.0 || cp < 0.0 || wp > 100.0 || cp > 100.0 || cp > wp)) {
502                 printf (_("\
503 INPUT ERROR: C_DFP (%f) should be less than W_DFP (%.1f) and both should be between zero and 100 percent, inclusive"),
504                         cp, wp);
505                 print_path (mypath);
506                 return ERROR;
507         }
508         else if ((w > 0 || c > 0) && (w == 0 || c == 0 || c > w)) {
509                 printf (_("\
510 INPUT ERROR: C_DF (%lu) should be less than W_DF (%lu) and both should be greater than zero"),
511                         (unsigned long)c, (unsigned long)w);
512                 print_path (mypath);
513                 return ERROR;
514         }
515         
516         if (units == NULL) {
517                 units = strdup ("MB");
518                 mult = (uintmax_t)1024 * 1024;
519         }
520         return OK;
525 \f
526 int
527 check_disk (double usp, uintmax_t free_disk)
529         int result = STATE_UNKNOWN;
530         /* check the percent used space against thresholds */
531         if (usp >= 0.0 && c_dfp >=0.0 && usp >= (100.0 - c_dfp))
532                 result = STATE_CRITICAL;
533         else if (c_df > 0 && free_disk <= c_df)
534                 result = STATE_CRITICAL;
535         else if (usp >= 0.0 && w_dfp >=0.0 && usp >= (100.0 - w_dfp))
536                 result = STATE_WARNING;
537         else if (w_df > 0 && free_disk <= w_df)
538                 result = STATE_WARNING;
539         else if (usp >= 0.0)
540                 result = STATE_OK;
541         return result;
546 int
547 walk_name_list (struct name_list *list, const char *name)
549         while (list) {
550                 if (! strcmp(list->name, name)) {
551                         list->found = 1;
552                         /* if required for name_lists that have not saved w_df, etc (eg exclude lists) */
553                         if (list->w_df) w_df = list->w_df;
554                         if (list->c_df) c_df = list->c_df;
555                         if (list->w_dfp>=0.0) w_dfp = list->w_dfp;
556                         if (list->c_dfp>=0.0) c_dfp = list->c_dfp;
557                         return TRUE;
558                 }
559                 list = list->name_next;
560         }
561         return FALSE;
568 \f
569 void
570 print_help (void)
572         print_revision (progname, revision);
574         printf (_("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n"));
575         printf (_(COPYRIGHT), copyright, email);
577         printf (_("\
578 This plugin checks the amount of used disk space on a mounted file system\n\
579 and generates an alert if free space is less than one of the threshold values."));
581         print_usage ();
583         printf (_(UT_HELP_VRSN));
585         printf (_("\
586  -w, --warning=INTEGER\n\
587    Exit with WARNING status if less than INTEGER kilobytes of disk are free\n\
588  -w, --warning=PERCENT%%\n\
589    Exit with WARNING status if less than PERCENT of disk space is free\n\
590  -c, --critical=INTEGER\n\
591    Exit with CRITICAL status if less than INTEGER kilobytes of disk are free\n\
592  -c, --critical=PERCENT%%\n\
593    Exit with CRITCAL status if less than PERCENT of disk space is free\n\
594  -C, --clear\n\
595     Clear thresholds\n"));
597         printf (_("\
598  -u, --units=STRING\n\
599     Choose bytes, kB, MB, GB, TB (default: MB)\n\
600  -k, --kilobytes\n\
601     Same as '--units kB'\n\
602  -m, --megabytes\n\
603     Same as '--units MB'\n"));
605         printf (_("\
606  -l, --local\n\
607     Only check local filesystems\n\
608  -p, --path=PATH, --partition=PARTITION\n\
609     Path or partition (may be repeated)\n\
610  -x, --exclude_device=PATH <STRING>\n\
611     Ignore device (only works if -p unspecified)\n\
612  -X, --exclude-type=TYPE <STRING>\n\
613     Ignore all filesystems of indicated type (may be repeated)\n\
614  -M, --mountpoint\n\
615     Display the mountpoint instead of the partition\n\
616  -e, --errors-only\n\
617     Display only devices/mountpoints with errors\n"));
619         printf (_(UT_WARN_CRIT));
621         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
623         printf (_(UT_VERBOSE));
625         printf ("%s", _("Examples:\n\
626  check_disk -w 10% -c 5% -p /tmp -p /var -C -w 100000 -c 50000 -p /\n\
627    Checks /tmp and /var at 10%,5% and / at 100MB, 50MB\n"));
629         printf (_(UT_SUPPORT));
635 void
636 print_usage (void)
638         printf (_("\
639 Usage: %s -w limit -c limit [-p path | -x device] [-t timeout] [-m] [-e]\n\
640         [-v] [-q]\n\
641        %s (-h|--help)\n\
642        %s (-V|--version)\n"),
643                 progname,  progname, progname);