Code

checkpoint
[nagiosplug.git] / plugins / check_disk.c
1 /******************************************************************************
2 *
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.
7 *
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.
12 *
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.
16 *
17 *****************************************************************************/
19 const char *progname = "check_disk";
20 const char *revision = "$Revision$";
21 const char *copyright = "1999-2003";
22 const char *authors = "Nagios Plugin Development Team";
23 const char *email = "nagiosplug-devel@lists.sourceforge.net";
25 const char *summary = "\
26 This plugin checks the amount of used disk space on a mounted file system\n\
27 and generates an alert if free space is less than one of the threshold values.";
29 const char *option_summary = "\
30 -w limit -c limit [-p path | -x device] [-t timeout] [-m] [-e]\n\
31         [-v] [-q]";
33 const char *options = "\
34  -w, --warning=INTEGER\n\
35    Exit with WARNING status if less than INTEGER kilobytes of disk are free\n\
36  -w, --warning=PERCENT%%\n\
37    Exit with WARNING status if less than PERCENT of disk space is free\n\
38  -c, --critical=INTEGER\n\
39    Exit with CRITICAL status if less than INTEGER kilobytes of disk are free\n\
40  -c, --critical=PERCENT%%\n\
41    Exit with CRITCAL status if less than PERCENT of disk space is free\n\
42  -u, --units=STRING\n\
43     Choose bytes, kB, MB, GB, TB (default: MB)\n\
44  -k, --kilobytes\n\
45     Same as '--units kB'\n\
46  -m, --megabytes\n\
47     Same as '--units MB'\n\
48  -l, --local\n\
49     Only check local filesystems\n\
50  -p, --path=PATH, --partition=PARTTION\n\
51     Path or partition  (may be repeated)\n\
52  -x, --exclude_device=PATH <STRING>\n\
53     Ignore device (only works if -p unspecified)\n\
54  -X, --exclude-type=TYPE <STRING>\n\
55     Ignore all filesystems of indicated type (may be repeated)\n\
56  -M, --mountpoint\n\
57     Display the mountpoint instead of the partition\n\
58  -e, --errors-only\n\
59     Display only devices/mountpoints with errors\n\
60  -v, --verbose\n\
61     Show details for command-line debugging (do not use with nagios server)\n\
62  -h, --help\n\
63     Print detailed help screen\n\
64  -V, --version\n\
65     Print version information\n";
67 const char *notes = "\
68 \n";
70 #include "common.h"
71 #if HAVE_INTTYPES_H
72 # include <inttypes.h>
73 #endif
74 #include <assert.h>
75 #include "popen.h"
76 #include "utils.h"
77 #include <stdarg.h>
78 #include "../lib/fsusage.h"
79 #include "../lib/mountlist.h"
80 #if HAVE_LIMITS_H
81 # include <limits.h>
82 #endif
84 /* If nonzero, show inode information. */
85 static int inode_format;
87 /* If nonzero, show even filesystems with zero size or
88    uninteresting types. */
89 static int show_all_fs = 1;
91 /* If nonzero, show only local filesystems.  */
92 static int show_local_fs = 0;
94 /* If positive, the units to use when printing sizes;
95    if negative, the human-readable base.  */
96 static int output_block_size;
98 /* If nonzero, invoke the `sync' system call before getting any usage data.
99    Using this option can make df very slow, especially with many or very
100    busy disks.  Note that this may make a difference on some systems --
101    SunOs4.1.3, for one.  It is *not* necessary on Linux.  */
102 static int require_sync = 0;
104 /* A filesystem type to display. */
106 struct name_list
108   char *name;
109   struct name_list *name_next;
110 };
112 /* Linked list of filesystem types to display.
113    If `fs_select_list' is NULL, list all types.
114    This table is generated dynamically from command-line options,
115    rather than hardcoding into the program what it thinks are the
116    valid filesystem types; let the user specify any filesystem type
117    they want to, and if there are any filesystems of that type, they
118    will be shown.
120    Some filesystem types:
121    4.2 4.3 ufs nfs swap ignore io vm efs dbg */
123 static struct name_list *fs_select_list;
125 /* Linked list of filesystem types to omit.
126    If the list is empty, don't exclude any types.  */
128 static struct name_list *fs_exclude_list;
130 static struct name_list *dp_exclude_list;
132 static struct name_list *path_select_list;
134 static struct name_list *dev_select_list;
136 /* Linked list of mounted filesystems. */
137 static struct mount_entry *mount_list;
139 /* For long options that have no equivalent short option, use a
140    non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
141 enum
143   SYNC_OPTION = CHAR_MAX + 1,
144   NO_SYNC_OPTION,
145   BLOCK_SIZE_OPTION
146 };
148 #ifdef _AIX
149  #pragma alloca
150 #endif
152 int process_arguments (int, char **);
153 int validate_arguments (void);
154 int check_disk (int usp, int free_disk);
155 int walk_name_list (struct name_list *list, const char *name);
156 void print_help (void);
157 void print_usage (void);
159 int w_df = -1;
160 int c_df = -1;
161 float w_dfp = -1.0;
162 float c_dfp = -1.0;
163 char *path = "";
164 char *exclude_device = "";
165 char *units = "MB";
166 unsigned long mult = 1024 * 1024;
167 int verbose = 0;
168 int erronly = FALSE;
169 int display_mntp = FALSE;
171 /* Linked list of mounted filesystems. */
172 static struct mount_entry *mount_list;
174 int
175 main (int argc, char **argv)
177         int usp = -1;
178         int total_disk = -1;
179         int used_disk = -1;
180         int free_disk = -1;
181         int result = STATE_UNKNOWN;
182         int disk_result = STATE_UNKNOWN;
183         char *command_line = "";
184         char input_buffer[MAX_INPUT_BUFFER];
185         char file_system[MAX_INPUT_BUFFER];
186         char mntp[MAX_INPUT_BUFFER];
187         char *output = "";
188         char *details = "";
190   struct mount_entry *me;
191         struct fs_usage fsp;
192         char *disk;
194         mount_list = read_filesystem_list (0);
196         if (process_arguments (argc, argv) != OK)
197                 usage ("Could not parse arguments\n");
199   for (me = mount_list; me; me = me->me_next) {
201                 if ((dev_select_list &&
202                      walk_name_list (dev_select_list, me->me_devname)) ||
203                     (path_select_list &&
204                      walk_name_list (path_select_list, me->me_mountdir)))
205                         get_fs_usage (me->me_mountdir, me->me_devname, &fsp);
206                 else if (dev_select_list || path_select_list)
207                         continue;
208                 else if (me->me_remote && show_local_fs)
209                         continue;
210                 else if (me->me_dummy && !show_all_fs)
211                         continue;
212                 else if (fs_exclude_list && walk_name_list (fs_exclude_list, me->me_type))
213                         continue;
214                 else if (dp_exclude_list && 
215                          walk_name_list (dp_exclude_list, me->me_devname) ||
216                          walk_name_list (dp_exclude_list, me->me_mountdir))
217                         continue;
218                 else
219                         get_fs_usage (me->me_mountdir, me->me_devname, &fsp);
221                 if (fsp.fsu_blocks && strcmp ("none", me->me_mountdir)) {
222                         usp = (fsp.fsu_blocks - fsp.fsu_bavail) * 100 / fsp.fsu_blocks;
223                         disk_result = check_disk (usp, fsp.fsu_bavail);
224                         result = max_state (disk_result, result);
225                         if (disk_result==STATE_OK && erronly && !verbose)
226                                 continue;
228                         if (disk_result!=STATE_OK || verbose>=0) 
229                                 asprintf (&output, "%s [%llu %s (%2.0f%%) free on %s]",
230                                           output,
231                                           fsp.fsu_bavail*fsp.fsu_blocksize/mult,
232                                                                         units,
233                                           (double)fsp.fsu_bavail*100/fsp.fsu_blocks,
234                                           (!strcmp(file_system, "none") || display_mntp) ? me->me_devname : me->me_mountdir);
235                         asprintf (&details, "%s\n%llu of %llu %s (%2.0f%%) free on %s (type %s mounted on %s)",
236                                   details,
237                                   fsp.fsu_bavail*fsp.fsu_blocksize/mult,
238                                                                 units,
239                                   fsp.fsu_blocks*fsp.fsu_blocksize/mult,
240                                   (double)fsp.fsu_bavail*100/fsp.fsu_blocks,
241                                   me->me_devname,
242                                   me->me_type,
243                                   me->me_mountdir);
244                 }
246         }
248         if (verbose > 2)
249                 asprintf (&output, "%s%s", output, details);
251         terminate (result, "DISK %s%s\n", state_text (result), output, details);
256 \f
257 /* process command-line arguments */
258 int
259 process_arguments (int argc, char **argv)
261         int c;
262   struct name_list *se;
263   struct name_list **pathtail = &path_select_list;
264   struct name_list **devtail = &dev_select_list;
265   struct name_list **fstail = &fs_exclude_list;
266   struct name_list **dptail = &dp_exclude_list;
268         int option_index = 0;
269         static struct option long_options[] = {
270                 {"timeout", required_argument, 0, 't'},
271                 {"warning", required_argument, 0, 'w'},
272                 {"critical", required_argument, 0, 'c'},
273                 {"local", required_argument, 0, 'l'},
274                 {"kilobytes", required_argument, 0, 'k'},
275                 {"megabytes", required_argument, 0, 'm'},
276                 {"units", required_argument, 0, 'u'},
277                 {"path", required_argument, 0, 'p'},
278                 {"partition", required_argument, 0, 'p'},
279                 {"device", required_argument, 0, 'd'},
280                 {"exclude_device", required_argument, 0, 'x'},
281                 {"exclude-type", required_argument, 0, 'X'},
282                 {"mountpoint", no_argument, 0, 'M'},
283                 {"errors-only", no_argument, 0, 'e'},
284                 {"verbose", no_argument, 0, 'v'},
285                 {"quiet", no_argument, 0, 'q'},
286                 {"version", no_argument, 0, 'V'},
287                 {"help", no_argument, 0, 'h'},
288                 {0, 0, 0, 0}
289         };
291         if (argc < 2)
292                 return ERROR;
294         se = (struct name_list *) malloc (sizeof (struct name_list));
295         se->name = strdup ("iso9660");
296         se->name_next = NULL;
297         *fstail = se;
298         fstail = &se->name_next;
300         for (c = 1; c < argc; c++)
301                 if (strcmp ("-to", argv[c]) == 0)
302                         strcpy (argv[c], "-t");
304         while (1) {
305                 c = getopt_long (argc, argv, "+?Vqhvet:c:w:u:p:d:x:X:mklM", long_options, &option_index);
307                 if (c == -1 || c == EOF)
308                         break;
310                 switch (c) {
311                 case 't':                                                                       /* timeout period */
312                         if (is_integer (optarg)) {
313                                 timeout_interval = atoi (optarg);
314                                 break;
315                         }
316                         else {
317                                 usage ("Timeout Interval must be an integer!\n");
318                         }
319                 case 'w':                                                                       /* warning time threshold */
320                         if (is_intnonneg (optarg)) {
321                                 w_df = atoi (optarg);
322                                 break;
323                         }
324                         else if (strpbrk (optarg, ",:") &&
325                                                          strstr (optarg, "%") &&
326                                                          sscanf (optarg, "%d%*[:,]%f%%", &w_df, &w_dfp) == 2) {
327                                 break;
328                         }
329                         else if (strstr (optarg, "%") && sscanf (optarg, "%f%%", &w_dfp) == 1) {
330                                 break;
331                         }
332                         else {
333                                 usage ("Warning threshold must be integer or percentage!\n");
334                         }
335                 case 'c':                                                                       /* critical time threshold */
336                         if (is_intnonneg (optarg)) {
337                                 c_df = atoi (optarg);
338                                 break;
339                         }
340                         else if (strpbrk (optarg, ",:") &&
341                                                          strstr (optarg, "%") &&
342                                                          sscanf (optarg, "%d%*[,:]%f%%", &c_df, &c_dfp) == 2) {
343                                 break;
344                         }
345                         else if (strstr (optarg, "%") && sscanf (optarg, "%f%%", &c_dfp) == 1) {
346                                 break;
347                         }
348                         else {
349                                 usage ("Critical threshold must be integer or percentage!\n");
350                         }
351                 case 'u':
352                         if (! strcmp (optarg, "bytes")) {
353                                 mult = 1;
354                                 units = "B";
355                         } else if (! strcmp (optarg, "kB")) {
356                                 mult = 1024;
357                                 units = "kB";
358                         } else if (! strcmp (optarg, "MB")) {
359                                 mult = 1024 * 1024;
360                                 units = "MB";
361                         } else if (! strcmp (optarg, "GB")) {
362                                 mult = 1024 * 1024 * 1024;
363                                 units = "GB";
364                         } else if (! strcmp (optarg, "TB")) {
365                                 mult = (unsigned long)1024 * 1024 * 1024 * 1024;
366                                 units = "TB";
367                         } else {
368                                 terminate (STATE_UNKNOWN, "unit type %s not known", optarg);
369                         }
370                         break;
371                 case 'k': /* display mountpoint */
372                         mult = 1024;
373                         units = "kB";
374                         break;
375                 case 'm': /* display mountpoint */
376                         mult = 1024 * 1024;
377                         units = "MB";
378                         break;
379                 case 'l':
380                         show_local_fs = 1;                      
381                         break;
382                 case 'p':                                                                       /* selec path */
383                         se = (struct name_list *) malloc (sizeof (struct name_list));
384                         se->name = strdup (optarg);
385                         se->name_next = NULL;
386                         *pathtail = se;
387                         pathtail = &se->name_next;
388                         break;
389                 case 'd':                                                                       /* select partition/device */
390                         se = (struct name_list *) malloc (sizeof (struct name_list));
391                         se->name = strdup (optarg);
392                         se->name_next = NULL;
393                         *devtail = se;
394                         devtail = &se->name_next;
395                         break;
396                 case 'x':                                                                       /* exclude path or partition */
397                         se = (struct name_list *) malloc (sizeof (struct name_list));
398                         se->name = strdup (optarg);
399                         se->name_next = NULL;
400                         *dptail = se;
401                         dptail = &se->name_next;
402                         break;
403                         break;
404                 case 'X':                                                                       /* exclude file system type */
405                         se = (struct name_list *) malloc (sizeof (struct name_list));
406                         se->name = strdup (optarg);
407                         se->name_next = NULL;
408                         *fstail = se;
409                         fstail = &se->name_next;
410                         break;
411                 case 'v':                                                                       /* verbose */
412                         verbose++;
413                         break;
414                 case 'q':                                                                       /* verbose */
415                         verbose--;
416                         break;
417                 case 'e':
418                         erronly = TRUE;
419                         break;
420                 case 'M': /* display mountpoint */
421                         display_mntp = TRUE;
422                         break;
423                 case 'V':                                                                       /* version */
424                         print_revision (progname, revision);
425                         exit (STATE_OK);
426                 case 'h':                                                                       /* help */
427                         print_help ();
428                         exit (STATE_OK);
429                 case '?':                                                                       /* help */
430                         usage ("check_disk: unrecognized option\n");
431                         break;
432                 }
433         }
435         c = optind;
436         if (w_dfp == -1 && argc > c && is_intnonneg (argv[c]))
437                 w_dfp = (100.0 - atof (argv[c++]));
439         if (c_dfp == -1 && argc > c && is_intnonneg (argv[c]))
440                 c_dfp = (100.0 - atof (argv[c++]));
442         if (argc > c && strlen (path) == 0)
443                 path = argv[c++];
445         return validate_arguments ();
450 int
451 validate_arguments ()
453         if (w_df < 0 && c_df < 0 && w_dfp < 0 && c_dfp < 0) {
454                 printf ("INPUT ERROR: Unable to parse command line\n");
455                 return ERROR;
456         }
457         else if ((w_dfp >= 0 || c_dfp >= 0)
458                                          && (w_dfp < 0 || c_dfp < 0 || w_dfp > 100 || c_dfp > 100
459                                                          || c_dfp > w_dfp)) {
460                 printf
461                         ("INPUT ERROR: C_DFP (%f) should be less than W_DFP (%f) and both should be between zero and 100 percent, inclusive\n",
462                          c_dfp, w_dfp);
463                 return ERROR;
464         }
465         else if ((w_df > 0 || c_df > 0) && (w_df < 0 || c_df < 0 || c_df > w_df)) {
466                 printf
467                         ("INPUT ERROR: C_DF (%d) should be less than W_DF (%d) and both should be greater than zero\n",
468                          c_df, w_df);
469                 return ERROR;
470         }
471         else {
472                 return OK;
473         }
478 \f
479 int
480 check_disk (int usp, int free_disk)
482         int result = STATE_UNKNOWN;
483         /* check the percent used space against thresholds */
484         if (usp >= 0 && usp >= (100.0 - c_dfp))
485                 result = STATE_CRITICAL;
486         else if (c_df >= 0 && free_disk <= c_df)
487                 result = STATE_CRITICAL;
488         else if (usp >= 0 && usp >= (100.0 - w_dfp))
489                 result = STATE_WARNING;
490         else if (w_df >= 0 && free_disk <= w_df)
491                 result = STATE_WARNING;
492         else if (usp >= 0.0)
493                 result = STATE_OK;
494         return result;
499 int
500 walk_name_list (struct name_list *list, const char *name)
502         while (list) {
503                 if (! strcmp(list->name, name))
504                         return TRUE;
505                 list = list->name_next;
506         }
507         return FALSE;
512 \f
513 void
514 print_help (void)
516         print_revision (progname, revision);
517         printf ("Copyright (c) %s %s\n\t<%s>\n\n%s\n",
518                  copyright, authors, email, summary);
519         print_usage ();
520         printf ("\nOptions:\n");
521         printf (options);
522         printf (notes);
523         support ();
526 void
527 print_usage (void)
529         printf
530                 ("Usage: %s %s\n"
531                  "       %s (-h|--help)\n"
532                  "       %s (-V|--version)\n", progname, option_summary, progname, progname);