check_load.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. #ifdef HAVE_GETOPT_H
  135. int option_index = 0;
  136. static struct option long_options[] = {
  137. {"warning", required_argument, 0, 'w'},
  138. {"critical", required_argument, 0, 'c'},
  139. {"version", no_argument, 0, 'V'},
  140. {"help", no_argument, 0, 'h'},
  141. {0, 0, 0, 0}
  142. };
  143. #endif
  144. #define OPTCHARS "Vhc:w:"
  145. if (argc < 2)
  146. return ERROR;
  147. while (1) {
  148. #ifdef HAVE_GETOPT_H
  149. c = getopt_long (argc, argv, OPTCHARS, long_options, &option_index);
  150. #else
  151. c = getopt (argc, argv, OPTCHARS);
  152. #endif
  153. if (c == -1 || c == EOF)
  154. break;
  155. switch (c) {
  156. case 'w': /* warning time threshold */
  157. if (is_intnonneg (optarg)) {
  158. wload1 = atof (optarg);
  159. wload5 = atof (optarg);
  160. wload15 = atof (optarg);
  161. break;
  162. }
  163. else if (strstr (optarg, ",") &&
  164. sscanf (optarg, "%f,%f,%f", &wload1, &wload5, &wload15) == 3)
  165. break;
  166. else if (strstr (optarg, ":") &&
  167. sscanf (optarg, "%f:%f:%f", &wload1, &wload5, &wload15) == 3)
  168. break;
  169. else
  170. usage ("Warning threshold must be float or float triplet!\n");
  171. break;
  172. case 'c': /* critical time threshold */
  173. if (is_intnonneg (optarg)) {
  174. cload1 = atof (optarg);
  175. cload5 = atof (optarg);
  176. cload15 = atof (optarg);
  177. break;
  178. }
  179. else if (strstr (optarg, ",") &&
  180. sscanf (optarg, "%f,%f,%f", &cload1, &cload5, &cload15) == 3)
  181. break;
  182. else if (strstr (optarg, ":") &&
  183. sscanf (optarg, "%f:%f:%f", &cload1, &cload5, &cload15) == 3)
  184. break;
  185. else
  186. usage ("Critical threshold must be float or float triplet!\n");
  187. break;
  188. case 'V': /* version */
  189. print_revision (progname, "$Revision$");
  190. exit (STATE_OK);
  191. case 'h': /* help */
  192. print_help ();
  193. exit (STATE_OK);
  194. case '?': /* help */
  195. usage ("Invalid argument\n");
  196. }
  197. }
  198. c = optind;
  199. if (c == argc)
  200. return validate_arguments ();
  201. if (wload1 < 0 && is_nonnegative (argv[c]))
  202. wload1 = atof (argv[c++]);
  203. if (c == argc)
  204. return validate_arguments ();
  205. if (cload1 < 0 && is_nonnegative (argv[c]))
  206. cload1 = atof (argv[c++]);
  207. if (c == argc)
  208. return validate_arguments ();
  209. if (wload5 < 0 && is_nonnegative (argv[c]))
  210. wload5 = atof (argv[c++]);
  211. if (c == argc)
  212. return validate_arguments ();
  213. if (cload5 < 0 && is_nonnegative (argv[c]))
  214. cload5 = atof (argv[c++]);
  215. if (c == argc)
  216. return validate_arguments ();
  217. if (wload15 < 0 && is_nonnegative (argv[c]))
  218. wload15 = atof (argv[c++]);
  219. if (c == argc)
  220. return validate_arguments ();
  221. if (cload15 < 0 && is_nonnegative (argv[c]))
  222. cload15 = atof (argv[c++]);
  223. return validate_arguments ();
  224. }
  225. int
  226. validate_arguments (void)
  227. {
  228. if (wload1 < 0)
  229. usage ("Warning threshold for 1-minute load average is not specified\n");
  230. if (wload5 < 0)
  231. usage ("Warning threshold for 5-minute load average is not specified\n");
  232. if (wload15 < 0)
  233. usage ("Warning threshold for 15-minute load average is not specified\n");
  234. if (cload1 < 0)
  235. usage ("Critical threshold for 1-minute load average is not specified\n");
  236. if (cload5 < 0)
  237. usage ("Critical threshold for 5-minute load average is not specified\n");
  238. if (cload15 < 0)
  239. usage ("Critical threshold for 15-minute load average is not specified\n");
  240. if (wload1 > cload1)
  241. usage ("Parameter inconsistency: 1-minute \"warning load\" greater than \"critical load\".\n");
  242. if (wload5 > cload5)
  243. usage ("Parameter inconsistency: 5-minute \"warning load\" greater than \"critical load\".\n");
  244. if (wload15 > cload15)
  245. usage ("Parameter inconsistency: 15-minute \"warning load\" greater than \"critical load\".\n");
  246. return OK;
  247. }
  248. void
  249. print_usage (void)
  250. {
  251. printf
  252. ("Usage: check_load -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15\n"
  253. " check_load --version\n" " check_load --help\n");
  254. }
  255. void
  256. print_help (void)
  257. {
  258. print_revision (progname, "$Revision$");
  259. printf
  260. ("Copyright (c) 1999 Felipe Gustavo de Almeida <galmeida@linux.ime.usp.br>\n"
  261. "Copyright (c) 2000 Karl DeBisschop\n\n"
  262. "This plugin tests the current system load average.\n\n");
  263. print_usage ();
  264. printf
  265. ("\nOptions:\n"
  266. " -w, --warning=WLOAD1,WLOAD5,WLOAD15\n"
  267. " Exit with WARNING status if load average exceeds WLOADn\n"
  268. " -c, --critical=CLOAD1,CLOAD5,CLOAD15\n"
  269. " Exit with CRITICAL status if load average exceed CLOADn\n"
  270. " -h, --help\n"
  271. " Print detailed help screen\n"
  272. " -V, --version\n"
  273. " Print version information\n\n"
  274. "the load average format is the same used by \"uptime\" and \"w\"\n\n");
  275. support ();
  276. }