Code

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