Code

Minor changes to build for Solaris packages
[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  -p, --path=PATH, --partition=PARTTION\n\
43     Path or partition (checks all mounted partitions if unspecified)\n\
44  -m, --mountpoint\n\
45     Display the mountpoint instead of the partition\n\
46  -x, --exclude_device=PATH\n\
47     Ignore device (only works if -p unspecified)\n\
48  -e, --errors-only\n\
49     Display only devices/mountpoints with errors\n\
50  -v, --verbose\n\
51     Show details for command-line debugging (do not use with nagios server)\n\
52  -h, --help\n\
53     Print detailed help screen\n\
54  -V, --version\n\
55     Print version information\n";
57 #include "common.h"
58 #if HAVE_INTTYPES_H
59 # include <inttypes.h>
60 #endif
61 #include <assert.h>
62 #include "popen.h"
63 #include "utils.h"
64 #include <stdarg.h>
65 #include "../lib/fsusage.h"
66 #include "../lib/mountlist.h"
67 #if HAVE_LIMITS_H
68 # include <limits.h>
69 #endif
71 /* If nonzero, show inode information. */
72 static int inode_format;
74 /* If nonzero, show even filesystems with zero size or
75    uninteresting types. */
76 static int show_all_fs;
78 /* If nonzero, show only local filesystems.  */
79 static int show_local_fs;
81 /* If nonzero, output data for each filesystem corresponding to a
82    command line argument -- even if it's a dummy (automounter) entry.  */
83 static int show_listed_fs;
85 /* If positive, the units to use when printing sizes;
86    if negative, the human-readable base.  */
87 static int output_block_size;
89 /* If nonzero, invoke the `sync' system call before getting any usage data.
90    Using this option can make df very slow, especially with many or very
91    busy disks.  Note that this may make a difference on some systems --
92    SunOs4.1.3, for one.  It is *not* necessary on Linux.  */
93 static int require_sync = 0;
95 /* A filesystem type to display. */
97 struct name_list
98 {
99   char *name;
100   struct name_list *name_next;
101 };
103 /* Linked list of filesystem types to display.
104    If `fs_select_list' is NULL, list all types.
105    This table is generated dynamically from command-line options,
106    rather than hardcoding into the program what it thinks are the
107    valid filesystem types; let the user specify any filesystem type
108    they want to, and if there are any filesystems of that type, they
109    will be shown.
111    Some filesystem types:
112    4.2 4.3 ufs nfs swap ignore io vm efs dbg */
114 static struct name_list *fs_select_list;
116 /* Linked list of filesystem types to omit.
117    If the list is empty, don't exclude any types.  */
119 static struct name_list *fs_exclude_list;
121 static struct name_list *dp_exclude_list;
123 static struct name_list *path_select_list;
125 static struct name_list *dev_select_list;
127 /* Linked list of mounted filesystems. */
128 static struct mount_entry *mount_list;
130 /* For long options that have no equivalent short option, use a
131    non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
132 enum
134   SYNC_OPTION = CHAR_MAX + 1,
135   NO_SYNC_OPTION,
136   BLOCK_SIZE_OPTION
137 };
139 #ifdef _AIX
140  #pragma alloca
141 #endif
143 int process_arguments (int, char **);
144 int validate_arguments (void);
145 int check_disk (int usp, int free_disk);
146 int walk_name_list (struct name_list *list, const char *name);
147 void print_help (void);
148 void print_usage (void);
150 int w_df = -1;
151 int c_df = -1;
152 float w_dfp = -1.0;
153 float c_dfp = -1.0;
154 char *path = "";
155 char *exclude_device = "";
156 int verbose = 0;
157 int erronly = FALSE;
158 int display_mntp = FALSE;
160 /* Linked list of mounted filesystems. */
161 static struct mount_entry *mount_list;
163 int
164 main (int argc, char **argv)
166         int usp = -1;
167         int total_disk = -1;
168         int used_disk = -1;
169         int free_disk = -1;
170         int result = STATE_UNKNOWN;
171         int disk_result = STATE_UNKNOWN;
172         char *command_line = "";
173         char input_buffer[MAX_INPUT_BUFFER];
174         char file_system[MAX_INPUT_BUFFER];
175         char mntp[MAX_INPUT_BUFFER];
176         char *output = "";
177         char *details = "";
179   struct mount_entry *me;
180         struct fs_usage fsp;
181         char *disk;
183         mount_list = read_filesystem_list (0);
185         if (process_arguments (argc, argv) != OK)
186                 usage ("Could not parse arguments\n");
188   for (me = mount_list; me; me = me->me_next) {
190                 if ((dev_select_list &&
191                      walk_name_list (dev_select_list, me->me_devname)) ||
192                     (path_select_list &&
193                      walk_name_list (path_select_list, me->me_mountdir)))
194                         get_fs_usage (me->me_mountdir, me->me_devname, &fsp);
195                 else if (dev_select_list || path_select_list)
196                         continue;
197                 else if (fs_exclude_list && walk_name_list (fs_exclude_list, me->me_type))
198                         continue;
199                 else if (dp_exclude_list && 
200                          walk_name_list (dp_exclude_list, me->me_devname) ||
201                          walk_name_list (dp_exclude_list, me->me_mountdir))
202                         continue;
203                 else
204                         get_fs_usage (me->me_mountdir, me->me_devname, &fsp);
206                 if (fsp.fsu_blocks && strcmp ("none", me->me_mountdir)) {
207                         usp = (fsp.fsu_blocks - fsp.fsu_bavail) * 100 / fsp.fsu_blocks;
208                         disk_result = check_disk (usp, fsp.fsu_bavail);
209                         result = max_state (disk_result, result);
210                         if (disk_result==STATE_OK && erronly && !verbose)
211                                 continue;
213                         if (disk_result!=STATE_OK || verbose>=0) 
214                                 asprintf (&output, "%s [%llu MB (%2.0f%%) free on %s]",
215                                           output,
216                                           fsp.fsu_bavail*fsp.fsu_blocksize/1024/1024,
217                                           (double)fsp.fsu_bavail*100/fsp.fsu_blocks,
218                                           (!strcmp(file_system, "none") || display_mntp) ? me->me_devname : me->me_mountdir);
219                         asprintf (&details, "%s\n%llu of %llu MB (%2.0f%%) free on %s (type %s mounted on %s)",
220                                   details,
221                                   fsp.fsu_bavail*fsp.fsu_blocksize/1024/1024,
222                                   fsp.fsu_blocks*fsp.fsu_blocksize/1024/1024,
223                                   (double)fsp.fsu_bavail*100/fsp.fsu_blocks,
224                                   me->me_devname,
225                                   me->me_type,
226                                   me->me_mountdir);
227                 }
229         }
231         if (verbose > 2)
232                 asprintf (&output, "%s%s", output, details);
234         terminate (result, "DISK %s%s\n", state_text (result), output, details);
239 \f
240 /* process command-line arguments */
241 int
242 process_arguments (int argc, char **argv)
244         int c;
245   struct name_list *se;
246   struct name_list **pathtail = &path_select_list;
247   struct name_list **devtail = &dev_select_list;
248   struct name_list **fstail = &fs_exclude_list;
249   struct name_list **dptail = &dp_exclude_list;
251         int option_index = 0;
252         static struct option long_options[] = {
253                 {"warning", required_argument, 0, 'w'},
254                 {"critical", required_argument, 0, 'c'},
255                 {"timeout", required_argument, 0, 't'},
256                 {"path", required_argument, 0, 'p'},
257                 {"partition", required_argument, 0, 'p'},
258                 {"verbose", no_argument, 0, 'v'},
259                 {"version", no_argument, 0, 'V'},
260                 {"errors-only", no_argument, 0, 'e'},
261                 {"help", no_argument, 0, 'h'},
262                 {"mountpoint", no_argument, 0, 'm'},
263                 {"device", no_argument, 0, 'd'},
264                 {"exclude_device", required_argument, 0, 'x'},
265                 {"quiet", no_argument, 0, 'q'},
267                 {0, 0, 0, 0}
268         };
270         if (argc < 2)
271                 return ERROR;
273         for (c = 1; c < argc; c++)
274                 if (strcmp ("-to", argv[c]) == 0)
275                         strcpy (argv[c], "-t");
277         while (1) {
278                 c = getopt_long (argc, argv, "+?Vqhvet:c:w:p:d:x:X:m", long_options, &option_index);
280                 if (c == -1 || c == EOF)
281                         break;
283                 switch (c) {
284                 case 'w':                                                                       /* warning time threshold */
285                         if (is_intnonneg (optarg)) {
286                                 w_df = atoi (optarg);
287                                 break;
288                         }
289                         else if (strpbrk (optarg, ",:") &&
290                                                          strstr (optarg, "%") &&
291                                                          sscanf (optarg, "%d%*[:,]%f%%", &w_df, &w_dfp) == 2) {
292                                 break;
293                         }
294                         else if (strstr (optarg, "%") && sscanf (optarg, "%f%%", &w_dfp) == 1) {
295                                 break;
296                         }
297                         else {
298                                 usage ("Warning threshold must be integer or percentage!\n");
299                         }
300                 case 'c':                                                                       /* critical time threshold */
301                         if (is_intnonneg (optarg)) {
302                                 c_df = atoi (optarg);
303                                 break;
304                         }
305                         else if (strpbrk (optarg, ",:") &&
306                                                          strstr (optarg, "%") &&
307                                                          sscanf (optarg, "%d%*[,:]%f%%", &c_df, &c_dfp) == 2) {
308                                 break;
309                         }
310                         else if (strstr (optarg, "%") && sscanf (optarg, "%f%%", &c_dfp) == 1) {
311                                 break;
312                         }
313                         else {
314                                 usage ("Critical threshold must be integer or percentage!\n");
315                         }
316                 case 't':                                                                       /* timeout period */
317                         if (is_integer (optarg)) {
318                                 timeout_interval = atoi (optarg);
319                                 break;
320                         }
321                         else {
322                                 usage ("Timeout Interval must be an integer!\n");
323                         }
324                 case 'p':                                                                       /* selec path */
325                         se = (struct name_list *) malloc (sizeof (struct name_list));
326                         se->name = strdup (optarg);
327                         se->name_next = NULL;
328                         *pathtail = se;
329                         pathtail = &se->name_next;
330                         break;
331                 case 'd':                                                                       /* select partition/device */
332                         se = (struct name_list *) malloc (sizeof (struct name_list));
333                         se->name = strdup (optarg);
334                         se->name_next = NULL;
335                         *devtail = se;
336                         devtail = &se->name_next;
337                         break;
338                 case 'x':                                                                       /* exclude path or partition */
339                         se = (struct name_list *) malloc (sizeof (struct name_list));
340                         se->name = strdup (optarg);
341                         se->name_next = NULL;
342                         *dptail = se;
343                         dptail = &se->name_next;
344                         break;
345                         break;
346                 case 'X':                                                                       /* exclude file system type */
347                         se = (struct name_list *) malloc (sizeof (struct name_list));
348                         se->name = strdup (optarg);
349                         se->name_next = NULL;
350                         *fstail = se;
351                         fstail = &se->name_next;
352                         break;
353                 case 'v':                                                                       /* verbose */
354                         verbose++;
355                         break;
356                 case 'q':                                                                       /* verbose */
357                         verbose--;
358                         break;
359                 case 'e':
360                         erronly = TRUE;
361                         break;
362                 case 'm': /* display mountpoint */
363                         display_mntp = TRUE;
364                         break;
365                 case 'V':                                                                       /* version */
366                         print_revision (progname, revision);
367                         exit (STATE_OK);
368                 case 'h':                                                                       /* help */
369                         print_help ();
370                         exit (STATE_OK);
371                 case '?':                                                                       /* help */
372                         usage ("check_disk: unrecognized option\n");
373                         break;
374                 }
375         }
377         c = optind;
378         if (w_dfp == -1 && argc > c && is_intnonneg (argv[c]))
379                 w_dfp = (100.0 - atof (argv[c++]));
381         if (c_dfp == -1 && argc > c && is_intnonneg (argv[c]))
382                 c_dfp = (100.0 - atof (argv[c++]));
384         if (argc > c && strlen (path) == 0)
385                 path = argv[c++];
387         return validate_arguments ();
392 int
393 validate_arguments ()
395         if (w_df < 0 && c_df < 0 && w_dfp < 0 && c_dfp < 0) {
396                 printf ("INPUT ERROR: Unable to parse command line\n");
397                 return ERROR;
398         }
399         else if ((w_dfp >= 0 || c_dfp >= 0)
400                                          && (w_dfp < 0 || c_dfp < 0 || w_dfp > 100 || c_dfp > 100
401                                                          || c_dfp > w_dfp)) {
402                 printf
403                         ("INPUT ERROR: C_DFP (%f) should be less than W_DFP (%f) and both should be between zero and 100 percent, inclusive\n",
404                          c_dfp, w_dfp);
405                 return ERROR;
406         }
407         else if ((w_df > 0 || c_df > 0) && (w_df < 0 || c_df < 0 || c_df > w_df)) {
408                 printf
409                         ("INPUT ERROR: C_DF (%d) should be less than W_DF (%d) and both should be greater than zero\n",
410                          c_df, w_df);
411                 return ERROR;
412         }
413         else {
414                 return OK;
415         }
420 \f
421 int
422 check_disk (int usp, int free_disk)
424         int result = STATE_UNKNOWN;
425         /* check the percent used space against thresholds */
426         if (usp >= 0 && usp >= (100.0 - c_dfp))
427                 result = STATE_CRITICAL;
428         else if (c_df >= 0 && free_disk <= c_df)
429                 result = STATE_CRITICAL;
430         else if (usp >= 0 && usp >= (100.0 - w_dfp))
431                 result = STATE_WARNING;
432         else if (w_df >= 0 && free_disk <= w_df)
433                 result = STATE_WARNING;
434         else if (usp >= 0.0)
435                 result = STATE_OK;
436         return result;
441 int
442 walk_name_list (struct name_list *list, const char *name)
444         while (list) {
445                 if (! strcmp(list->name, name))
446                         return TRUE;
447                 list = list->name_next;
448         }
449         return FALSE;
454 \f
455 void
456 print_help (void)
458         print_revision (progname, revision);
459         printf ("Copyright (c) %s %s\n\t<%s>\n\n%s\n",
460                  copyright, authors, email, summary);
461         print_usage ();
462         printf ("\nOptions:\n");
463         printf (options);
464         support ();
467 void
468 print_usage (void)
470         printf
471                 ("Usage: %s %s\n"
472                  "       %s (-h|--help)\n"
473                  "       %s (-V|--version)\n", progname, option_summary, progname, progname);