diff options
Diffstat (limited to 'web/attachments/178709-check_disk.c')
| -rw-r--r-- | web/attachments/178709-check_disk.c | 914 |
1 files changed, 914 insertions, 0 deletions
diff --git a/web/attachments/178709-check_disk.c b/web/attachments/178709-check_disk.c new file mode 100644 index 0000000..0e97150 --- /dev/null +++ b/web/attachments/178709-check_disk.c | |||
| @@ -0,0 +1,914 @@ | |||
| 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 | $Id: check_disk.c,v 1.65.3m 2006/05/20 21:34:01 maemigh Exp $ | ||
| 18 | |||
| 19 | *****************************************************************************/ | ||
| 20 | |||
| 21 | const char *progname = "check_disk"; | ||
| 22 | const char *program_name = "check_disk"; /* Required for coreutils libs */ | ||
| 23 | const char *revision = "$Revision: 1.65.3 maemigh $"; | ||
| 24 | const char *copyright = "1999-2005"; | ||
| 25 | const char *email = "nagiosplug-devel@lists.sourceforge.net"; | ||
| 26 | |||
| 27 | /* | ||
| 28 | * Additional inode code by Jorgen Lundman <lundman@lundman.net> | ||
| 29 | */ | ||
| 30 | |||
| 31 | |||
| 32 | #include "common.h" | ||
| 33 | #if HAVE_INTTYPES_H | ||
| 34 | # include <inttypes.h> | ||
| 35 | #endif | ||
| 36 | #include <assert.h> | ||
| 37 | #include "popen.h" | ||
| 38 | #include "utils.h" | ||
| 39 | #include <stdarg.h> | ||
| 40 | #include "../lib/fsusage.h" | ||
| 41 | #include "../lib/mountlist.h" | ||
| 42 | #if HAVE_LIMITS_H | ||
| 43 | # include <limits.h> | ||
| 44 | #endif | ||
| 45 | |||
| 46 | /* If nonzero, show even filesystems with zero size or | ||
| 47 | uninteresting types. */ | ||
| 48 | static int show_all_fs = 1; | ||
| 49 | |||
| 50 | /* If nonzero, show only local filesystems. */ | ||
| 51 | static int show_local_fs = 0; | ||
| 52 | |||
| 53 | /* If nonzero, show output in HTML format. */ | ||
| 54 | static int show_html = 0; | ||
| 55 | |||
| 56 | /* If nonzero, output percent of space and inodes USED rather than percent free */ | ||
| 57 | static int show_used = 0; | ||
| 58 | |||
| 59 | /* If zero, display all mounts even with -p specified */ | ||
| 60 | static int path_select_exclude_others = 1; | ||
| 61 | |||
| 62 | /* If positive, the units to use when printing sizes; | ||
| 63 | if negative, the human-readable base. */ | ||
| 64 | /* static int output_block_size; */ | ||
| 65 | |||
| 66 | /* If nonzero, invoke the `sync' system call before getting any usage data. | ||
| 67 | Using this option can make df very slow, especially with many or very | ||
| 68 | busy disks. Note that this may make a difference on some systems -- | ||
| 69 | SunOs4.1.3, for one. It is *not* necessary on Linux. */ | ||
| 70 | /* static int require_sync = 0; */ | ||
| 71 | |||
| 72 | /* A filesystem type to display. */ | ||
| 73 | |||
| 74 | struct name_list | ||
| 75 | { | ||
| 76 | char *name; | ||
| 77 | int exclude; | ||
| 78 | int found; | ||
| 79 | int foundexact; | ||
| 80 | int found_len; | ||
| 81 | uintmax_t w_df; | ||
| 82 | uintmax_t c_df; | ||
| 83 | double w_dfp; | ||
| 84 | double c_dfp; | ||
| 85 | double w_idfp; | ||
| 86 | double c_idfp; | ||
| 87 | struct name_list *name_next; | ||
| 88 | }; | ||
| 89 | |||
| 90 | /* Linked list of filesystem types to display. | ||
| 91 | If `fs_select_list' is NULL, list all types. | ||
| 92 | This table is generated dynamically from command-line options, | ||
| 93 | rather than hardcoding into the program what it thinks are the | ||
| 94 | valid filesystem types; let the user specify any filesystem type | ||
| 95 | they want to, and if there are any filesystems of that type, they | ||
| 96 | will be shown. | ||
| 97 | |||
| 98 | Some filesystem types: | ||
| 99 | 4.2 4.3 ufs nfs swap ignore io vm efs dbg */ | ||
| 100 | |||
| 101 | /* static struct name_list *fs_select_list; */ | ||
| 102 | |||
| 103 | /* Linked list of filesystem types to omit. | ||
| 104 | If the list is empty, don't exclude any types. */ | ||
| 105 | |||
| 106 | static struct name_list *fs_exclude_list; | ||
| 107 | |||
| 108 | static struct name_list *dp_exclude_list; | ||
| 109 | |||
| 110 | static struct name_list *path_select_list; | ||
| 111 | |||
| 112 | static struct name_list *dev_select_list; | ||
| 113 | |||
| 114 | /* Linked list of mounted filesystems. */ | ||
| 115 | static struct mount_entry *mount_list; | ||
| 116 | |||
| 117 | /* For long options that have no equivalent short option, use a | ||
| 118 | non-character as a pseudo short option, starting with CHAR_MAX + 1. */ | ||
| 119 | enum | ||
| 120 | { | ||
| 121 | SYNC_OPTION = CHAR_MAX + 1, | ||
| 122 | NO_SYNC_OPTION, | ||
| 123 | BLOCK_SIZE_OPTION | ||
| 124 | }; | ||
| 125 | |||
| 126 | #ifdef _AIX | ||
| 127 | #pragma alloca | ||
| 128 | #endif | ||
| 129 | |||
| 130 | int process_arguments (int, char **); | ||
| 131 | void print_path (const char *mypath); | ||
| 132 | int validate_arguments (uintmax_t, uintmax_t, double, double, double, double, char *); | ||
| 133 | int check_disk (double usp, uintmax_t free_disk, double uisp); | ||
| 134 | int walk_name_list (struct name_list *list, const char *name); | ||
| 135 | void print_help (void); | ||
| 136 | void print_usage (void); | ||
| 137 | |||
| 138 | uintmax_t w_df = 0; | ||
| 139 | uintmax_t c_df = 0; | ||
| 140 | double w_dfp = -1.0; | ||
| 141 | double c_dfp = -1.0; | ||
| 142 | double w_idfp = -1.0; | ||
| 143 | double c_idfp = -1.0; | ||
| 144 | uintmax_t dw_df = 0; | ||
| 145 | uintmax_t dc_df = 0; | ||
| 146 | double dw_dfp = -1.0; | ||
| 147 | double dc_dfp = -1.0; | ||
| 148 | double dw_idfp = -1.0; | ||
| 149 | double dc_idfp = -1.0; | ||
| 150 | |||
| 151 | char *path; | ||
| 152 | char *exclude_device; | ||
| 153 | char *units; | ||
| 154 | uintmax_t mult = 1024 * 1024; | ||
| 155 | int verbose = 0; | ||
| 156 | int erronly = FALSE; | ||
| 157 | int display_mntp = FALSE; | ||
| 158 | int longest_length = 0; | ||
| 159 | /* Linked list of mounted filesystems. */ | ||
| 160 | static struct mount_entry *mount_list; | ||
| 161 | |||
| 162 | |||
| 163 | |||
| 164 | int | ||
| 165 | main (int argc, char **argv) | ||
| 166 | { | ||
| 167 | double usp = -1.0, uisp = -1.0; | ||
| 168 | int result = STATE_UNKNOWN; | ||
| 169 | int disk_result = STATE_UNKNOWN; | ||
| 170 | char file_system[MAX_INPUT_BUFFER]; | ||
| 171 | char *output; | ||
| 172 | char *details; | ||
| 173 | char *perf; | ||
| 174 | uintmax_t psize; | ||
| 175 | float free_space, free_space_pct, total_space, inode_space_pct; | ||
| 176 | |||
| 177 | struct mount_entry *me; | ||
| 178 | struct fs_usage fsp; | ||
| 179 | struct name_list *temp_list; | ||
| 180 | |||
| 181 | output = strdup (" - free space:"); | ||
| 182 | details = strdup (""); | ||
| 183 | perf = strdup (""); | ||
| 184 | |||
| 185 | setlocale (LC_ALL, ""); | ||
| 186 | bindtextdomain (PACKAGE, LOCALEDIR); | ||
| 187 | textdomain (PACKAGE); | ||
| 188 | |||
| 189 | mount_list = read_filesystem_list (0); | ||
| 190 | |||
| 191 | if (process_arguments (argc, argv) == ERROR) | ||
| 192 | usage4 (_("Could not parse arguments")); | ||
| 193 | |||
| 194 | /* if a list of paths has been selected, preseed the list with | ||
| 195 | * the longest matching filesystem name by iterating across | ||
| 196 | * the mountlist once ahead of time. this will allow a query on | ||
| 197 | * "/var/log" to return information about "/var" if no "/var/log" | ||
| 198 | * filesystem exists, etc. this is the default behavior already | ||
| 199 | * with df-based checks, but for systems with their own space | ||
| 200 | * checking routines, this should make them more consistent. | ||
| 201 | * | ||
| 202 | * Also keep track of the longest path that will be displayed | ||
| 203 | * in order to format in HTML. | ||
| 204 | */ | ||
| 205 | |||
| 206 | /* The walk_name_list function will keep track of longest_length */ | ||
| 207 | if(path_select_list){ | ||
| 208 | for (me = mount_list; me; me = me->me_next) { | ||
| 209 | walk_name_list(path_select_list, me->me_devname); | ||
| 210 | walk_name_list(path_select_list, me->me_mountdir); | ||
| 211 | } | ||
| 212 | } | ||
| 213 | /* Longest length is only kept track of when the list passed is NOT | ||
| 214 | * and exclude list. Thus we must verify the longest path that | ||
| 215 | * will be displayed in the output. | ||
| 216 | */ | ||
| 217 | else { | ||
| 218 | for (me = mount_list; me; me = me->me_next) { | ||
| 219 | if(!walk_name_list(fs_exclude_list, me->me_type) && | ||
| 220 | !walk_name_list(dp_exclude_list, me->me_devname) && | ||
| 221 | !walk_name_list(dp_exclude_list, me->me_mountdir)) { | ||
| 222 | get_fs_usage (me->me_mountdir, me->me_devname, &fsp); | ||
| 223 | if (fsp.fsu_blocks && strcmp ("none", me->me_mountdir)) { | ||
| 224 | if (!strcmp(file_system, "none") || display_mntp) { | ||
| 225 | if(strlen(me->me_devname) > longest_length) longest_length = strlen(me->me_devname); | ||
| 226 | } else { | ||
| 227 | if(strlen(me->me_mountdir) > longest_length) longest_length = strlen(me->me_mountdir); | ||
| 228 | } | ||
| 229 | } | ||
| 230 | } | ||
| 231 | } | ||
| 232 | } | ||
| 233 | /* now pretend we never saw anything, but keep found_len. | ||
| 234 | * thus future searches will only match the best match */ | ||
| 235 | for (temp_list = path_select_list; temp_list; temp_list=temp_list->name_next){ | ||
| 236 | temp_list->found=0; | ||
| 237 | } | ||
| 238 | |||
| 239 | /* for every mount entry */ | ||
| 240 | for (me = mount_list; me; me = me->me_next) { | ||
| 241 | /* if there's a list of paths to select, the current mount | ||
| 242 | * entry matches in path or device name, get fs usage */ | ||
| 243 | if (path_select_list && | ||
| 244 | (walk_name_list(path_select_list, me->me_devname) || | ||
| 245 | walk_name_list(path_select_list, me->me_mountdir))) { | ||
| 246 | get_fs_usage (me->me_mountdir, me->me_devname, &fsp); | ||
| 247 | /* else if there's a list of paths/devices to select (but | ||
| 248 | * we didn't match above) skip to the next mount entry */ | ||
| 249 | } else if (dev_select_list || (path_select_list && path_select_exclude_others)) { | ||
| 250 | continue; | ||
| 251 | /* skip remote filesystems if we're not interested in them */ | ||
| 252 | } else if (me->me_remote && show_local_fs) { | ||
| 253 | continue; | ||
| 254 | /* skip pseudo fs's if we haven't asked for all fs's */ | ||
| 255 | } else if (me->me_dummy && !show_all_fs) { | ||
| 256 | continue; | ||
| 257 | /* skip excluded fstypes */ | ||
| 258 | } else if (fs_exclude_list && walk_name_list (fs_exclude_list, me->me_type)) { | ||
| 259 | continue; | ||
| 260 | /* skip excluded fs's */ | ||
| 261 | } else if (dp_exclude_list && | ||
| 262 | (walk_name_list (dp_exclude_list, me->me_devname) || | ||
| 263 | walk_name_list (dp_exclude_list, me->me_mountdir))) { | ||
| 264 | continue; | ||
| 265 | /* otherwise, get fs usage */ | ||
| 266 | } else { | ||
| 267 | /* Reset thresholds to defaults */ | ||
| 268 | w_df = dw_df; | ||
| 269 | c_df = dc_df; | ||
| 270 | w_dfp = dw_dfp; | ||
| 271 | c_dfp = dc_dfp; | ||
| 272 | w_idfp = dw_idfp; | ||
| 273 | c_idfp = dc_idfp; | ||
| 274 | get_fs_usage (me->me_mountdir, me->me_devname, &fsp); | ||
| 275 | } | ||
| 276 | if (fsp.fsu_blocks && strcmp ("none", me->me_mountdir)) { | ||
| 277 | usp = (double)(fsp.fsu_blocks - fsp.fsu_bavail) * 100 / fsp.fsu_blocks; | ||
| 278 | uisp = (double)(fsp.fsu_files - fsp.fsu_ffree) * 100 / fsp.fsu_files; | ||
| 279 | disk_result = check_disk (usp, fsp.fsu_bavail, uisp); | ||
| 280 | |||
| 281 | |||
| 282 | result = max_state (disk_result, result); | ||
| 283 | psize = fsp.fsu_blocks*fsp.fsu_blocksize/mult; | ||
| 284 | |||
| 285 | |||
| 286 | /* Moved this computation up here so we can add it | ||
| 287 | * to perf */ | ||
| 288 | inode_space_pct = (float)fsp.fsu_ffree*100/fsp.fsu_files; | ||
| 289 | |||
| 290 | asprintf (&perf, "%s %s", perf, | ||
| 291 | perfdata ((!strcmp(file_system, "none") || display_mntp) ? me->me_devname : me->me_mountdir, | ||
| 292 | psize-(fsp.fsu_bavail*fsp.fsu_blocksize/mult), units, | ||
| 293 | TRUE, min ((uintmax_t)psize-(uintmax_t)w_df, (uintmax_t)((1.0-w_dfp/100.0)*psize)), | ||
| 294 | TRUE, min ((uintmax_t)psize-(uintmax_t)c_df, (uintmax_t)((1.0-c_dfp/100.0)*psize)), | ||
| 295 | TRUE, inode_space_pct, | ||
| 296 | |||
| 297 | TRUE, psize)); | ||
| 298 | if (disk_result==STATE_OK && erronly && !verbose) | ||
| 299 | continue; | ||
| 300 | |||
| 301 | free_space = (float)fsp.fsu_bavail*fsp.fsu_blocksize/mult; | ||
| 302 | free_space_pct = (float)fsp.fsu_bavail*100/fsp.fsu_blocks; | ||
| 303 | total_space = (float)fsp.fsu_blocks*fsp.fsu_blocksize/mult; | ||
| 304 | if (disk_result!=STATE_OK || verbose>=0) { | ||
| 305 | if (show_html == 1) { | ||
| 306 | int x = 0; | ||
| 307 | int delta_len = (longest_length - strlen((!strcmp(file_system, "none") || display_mntp) ? me->me_devname : me->me_mountdir)); | ||
| 308 | for(x=delta_len; x >= 0; x--) { | ||
| 309 | if((!strcmp(file_system, "none") || display_mntp)) strcat(me->me_devname, " "); | ||
| 310 | else strcat(me->me_mountdir, " "); | ||
| 311 | } | ||
| 312 | asprintf (&output, ("%s <br/>%s%s %6.0f %s (%3.0f%% inode=%3.0f%%);%s"), | ||
| 313 | output, | ||
| 314 | (disk_result!=STATE_OK) ? "*" : " ", | ||
| 315 | (!strcmp(file_system, "none") || display_mntp) ? me->me_devname : me->me_mountdir, | ||
| 316 | free_space, | ||
| 317 | units, | ||
| 318 | (show_used == 1) ? 100-free_space_pct : free_space_pct, | ||
| 319 | (show_used == 1) ? 100-inode_space_pct : inode_space_pct, | ||
| 320 | (disk_result!=STATE_OK) ? "*" : " "); | ||
| 321 | } else { | ||
| 322 | asprintf (&output, ("%s %s %.0f %s (%.0f%% inode=%.0f%%);"), | ||
| 323 | output, | ||
| 324 | (!strcmp(file_system, "none") || display_mntp) ? me->me_devname : me->me_mountdir, | ||
| 325 | free_space, | ||
| 326 | units, | ||
| 327 | (show_used == 1) ? 100-free_space_pct : free_space_pct, | ||
| 328 | (show_used == 1) ? 100-inode_space_pct : inode_space_pct); | ||
| 329 | } | ||
| 330 | } | ||
| 331 | asprintf (&details, _("%s\n\ | ||
| 332 | %.0f of %.0f %s (%.0f%% inode=%.0f%%) free on %s (type %s mounted on %s) warn:%lu crit:%lu warn%%:%.0f%% crit%%:%.0f%%"), | ||
| 333 | details, free_space, total_space, units, free_space_pct, inode_space_pct, | ||
| 334 | me->me_devname, me->me_type, me->me_mountdir, | ||
| 335 | (unsigned long)w_df, (unsigned long)c_df, w_dfp, c_dfp); | ||
| 336 | |||
| 337 | } | ||
| 338 | |||
| 339 | } | ||
| 340 | |||
| 341 | if (verbose > 2) | ||
| 342 | asprintf (&output, "%s%s", output, details); | ||
| 343 | |||
| 344 | /* Override result if paths specified and not found */ | ||
| 345 | temp_list = path_select_list; | ||
| 346 | while (temp_list) { | ||
| 347 | if (!temp_list->found) { | ||
| 348 | asprintf (&output, _("%s [%s not found]"), output, temp_list->name); | ||
| 349 | result = STATE_CRITICAL; | ||
| 350 | } | ||
| 351 | temp_list = temp_list->name_next; | ||
| 352 | } | ||
| 353 | if (show_html == 1) | ||
| 354 | printf ("<pre>DISK %s%s|%s\n", state_text (result), output, perf); | ||
| 355 | else | ||
| 356 | printf ("DISK %s%s|%s\n", state_text (result), output, perf); | ||
| 357 | return result; | ||
| 358 | } | ||
| 359 | |||
| 360 | |||
| 361 | |||
| 362 | /* process command-line arguments */ | ||
| 363 | int | ||
| 364 | process_arguments (int argc, char **argv) | ||
| 365 | { | ||
| 366 | int c; | ||
| 367 | struct name_list *se; | ||
| 368 | struct name_list **pathtail = &path_select_list; | ||
| 369 | struct name_list **fstail = &fs_exclude_list; | ||
| 370 | struct name_list **dptail = &dp_exclude_list; | ||
| 371 | struct name_list *temp_list; | ||
| 372 | int result = OK; | ||
| 373 | struct stat *stat_buf; | ||
| 374 | |||
| 375 | unsigned long l; | ||
| 376 | |||
| 377 | int option = 0; | ||
| 378 | static struct option longopts[] = { | ||
| 379 | {"timeout", required_argument, 0, 't'}, | ||
| 380 | {"warning", required_argument, 0, 'w'}, | ||
| 381 | {"critical", required_argument, 0, 'c'}, | ||
| 382 | {"iwarning", required_argument, 0, 'W'}, | ||
| 383 | /* Dang, -C is taken. We might want to reshuffle this. */ | ||
| 384 | {"icritical", required_argument, 0, 'K'}, | ||
| 385 | {"local", required_argument, 0, 'l'}, | ||
| 386 | {"kilobytes", required_argument, 0, 'k'}, | ||
| 387 | {"megabytes", required_argument, 0, 'm'}, | ||
| 388 | {"units", required_argument, 0, 'u'}, | ||
| 389 | {"all", no_argument, 0, 'a'}, | ||
| 390 | {"path", required_argument, 0, 'p'}, | ||
| 391 | {"partition", required_argument, 0, 'p'}, | ||
| 392 | {"exclude_device", required_argument, 0, 'x'}, | ||
| 393 | {"exclude_path", required_argument, 0, 's'}, | ||
| 394 | {"exclude-type", required_argument, 0, 'X'}, | ||
| 395 | {"mountpoint", no_argument, 0, 'M'}, | ||
| 396 | {"errors-only", no_argument, 0, 'e'}, | ||
| 397 | {"verbose", no_argument, 0, 'v'}, | ||
| 398 | {"quiet", no_argument, 0, 'q'}, | ||
| 399 | {"clear", no_argument, 0, 'C'}, | ||
| 400 | {"version", no_argument, 0, 'V'}, | ||
| 401 | {"help", no_argument, 0, 'h'}, | ||
| 402 | {"html", no_argument, 0, 'H'}, | ||
| 403 | {"used", no_argument, 0, 'U'}, | ||
| 404 | {0, 0, 0, 0} | ||
| 405 | }; | ||
| 406 | |||
| 407 | if (argc < 2) | ||
| 408 | return ERROR; | ||
| 409 | |||
| 410 | se = (struct name_list *) malloc (sizeof (struct name_list)); | ||
| 411 | se->name = strdup ("iso9660"); | ||
| 412 | se->name_next = NULL; | ||
| 413 | se->found = 0; | ||
| 414 | se->foundexact = 0; | ||
| 415 | se->exclude = 0; | ||
| 416 | se->found_len = 0; | ||
| 417 | *fstail = se; | ||
| 418 | fstail = &se->name_next; | ||
| 419 | for (c = 1; c < argc; c++) | ||
| 420 | if (strcmp ("-to", argv[c]) == 0) | ||
| 421 | strcpy (argv[c], "-t"); | ||
| 422 | |||
| 423 | while (1) { | ||
| 424 | c = getopt_long (argc, argv, "+?VqhHiUveCat:c:w:K:W:u:p:x:s:X:mklM", longopts, &option); | ||
| 425 | |||
| 426 | if (c == -1 || c == EOF) | ||
| 427 | break; | ||
| 428 | |||
| 429 | switch (c) { | ||
| 430 | case 't': /* timeout period */ | ||
| 431 | if (is_integer (optarg)) { | ||
| 432 | timeout_interval = atoi (optarg); | ||
| 433 | break; | ||
| 434 | } | ||
| 435 | else { | ||
| 436 | usage2 (_("Timeout interval must be a positive integer"), optarg); | ||
| 437 | } | ||
| 438 | case 'w': /* warning threshold */ | ||
| 439 | if (is_intnonneg (optarg)) { | ||
| 440 | w_df = atoi (optarg); | ||
| 441 | break; | ||
| 442 | } | ||
| 443 | else if (strpbrk (optarg, ",:") && | ||
| 444 | strstr (optarg, "%") && | ||
| 445 | sscanf (optarg, "%lu%*[:,]%lf%%", &l, &w_dfp) == 2) { | ||
| 446 | w_df = (uintmax_t)l; | ||
| 447 | break; | ||
| 448 | } | ||
| 449 | else if (strstr (optarg, "%") && sscanf (optarg, "%lf%%", &w_dfp) == 1) { | ||
| 450 | break; | ||
| 451 | } | ||
| 452 | else { | ||
| 453 | usage4 (_("Warning threshold must be integer or percentage!")); | ||
| 454 | } | ||
| 455 | case 'c': /* critical threshold */ | ||
| 456 | if (is_intnonneg (optarg)) { | ||
| 457 | c_df = atoi (optarg); | ||
| 458 | break; | ||
| 459 | } | ||
| 460 | else if (strpbrk (optarg, ",:") && | ||
| 461 | strstr (optarg, "%") && | ||
| 462 | sscanf (optarg, "%lu%*[,:]%lf%%", &l, &c_dfp) == 2) { | ||
| 463 | c_df = (uintmax_t)l; | ||
| 464 | break; | ||
| 465 | } | ||
| 466 | else if (strstr (optarg, "%") && sscanf (optarg, "%lf%%", &c_dfp) == 1) { | ||
| 467 | break; | ||
| 468 | } | ||
| 469 | else { | ||
| 470 | usage4 (_("Critical threshold must be integer or percentage!")); | ||
| 471 | } | ||
| 472 | |||
| 473 | |||
| 474 | case 'W': /* warning inode threshold */ | ||
| 475 | if (strstr (optarg, "%") && sscanf (optarg, "%lf%%", &w_idfp) == 1) { | ||
| 476 | break; | ||
| 477 | } | ||
| 478 | else { | ||
| 479 | usage (_("Warning inode threshold must be percentage!\n")); | ||
| 480 | } | ||
| 481 | case 'K': /* kritical inode threshold */ | ||
| 482 | if (strstr (optarg, "%") && sscanf (optarg, "%lf%%", &c_idfp) == 1) { | ||
| 483 | break; | ||
| 484 | } | ||
| 485 | else { | ||
| 486 | usage (_("Critical inode threshold must be percentage!\n")); | ||
| 487 | } | ||
| 488 | case 'u': | ||
| 489 | if (units) | ||
| 490 | free(units); | ||
| 491 | if (! strcmp (optarg, "bytes")) { | ||
| 492 | mult = (uintmax_t)1; | ||
| 493 | units = strdup ("B"); | ||
| 494 | } else if (! strcmp (optarg, "kB")) { | ||
| 495 | mult = (uintmax_t)1024; | ||
| 496 | units = strdup ("kB"); | ||
| 497 | } else if (! strcmp (optarg, "MB")) { | ||
| 498 | mult = (uintmax_t)1024 * 1024; | ||
| 499 | units = strdup ("MB"); | ||
| 500 | } else if (! strcmp (optarg, "GB")) { | ||
| 501 | mult = (uintmax_t)1024 * 1024 * 1024; | ||
| 502 | units = strdup ("GB"); | ||
| 503 | } else if (! strcmp (optarg, "TB")) { | ||
| 504 | mult = (uintmax_t)1024 * 1024 * 1024 * 1024; | ||
| 505 | units = strdup ("TB"); | ||
| 506 | } else { | ||
| 507 | die (STATE_UNKNOWN, _("unit type %s not known\n"), optarg); | ||
| 508 | } | ||
| 509 | if (units == NULL) | ||
| 510 | die (STATE_UNKNOWN, _("failed allocating storage for '%s'\n"), "units"); | ||
| 511 | break; | ||
| 512 | case 'k': /* display mountpoint */ | ||
| 513 | mult = 1024; | ||
| 514 | if (units) | ||
| 515 | free(units); | ||
| 516 | units = strdup ("kB"); | ||
| 517 | break; | ||
| 518 | case 'm': /* display mountpoint */ | ||
| 519 | mult = 1024 * 1024; | ||
| 520 | if (units) | ||
| 521 | free(units); | ||
| 522 | units = strdup ("MB"); | ||
| 523 | break; | ||
| 524 | case 'l': | ||
| 525 | show_local_fs = 1; | ||
| 526 | break; | ||
| 527 | case 'a': /* Choose all paths */ | ||
| 528 | /* Remember the values passed in for all paths */ | ||
| 529 | path_select_exclude_others=0; | ||
| 530 | dw_df = w_df; | ||
| 531 | dc_df = c_df; | ||
| 532 | dw_dfp = w_dfp; | ||
| 533 | dc_dfp = c_dfp; | ||
| 534 | dw_idfp = w_idfp; | ||
| 535 | dc_idfp = c_idfp; | ||
| 536 | break; | ||
| 537 | case 'p': /* select path */ | ||
| 538 | se = (struct name_list *) malloc (sizeof (struct name_list)); | ||
| 539 | se->name = optarg; | ||
| 540 | se->name_next = NULL; | ||
| 541 | se->w_df = w_df; | ||
| 542 | se->c_df = c_df; | ||
| 543 | se->w_dfp = w_dfp; | ||
| 544 | se->c_dfp = c_dfp; | ||
| 545 | se->w_idfp = w_idfp; | ||
| 546 | se->c_idfp = c_idfp; | ||
| 547 | se->found = 0; | ||
| 548 | se->foundexact = 0; | ||
| 549 | se->found_len = 0; | ||
| 550 | se->exclude = 0; | ||
| 551 | *pathtail = se; | ||
| 552 | pathtail = &se->name_next; | ||
| 553 | break; | ||
| 554 | case 'x': /* exclude path or partition */ | ||
| 555 | se = (struct name_list *) malloc (sizeof (struct name_list)); | ||
| 556 | se->name = optarg; | ||
| 557 | se->name_next = NULL; | ||
| 558 | |||
| 559 | /* If you don't clear the w_fd etc values here, they | ||
| 560 | * get processed when you walk the list and assigned | ||
| 561 | * to the global w_df! | ||
| 562 | */ | ||
| 563 | se->w_df = 0; | ||
| 564 | se->c_df = 0; | ||
| 565 | se->w_dfp = 0; | ||
| 566 | se->c_dfp = 0; | ||
| 567 | se->w_idfp = 0; | ||
| 568 | se->c_idfp = 0; | ||
| 569 | se->found = 0; | ||
| 570 | se->foundexact = 0; | ||
| 571 | se->found_len = 0; | ||
| 572 | se->exclude = 1; | ||
| 573 | *dptail = se; | ||
| 574 | dptail = &se->name_next; | ||
| 575 | break; | ||
| 576 | case 's': /* exclude path or partition */ | ||
| 577 | se = (struct name_list *) malloc (sizeof (struct name_list)); | ||
| 578 | se->name = optarg; | ||
| 579 | se->name_next = NULL; | ||
| 580 | |||
| 581 | /* If you don't clear the w_fd etc values here, they | ||
| 582 | * get processed when you walk the list and assigned | ||
| 583 | * to the global w_df! | ||
| 584 | */ | ||
| 585 | se->w_df = 0; | ||
| 586 | se->c_df = 0; | ||
| 587 | se->w_dfp = 0; | ||
| 588 | se->c_dfp = 0; | ||
| 589 | se->w_idfp = 0; | ||
| 590 | se->c_idfp = 0; | ||
| 591 | se->found = 0; | ||
| 592 | se->foundexact = 0; | ||
| 593 | se->found_len = 0; | ||
| 594 | se->exclude = 2; | ||
| 595 | *dptail = se; | ||
| 596 | dptail = &se->name_next; | ||
| 597 | break; | ||
| 598 | case 'X': /* exclude file system type */ | ||
| 599 | se = (struct name_list *) malloc (sizeof (struct name_list)); | ||
| 600 | se->name = optarg; | ||
| 601 | se->name_next = NULL; | ||
| 602 | /* If you don't clear the w_fd etc values here, they | ||
| 603 | * get processed when you walk the list and assigned | ||
| 604 | * to the global w_df! | ||
| 605 | */ | ||
| 606 | se->w_df = 0; | ||
| 607 | se->c_df = 0; | ||
| 608 | se->w_dfp = 0; | ||
| 609 | se->c_dfp = 0; | ||
| 610 | se->w_idfp = 0; | ||
| 611 | se->c_idfp = 0; | ||
| 612 | se->found = 0; | ||
| 613 | se->foundexact = 0; | ||
| 614 | se->found_len = 0; | ||
| 615 | *fstail = se; | ||
| 616 | fstail = &se->name_next; | ||
| 617 | break; | ||
| 618 | case 'v': /* verbose */ | ||
| 619 | verbose++; | ||
| 620 | break; | ||
| 621 | case 'q': /* verbose */ | ||
| 622 | verbose--; | ||
| 623 | break; | ||
| 624 | case 'e': | ||
| 625 | erronly = TRUE; | ||
| 626 | break; | ||
| 627 | case 'M': /* display mountpoint */ | ||
| 628 | display_mntp = TRUE; | ||
| 629 | break; | ||
| 630 | case 'C': | ||
| 631 | w_df = 0; | ||
| 632 | c_df = 0; | ||
| 633 | w_dfp = -1.0; | ||
| 634 | c_dfp = -1.0; | ||
| 635 | w_idfp = -1.0; | ||
| 636 | c_idfp = -1.0; | ||
| 637 | break; | ||
| 638 | case 'H': /* HTML formatted output */ | ||
| 639 | show_html = 1; | ||
| 640 | break; | ||
| 641 | case 'U': | ||
| 642 | show_used = 1; | ||
| 643 | break; | ||
| 644 | case 'V': /* version */ | ||
| 645 | print_revision (progname, revision); | ||
| 646 | exit (STATE_OK); | ||
| 647 | case 'h': /* help */ | ||
| 648 | print_help (); | ||
| 649 | exit (STATE_OK); | ||
| 650 | case '?': /* help */ | ||
| 651 | usage (_("Unknown argument")); | ||
| 652 | } | ||
| 653 | } | ||
| 654 | |||
| 655 | /* Support for "check_disk warn crit [fs]" with thresholds at used level */ | ||
| 656 | c = optind; | ||
| 657 | if (w_dfp < 0 && argc > c && is_intnonneg (argv[c])) | ||
| 658 | w_dfp = (100.0 - atof (argv[c++])); | ||
| 659 | |||
| 660 | if (c_dfp < 0 && argc > c && is_intnonneg (argv[c])) | ||
| 661 | c_dfp = (100.0 - atof (argv[c++])); | ||
| 662 | |||
| 663 | if (argc > c && path == NULL) { | ||
| 664 | se = (struct name_list *) malloc (sizeof (struct name_list)); | ||
| 665 | se->name = strdup (argv[c++]); | ||
| 666 | se->name_next = NULL; | ||
| 667 | se->w_df = w_df; | ||
| 668 | se->c_df = c_df; | ||
| 669 | se->w_dfp = w_dfp; | ||
| 670 | se->c_dfp = c_dfp; | ||
| 671 | se->w_idfp = w_idfp; | ||
| 672 | se->c_idfp = c_idfp; | ||
| 673 | se->found =0; | ||
| 674 | se->foundexact = 0; | ||
| 675 | se->found_len = 0; | ||
| 676 | se->exclude = 0; | ||
| 677 | *pathtail = se; | ||
| 678 | } | ||
| 679 | |||
| 680 | /* Remember the default values */ | ||
| 681 | w_df = 0; | ||
| 682 | c_df = 0; | ||
| 683 | w_dfp = -1.0; | ||
| 684 | c_dfp = -1.0; | ||
| 685 | w_idfp = -1.0; | ||
| 686 | c_idfp = -1.0; | ||
| 687 | |||
| 688 | if (path_select_list) { | ||
| 689 | temp_list = path_select_list; | ||
| 690 | stat_buf = malloc(sizeof *stat_buf); | ||
| 691 | while (temp_list) { | ||
| 692 | /* Stat each entry to check that dir exists */ | ||
| 693 | if (stat (temp_list->name, &stat_buf[0])) { | ||
| 694 | printf("DISK %s - ", _("CRITICAL")); | ||
| 695 | die (STATE_CRITICAL, _("%s does not exist\n"), temp_list->name); | ||
| 696 | } | ||
| 697 | if (validate_arguments (temp_list->w_df, | ||
| 698 | temp_list->c_df, | ||
| 699 | temp_list->w_dfp, | ||
| 700 | temp_list->c_dfp, | ||
| 701 | temp_list->w_idfp, | ||
| 702 | temp_list->c_idfp, | ||
| 703 | temp_list->name) == ERROR) | ||
| 704 | result = ERROR; | ||
| 705 | temp_list = temp_list->name_next; | ||
| 706 | } | ||
| 707 | free(stat_buf); | ||
| 708 | return result; | ||
| 709 | } else { | ||
| 710 | return validate_arguments (w_df, c_df, w_dfp, c_dfp, w_idfp, c_idfp, NULL); | ||
| 711 | } | ||
| 712 | } | ||
| 713 | |||
| 714 | |||
| 715 | |||
| 716 | void | ||
| 717 | print_path (const char *mypath) | ||
| 718 | { | ||
| 719 | if (mypath == NULL) | ||
| 720 | printf ("\n"); | ||
| 721 | else | ||
| 722 | printf (_(" for %s\n"), mypath); | ||
| 723 | |||
| 724 | return; | ||
| 725 | } | ||
| 726 | |||
| 727 | |||
| 728 | |||
| 729 | int | ||
| 730 | validate_arguments (uintmax_t w, uintmax_t c, double wp, double cp, double iwp, double icp, char *mypath) | ||
| 731 | { | ||
| 732 | if (w < 0 && c < 0 && wp < 0.0 && cp < 0.0) { | ||
| 733 | printf (_("INPUT ERROR: No thresholds specified")); | ||
| 734 | print_path (mypath); | ||
| 735 | return ERROR; | ||
| 736 | } | ||
| 737 | else if ((wp >= 0.0 || cp >= 0.0) && | ||
| 738 | (wp < 0.0 || cp < 0.0 || wp > 100.0 || cp > 100.0 || cp > wp)) { | ||
| 739 | printf (_("\ | ||
| 740 | INPUT ERROR: C_DFP (%f) should be less than W_DFP (%.1f) and both should be between zero and 100 percent, inclusive"), | ||
| 741 | cp, wp); | ||
| 742 | print_path (mypath); | ||
| 743 | return ERROR; | ||
| 744 | } | ||
| 745 | else if ((iwp >= 0.0 || icp >= 0.0) && | ||
| 746 | (iwp < 0.0 || icp < 0.0 || iwp > 100.0 || icp > 100.0 || icp > iwp)) { | ||
| 747 | printf (_("\ | ||
| 748 | INPUT ERROR: C_IDFP (%f) should be less than W_IDFP (%.1f) and both should be between zero and 100 percent, inclusive"), | ||
| 749 | icp, iwp); | ||
| 750 | print_path (mypath); | ||
| 751 | return ERROR; | ||
| 752 | } | ||
| 753 | else if ((w > 0 || c > 0) && (w == 0 || c == 0 || c > w)) { | ||
| 754 | printf (_("\ | ||
| 755 | INPUT ERROR: C_DF (%lu) should be less than W_DF (%lu) and both should be greater than zero"), | ||
| 756 | (unsigned long)c, (unsigned long)w); | ||
| 757 | print_path (mypath); | ||
| 758 | return ERROR; | ||
| 759 | } | ||
| 760 | |||
| 761 | if (units == NULL) { | ||
| 762 | units = strdup ("MB"); | ||
| 763 | mult = (uintmax_t)1024 * 1024; | ||
| 764 | } | ||
| 765 | return OK; | ||
| 766 | } | ||
| 767 | |||
| 768 | |||
| 769 | |||
| 770 | int | ||
| 771 | |||
| 772 | check_disk (double usp, uintmax_t free_disk, double uisp) | ||
| 773 | { | ||
| 774 | int result = STATE_UNKNOWN; | ||
| 775 | /* check the percent used space against thresholds */ | ||
| 776 | if (usp >= 0.0 && c_dfp >=0.0 && usp >= (100.0 - c_dfp)) | ||
| 777 | result = STATE_CRITICAL; | ||
| 778 | else if (uisp >= 0.0 && c_idfp >=0.0 && uisp >= (100.0 - c_idfp)) | ||
| 779 | result = STATE_CRITICAL; | ||
| 780 | else if (c_df > 0 && free_disk <= c_df) | ||
| 781 | result = STATE_CRITICAL; | ||
| 782 | else if (usp >= 0.0 && w_dfp >=0.0 && usp >= (100.0 - w_dfp)) | ||
| 783 | result = STATE_WARNING; | ||
| 784 | else if (uisp >= 0.0 && w_idfp >=0.0 && uisp >= (100.0 - w_idfp)) | ||
| 785 | result = STATE_WARNING; | ||
| 786 | else if (w_df > 0 && free_disk <= w_df) | ||
| 787 | result = STATE_WARNING; | ||
| 788 | else if (usp >= 0.0) | ||
| 789 | result = STATE_OK; | ||
| 790 | return result; | ||
| 791 | } | ||
| 792 | |||
| 793 | |||
| 794 | |||
| 795 | int | ||
| 796 | walk_name_list (struct name_list *list, const char *name) | ||
| 797 | { | ||
| 798 | int foundexact = FALSE, foundmatch = FALSE; | ||
| 799 | int name_len; | ||
| 800 | name_len = strlen(name); | ||
| 801 | while (list) { | ||
| 802 | int list_name_len; | ||
| 803 | list_name_len = strlen(list->name); | ||
| 804 | if ((list->exclude == 1 && name_len == list_name_len && strncmp(list->name, name, list_name_len-1) == 0) || | ||
| 805 | (list->foundexact == 0 && list->exclude == 0 && name_len < list_name_len && strncmp(list->name, name, name_len-1) == 0) || | ||
| 806 | (list->exclude == 0 && name_len == list_name_len && strncmp(list->name, name, name_len-1) == 0) || | ||
| 807 | (list->exclude == 2 && name_len >= list_name_len && strncmp(list->name, name, list_name_len-1) == 0)) { | ||
| 808 | /* Set to -1 for now to note it was a potential match. If an EXACT match is found later, | ||
| 809 | then reset this found to 0. If not, set it to 1. */ | ||
| 810 | list->found = -1; | ||
| 811 | list->found_len = name_len; | ||
| 812 | /* Grab the longest string length that is not being excluded in order to format HTML properly */ | ||
| 813 | if (name_len > longest_length && list->exclude == 0) {longest_length = name_len;} | ||
| 814 | /* if required for name_lists that have not saved w_df, etc (eg exclude lists) */ | ||
| 815 | if (list->w_df) w_df = list->w_df; | ||
| 816 | if (list->c_df) c_df = list->c_df; | ||
| 817 | if (list->w_dfp>=0.0) w_dfp = list->w_dfp; | ||
| 818 | if (list->c_dfp>=0.0) c_dfp = list->c_dfp; | ||
| 819 | if (name_len == list_name_len) { | ||
| 820 | foundexact=TRUE; | ||
| 821 | list->foundexact = TRUE; | ||
| 822 | list->found = 1; | ||
| 823 | } | ||
| 824 | foundmatch = TRUE; | ||
| 825 | } | ||
| 826 | list = list->name_next; | ||
| 827 | } | ||
| 828 | /* Traverse the list again to reset the found variable properly */ | ||
| 829 | while (list) { | ||
| 830 | if (list->found == -1 && foundexact == TRUE) list->found = 0; | ||
| 831 | if (list->found == -1 && foundexact == FALSE) list->found =1; | ||
| 832 | list = list->name_next; | ||
| 833 | printf("tried "); | ||
| 834 | } | ||
| 835 | return foundmatch; | ||
| 836 | } | ||
| 837 | |||
| 838 | |||
| 839 | |||
| 840 | void | ||
| 841 | print_help (void) | ||
| 842 | { | ||
| 843 | print_revision (progname, revision); | ||
| 844 | |||
| 845 | printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n"); | ||
| 846 | printf (COPYRIGHT, copyright, email); | ||
| 847 | |||
| 848 | printf (_("This plugin checks the amount of used disk space on a mounted file system")); | ||
| 849 | printf (_("and generates an alert if free space is less than one of the threshold values")); | ||
| 850 | |||
| 851 | printf ("\n\n"); | ||
| 852 | |||
| 853 | print_usage (); | ||
| 854 | |||
| 855 | printf (_(UT_HELP_VRSN)); | ||
| 856 | |||
| 857 | printf (" %s\n", "-w, --warning=INTEGER"); | ||
| 858 | printf (" %s\n", _("Exit with WARNING status if less than INTEGER units of disk are free")); | ||
| 859 | printf (" %s\n", "-w, --warning=PERCENT%"); | ||
| 860 | printf (" %s\n", _("Exit with WARNING status if less than PERCENT of disk space is free")); | ||
| 861 | printf (" %s\n", "-W, --iwarning=PERCENT%"); | ||
| 862 | printf (" %s\n", _("Exit with WARNING status if less than PERCENT of inode space is free")); | ||
| 863 | printf (" %s\n", "-K, --icritical=PERCENT%"); | ||
| 864 | printf (" %s\n", _("Exit with CRITICAL status if less than PERCENT of inode space is free")); | ||
| 865 | printf (" %s\n", "-c, --critical=INTEGER"); | ||
| 866 | printf (" %s\n", _("Exit with CRITICAL status if less than INTEGER units of disk are free")); | ||
| 867 | printf (" %s\n", "-c, --critical=PERCENT%"); | ||
| 868 | printf (" %s\n", _("Exit with CRITCAL status if less than PERCENT of disk space is free")); | ||
| 869 | printf (" %s\n", "-C, --clear"); | ||
| 870 | printf (" %s\n", _("Clear thresholds")); | ||
| 871 | printf (" %s\n", "-u, --units=STRING"); | ||
| 872 | printf (" %s\n", _("Choose bytes, kB, MB, GB, TB (default: MB)")); | ||
| 873 | printf (" %s\n", "-k, --kilobytes"); | ||
| 874 | printf (" %s\n", _("Same as '--units kB'")); | ||
| 875 | printf (" %s\n", "-m, --megabytes"); | ||
| 876 | printf (" %s\n", _("Same as '--units MB'")); | ||
| 877 | printf (" %s\n", "-l, --local"); | ||
| 878 | printf (" %s\n", _("Only check local filesystems")); | ||
| 879 | printf (" %s\n", "-p, --path=PATH, --partition=PARTITION"); | ||
| 880 | printf (" %s\n", _("Path or partition (may be repeated)")); | ||
| 881 | printf (" %s\n", "-x, --exclude_device=PATH <STRING>"); | ||
| 882 | printf (" %s\n", _("Ignore device (only works if -p unspecified)")); | ||
| 883 | printf (" %s\n", "-s, --exclude_path=PATH <STRING>"); | ||
| 884 | printf (" %s\n", _("Ignore device (only works if -p unspecified)")); | ||
| 885 | printf (" %s\n", _("-X, --exclude-type=TYPE <STRING>")); | ||
| 886 | printf (" %s\n", _("Ignore all filesystems of indicated type (may be repeated)")); | ||
| 887 | printf (" %s\n", "-m, --mountpoint"); | ||
| 888 | printf (" %s\n", _("Display the mountpoint instead of the partition")); | ||
| 889 | printf (" %s\n", "-e, --errors-only"); | ||
| 890 | printf (" %s\n", _("Display only devices/mountpoints with errors")); | ||
| 891 | printf (" %s\n", "-H, --html"); | ||
| 892 | printf (" %s\n", _("Output in HTML format")); | ||
| 893 | printf (" %s\n", "-U, --used"); | ||
| 894 | printf (" %s\n", _("Output percentage of space/inodes used rather than free")); | ||
| 895 | printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT); | ||
| 896 | printf (_(UT_VERBOSE)); | ||
| 897 | printf ("\n"); | ||
| 898 | printf ("%s\n", _("Examples:")); | ||
| 899 | printf (" %s\n", "check_disk -w 10% -c 5% -p /tmp -p /var -C -w 100000 -c 50000 -p /"); | ||
| 900 | printf (" %s\n", _("Checks /tmp and /var at 10% and 5%, and / at 100MB and 50MB")); | ||
| 901 | printf (" %s\n", "check_disk -w 10% -c 5% -a -C -w 5% -c 3% -p /"); | ||
| 902 | printf (" %s\n", _("Checks / at 5% and 3% and every other mount at 10% and 5%")); | ||
| 903 | printf (_(UT_SUPPORT)); | ||
| 904 | } | ||
| 905 | |||
| 906 | |||
| 907 | |||
| 908 | void | ||
| 909 | print_usage (void) | ||
| 910 | { | ||
| 911 | printf (_("Usage:")); | ||
| 912 | printf (" %s -w limit -c limit [-a | -p path | -x device | -s path] [-t timeout]", progname); | ||
| 913 | printf ("[-m] [-e] [-W limit] [-K limit] [-H] [-U] [-v] [-q]\n"); | ||
| 914 | } | ||
