check_disk.c 16 KB

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