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