Code

afs checking
[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"
67 /* If nonzero, show inode information. */
68 static int inode_format;
70 /* If nonzero, show even filesystems with zero size or
71    uninteresting types. */
72 static int show_all_fs;
74 /* If nonzero, show only local filesystems.  */
75 static int show_local_fs;
77 /* If nonzero, output data for each filesystem corresponding to a
78    command line argument -- even if it's a dummy (automounter) entry.  */
79 static int show_listed_fs;
81 /* If positive, the units to use when printing sizes;
82    if negative, the human-readable base.  */
83 static int output_block_size;
85 /* If nonzero, invoke the `sync' system call before getting any usage data.
86    Using this option can make df very slow, especially with many or very
87    busy disks.  Note that this may make a difference on some systems --
88    SunOs4.1.3, for one.  It is *not* necessary on Linux.  */
89 static int require_sync = 0;
91 /* A filesystem type to display. */
93 struct fs_type_list
94 {
95   char *fs_name;
96   struct fs_type_list *fs_next;
97 };
99 /* Linked list of filesystem types to display.
100    If `fs_select_list' is NULL, list all types.
101    This table is generated dynamically from command-line options,
102    rather than hardcoding into the program what it thinks are the
103    valid filesystem types; let the user specify any filesystem type
104    they want to, and if there are any filesystems of that type, they
105    will be shown.
107    Some filesystem types:
108    4.2 4.3 ufs nfs swap ignore io vm efs dbg */
110 static struct fs_type_list *fs_select_list;
112 /* Linked list of filesystem types to omit.
113    If the list is empty, don't exclude any types.  */
115 static struct fs_type_list *fs_exclude_list;
117 /* Linked list of mounted filesystems. */
118 static struct mount_entry *mount_list;
120 /* For long options that have no equivalent short option, use a
121    non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
122 enum
124   SYNC_OPTION = CHAR_MAX + 1,
125   NO_SYNC_OPTION,
126   BLOCK_SIZE_OPTION
127 };
129 #ifdef _AIX
130  #pragma alloca
131 #endif
133 int process_arguments (int, char **);
134 int validate_arguments (void);
135 int check_disk (int usp, int free_disk);
136 void print_help (void);
137 void print_usage (void);
139 int w_df = -1;
140 int c_df = -1;
141 float w_dfp = -1.0;
142 float c_dfp = -1.0;
143 char *path = "";
144 char *exclude_device = "";
145 int verbose = 0;
146 int erronly = FALSE;
147 int display_mntp = FALSE;
150 int
151 main (int argc, char **argv)
153         int usp = -1;
154         int total_disk = -1;
155         int used_disk = -1;
156         int free_disk = -1;
157         int result = STATE_UNKNOWN;
158         int disk_result = STATE_UNKNOWN;
159         char *command_line = "";
160         char input_buffer[MAX_INPUT_BUFFER];
161         char file_system[MAX_INPUT_BUFFER];
162         char mntp[MAX_INPUT_BUFFER];
163         char *output = "";
165         struct fs_usage fsp;
166         char *disk;
168         if (process_arguments (argc, argv) != OK)
169                 usage ("Could not parse arguments\n");
171         get_fs_usage (path, disk, &fsp);
173         usp = (fsp.fsu_blocks - fsp.fsu_bavail) / fsp.fsu_blocks;
174         disk_result = check_disk (usp, fsp.fsu_bavail);
175         result = disk_result;
176         asprintf (&output, "%llu of %llu kB (%2.0f%%) free (%d-byte blocks)",
177                   fsp.fsu_bavail*fsp.fsu_blocksize/1024,
178                   fsp.fsu_blocks*fsp.fsu_blocksize/1024,
179                   (double)fsp.fsu_bavail*100/fsp.fsu_blocks,
180                   fsp.fsu_blocksize);
182         terminate (result, "DISK %s %s\n", state_text (result), output);
185 /* process command-line arguments */
186 int
187 process_arguments (int argc, char **argv)
189         int c;
191         int option_index = 0;
192         static struct option long_options[] = {
193                 {"warning", required_argument, 0, 'w'},
194                 {"critical", required_argument, 0, 'c'},
195                 {"timeout", required_argument, 0, 't'},
196                 {"path", required_argument, 0, 'p'},
197                 {"partition", required_argument, 0, 'p'},
198                 {"verbose", no_argument, 0, 'v'},
199                 {"version", no_argument, 0, 'V'},
200                 {"errors-only", no_argument, 0, 'e'},
201                 {"help", no_argument, 0, 'h'},
202                 {"mountpoint", no_argument, 0, 'm'},
203                 {"exclude_device", required_argument, 0, 'x'},
204                 {"quiet", no_argument, 0, 'q'},
206                 {0, 0, 0, 0}
207         };
209         if (argc < 2)
210                 return ERROR;
212         for (c = 1; c < argc; c++)
213                 if (strcmp ("-to", argv[c]) == 0)
214                         strcpy (argv[c], "-t");
216         while (1) {
217                 c = getopt_long (argc, argv, "+?Vqhvet:c:w:p:x:m", long_options, &option_index);
219                 if (c == -1 || c == EOF)
220                         break;
222                 switch (c) {
223                 case 'w':                                                                       /* warning time threshold */
224                         if (is_intnonneg (optarg)) {
225                                 w_df = atoi (optarg);
226                                 break;
227                         }
228                         else if (strpbrk (optarg, ",:") &&
229                                                          strstr (optarg, "%") &&
230                                                          sscanf (optarg, "%d%*[:,]%f%%", &w_df, &w_dfp) == 2) {
231                                 break;
232                         }
233                         else if (strstr (optarg, "%") && sscanf (optarg, "%f%%", &w_dfp) == 1) {
234                                 break;
235                         }
236                         else {
237                                 usage ("Warning threshold must be integer or percentage!\n");
238                         }
239                 case 'c':                                                                       /* critical time threshold */
240                         if (is_intnonneg (optarg)) {
241                                 c_df = atoi (optarg);
242                                 break;
243                         }
244                         else if (strpbrk (optarg, ",:") &&
245                                                          strstr (optarg, "%") &&
246                                                          sscanf (optarg, "%d%*[,:]%f%%", &c_df, &c_dfp) == 2) {
247                                 break;
248                         }
249                         else if (strstr (optarg, "%") && sscanf (optarg, "%f%%", &c_dfp) == 1) {
250                                 break;
251                         }
252                         else {
253                                 usage ("Critical threshold must be integer or percentage!\n");
254                         }
255                 case 't':                                                                       /* timeout period */
256                         if (is_integer (optarg)) {
257                                 timeout_interval = atoi (optarg);
258                                 break;
259                         }
260                         else {
261                                 usage ("Timeout Interval must be an integer!\n");
262                         }
263                 case 'p':                                                                       /* path or partition */
264                         path = optarg;
265                         break;
266                 case 'v':                                                                       /* verbose */
267                         verbose++;
268                         break;
269                 case 'q':                                                                       /* verbose */
270                         verbose--;
271                         break;
272                 case 'e':
273                         erronly = TRUE;
274                         break;
275                 case 'm': /* display mountpoint */
276                         display_mntp = TRUE;
277                         break;
278                 case 'x':                                                                       /* exclude path or partition */
279                         exclude_device = optarg;
280                         break;
281                 case 'V':                                                                       /* version */
282                         print_revision (progname, revision);
283                         exit (STATE_OK);
284                 case 'h':                                                                       /* help */
285                         print_help ();
286                         exit (STATE_OK);
287                 case '?':                                                                       /* help */
288                         usage ("check_disk: unrecognized option\n");
289                         break;
290                 }
291         }
293         c = optind;
294         if (w_dfp == -1 && argc > c && is_intnonneg (argv[c]))
295                 w_dfp = (100.0 - atof (argv[c++]));
297         if (c_dfp == -1 && argc > c && is_intnonneg (argv[c]))
298                 c_dfp = (100.0 - atof (argv[c++]));
300         if (argc > c && strlen (path) == 0)
301                 path = argv[c++];
303         return validate_arguments ();
306 int
307 validate_arguments ()
309         if (w_df < 0 && c_df < 0 && w_dfp < 0 && c_dfp < 0) {
310                 printf ("INPUT ERROR: Unable to parse command line\n");
311                 return ERROR;
312         }
313         else if ((w_dfp >= 0 || c_dfp >= 0)
314                                          && (w_dfp < 0 || c_dfp < 0 || w_dfp > 100 || c_dfp > 100
315                                                          || c_dfp > w_dfp)) {
316                 printf
317                         ("INPUT ERROR: C_DFP (%f) should be less than W_DFP (%f) and both should be between zero and 100 percent, inclusive\n",
318                          c_dfp, w_dfp);
319                 return ERROR;
320         }
321         else if ((w_df > 0 || c_df > 0) && (w_df < 0 || c_df < 0 || c_df > w_df)) {
322                 printf
323                         ("INPUT ERROR: C_DF (%d) should be less than W_DF (%d) and both should be greater than zero\n",
324                          c_df, w_df);
325                 return ERROR;
326         }
327         else {
328                 return OK;
329         }
332 int
333 check_disk (usp, free_disk)
335         int result = STATE_UNKNOWN;
336         /* check the percent used space against thresholds */
337         if (usp >= 0 && usp >= (100.0 - c_dfp))
338                 result = STATE_CRITICAL;
339         else if (c_df >= 0 && free_disk <= c_df)
340                 result = STATE_CRITICAL;
341         else if (usp >= 0 && usp >= (100.0 - w_dfp))
342                 result = STATE_WARNING;
343         else if (w_df >= 0 && free_disk <= w_df)
344                 result = STATE_WARNING;
345         else if (usp >= 0.0)
346                 result = STATE_OK;
347         return result;
350 void
351 print_help (void)
353         print_revision (progname, revision);
354         printf ("Copyright (c) %s %s\n\t<%s>\n\n%s\n",
355                  copyright, authors, email, summary);
356         print_usage ();
357         printf ("\nOptions:\n");
358         printf (options);
359         support ();
362 void
363 print_usage (void)
365         printf
366                 ("Usage: %s %s\n"
367                  "       %s (-h|--help)\n"
368                  "       %s (-V|--version)\n", progname, option_summary, progname, progname);