check_load.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /******************************************************************************
  2. *
  3. * CHECK_LOAD.C
  4. *
  5. * Written by Felipe Gustavo de Almeida <galmeida@linux.ime.usp.br>
  6. * License: GPL
  7. * Command line: CHECK_LOAD <wload1> <cload1> <wload5> <cload5> <wload15> <cload15>
  8. * First Written: 04/17/99
  9. *
  10. * Modifications:
  11. *
  12. * 05/18/1999 - Modified to work getloadavg where available, and use uptime
  13. * where neither proc or getloadavg are found. Also use autoconf.
  14. * mods by Karl DeBisschop (kdebiss@alum.mit.edu)
  15. * 07/01/1999 - Added some #DEFINEs to allow compilation under NetBSD, as
  16. * suggested by Andy Doran.
  17. * mods by Ethan Galstad (nagios@nagios.org)
  18. * 07/17/1999 - Initialized la[] array to prevent NetBSD from complaining
  19. * mods by Ethan Galstad (nagios@nagios.org)
  20. * 08/18/1999 - Integrated some code with common plugin utilities
  21. * mods by Ethan Galstad (nagios@nagios.org)
  22. * $Date$
  23. * Note: The load format is the same used by "uptime" and "w"
  24. *
  25. *****************************************************************************/
  26. #include "config.h"
  27. #include "common.h"
  28. #include "utils.h"
  29. #ifdef HAVE_SYS_LOADAVG_H
  30. #include <sys/loadavg.h>
  31. #endif
  32. /* needed for compilation under NetBSD, as suggested by Andy Doran */
  33. #ifndef LOADAVG_1MIN
  34. #define LOADAVG_1MIN 0
  35. #define LOADAVG_5MIN 1
  36. #define LOADAVG_15MIN 2
  37. #endif /* !defined LOADAVG_1MIN */
  38. #include "popen.h"
  39. #ifdef HAVE_PROC_LOADAVG
  40. #endif
  41. const char *progname = "check_load";
  42. int process_arguments (int argc, char **argv);
  43. int validate_arguments (void);
  44. void print_usage (void);
  45. void print_help (void);
  46. float wload1 = -1, wload5 = -1, wload15 = -1;
  47. float cload1 = -1, cload5 = -1, cload15 = -1;
  48. int
  49. main (int argc, char **argv)
  50. {
  51. #if HAVE_GETLOADAVG==1
  52. int result;
  53. double la[3] = { 0.0, 0.0, 0.0 }; /* NetBSD complains about unitialized arrays */
  54. #elif HAVE_PROC_LOADAVG==1
  55. FILE *fp;
  56. char input_buffer[MAX_INPUT_BUFFER];
  57. char *tmp_ptr;
  58. #else
  59. int result;
  60. char input_buffer[MAX_INPUT_BUFFER];
  61. #endif
  62. float la1, la5, la15;
  63. if (process_arguments (argc, argv) == ERROR)
  64. usage ("\n");
  65. #if HAVE_GETLOADAVG==1
  66. result = getloadavg (la, 3);
  67. if (result == -1)
  68. return STATE_UNKNOWN;
  69. la1 = la[LOADAVG_1MIN];
  70. la5 = la[LOADAVG_5MIN];
  71. la15 = la[LOADAVG_15MIN];
  72. #elif HAVE_PROC_LOADAVG==1
  73. fp = fopen (PROC_LOADAVG, "r");
  74. if (fp == NULL) {
  75. printf ("Error opening %s\n", PROC_LOADAVG);
  76. return STATE_UNKNOWN;
  77. }
  78. la1 = la5 = la15 = -1;
  79. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
  80. tmp_ptr = strtok (input_buffer, " ");
  81. la1 = atof (tmp_ptr);
  82. tmp_ptr = strtok (NULL, " ");
  83. la5 = atof (tmp_ptr);
  84. tmp_ptr = strtok (NULL, " ");
  85. la15 = atof (tmp_ptr);
  86. }
  87. fclose (fp);
  88. #else
  89. child_process = spopen (PATH_TO_UPTIME);
  90. if (child_process == NULL) {
  91. printf ("Error opening %s\n", PATH_TO_UPTIME);
  92. return STATE_UNKNOWN;
  93. }
  94. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  95. if (child_stderr == NULL) {
  96. printf ("Could not open stderr for %s\n", PATH_TO_UPTIME);
  97. }
  98. fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process);
  99. sscanf (input_buffer, "%*[^l]load average: %f, %f, %f", &la1, &la5, &la15);
  100. result = spclose (child_process);
  101. if (result) {
  102. printf ("Error code %d returned in %s\n", result, PATH_TO_UPTIME);
  103. return STATE_UNKNOWN;
  104. }
  105. #endif
  106. if ((la1 == -1) || (la5 == -1) || (la15 == -1)) {
  107. #if HAVE_GETLOADAVG==1
  108. printf ("Error in getloadavg()\n");
  109. #elif HAVE_PROC_LOADAVG==1
  110. printf ("Error processing %s\n", PROC_LOADAVG);
  111. #else
  112. printf ("Error processing %s\n", PATH_TO_UPTIME);
  113. #endif
  114. return STATE_UNKNOWN;
  115. }
  116. printf ("load average: %.2f, %.2f, %.2f", la1, la5, la15);
  117. if ((la1 >= cload1) || (la5 >= cload5) || (la15 >= cload15)) {
  118. printf (" CRITICAL\n");
  119. return STATE_CRITICAL;
  120. }
  121. if ((la1 >= wload1) || (la5 >= wload5) || (la15 >= wload15)) {
  122. printf (" WARNING\n");
  123. return STATE_WARNING;
  124. }
  125. printf ("\n");
  126. return STATE_OK;
  127. }
  128. /* process command-line arguments */
  129. int
  130. process_arguments (int argc, char **argv)
  131. {
  132. int c = 0;
  133. #ifdef HAVE_GETOPT_H
  134. int option_index = 0;
  135. static struct option long_options[] = {
  136. {"warning", required_argument, 0, 'w'},
  137. {"critical", required_argument, 0, 'c'},
  138. {"version", no_argument, 0, 'V'},
  139. {"help", no_argument, 0, 'h'},
  140. {0, 0, 0, 0}
  141. };
  142. #endif
  143. #define OPTCHARS "Vhc:w:"
  144. if (argc < 2)
  145. return ERROR;
  146. while (1) {
  147. #ifdef HAVE_GETOPT_H
  148. c = getopt_long (argc, argv, OPTCHARS, long_options, &option_index);
  149. #else
  150. c = getopt (argc, argv, OPTCHARS);
  151. #endif
  152. if (c == -1 || c == EOF)
  153. break;
  154. switch (c) {
  155. case 'w': /* warning time threshold */
  156. if (is_intnonneg (optarg)) {
  157. wload1 = atof (optarg);
  158. wload5 = atof (optarg);
  159. wload15 = atof (optarg);
  160. break;
  161. }
  162. else if (strstr (optarg, ",") &&
  163. sscanf (optarg, "%f,%f,%f", &wload1, &wload5, &wload15) == 3)
  164. break;
  165. else if (strstr (optarg, ":") &&
  166. sscanf (optarg, "%f:%f:%f", &wload1, &wload5, &wload15) == 3)
  167. break;
  168. else
  169. usage ("Warning threshold must be float or float triplet!\n");
  170. break;
  171. case 'c': /* critical time threshold */
  172. if (is_intnonneg (optarg)) {
  173. cload1 = atof (optarg);
  174. cload5 = atof (optarg);
  175. cload15 = atof (optarg);
  176. break;
  177. }
  178. else if (strstr (optarg, ",") &&
  179. sscanf (optarg, "%f,%f,%f", &cload1, &cload5, &cload15) == 3)
  180. break;
  181. else if (strstr (optarg, ":") &&
  182. sscanf (optarg, "%f:%f:%f", &cload1, &cload5, &cload15) == 3)
  183. break;
  184. else
  185. usage ("Critical threshold must be float or float triplet!\n");
  186. break;
  187. case 'V': /* version */
  188. print_revision (progname, "$Revision$");
  189. exit (STATE_OK);
  190. case 'h': /* help */
  191. print_help ();
  192. exit (STATE_OK);
  193. case '?': /* help */
  194. usage ("Invalid argument\n");
  195. }
  196. }
  197. c = optind;
  198. if (c == argc)
  199. return validate_arguments ();
  200. if (wload1 < 0 && is_nonnegative (argv[c]))
  201. wload1 = atof (argv[c++]);
  202. if (c == argc)
  203. return validate_arguments ();
  204. if (cload1 < 0 && is_nonnegative (argv[c]))
  205. cload1 = atof (argv[c++]);
  206. if (c == argc)
  207. return validate_arguments ();
  208. if (wload5 < 0 && is_nonnegative (argv[c]))
  209. wload5 = atof (argv[c++]);
  210. if (c == argc)
  211. return validate_arguments ();
  212. if (cload5 < 0 && is_nonnegative (argv[c]))
  213. cload5 = atof (argv[c++]);
  214. if (c == argc)
  215. return validate_arguments ();
  216. if (wload15 < 0 && is_nonnegative (argv[c]))
  217. wload15 = atof (argv[c++]);
  218. if (c == argc)
  219. return validate_arguments ();
  220. if (cload15 < 0 && is_nonnegative (argv[c]))
  221. cload15 = atof (argv[c++]);
  222. return validate_arguments ();
  223. }
  224. int
  225. validate_arguments (void)
  226. {
  227. if (wload1 < 0)
  228. usage ("Warning threshold for 1-minute load average is not specified\n");
  229. if (wload5 < 0)
  230. usage ("Warning threshold for 5-minute load average is not specified\n");
  231. if (wload15 < 0)
  232. usage ("Warning threshold for 15-minute load average is not specified\n");
  233. if (cload1 < 0)
  234. usage ("Critical threshold for 1-minute load average is not specified\n");
  235. if (cload5 < 0)
  236. usage ("Critical threshold for 5-minute load average is not specified\n");
  237. if (cload15 < 0)
  238. usage ("Critical threshold for 15-minute load average is not specified\n");
  239. if (wload1 > cload1)
  240. usage ("Parameter inconsistency: 1-minute \"warning load\" greater than \"critical load\".\n");
  241. if (wload5 > cload5)
  242. usage ("Parameter inconsistency: 5-minute \"warning load\" greater than \"critical load\".\n");
  243. if (wload15 > cload15)
  244. usage ("Parameter inconsistency: 15-minute \"warning load\" greater than \"critical load\".\n");
  245. return OK;
  246. }
  247. void
  248. print_usage (void)
  249. {
  250. printf
  251. ("Usage: check_load -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15\n"
  252. " check_load --version\n" " check_load --help\n");
  253. }
  254. void
  255. print_help (void)
  256. {
  257. print_revision (progname, "$Revision$");
  258. printf
  259. ("Copyright (c) 1999 Felipe Gustavo de Almeida <galmeida@linux.ime.usp.br>\n"
  260. "Copyright (c) 2000 Karl DeBisschop\n\n"
  261. "This plugin tests the current system load average.\n\n");
  262. print_usage ();
  263. printf
  264. ("\nOptions:\n"
  265. " -w, --warning=WLOAD1,WLOAD5,WLOAD15\n"
  266. " Exit with WARNING status if load average exceeds WLOADn\n"
  267. " -c, --critical=CLOAD1,CLOAD5,CLOAD15\n"
  268. " Exit with CRITICAL status if load average exceed CLOADn\n"
  269. " -h, --help\n"
  270. " Print detailed help screen\n"
  271. " -V, --version\n"
  272. " Print version information\n\n"
  273. "the load average format is the same used by \"uptime\" and \"w\"\n\n");
  274. support ();
  275. }