check_disk.c 15 KB

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