check_disk.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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. *****************************************************************************/
  18. const char *progname = "check_disk";
  19. const char *revision = "$Revision$";
  20. const char *copyright = "1999-2003";
  21. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  22. #include "common.h"
  23. #if HAVE_INTTYPES_H
  24. # include <inttypes.h>
  25. #endif
  26. #include <assert.h>
  27. #include "popen.h"
  28. #include "utils.h"
  29. #include <stdarg.h>
  30. #include "../lib/fsusage.h"
  31. #include "../lib/mountlist.h"
  32. #if HAVE_LIMITS_H
  33. # include <limits.h>
  34. #endif
  35. void
  36. print_usage (void)
  37. {
  38. printf (_("\
  39. Usage: %s -w limit -c limit [-p path | -x device] [-t timeout] [-m] [-e]\n\
  40. [-v] [-q]\n\
  41. %s (-h|--help)\n\
  42. %s (-V|--version)\n"),
  43. progname, progname, progname);
  44. }
  45. void
  46. print_help (void)
  47. {
  48. print_revision (progname, revision);
  49. printf (_(COPYRIGHT), copyright, email);
  50. printf (_("\
  51. This plugin checks the amount of used disk space on a mounted file system\n\
  52. and generates an alert if free space is less than one of the threshold values."));
  53. print_usage ();
  54. printf (_(HELP_VRSN));
  55. printf (_("\
  56. -w, --warning=INTEGER\n\
  57. Exit with WARNING status if less than INTEGER kilobytes of disk are free\n\
  58. -w, --warning=PERCENT%%\n\
  59. Exit with WARNING status if less than PERCENT of disk space is free\n\
  60. -c, --critical=INTEGER\n\
  61. Exit with CRITICAL status if less than INTEGER kilobytes of disk are free\n\
  62. -c, --critical=PERCENT%%\n\
  63. Exit with CRITCAL status if less than PERCENT of disk space is free\n\
  64. -C, --clear\n\
  65. Clear thresholds\n"));
  66. printf (_("\
  67. -u, --units=STRING\n\
  68. Choose bytes, kB, MB, GB, TB (default: MB)\n\
  69. -k, --kilobytes\n\
  70. Same as '--units kB'\n\
  71. -m, --megabytes\n\
  72. Same as '--units MB'\n"));
  73. printf (_("\
  74. -l, --local\n\
  75. Only check local filesystems\n\
  76. -p, --path=PATH, --partition=PARTITION\n\
  77. Path or partition (may be repeated)\n\
  78. -x, --exclude_device=PATH <STRING>\n\
  79. Ignore device (only works if -p unspecified)\n\
  80. -X, --exclude-type=TYPE <STRING>\n\
  81. Ignore all filesystems of indicated type (may be repeated)\n\
  82. -M, --mountpoint\n\
  83. Display the mountpoint instead of the partition\n\
  84. -e, --errors-only\n\
  85. Display only devices/mountpoints with errors\n"));
  86. printf (_(TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  87. printf (_(VRBS));
  88. printf ("%s", _("Examples:\n\
  89. check_disk -w 10% -c 5% -p /tmp -p /var -C -w 100000 -c 50000 -p /\n\
  90. Checks /tmp and /var at 10%,5% and / at 100MB, 50MB\n"));
  91. support ();
  92. }
  93. /* If nonzero, show inode information. */
  94. /* static int inode_format; */
  95. /* If nonzero, show even filesystems with zero size or
  96. uninteresting types. */
  97. static int show_all_fs = 1;
  98. /* If nonzero, show only local filesystems. */
  99. static int show_local_fs = 0;
  100. /* If positive, the units to use when printing sizes;
  101. if negative, the human-readable base. */
  102. /* static int output_block_size; */
  103. /* If nonzero, invoke the `sync' system call before getting any usage data.
  104. Using this option can make df very slow, especially with many or very
  105. busy disks. Note that this may make a difference on some systems --
  106. SunOs4.1.3, for one. It is *not* necessary on Linux. */
  107. /* static int require_sync = 0; */
  108. /* A filesystem type to display. */
  109. struct name_list
  110. {
  111. char *name;
  112. int found;
  113. int w_df;
  114. int c_df;
  115. double w_dfp;
  116. double c_dfp;
  117. struct name_list *name_next;
  118. };
  119. /* Linked list of filesystem types to display.
  120. If `fs_select_list' is NULL, list all types.
  121. This table is generated dynamically from command-line options,
  122. rather than hardcoding into the program what it thinks are the
  123. valid filesystem types; let the user specify any filesystem type
  124. they want to, and if there are any filesystems of that type, they
  125. will be shown.
  126. Some filesystem types:
  127. 4.2 4.3 ufs nfs swap ignore io vm efs dbg */
  128. /* static struct name_list *fs_select_list; */
  129. /* Linked list of filesystem types to omit.
  130. If the list is empty, don't exclude any types. */
  131. static struct name_list *fs_exclude_list;
  132. static struct name_list *dp_exclude_list;
  133. static struct name_list *path_select_list;
  134. static struct name_list *dev_select_list;
  135. /* Linked list of mounted filesystems. */
  136. static struct mount_entry *mount_list;
  137. /* For long options that have no equivalent short option, use a
  138. non-character as a pseudo short option, starting with CHAR_MAX + 1. */
  139. enum
  140. {
  141. SYNC_OPTION = CHAR_MAX + 1,
  142. NO_SYNC_OPTION,
  143. BLOCK_SIZE_OPTION
  144. };
  145. #ifdef _AIX
  146. #pragma alloca
  147. #endif
  148. int process_arguments (int, char **);
  149. int validate_arguments (int, int, double, double, char *);
  150. int check_disk (int usp, int free_disk);
  151. int walk_name_list (struct name_list *list, const char *name);
  152. int w_df = -1;
  153. int c_df = -1;
  154. double w_dfp = -1.0;
  155. double c_dfp = -1.0;
  156. char *path = "";
  157. char *exclude_device = "";
  158. char *units = "MB";
  159. unsigned long mult = 1024 * 1024;
  160. int verbose = 0;
  161. int erronly = FALSE;
  162. int display_mntp = FALSE;
  163. /* Linked list of mounted filesystems. */
  164. static struct mount_entry *mount_list;
  165. int
  166. main (int argc, char **argv)
  167. {
  168. int usp = -1;
  169. int result = STATE_UNKNOWN;
  170. int disk_result = STATE_UNKNOWN;
  171. char file_system[MAX_INPUT_BUFFER];
  172. char *output = "";
  173. char *details = "";
  174. float free_space, free_space_pct, total_space;
  175. struct mount_entry *me;
  176. struct fs_usage fsp;
  177. struct name_list *temp_list;
  178. mount_list = read_filesystem_list (0);
  179. if (process_arguments (argc, argv) != OK)
  180. usage (_("Could not parse arguments\n"));
  181. for (me = mount_list; me; me = me->me_next) {
  182. if (path_select_list &&
  183. (walk_name_list (path_select_list, me->me_mountdir) ||
  184. walk_name_list (path_select_list, me->me_devname) ) )
  185. get_fs_usage (me->me_mountdir, me->me_devname, &fsp);
  186. else if (dev_select_list || path_select_list)
  187. continue;
  188. else if (me->me_remote && show_local_fs)
  189. continue;
  190. else if (me->me_dummy && !show_all_fs)
  191. continue;
  192. else if (fs_exclude_list && walk_name_list (fs_exclude_list, me->me_type))
  193. continue;
  194. else if (dp_exclude_list &&
  195. (walk_name_list (dp_exclude_list, me->me_devname) ||
  196. walk_name_list (dp_exclude_list, me->me_mountdir)))
  197. continue;
  198. else
  199. get_fs_usage (me->me_mountdir, me->me_devname, &fsp);
  200. if (fsp.fsu_blocks && strcmp ("none", me->me_mountdir)) {
  201. usp = (fsp.fsu_blocks - fsp.fsu_bavail) * 100 / fsp.fsu_blocks;
  202. disk_result = check_disk (usp, fsp.fsu_bavail);
  203. result = max_state (disk_result, result);
  204. if (disk_result==STATE_OK && erronly && !verbose)
  205. continue;
  206. free_space = (float)fsp.fsu_bavail*fsp.fsu_blocksize/mult;
  207. free_space_pct = (float)fsp.fsu_bavail*100/fsp.fsu_blocks;
  208. total_space = (float)fsp.fsu_blocks*fsp.fsu_blocksize/mult;
  209. if (disk_result!=STATE_OK || verbose>=0)
  210. asprintf (&output, ("%s [%.0f %s (%.0f%%) free on %s]"),
  211. output,
  212. free_space,
  213. units,
  214. free_space_pct,
  215. (!strcmp(file_system, "none") || display_mntp) ? me->me_devname : me->me_mountdir);
  216. asprintf (&details, _("%s\n%.0f of %.0f %s (%.0f%%) free on %s (type %s mounted on %s) warn:%d crit:%d warn%%:%.0f%% crit%%:%.0f%%"),
  217. details,
  218. free_space,
  219. total_space,
  220. units,
  221. free_space_pct,
  222. me->me_devname,
  223. me->me_type,
  224. me->me_mountdir,
  225. w_df, c_df, w_dfp, c_dfp);
  226. }
  227. }
  228. if (verbose > 2)
  229. asprintf (&output, "%s%s", output, details);
  230. /* Override result if paths specified and not found */
  231. temp_list = path_select_list;
  232. while (temp_list) {
  233. if (temp_list->found != TRUE) {
  234. asprintf (&output, _("%s [%s not found]"), output, temp_list->name);
  235. result = STATE_CRITICAL;
  236. }
  237. temp_list = temp_list->name_next;
  238. }
  239. terminate (result, "DISK %s%s\n", state_text (result), output, details);
  240. return STATE_UNKNOWN;
  241. }
  242. /* process command-line arguments */
  243. int
  244. process_arguments (int argc, char **argv)
  245. {
  246. int c;
  247. struct name_list *se;
  248. struct name_list **pathtail = &path_select_list;
  249. struct name_list **fstail = &fs_exclude_list;
  250. struct name_list **dptail = &dp_exclude_list;
  251. struct name_list *temp_list;
  252. int result = OK;
  253. int option_index = 0;
  254. static struct option long_options[] = {
  255. {"timeout", required_argument, 0, 't'},
  256. {"warning", required_argument, 0, 'w'},
  257. {"critical", required_argument, 0, 'c'},
  258. {"local", required_argument, 0, 'l'},
  259. {"kilobytes", required_argument, 0, 'k'},
  260. {"megabytes", required_argument, 0, 'm'},
  261. {"units", required_argument, 0, 'u'},
  262. {"path", required_argument, 0, 'p'},
  263. {"partition", required_argument, 0, 'p'},
  264. {"exclude_device", required_argument, 0, 'x'},
  265. {"exclude-type", required_argument, 0, 'X'},
  266. {"mountpoint", no_argument, 0, 'M'},
  267. {"errors-only", no_argument, 0, 'e'},
  268. {"verbose", no_argument, 0, 'v'},
  269. {"quiet", no_argument, 0, 'q'},
  270. {"clear", no_argument, 0, 'C'},
  271. {"version", no_argument, 0, 'V'},
  272. {"help", no_argument, 0, 'h'},
  273. {0, 0, 0, 0}
  274. };
  275. if (argc < 2)
  276. return ERROR;
  277. se = (struct name_list *) malloc (sizeof (struct name_list));
  278. se->name = strdup ("iso9660");
  279. se->name_next = NULL;
  280. *fstail = se;
  281. fstail = &se->name_next;
  282. for (c = 1; c < argc; c++)
  283. if (strcmp ("-to", argv[c]) == 0)
  284. strcpy (argv[c], "-t");
  285. while (1) {
  286. c = getopt_long (argc, argv, "+?VqhveCt:c:w:u:p:x:X:mklM", long_options, &option_index);
  287. if (c == -1 || c == EOF)
  288. break;
  289. switch (c) {
  290. case 't': /* timeout period */
  291. if (is_integer (optarg)) {
  292. timeout_interval = atoi (optarg);
  293. break;
  294. }
  295. else {
  296. usage (_("Timeout Interval must be an integer!\n"));
  297. }
  298. case 'w': /* warning threshold */
  299. if (is_intnonneg (optarg)) {
  300. w_df = atoi (optarg);
  301. break;
  302. }
  303. else if (strpbrk (optarg, ",:") &&
  304. strstr (optarg, "%") &&
  305. sscanf (optarg, "%d%*[:,]%lf%%", &w_df, &w_dfp) == 2) {
  306. break;
  307. }
  308. else if (strstr (optarg, "%") && sscanf (optarg, "%lf%%", &w_dfp) == 1) {
  309. break;
  310. }
  311. else {
  312. usage (_("Warning threshold must be integer or percentage!\n"));
  313. }
  314. case 'c': /* critical threshold */
  315. if (is_intnonneg (optarg)) {
  316. c_df = atoi (optarg);
  317. break;
  318. }
  319. else if (strpbrk (optarg, ",:") &&
  320. strstr (optarg, "%") &&
  321. sscanf (optarg, "%d%*[,:]%lf%%", &c_df, &c_dfp) == 2) {
  322. break;
  323. }
  324. else if (strstr (optarg, "%") && sscanf (optarg, "%lf%%", &c_dfp) == 1) {
  325. break;
  326. }
  327. else {
  328. usage (_("Critical threshold must be integer or percentage!\n"));
  329. }
  330. case 'u':
  331. if (! strcmp (optarg, "bytes")) {
  332. mult = 1;
  333. units = "B";
  334. } else if (! strcmp (optarg, "kB")) {
  335. mult = 1024;
  336. units = "kB";
  337. } else if (! strcmp (optarg, "MB")) {
  338. mult = 1024 * 1024;
  339. units = "MB";
  340. } else if (! strcmp (optarg, "GB")) {
  341. mult = 1024 * 1024 * 1024;
  342. units = "GB";
  343. } else if (! strcmp (optarg, "TB")) {
  344. mult = (unsigned long)1024 * 1024 * 1024 * 1024;
  345. units = "TB";
  346. } else {
  347. terminate (STATE_UNKNOWN, _("unit type %s not known\n"), optarg);
  348. }
  349. break;
  350. case 'k': /* display mountpoint */
  351. mult = 1024;
  352. units = "kB";
  353. break;
  354. case 'm': /* display mountpoint */
  355. mult = 1024 * 1024;
  356. units = "MB";
  357. break;
  358. case 'l':
  359. show_local_fs = 1;
  360. break;
  361. case 'p': /* select path */
  362. se = (struct name_list *) malloc (sizeof (struct name_list));
  363. se->name = strdup (optarg);
  364. se->name_next = NULL;
  365. se->w_df = w_df;
  366. se->c_df = c_df;
  367. se->w_dfp = w_dfp;
  368. se->c_dfp = c_dfp;
  369. *pathtail = se;
  370. pathtail = &se->name_next;
  371. break;
  372. case 'x': /* exclude path or partition */
  373. se = (struct name_list *) malloc (sizeof (struct name_list));
  374. se->name = strdup (optarg);
  375. se->name_next = NULL;
  376. *dptail = se;
  377. dptail = &se->name_next;
  378. break;
  379. case 'X': /* exclude file system type */
  380. se = (struct name_list *) malloc (sizeof (struct name_list));
  381. se->name = strdup (optarg);
  382. se->name_next = NULL;
  383. *fstail = se;
  384. fstail = &se->name_next;
  385. break;
  386. case 'v': /* verbose */
  387. verbose++;
  388. break;
  389. case 'q': /* verbose */
  390. verbose--;
  391. break;
  392. case 'e':
  393. erronly = TRUE;
  394. break;
  395. case 'M': /* display mountpoint */
  396. display_mntp = TRUE;
  397. break;
  398. case 'C':
  399. w_df = -1;
  400. c_df = -1;
  401. w_dfp = -1.0;
  402. c_dfp = -1.0;
  403. break;
  404. case 'V': /* version */
  405. print_revision (progname, revision);
  406. exit (STATE_OK);
  407. case 'h': /* help */
  408. print_help ();
  409. exit (STATE_OK);
  410. case '?': /* help */
  411. usage (_("check_disk: unrecognized option\n"));
  412. break;
  413. }
  414. }
  415. /* Support for "check_disk warn crit [fs]" with thresholds at used level */
  416. c = optind;
  417. if (w_dfp == -1 && argc > c && is_intnonneg (argv[c]))
  418. w_dfp = (100.0 - atof (argv[c++]));
  419. if (c_dfp == -1 && argc > c && is_intnonneg (argv[c]))
  420. c_dfp = (100.0 - atof (argv[c++]));
  421. if (argc > c && strlen (path) == 0) {
  422. se = (struct name_list *) malloc (sizeof (struct name_list));
  423. se->name = strdup (argv[c++]);
  424. se->name_next = NULL;
  425. se->w_df = w_df;
  426. se->c_df = c_df;
  427. se->w_dfp = w_dfp;
  428. se->c_dfp = c_dfp;
  429. *pathtail = se;
  430. }
  431. if (path_select_list) {
  432. temp_list = path_select_list;
  433. while (temp_list) {
  434. if (validate_arguments (temp_list->w_df,
  435. temp_list->c_df,
  436. temp_list->w_dfp,
  437. temp_list->c_dfp,
  438. temp_list->name) == ERROR)
  439. result = ERROR;
  440. temp_list = temp_list->name_next;
  441. }
  442. return result;
  443. } else {
  444. return validate_arguments (w_df, c_df, w_dfp, c_dfp, NULL);
  445. }
  446. }
  447. void print_path (char *mypath)
  448. {
  449. if (mypath)
  450. printf (" for %s", mypath);
  451. printf ("\n");
  452. }
  453. int
  454. validate_arguments (int w, int c, double wp, double cp, char *mypath)
  455. {
  456. if (w < 0 && c < 0 && wp < 0.0 && cp < 0.0) {
  457. printf (_("INPUT ERROR: No thresholds specified"));
  458. print_path (mypath);
  459. return ERROR;
  460. }
  461. else if ((wp >= 0.0 || cp >= 0.0) &&
  462. (wp < 0.0 || cp < 0.0 || wp > 100.0 || cp > 100.0 || cp > wp)) {
  463. printf (_("\
  464. INPUT ERROR: C_DFP (%f) should be less than W_DFP (%.1f) and both should be between zero and 100 percent, inclusive"),
  465. cp, wp);
  466. print_path (path);
  467. return ERROR;
  468. }
  469. else if ((w > 0 || c > 0) && (w < 0 || c < 0 || c > w)) {
  470. printf (_("\
  471. INPUT ERROR: C_DF (%d) should be less than W_DF (%d) and both should be greater than zero"),
  472. c, w);
  473. print_path (path);
  474. return ERROR;
  475. }
  476. else {
  477. return OK;
  478. }
  479. }
  480. int
  481. check_disk (int usp, int free_disk)
  482. {
  483. int result = STATE_UNKNOWN;
  484. /* check the percent used space against thresholds */
  485. if (usp >= 0 && usp >= (100.0 - c_dfp))
  486. result = STATE_CRITICAL;
  487. else if (c_df >= 0 && free_disk <= c_df)
  488. result = STATE_CRITICAL;
  489. else if (usp >= 0 && usp >= (100.0 - w_dfp))
  490. result = STATE_WARNING;
  491. else if (w_df >= 0 && free_disk <= w_df)
  492. result = STATE_WARNING;
  493. else if (usp >= 0.0)
  494. result = STATE_OK;
  495. return result;
  496. }
  497. int
  498. walk_name_list (struct name_list *list, const char *name)
  499. {
  500. while (list) {
  501. if (! strcmp(list->name, name)) {
  502. list->found = 1;
  503. /* if required for name_lists that have not saved w_df, etc (eg exclude lists) */
  504. if (list->w_df) w_df = list->w_df;
  505. if (list->c_df) c_df = list->c_df;
  506. if (list->w_dfp) w_dfp = list->w_dfp;
  507. if (list->c_dfp) c_dfp = list->c_dfp;
  508. return TRUE;
  509. }
  510. list = list->name_next;
  511. }
  512. return FALSE;
  513. }