check_load.c 8.3 KB

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