Code

fd3b9770a93b014aa7ca117ee603a112a42b9516
[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=PARTITION\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   int found;
110   struct name_list *name_next;
111 };
113 /* Linked list of filesystem types to display.
114    If `fs_select_list' is NULL, list all types.
115    This table is generated dynamically from command-line options,
116    rather than hardcoding into the program what it thinks are the
117    valid filesystem types; let the user specify any filesystem type
118    they want to, and if there are any filesystems of that type, they
119    will be shown.
121    Some filesystem types:
122    4.2 4.3 ufs nfs swap ignore io vm efs dbg */
124 static struct name_list *fs_select_list;
126 /* Linked list of filesystem types to omit.
127    If the list is empty, don't exclude any types.  */
129 static struct name_list *fs_exclude_list;
131 static struct name_list *dp_exclude_list;
133 static struct name_list *path_select_list;
135 static struct name_list *dev_select_list;
137 /* Linked list of mounted filesystems. */
138 static struct mount_entry *mount_list;
140 /* For long options that have no equivalent short option, use a
141    non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
142 enum
144   SYNC_OPTION = CHAR_MAX + 1,
145   NO_SYNC_OPTION,
146   BLOCK_SIZE_OPTION
147 };
149 #ifdef _AIX
150  #pragma alloca
151 #endif
153 int process_arguments (int, char **);
154 int validate_arguments (void);
155 int check_disk (int usp, int free_disk);
156 int walk_name_list (struct name_list *list, const char *name);
157 void print_help (void);
158 void print_usage (void);
160 int w_df = -1;
161 int c_df = -1;
162 float w_dfp = -1.0;
163 float c_dfp = -1.0;
164 char *path = "";
165 char *exclude_device = "";
166 char *units = "MB";
167 unsigned long mult = 1024 * 1024;
168 int verbose = 0;
169 int erronly = FALSE;
170 int display_mntp = FALSE;
172 /* Linked list of mounted filesystems. */
173 static struct mount_entry *mount_list;
175 int
176 main (int argc, char **argv)
178         int usp = -1;
179         int total_disk = -1;
180         int used_disk = -1;
181         int free_disk = -1;
182         int result = STATE_UNKNOWN;
183         int disk_result = STATE_UNKNOWN;
184         char *command_line = "";
185         char input_buffer[MAX_INPUT_BUFFER];
186         char file_system[MAX_INPUT_BUFFER];
187         char mntp[MAX_INPUT_BUFFER];
188         char *output = "";
189         char *details = "";
190         float free_space, free_space_pct, total_space;
192         struct mount_entry *me;
193         struct fs_usage fsp;
194         struct name_list *temp_list;
195         char *disk;
197         mount_list = read_filesystem_list (0);
199         if (process_arguments (argc, argv) != OK)
200                 usage ("Could not parse arguments\n");
202         for (me = mount_list; me; me = me->me_next) {
204                 if (path_select_list &&
205                      (walk_name_list (path_select_list, me->me_mountdir) ||
206                       walk_name_list (path_select_list, me->me_devname) ) )
207                         get_fs_usage (me->me_mountdir, me->me_devname, &fsp);
208                 else if (dev_select_list || path_select_list)
209                         continue;
210                 else if (me->me_remote && show_local_fs)
211                         continue;
212                 else if (me->me_dummy && !show_all_fs)
213                         continue;
214                 else if (fs_exclude_list && walk_name_list (fs_exclude_list, me->me_type))
215                         continue;
216                 else if (dp_exclude_list && 
217                          walk_name_list (dp_exclude_list, me->me_devname) ||
218                          walk_name_list (dp_exclude_list, me->me_mountdir))
219                         continue;
220                 else
221                         get_fs_usage (me->me_mountdir, me->me_devname, &fsp);
223                 if (fsp.fsu_blocks && strcmp ("none", me->me_mountdir)) {
224                         usp = (fsp.fsu_blocks - fsp.fsu_bavail) * 100 / fsp.fsu_blocks;
225                         disk_result = check_disk (usp, fsp.fsu_bavail);
226                         result = max_state (disk_result, result);
227                         if (disk_result==STATE_OK && erronly && !verbose)
228                                 continue;
230                         free_space = (float)fsp.fsu_bavail*fsp.fsu_blocksize/mult;
231                         free_space_pct = (float)fsp.fsu_bavail*100/fsp.fsu_blocks;
232                         total_space = (float)fsp.fsu_blocks*fsp.fsu_blocksize/mult;
233                         if (disk_result!=STATE_OK || verbose>=0)
234                                 asprintf (&output, "%s [%.0f %s (%2.0f%%) free on %s]",
235                                           output,
236                                           free_space,
237                                           units,
238                                           free_space_pct,
239                                           (!strcmp(file_system, "none") || display_mntp) ? me->me_devname : me->me_mountdir);
240                         asprintf (&details, "%s\n%.0f of %.0f %s (%2.0f%%) free on %s (type %s mounted on %s)",
241                                   details,
242                                   free_space,
243                                   total_space,
244                                   units,
245                                   free_space_pct,
246                                   me->me_devname,
247                                   me->me_type,
248                                   me->me_mountdir);
249                 }
251         }
253         if (verbose > 2)
254                 asprintf (&output, "%s%s", output, details);
256         /* Override result if paths specified and not found */
257         temp_list = path_select_list;
258         while (temp_list) {
259                 if (temp_list->found != TRUE) {
260                         asprintf (&output, "%s [%s not found]", output, temp_list->name);
261                         result = STATE_CRITICAL;
262                 }
263                 temp_list = temp_list->name_next;
264         }
266         terminate (result, "DISK %s%s\n", state_text (result), output, details);
271 \f
272 /* process command-line arguments */
273 int
274 process_arguments (int argc, char **argv)
276         int c;
277         struct name_list *se;
278         struct name_list **pathtail = &path_select_list;
279         struct name_list **devtail = &dev_select_list;
280         struct name_list **fstail = &fs_exclude_list;
281         struct name_list **dptail = &dp_exclude_list;
283         int option_index = 0;
284         static struct option long_options[] = {
285                 {"timeout", required_argument, 0, 't'},
286                 {"warning", required_argument, 0, 'w'},
287                 {"critical", required_argument, 0, 'c'},
288                 {"local", required_argument, 0, 'l'},
289                 {"kilobytes", required_argument, 0, 'k'},
290                 {"megabytes", required_argument, 0, 'm'},
291                 {"units", required_argument, 0, 'u'},
292                 {"path", required_argument, 0, 'p'},
293                 {"partition", required_argument, 0, 'p'},
294                 {"exclude_device", required_argument, 0, 'x'},
295                 {"exclude-type", required_argument, 0, 'X'},
296                 {"mountpoint", no_argument, 0, 'M'},
297                 {"errors-only", no_argument, 0, 'e'},
298                 {"verbose", no_argument, 0, 'v'},
299                 {"quiet", no_argument, 0, 'q'},
300                 {"version", no_argument, 0, 'V'},
301                 {"help", no_argument, 0, 'h'},
302                 {0, 0, 0, 0}
303         };
305         if (argc < 2)
306                 return ERROR;
308         se = (struct name_list *) malloc (sizeof (struct name_list));
309         se->name = strdup ("iso9660");
310         se->name_next = NULL;
311         *fstail = se;
312         fstail = &se->name_next;
314         for (c = 1; c < argc; c++)
315                 if (strcmp ("-to", argv[c]) == 0)
316                         strcpy (argv[c], "-t");
318         while (1) {
319                 c = getopt_long (argc, argv, "+?Vqhvet:c:w:u:p:x:X:mklM", long_options, &option_index);
321                 if (c == -1 || c == EOF)
322                         break;
324                 switch (c) {
325                 case 't':                                                                       /* timeout period */
326                         if (is_integer (optarg)) {
327                                 timeout_interval = atoi (optarg);
328                                 break;
329                         }
330                         else {
331                                 usage ("Timeout Interval must be an integer!\n");
332                         }
333                 case 'w':                                                                       /* warning time threshold */
334                         if (is_intnonneg (optarg)) {
335                                 w_df = atoi (optarg);
336                                 break;
337                         }
338                         else if (strpbrk (optarg, ",:") &&
339                                                          strstr (optarg, "%") &&
340                                                          sscanf (optarg, "%d%*[:,]%f%%", &w_df, &w_dfp) == 2) {
341                                 break;
342                         }
343                         else if (strstr (optarg, "%") && sscanf (optarg, "%f%%", &w_dfp) == 1) {
344                                 break;
345                         }
346                         else {
347                                 usage ("Warning threshold must be integer or percentage!\n");
348                         }
349                 case 'c':                                                                       /* critical time threshold */
350                         if (is_intnonneg (optarg)) {
351                                 c_df = atoi (optarg);
352                                 break;
353                         }
354                         else if (strpbrk (optarg, ",:") &&
355                                                          strstr (optarg, "%") &&
356                                                          sscanf (optarg, "%d%*[,:]%f%%", &c_df, &c_dfp) == 2) {
357                                 break;
358                         }
359                         else if (strstr (optarg, "%") && sscanf (optarg, "%f%%", &c_dfp) == 1) {
360                                 break;
361                         }
362                         else {
363                                 usage ("Critical threshold must be integer or percentage!\n");
364                         }
365                 case 'u':
366                         if (! strcmp (optarg, "bytes")) {
367                                 mult = 1;
368                                 units = "B";
369                         } else if (! strcmp (optarg, "kB")) {
370                                 mult = 1024;
371                                 units = "kB";
372                         } else if (! strcmp (optarg, "MB")) {
373                                 mult = 1024 * 1024;
374                                 units = "MB";
375                         } else if (! strcmp (optarg, "GB")) {
376                                 mult = 1024 * 1024 * 1024;
377                                 units = "GB";
378                         } else if (! strcmp (optarg, "TB")) {
379                                 mult = (unsigned long)1024 * 1024 * 1024 * 1024;
380                                 units = "TB";
381                         } else {
382                                 terminate (STATE_UNKNOWN, "unit type %s not known\n", optarg);
383                         }
384                         break;
385                 case 'k': /* display mountpoint */
386                         mult = 1024;
387                         units = "kB";
388                         break;
389                 case 'm': /* display mountpoint */
390                         mult = 1024 * 1024;
391                         units = "MB";
392                         break;
393                 case 'l':
394                         show_local_fs = 1;                      
395                         break;
396                 case 'p':                                                                       /* select path */
397                         se = (struct name_list *) malloc (sizeof (struct name_list));
398                         se->name = strdup (optarg);
399                         se->name_next = NULL;
400                         *pathtail = se;
401                         pathtail = &se->name_next;
402                         break;
403                 case 'x':                                                                       /* exclude path or partition */
404                         se = (struct name_list *) malloc (sizeof (struct name_list));
405                         se->name = strdup (optarg);
406                         se->name_next = NULL;
407                         *dptail = se;
408                         dptail = &se->name_next;
409                         break;
410                         break;
411                 case 'X':                                                                       /* exclude file system type */
412                         se = (struct name_list *) malloc (sizeof (struct name_list));
413                         se->name = strdup (optarg);
414                         se->name_next = NULL;
415                         *fstail = se;
416                         fstail = &se->name_next;
417                         break;
418                 case 'v':                                                                       /* verbose */
419                         verbose++;
420                         break;
421                 case 'q':                                                                       /* verbose */
422                         verbose--;
423                         break;
424                 case 'e':
425                         erronly = TRUE;
426                         break;
427                 case 'M': /* display mountpoint */
428                         display_mntp = TRUE;
429                         break;
430                 case 'V':                                                                       /* version */
431                         print_revision (progname, revision);
432                         exit (STATE_OK);
433                 case 'h':                                                                       /* help */
434                         print_help ();
435                         exit (STATE_OK);
436                 case '?':                                                                       /* help */
437                         usage ("check_disk: unrecognized option\n");
438                         break;
439                 }
440         }
442         c = optind;
443         if (w_dfp == -1 && argc > c && is_intnonneg (argv[c]))
444                 w_dfp = (100.0 - atof (argv[c++]));
446         if (c_dfp == -1 && argc > c && is_intnonneg (argv[c]))
447                 c_dfp = (100.0 - atof (argv[c++]));
449         if (argc > c && strlen (path) == 0)
450                 path = argv[c++];
452         return validate_arguments ();
457 int
458 validate_arguments ()
460         if (w_df < 0 && c_df < 0 && w_dfp < 0 && c_dfp < 0) {
461                 printf ("INPUT ERROR: Unable to parse command line\n");
462                 return ERROR;
463         }
464         else if ((w_dfp >= 0 || c_dfp >= 0)
465                                          && (w_dfp < 0 || c_dfp < 0 || w_dfp > 100 || c_dfp > 100
466                                                          || c_dfp > w_dfp)) {
467                 printf
468                         ("INPUT ERROR: C_DFP (%f) should be less than W_DFP (%f) and both should be between zero and 100 percent, inclusive\n",
469                          c_dfp, w_dfp);
470                 return ERROR;
471         }
472         else if ((w_df > 0 || c_df > 0) && (w_df < 0 || c_df < 0 || c_df > w_df)) {
473                 printf
474                         ("INPUT ERROR: C_DF (%d) should be less than W_DF (%d) and both should be greater than zero\n",
475                          c_df, w_df);
476                 return ERROR;
477         }
478         else {
479                 return OK;
480         }
485 \f
486 int
487 check_disk (int usp, int free_disk)
489         int result = STATE_UNKNOWN;
490         /* check the percent used space against thresholds */
491         if (usp >= 0 && usp >= (100.0 - c_dfp))
492                 result = STATE_CRITICAL;
493         else if (c_df >= 0 && free_disk <= c_df)
494                 result = STATE_CRITICAL;
495         else if (usp >= 0 && usp >= (100.0 - w_dfp))
496                 result = STATE_WARNING;
497         else if (w_df >= 0 && free_disk <= w_df)
498                 result = STATE_WARNING;
499         else if (usp >= 0.0)
500                 result = STATE_OK;
501         return result;
506 int
507 walk_name_list (struct name_list *list, const char *name)
509         while (list) {
510                 if (! strcmp(list->name, name)) {
511                         list->found = 1;
512                         return TRUE;
513                 }
514                 list = list->name_next;
515         }
516         return FALSE;
521 \f
522 void
523 print_help (void)
525         print_revision (progname, revision);
526         printf ("Copyright (c) %s %s\n\t<%s>\n\n%s\n",
527                  copyright, authors, email, summary);
528         print_usage ();
529         printf ("\nOptions:\n");
530         printf (options);
531         printf (notes);
532         support ();
535 void
536 print_usage (void)
538         printf
539                 ("Usage: %s %s\n"
540                  "       %s (-h|--help)\n"
541                  "       %s (-V|--version)\n", progname, option_summary, progname, progname);