Code

Problems compiling on SunOS 5.6 with gcc 2.8.1. Fixed by specifying
[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   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 = "";
189         float free_space, free_space_pct, total_space;
191         struct mount_entry *me;
192         struct fs_usage fsp;
193         char *disk;
195         mount_list = read_filesystem_list (0);
197         if (process_arguments (argc, argv) != OK)
198                 usage ("Could not parse arguments\n");
200         for (me = mount_list; me; me = me->me_next) {
202                 if ((dev_select_list &&
203                      walk_name_list (dev_select_list, me->me_devname)) ||
204                     (path_select_list &&
205                      walk_name_list (path_select_list, me->me_mountdir)))
206                         get_fs_usage (me->me_mountdir, me->me_devname, &fsp);
207                 else if (dev_select_list || path_select_list)
208                         continue;
209                 else if (me->me_remote && show_local_fs)
210                         continue;
211                 else if (me->me_dummy && !show_all_fs)
212                         continue;
213                 else if (fs_exclude_list && walk_name_list (fs_exclude_list, me->me_type))
214                         continue;
215                 else if (dp_exclude_list && 
216                          walk_name_list (dp_exclude_list, me->me_devname) ||
217                          walk_name_list (dp_exclude_list, me->me_mountdir))
218                         continue;
219                 else
220                         get_fs_usage (me->me_mountdir, me->me_devname, &fsp);
222                 if (fsp.fsu_blocks && strcmp ("none", me->me_mountdir)) {
223                         usp = (fsp.fsu_blocks - fsp.fsu_bavail) * 100 / fsp.fsu_blocks;
224                         disk_result = check_disk (usp, fsp.fsu_bavail);
225                         result = max_state (disk_result, result);
226                         if (disk_result==STATE_OK && erronly && !verbose)
227                                 continue;
229                         free_space = (float)fsp.fsu_bavail*fsp.fsu_blocksize/mult;
230                         free_space_pct = (float)fsp.fsu_bavail*100/fsp.fsu_blocks;
231                         total_space = (float)fsp.fsu_blocks*fsp.fsu_blocksize/mult;
232                         if (disk_result!=STATE_OK || verbose>=0)
233                                 asprintf (&output, "%s [%.0f %s (%2.0f%%) free on %s]",
234                                           output,
235                                           free_space,
236                                           units,
237                                           free_space_pct,
238                                           (!strcmp(file_system, "none") || display_mntp) ? me->me_devname : me->me_mountdir);
239                         asprintf (&details, "%s\n%.0f of %.0f %s (%2.0f%%) free on %s (type %s mounted on %s)",
240                                   details,
241                                   free_space,
242                                   total_space,
243                                   units,
244                                   free_space_pct,
245                                   me->me_devname,
246                                   me->me_type,
247                                   me->me_mountdir);
248                 }
250         }
252         if (verbose > 2)
253                 asprintf (&output, "%s%s", output, details);
255         terminate (result, "DISK %s%s\n", state_text (result), output, details);
260 \f
261 /* process command-line arguments */
262 int
263 process_arguments (int argc, char **argv)
265         int c;
266   struct name_list *se;
267   struct name_list **pathtail = &path_select_list;
268   struct name_list **devtail = &dev_select_list;
269   struct name_list **fstail = &fs_exclude_list;
270   struct name_list **dptail = &dp_exclude_list;
272         int option_index = 0;
273         static struct option long_options[] = {
274                 {"timeout", required_argument, 0, 't'},
275                 {"warning", required_argument, 0, 'w'},
276                 {"critical", required_argument, 0, 'c'},
277                 {"local", required_argument, 0, 'l'},
278                 {"kilobytes", required_argument, 0, 'k'},
279                 {"megabytes", required_argument, 0, 'm'},
280                 {"units", required_argument, 0, 'u'},
281                 {"path", required_argument, 0, 'p'},
282                 {"partition", required_argument, 0, 'p'},
283                 {"device", required_argument, 0, 'd'},
284                 {"exclude_device", required_argument, 0, 'x'},
285                 {"exclude-type", required_argument, 0, 'X'},
286                 {"mountpoint", no_argument, 0, 'M'},
287                 {"errors-only", no_argument, 0, 'e'},
288                 {"verbose", no_argument, 0, 'v'},
289                 {"quiet", no_argument, 0, 'q'},
290                 {"version", no_argument, 0, 'V'},
291                 {"help", no_argument, 0, 'h'},
292                 {0, 0, 0, 0}
293         };
295         if (argc < 2)
296                 return ERROR;
298         se = (struct name_list *) malloc (sizeof (struct name_list));
299         se->name = strdup ("iso9660");
300         se->name_next = NULL;
301         *fstail = se;
302         fstail = &se->name_next;
304         for (c = 1; c < argc; c++)
305                 if (strcmp ("-to", argv[c]) == 0)
306                         strcpy (argv[c], "-t");
308         while (1) {
309                 c = getopt_long (argc, argv, "+?Vqhvet:c:w:u:p:d:x:X:mklM", long_options, &option_index);
311                 if (c == -1 || c == EOF)
312                         break;
314                 switch (c) {
315                 case 't':                                                                       /* timeout period */
316                         if (is_integer (optarg)) {
317                                 timeout_interval = atoi (optarg);
318                                 break;
319                         }
320                         else {
321                                 usage ("Timeout Interval must be an integer!\n");
322                         }
323                 case 'w':                                                                       /* warning time threshold */
324                         if (is_intnonneg (optarg)) {
325                                 w_df = atoi (optarg);
326                                 break;
327                         }
328                         else if (strpbrk (optarg, ",:") &&
329                                                          strstr (optarg, "%") &&
330                                                          sscanf (optarg, "%d%*[:,]%f%%", &w_df, &w_dfp) == 2) {
331                                 break;
332                         }
333                         else if (strstr (optarg, "%") && sscanf (optarg, "%f%%", &w_dfp) == 1) {
334                                 break;
335                         }
336                         else {
337                                 usage ("Warning threshold must be integer or percentage!\n");
338                         }
339                 case 'c':                                                                       /* critical time threshold */
340                         if (is_intnonneg (optarg)) {
341                                 c_df = atoi (optarg);
342                                 break;
343                         }
344                         else if (strpbrk (optarg, ",:") &&
345                                                          strstr (optarg, "%") &&
346                                                          sscanf (optarg, "%d%*[,:]%f%%", &c_df, &c_dfp) == 2) {
347                                 break;
348                         }
349                         else if (strstr (optarg, "%") && sscanf (optarg, "%f%%", &c_dfp) == 1) {
350                                 break;
351                         }
352                         else {
353                                 usage ("Critical threshold must be integer or percentage!\n");
354                         }
355                 case 'u':
356                         if (! strcmp (optarg, "bytes")) {
357                                 mult = 1;
358                                 units = "B";
359                         } else if (! strcmp (optarg, "kB")) {
360                                 mult = 1024;
361                                 units = "kB";
362                         } else if (! strcmp (optarg, "MB")) {
363                                 mult = 1024 * 1024;
364                                 units = "MB";
365                         } else if (! strcmp (optarg, "GB")) {
366                                 mult = 1024 * 1024 * 1024;
367                                 units = "GB";
368                         } else if (! strcmp (optarg, "TB")) {
369                                 mult = (unsigned long)1024 * 1024 * 1024 * 1024;
370                                 units = "TB";
371                         } else {
372                                 terminate (STATE_UNKNOWN, "unit type %s not known", optarg);
373                         }
374                         break;
375                 case 'k': /* display mountpoint */
376                         mult = 1024;
377                         units = "kB";
378                         break;
379                 case 'm': /* display mountpoint */
380                         mult = 1024 * 1024;
381                         units = "MB";
382                         break;
383                 case 'l':
384                         show_local_fs = 1;                      
385                         break;
386                 case 'p':                                                                       /* selec path */
387                         se = (struct name_list *) malloc (sizeof (struct name_list));
388                         se->name = strdup (optarg);
389                         se->name_next = NULL;
390                         *pathtail = se;
391                         pathtail = &se->name_next;
392                         break;
393                 case 'd':                                                                       /* select partition/device */
394                         se = (struct name_list *) malloc (sizeof (struct name_list));
395                         se->name = strdup (optarg);
396                         se->name_next = NULL;
397                         *devtail = se;
398                         devtail = &se->name_next;
399                         break;
400                 case 'x':                                                                       /* exclude path or partition */
401                         se = (struct name_list *) malloc (sizeof (struct name_list));
402                         se->name = strdup (optarg);
403                         se->name_next = NULL;
404                         *dptail = se;
405                         dptail = &se->name_next;
406                         break;
407                         break;
408                 case 'X':                                                                       /* exclude file system type */
409                         se = (struct name_list *) malloc (sizeof (struct name_list));
410                         se->name = strdup (optarg);
411                         se->name_next = NULL;
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 'V':                                                                       /* version */
428                         print_revision (progname, revision);
429                         exit (STATE_OK);
430                 case 'h':                                                                       /* help */
431                         print_help ();
432                         exit (STATE_OK);
433                 case '?':                                                                       /* help */
434                         usage ("check_disk: unrecognized option\n");
435                         break;
436                 }
437         }
439         c = optind;
440         if (w_dfp == -1 && argc > c && is_intnonneg (argv[c]))
441                 w_dfp = (100.0 - atof (argv[c++]));
443         if (c_dfp == -1 && argc > c && is_intnonneg (argv[c]))
444                 c_dfp = (100.0 - atof (argv[c++]));
446         if (argc > c && strlen (path) == 0)
447                 path = argv[c++];
449         return validate_arguments ();
454 int
455 validate_arguments ()
457         if (w_df < 0 && c_df < 0 && w_dfp < 0 && c_dfp < 0) {
458                 printf ("INPUT ERROR: Unable to parse command line\n");
459                 return ERROR;
460         }
461         else if ((w_dfp >= 0 || c_dfp >= 0)
462                                          && (w_dfp < 0 || c_dfp < 0 || w_dfp > 100 || c_dfp > 100
463                                                          || c_dfp > w_dfp)) {
464                 printf
465                         ("INPUT ERROR: C_DFP (%f) should be less than W_DFP (%f) and both should be between zero and 100 percent, inclusive\n",
466                          c_dfp, w_dfp);
467                 return ERROR;
468         }
469         else if ((w_df > 0 || c_df > 0) && (w_df < 0 || c_df < 0 || c_df > w_df)) {
470                 printf
471                         ("INPUT ERROR: C_DF (%d) should be less than W_DF (%d) and both should be greater than zero\n",
472                          c_df, w_df);
473                 return ERROR;
474         }
475         else {
476                 return OK;
477         }
482 \f
483 int
484 check_disk (int usp, int free_disk)
486         int result = STATE_UNKNOWN;
487         /* check the percent used space against thresholds */
488         if (usp >= 0 && usp >= (100.0 - c_dfp))
489                 result = STATE_CRITICAL;
490         else if (c_df >= 0 && free_disk <= c_df)
491                 result = STATE_CRITICAL;
492         else if (usp >= 0 && usp >= (100.0 - w_dfp))
493                 result = STATE_WARNING;
494         else if (w_df >= 0 && free_disk <= w_df)
495                 result = STATE_WARNING;
496         else if (usp >= 0.0)
497                 result = STATE_OK;
498         return result;
503 int
504 walk_name_list (struct name_list *list, const char *name)
506         while (list) {
507                 if (! strcmp(list->name, name))
508                         return TRUE;
509                 list = list->name_next;
510         }
511         return FALSE;
516 \f
517 void
518 print_help (void)
520         print_revision (progname, revision);
521         printf ("Copyright (c) %s %s\n\t<%s>\n\n%s\n",
522                  copyright, authors, email, summary);
523         print_usage ();
524         printf ("\nOptions:\n");
525         printf (options);
526         printf (notes);
527         support ();
530 void
531 print_usage (void)
533         printf
534                 ("Usage: %s %s\n"
535                  "       %s (-h|--help)\n"
536                  "       %s (-V|--version)\n", progname, option_summary, progname, progname);