check_load.c 8.0 KB

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