check_disk.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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 (path_select_list &&
  172. (walk_name_list (path_select_list, me->me_mountdir) ||
  173. walk_name_list (path_select_list, me->me_devname) ) )
  174. get_fs_usage (me->me_mountdir, me->me_devname, &fsp);
  175. else if (dev_select_list || path_select_list)
  176. continue;
  177. else if (me->me_remote && show_local_fs)
  178. continue;
  179. else if (me->me_dummy && !show_all_fs)
  180. continue;
  181. else if (fs_exclude_list && walk_name_list (fs_exclude_list, me->me_type))
  182. continue;
  183. else if (dp_exclude_list &&
  184. walk_name_list (dp_exclude_list, me->me_devname) ||
  185. walk_name_list (dp_exclude_list, me->me_mountdir))
  186. continue;
  187. else
  188. get_fs_usage (me->me_mountdir, me->me_devname, &fsp);
  189. if (fsp.fsu_blocks && strcmp ("none", me->me_mountdir)) {
  190. usp = (fsp.fsu_blocks - fsp.fsu_bavail) * 100 / fsp.fsu_blocks;
  191. disk_result = check_disk (usp, fsp.fsu_bavail);
  192. result = max_state (disk_result, result);
  193. if (disk_result==STATE_OK && erronly && !verbose)
  194. continue;
  195. free_space = (float)fsp.fsu_bavail*fsp.fsu_blocksize/mult;
  196. free_space_pct = (float)fsp.fsu_bavail*100/fsp.fsu_blocks;
  197. total_space = (float)fsp.fsu_blocks*fsp.fsu_blocksize/mult;
  198. if (disk_result!=STATE_OK || verbose>=0)
  199. asprintf (&output, "%s [%.0f %s (%2.0f%%) free on %s]",
  200. output,
  201. free_space,
  202. units,
  203. free_space_pct,
  204. (!strcmp(file_system, "none") || display_mntp) ? me->me_devname : me->me_mountdir);
  205. asprintf (&details, "%s\n%.0f of %.0f %s (%2.0f%%) free on %s (type %s mounted on %s)",
  206. details,
  207. free_space,
  208. total_space,
  209. units,
  210. free_space_pct,
  211. me->me_devname,
  212. me->me_type,
  213. me->me_mountdir);
  214. }
  215. }
  216. if (verbose > 2)
  217. asprintf (&output, "%s%s", output, details);
  218. /* Override result if paths specified and not found */
  219. temp_list = path_select_list;
  220. while (temp_list) {
  221. if (temp_list->found != TRUE) {
  222. asprintf (&output, "%s [%s not found]", output, temp_list->name);
  223. result = STATE_CRITICAL;
  224. }
  225. temp_list = temp_list->name_next;
  226. }
  227. terminate (result, "DISK %s%s\n", state_text (result), output, details);
  228. }
  229. /* process command-line arguments */
  230. int
  231. process_arguments (int argc, char **argv)
  232. {
  233. int c;
  234. struct name_list *se;
  235. struct name_list **pathtail = &path_select_list;
  236. struct name_list **devtail = &dev_select_list;
  237. struct name_list **fstail = &fs_exclude_list;
  238. struct name_list **dptail = &dp_exclude_list;
  239. int option_index = 0;
  240. static struct option long_options[] = {
  241. {"timeout", required_argument, 0, 't'},
  242. {"warning", required_argument, 0, 'w'},
  243. {"critical", required_argument, 0, 'c'},
  244. {"local", required_argument, 0, 'l'},
  245. {"kilobytes", required_argument, 0, 'k'},
  246. {"megabytes", required_argument, 0, 'm'},
  247. {"units", required_argument, 0, 'u'},
  248. {"path", required_argument, 0, 'p'},
  249. {"partition", required_argument, 0, 'p'},
  250. {"exclude_device", required_argument, 0, 'x'},
  251. {"exclude-type", required_argument, 0, 'X'},
  252. {"mountpoint", no_argument, 0, 'M'},
  253. {"errors-only", no_argument, 0, 'e'},
  254. {"verbose", no_argument, 0, 'v'},
  255. {"quiet", no_argument, 0, 'q'},
  256. {"version", no_argument, 0, 'V'},
  257. {"help", no_argument, 0, 'h'},
  258. {0, 0, 0, 0}
  259. };
  260. if (argc < 2)
  261. return ERROR;
  262. se = (struct name_list *) malloc (sizeof (struct name_list));
  263. se->name = strdup ("iso9660");
  264. se->name_next = NULL;
  265. *fstail = se;
  266. fstail = &se->name_next;
  267. for (c = 1; c < argc; c++)
  268. if (strcmp ("-to", argv[c]) == 0)
  269. strcpy (argv[c], "-t");
  270. while (1) {
  271. c = getopt_long (argc, argv, "+?Vqhvet:c:w:u:p:x:X:mklM", long_options, &option_index);
  272. if (c == -1 || c == EOF)
  273. break;
  274. switch (c) {
  275. case 't': /* timeout period */
  276. if (is_integer (optarg)) {
  277. timeout_interval = atoi (optarg);
  278. break;
  279. }
  280. else {
  281. usage ("Timeout Interval must be an integer!\n");
  282. }
  283. case 'w': /* warning time threshold */
  284. if (is_intnonneg (optarg)) {
  285. w_df = atoi (optarg);
  286. break;
  287. }
  288. else if (strpbrk (optarg, ",:") &&
  289. strstr (optarg, "%") &&
  290. sscanf (optarg, "%d%*[:,]%f%%", &w_df, &w_dfp) == 2) {
  291. break;
  292. }
  293. else if (strstr (optarg, "%") && sscanf (optarg, "%f%%", &w_dfp) == 1) {
  294. break;
  295. }
  296. else {
  297. usage ("Warning threshold must be integer or percentage!\n");
  298. }
  299. case 'c': /* critical time threshold */
  300. if (is_intnonneg (optarg)) {
  301. c_df = atoi (optarg);
  302. break;
  303. }
  304. else if (strpbrk (optarg, ",:") &&
  305. strstr (optarg, "%") &&
  306. sscanf (optarg, "%d%*[,:]%f%%", &c_df, &c_dfp) == 2) {
  307. break;
  308. }
  309. else if (strstr (optarg, "%") && sscanf (optarg, "%f%%", &c_dfp) == 1) {
  310. break;
  311. }
  312. else {
  313. usage ("Critical threshold must be integer or percentage!\n");
  314. }
  315. case 'u':
  316. if (! strcmp (optarg, "bytes")) {
  317. mult = 1;
  318. units = "B";
  319. } else if (! strcmp (optarg, "kB")) {
  320. mult = 1024;
  321. units = "kB";
  322. } else if (! strcmp (optarg, "MB")) {
  323. mult = 1024 * 1024;
  324. units = "MB";
  325. } else if (! strcmp (optarg, "GB")) {
  326. mult = 1024 * 1024 * 1024;
  327. units = "GB";
  328. } else if (! strcmp (optarg, "TB")) {
  329. mult = (unsigned long)1024 * 1024 * 1024 * 1024;
  330. units = "TB";
  331. } else {
  332. terminate (STATE_UNKNOWN, "unit type %s not known\n", optarg);
  333. }
  334. break;
  335. case 'k': /* display mountpoint */
  336. mult = 1024;
  337. units = "kB";
  338. break;
  339. case 'm': /* display mountpoint */
  340. mult = 1024 * 1024;
  341. units = "MB";
  342. break;
  343. case 'l':
  344. show_local_fs = 1;
  345. break;
  346. case 'p': /* select path */
  347. se = (struct name_list *) malloc (sizeof (struct name_list));
  348. se->name = strdup (optarg);
  349. se->name_next = NULL;
  350. *pathtail = se;
  351. pathtail = &se->name_next;
  352. break;
  353. case 'x': /* exclude path or partition */
  354. se = (struct name_list *) malloc (sizeof (struct name_list));
  355. se->name = strdup (optarg);
  356. se->name_next = NULL;
  357. *dptail = se;
  358. dptail = &se->name_next;
  359. break;
  360. break;
  361. case 'X': /* exclude file system type */
  362. se = (struct name_list *) malloc (sizeof (struct name_list));
  363. se->name = strdup (optarg);
  364. se->name_next = NULL;
  365. *fstail = se;
  366. fstail = &se->name_next;
  367. break;
  368. case 'v': /* verbose */
  369. verbose++;
  370. break;
  371. case 'q': /* verbose */
  372. verbose--;
  373. break;
  374. case 'e':
  375. erronly = TRUE;
  376. break;
  377. case 'M': /* display mountpoint */
  378. display_mntp = TRUE;
  379. break;
  380. case 'V': /* version */
  381. print_revision (progname, revision);
  382. exit (STATE_OK);
  383. case 'h': /* help */
  384. print_help ();
  385. exit (STATE_OK);
  386. case '?': /* help */
  387. usage ("check_disk: unrecognized option\n");
  388. break;
  389. }
  390. }
  391. c = optind;
  392. if (w_dfp == -1 && argc > c && is_intnonneg (argv[c]))
  393. w_dfp = (100.0 - atof (argv[c++]));
  394. if (c_dfp == -1 && argc > c && is_intnonneg (argv[c]))
  395. c_dfp = (100.0 - atof (argv[c++]));
  396. if (argc > c && strlen (path) == 0)
  397. path = argv[c++];
  398. return validate_arguments ();
  399. }
  400. int
  401. validate_arguments ()
  402. {
  403. if (w_df < 0 && c_df < 0 && w_dfp < 0 && c_dfp < 0) {
  404. printf ("INPUT ERROR: Unable to parse command line\n");
  405. return ERROR;
  406. }
  407. else if ((w_dfp >= 0 || c_dfp >= 0)
  408. && (w_dfp < 0 || c_dfp < 0 || w_dfp > 100 || c_dfp > 100
  409. || c_dfp > w_dfp)) {
  410. printf
  411. ("INPUT ERROR: C_DFP (%f) should be less than W_DFP (%f) and both should be between zero and 100 percent, inclusive\n",
  412. c_dfp, w_dfp);
  413. return ERROR;
  414. }
  415. else if ((w_df > 0 || c_df > 0) && (w_df < 0 || c_df < 0 || c_df > w_df)) {
  416. printf
  417. ("INPUT ERROR: C_DF (%d) should be less than W_DF (%d) and both should be greater than zero\n",
  418. c_df, w_df);
  419. return ERROR;
  420. }
  421. else {
  422. return OK;
  423. }
  424. }
  425. int
  426. check_disk (int usp, int free_disk)
  427. {
  428. int result = STATE_UNKNOWN;
  429. /* check the percent used space against thresholds */
  430. if (usp >= 0 && usp >= (100.0 - c_dfp))
  431. result = STATE_CRITICAL;
  432. else if (c_df >= 0 && free_disk <= c_df)
  433. result = STATE_CRITICAL;
  434. else if (usp >= 0 && usp >= (100.0 - w_dfp))
  435. result = STATE_WARNING;
  436. else if (w_df >= 0 && free_disk <= w_df)
  437. result = STATE_WARNING;
  438. else if (usp >= 0.0)
  439. result = STATE_OK;
  440. return result;
  441. }
  442. int
  443. walk_name_list (struct name_list *list, const char *name)
  444. {
  445. while (list) {
  446. if (! strcmp(list->name, name)) {
  447. list->found = 1;
  448. return TRUE;
  449. }
  450. list = list->name_next;
  451. }
  452. return FALSE;
  453. }
  454. void
  455. print_help (void)
  456. {
  457. print_revision (progname, revision);
  458. printf ("Copyright (c) %s %s\n\t<%s>\n\n%s\n",
  459. copyright, authors, email, summary);
  460. print_usage ();
  461. printf ("\nOptions:\n");
  462. printf (options);
  463. printf (notes);
  464. support ();
  465. }
  466. void
  467. print_usage (void)
  468. {
  469. printf
  470. ("Usage: %s %s\n"
  471. " %s (-h|--help)\n"
  472. " %s (-V|--version)\n", progname, option_summary, progname, progname);
  473. }