check_load.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. char *status_line = "";
  49. int
  50. main (int argc, char **argv)
  51. {
  52. #if HAVE_GETLOADAVG==1
  53. int result;
  54. double la[3] = { 0.0, 0.0, 0.0 }; /* NetBSD complains about unitialized arrays */
  55. #elif HAVE_PROC_LOADAVG==1
  56. FILE *fp;
  57. char input_buffer[MAX_INPUT_BUFFER];
  58. char *tmp_ptr;
  59. #else
  60. int result;
  61. char input_buffer[MAX_INPUT_BUFFER];
  62. #endif
  63. float la1, la5, la15;
  64. if (process_arguments (argc, argv) == ERROR)
  65. usage ("\n");
  66. #if HAVE_GETLOADAVG==1
  67. result = getloadavg (la, 3);
  68. if (result == -1)
  69. return STATE_UNKNOWN;
  70. la1 = la[LOADAVG_1MIN];
  71. la5 = la[LOADAVG_5MIN];
  72. la15 = la[LOADAVG_15MIN];
  73. #elif HAVE_PROC_LOADAVG==1
  74. fp = fopen (PROC_LOADAVG, "r");
  75. if (fp == NULL) {
  76. printf ("Error opening %s\n", PROC_LOADAVG);
  77. return STATE_UNKNOWN;
  78. }
  79. la1 = la5 = la15 = -1;
  80. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
  81. tmp_ptr = strtok (input_buffer, " ");
  82. la1 = atof (tmp_ptr);
  83. tmp_ptr = strtok (NULL, " ");
  84. la5 = atof (tmp_ptr);
  85. tmp_ptr = strtok (NULL, " ");
  86. la15 = atof (tmp_ptr);
  87. }
  88. fclose (fp);
  89. #else
  90. child_process = spopen (PATH_TO_UPTIME);
  91. if (child_process == NULL) {
  92. printf ("Error opening %s\n", PATH_TO_UPTIME);
  93. return STATE_UNKNOWN;
  94. }
  95. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  96. if (child_stderr == NULL) {
  97. printf ("Could not open stderr for %s\n", PATH_TO_UPTIME);
  98. }
  99. fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process);
  100. sscanf (input_buffer, "%*[^l]load average: %f, %f, %f", &la1, &la5, &la15);
  101. result = spclose (child_process);
  102. if (result) {
  103. printf ("Error code %d returned in %s\n", result, PATH_TO_UPTIME);
  104. return STATE_UNKNOWN;
  105. }
  106. #endif
  107. if ((la1 == -1) || (la5 == -1) || (la15 == -1)) {
  108. #if HAVE_GETLOADAVG==1
  109. printf ("Error in getloadavg()\n");
  110. #elif HAVE_PROC_LOADAVG==1
  111. printf ("Error processing %s\n", PROC_LOADAVG);
  112. #else
  113. printf ("Error processing %s\n", PATH_TO_UPTIME);
  114. #endif
  115. return STATE_UNKNOWN;
  116. }
  117. asprintf(&status_line, "load average: %.2f, %.2f, %.2f", la1, la5, la15);
  118. if ((la1 >= cload1) || (la5 >= cload5) || (la15 >= cload15)) {
  119. printf("CRITICAL - %s\n", status_line);
  120. return STATE_CRITICAL;
  121. }
  122. if ((la1 >= wload1) || (la5 >= wload5) || (la15 >= wload15)) {
  123. printf ("WARNING - %s\n", status_line);
  124. return STATE_WARNING;
  125. }
  126. printf ("OK - %s\n", status_line);
  127. return STATE_OK;
  128. }
  129. /* process command-line arguments */
  130. int
  131. process_arguments (int argc, char **argv)
  132. {
  133. int c = 0;
  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. if (argc < 2)
  143. return ERROR;
  144. while (1) {
  145. c = getopt_long (argc, argv, "Vhc:w:", long_options, &option_index);
  146. if (c == -1 || c == EOF)
  147. break;
  148. switch (c) {
  149. case 'w': /* warning time threshold */
  150. if (is_intnonneg (optarg)) {
  151. wload1 = atof (optarg);
  152. wload5 = atof (optarg);
  153. wload15 = atof (optarg);
  154. break;
  155. }
  156. else if (strstr (optarg, ",") &&
  157. sscanf (optarg, "%f,%f,%f", &wload1, &wload5, &wload15) == 3)
  158. break;
  159. else if (strstr (optarg, ":") &&
  160. sscanf (optarg, "%f:%f:%f", &wload1, &wload5, &wload15) == 3)
  161. break;
  162. else
  163. usage ("Warning threshold must be float or float triplet!\n");
  164. break;
  165. case 'c': /* critical time threshold */
  166. if (is_intnonneg (optarg)) {
  167. cload1 = atof (optarg);
  168. cload5 = atof (optarg);
  169. cload15 = atof (optarg);
  170. break;
  171. }
  172. else if (strstr (optarg, ",") &&
  173. sscanf (optarg, "%f,%f,%f", &cload1, &cload5, &cload15) == 3)
  174. break;
  175. else if (strstr (optarg, ":") &&
  176. sscanf (optarg, "%f:%f:%f", &cload1, &cload5, &cload15) == 3)
  177. break;
  178. else
  179. usage ("Critical threshold must be float or float triplet!\n");
  180. break;
  181. case 'V': /* version */
  182. print_revision (progname, "$Revision$");
  183. exit (STATE_OK);
  184. case 'h': /* help */
  185. print_help ();
  186. exit (STATE_OK);
  187. case '?': /* help */
  188. usage ("Invalid argument\n");
  189. }
  190. }
  191. c = optind;
  192. if (c == argc)
  193. return validate_arguments ();
  194. if (wload1 < 0 && is_nonnegative (argv[c]))
  195. wload1 = atof (argv[c++]);
  196. if (c == argc)
  197. return validate_arguments ();
  198. if (cload1 < 0 && is_nonnegative (argv[c]))
  199. cload1 = atof (argv[c++]);
  200. if (c == argc)
  201. return validate_arguments ();
  202. if (wload5 < 0 && is_nonnegative (argv[c]))
  203. wload5 = atof (argv[c++]);
  204. if (c == argc)
  205. return validate_arguments ();
  206. if (cload5 < 0 && is_nonnegative (argv[c]))
  207. cload5 = atof (argv[c++]);
  208. if (c == argc)
  209. return validate_arguments ();
  210. if (wload15 < 0 && is_nonnegative (argv[c]))
  211. wload15 = atof (argv[c++]);
  212. if (c == argc)
  213. return validate_arguments ();
  214. if (cload15 < 0 && is_nonnegative (argv[c]))
  215. cload15 = atof (argv[c++]);
  216. return validate_arguments ();
  217. }
  218. int
  219. validate_arguments (void)
  220. {
  221. if (wload1 < 0)
  222. usage ("Warning threshold for 1-minute load average is not specified\n");
  223. if (wload5 < 0)
  224. usage ("Warning threshold for 5-minute load average is not specified\n");
  225. if (wload15 < 0)
  226. usage ("Warning threshold for 15-minute load average is not specified\n");
  227. if (cload1 < 0)
  228. usage ("Critical threshold for 1-minute load average is not specified\n");
  229. if (cload5 < 0)
  230. usage ("Critical threshold for 5-minute load average is not specified\n");
  231. if (cload15 < 0)
  232. usage ("Critical threshold for 15-minute load average is not specified\n");
  233. if (wload1 > cload1)
  234. usage ("Parameter inconsistency: 1-minute \"warning load\" greater than \"critical load\".\n");
  235. if (wload5 > cload5)
  236. usage ("Parameter inconsistency: 5-minute \"warning load\" greater than \"critical load\".\n");
  237. if (wload15 > cload15)
  238. usage ("Parameter inconsistency: 15-minute \"warning load\" greater than \"critical load\".\n");
  239. return OK;
  240. }
  241. void
  242. print_usage (void)
  243. {
  244. printf
  245. ("Usage: check_load -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15\n"
  246. " check_load --version\n" " check_load --help\n");
  247. }
  248. void
  249. print_help (void)
  250. {
  251. print_revision (progname, "$Revision$");
  252. printf
  253. ("Copyright (c) 1999 Felipe Gustavo de Almeida <galmeida@linux.ime.usp.br>\n"
  254. "Copyright (c) 2000 Karl DeBisschop\n\n"
  255. "This plugin tests the current system load average.\n\n");
  256. print_usage ();
  257. printf
  258. ("\nOptions:\n"
  259. " -w, --warning=WLOAD1,WLOAD5,WLOAD15\n"
  260. " Exit with WARNING status if load average exceeds WLOADn\n"
  261. " -c, --critical=CLOAD1,CLOAD5,CLOAD15\n"
  262. " Exit with CRITICAL status if load average exceed CLOADn\n"
  263. " -h, --help\n"
  264. " Print detailed help screen\n"
  265. " -V, --version\n"
  266. " Print version information\n\n"
  267. "the load average format is the same used by \"uptime\" and \"w\"\n\n");
  268. support ();
  269. }