Code

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