check_load.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*****************************************************************************
  2. *
  3. * Nagios check_load plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 1999-2007 Nagios Plugins Development Team
  7. *
  8. * Last Modified: $Date$
  9. *
  10. * Description:
  11. *
  12. * This file contains the check_load plugin
  13. *
  14. * This plugin tests the current system load average.
  15. *
  16. *
  17. * This program is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation, either version 3 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. *
  30. * $Id$
  31. *
  32. *****************************************************************************/
  33. const char *progname = "check_load";
  34. const char *revision = "$Revision$";
  35. const char *copyright = "1999-2007";
  36. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  37. #include "common.h"
  38. #include "utils.h"
  39. #include "popen.h"
  40. #ifdef HAVE_SYS_LOADAVG_H
  41. #include <sys/loadavg.h>
  42. #endif
  43. /* needed for compilation under NetBSD, as suggested by Andy Doran */
  44. #ifndef LOADAVG_1MIN
  45. #define LOADAVG_1MIN 0
  46. #define LOADAVG_5MIN 1
  47. #define LOADAVG_15MIN 2
  48. #endif /* !defined LOADAVG_1MIN */
  49. static int process_arguments (int argc, char **argv);
  50. static int validate_arguments (void);
  51. void print_help (void);
  52. void print_usage (void);
  53. /* strictly for pretty-print usage in loops */
  54. static const int nums[3] = { 1, 5, 15 };
  55. /* provide some fairly sane defaults */
  56. double wload[3] = { 0.0, 0.0, 0.0 };
  57. double cload[3] = { 0.0, 0.0, 0.0 };
  58. #define la1 la[0]
  59. #define la5 la[1]
  60. #define la15 la[2]
  61. char *status_line;
  62. int take_into_account_cpus = 0;
  63. static void
  64. get_threshold(char *arg, double *th)
  65. {
  66. size_t i, n;
  67. int valid = 0;
  68. char *str = arg, *p;
  69. n = strlen(arg);
  70. for(i = 0; i < 3; i++) {
  71. th[i] = strtod(str, &p);
  72. if(p == str) break;
  73. valid = 1;
  74. str = p + 1;
  75. if(n <= (size_t)(str - arg)) break;
  76. }
  77. /* empty argument or non-floatish, so warn about it and die */
  78. if(!i && !valid) usage (_("Warning threshold must be float or float triplet!\n"));
  79. if(i != 2) {
  80. /* one or more numbers were given, so fill array with last
  81. * we got (most likely to NOT produce the least expected result) */
  82. for(n = i; n < 3; n++) th[n] = th[i];
  83. }
  84. }
  85. int
  86. main (int argc, char **argv)
  87. {
  88. int result;
  89. int i;
  90. long numcpus;
  91. double la[3] = { 0.0, 0.0, 0.0 }; /* NetBSD complains about unitialized arrays */
  92. #ifndef HAVE_GETLOADAVG
  93. char input_buffer[MAX_INPUT_BUFFER];
  94. # ifdef HAVE_PROC_LOADAVG
  95. FILE *fp;
  96. char *str, *next;
  97. # endif
  98. #endif
  99. setlocale (LC_ALL, "");
  100. bindtextdomain (PACKAGE, LOCALEDIR);
  101. textdomain (PACKAGE);
  102. setlocale(LC_NUMERIC, "POSIX");
  103. /* Parse extra opts if any */
  104. argv = np_extra_opts (&argc, argv, progname);
  105. if (process_arguments (argc, argv) == ERROR)
  106. usage4 (_("Could not parse arguments"));
  107. #ifdef HAVE_GETLOADAVG
  108. result = getloadavg (la, 3);
  109. if (result != 3)
  110. return STATE_UNKNOWN;
  111. #else
  112. # ifdef HAVE_PROC_LOADAVG
  113. fp = fopen (PROC_LOADAVG, "r");
  114. if (fp == NULL) {
  115. printf (_("Error opening %s\n"), PROC_LOADAVG);
  116. return STATE_UNKNOWN;
  117. }
  118. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
  119. str = (char *)input_buffer;
  120. for(i = 0; i < 3; i++) {
  121. la[i] = strtod(str, &next);
  122. str = next;
  123. }
  124. }
  125. fclose (fp);
  126. # else
  127. child_process = spopen (PATH_TO_UPTIME);
  128. if (child_process == NULL) {
  129. printf (_("Error opening %s\n"), PATH_TO_UPTIME);
  130. return STATE_UNKNOWN;
  131. }
  132. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  133. if (child_stderr == NULL) {
  134. printf (_("Could not open stderr for %s\n"), PATH_TO_UPTIME);
  135. }
  136. fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process);
  137. sscanf (input_buffer, "%*[^l]load average: %lf, %lf, %lf", &la1, &la5, &la15);
  138. result = spclose (child_process);
  139. if (result) {
  140. printf (_("Error code %d returned in %s\n"), result, PATH_TO_UPTIME);
  141. return STATE_UNKNOWN;
  142. }
  143. # endif
  144. #endif
  145. if (take_into_account_cpus == 1) {
  146. if ((numcpus = GET_NUMBER_OF_CPUS()) > 0) {
  147. la[0] = la[0] / numcpus;
  148. la[1] = la[1] / numcpus;
  149. la[2] = la[2] / numcpus;
  150. }
  151. }
  152. if ((la[0] < 0.0) || (la[1] < 0.0) || (la[2] < 0.0)) {
  153. #ifdef HAVE_GETLOADAVG
  154. printf (_("Error in getloadavg()\n"));
  155. #else
  156. # ifdef HAVE_PROC_LOADAVG
  157. printf (_("Error processing %s\n"), PROC_LOADAVG);
  158. # else
  159. printf (_("Error processing %s\n"), PATH_TO_UPTIME);
  160. # endif
  161. #endif
  162. return STATE_UNKNOWN;
  163. }
  164. /* we got this far, so assume OK until we've measured */
  165. result = STATE_OK;
  166. asprintf(&status_line, _("load average: %.2f, %.2f, %.2f"), la1, la5, la15);
  167. for(i = 0; i < 3; i++) {
  168. if(la[i] > cload[i]) {
  169. result = STATE_CRITICAL;
  170. break;
  171. }
  172. else if(la[i] > wload[i]) result = STATE_WARNING;
  173. }
  174. printf("%s - %s|", state_text(result), status_line);
  175. for(i = 0; i < 3; i++)
  176. printf("load%d=%.3f;%.3f;%.3f;0; ", nums[i], la[i], wload[i], cload[i]);
  177. putchar('\n');
  178. return result;
  179. }
  180. /* process command-line arguments */
  181. static int
  182. process_arguments (int argc, char **argv)
  183. {
  184. int c = 0;
  185. int option = 0;
  186. static struct option longopts[] = {
  187. {"warning", required_argument, 0, 'w'},
  188. {"critical", required_argument, 0, 'c'},
  189. {"percpu", no_argument, 0, 'r'},
  190. {"version", no_argument, 0, 'V'},
  191. {"help", no_argument, 0, 'h'},
  192. {0, 0, 0, 0}
  193. };
  194. if (argc < 2)
  195. return ERROR;
  196. while (1) {
  197. c = getopt_long (argc, argv, "Vhrc:w:", longopts, &option);
  198. if (c == -1 || c == EOF)
  199. break;
  200. switch (c) {
  201. case 'w': /* warning time threshold */
  202. get_threshold(optarg, wload);
  203. break;
  204. case 'c': /* critical time threshold */
  205. get_threshold(optarg, cload);
  206. break;
  207. case 'r': /* Divide load average by number of CPUs */
  208. take_into_account_cpus = 1;
  209. break;
  210. case 'V': /* version */
  211. print_revision (progname, revision);
  212. exit (STATE_OK);
  213. case 'h': /* help */
  214. print_help ();
  215. exit (STATE_OK);
  216. case '?': /* help */
  217. usage5 ();
  218. }
  219. }
  220. c = optind;
  221. if (c == argc)
  222. return validate_arguments ();
  223. /* handle the case if both arguments are missing,
  224. * but not if only one is given without -c or -w flag */
  225. if(c - argc == 2) {
  226. get_threshold(argv[c++], wload);
  227. get_threshold(argv[c++], cload);
  228. }
  229. else if(c - argc == 1) {
  230. get_threshold(argv[c++], cload);
  231. }
  232. return validate_arguments ();
  233. }
  234. static int
  235. validate_arguments (void)
  236. {
  237. int i = 0;
  238. /* match cload first, as it will give the most friendly error message
  239. * if user hasn't given the -c switch properly */
  240. for(i = 0; i < 3; i++) {
  241. if(cload[i] < 0)
  242. die (STATE_UNKNOWN, _("Critical threshold for %d-minute load average is not specified\n"), nums[i]);
  243. if(wload[i] < 0)
  244. die (STATE_UNKNOWN, _("Warning threshold for %d-minute load average is not specified\n"), nums[i]);
  245. if(wload[i] > cload[i])
  246. die (STATE_UNKNOWN, _("Parameter inconsistency: %d-minute \"warning load\" is greater than \"critical load\"\n"), nums[i]);
  247. }
  248. return OK;
  249. }
  250. void
  251. print_help (void)
  252. {
  253. print_revision (progname, revision);
  254. printf ("Copyright (c) 1999 Felipe Gustavo de Almeida <galmeida@linux.ime.usp.br>\n");
  255. printf (COPYRIGHT, copyright, email);
  256. printf (_("This plugin tests the current system load average."));
  257. printf ("\n\n");
  258. print_usage ();
  259. printf (_(UT_HELP_VRSN));
  260. printf (_(UT_EXTRA_OPTS));
  261. printf (" %s\n", "-w, --warning=WLOAD1,WLOAD5,WLOAD15");
  262. printf (" %s\n", _("Exit with WARNING status if load average exceeds WLOADn"));
  263. printf (" %s\n", "-c, --critical=CLOAD1,CLOAD5,CLOAD15");
  264. printf (" %s\n", _("Exit with CRITICAL status if load average exceed CLOADn"));
  265. printf (" %s\n", _("the load average format is the same used by \"uptime\" and \"w\""));
  266. printf (" %s\n", "-r, --percpu");
  267. printf (" %s\n", _("Divide the load averages by the number of CPUs (when possible)"));
  268. #ifdef NP_EXTRA_OPTS
  269. printf ("\n");
  270. printf ("%s\n", _("Notes:"));
  271. printf (_(UT_EXTRA_OPTS_NOTES));
  272. #endif
  273. printf (_(UT_SUPPORT));
  274. }
  275. void
  276. print_usage (void)
  277. {
  278. printf (_("Usage:"));
  279. printf ("%s [-r] -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15\n", progname);
  280. }