check_disk.c 15 KB

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