check_load.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. 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. str = p + 1;
  74. if(n <= (size_t)(str - arg)) break;
  75. }
  76. /* empty argument or non-floatish, so warn about it and die */
  77. if(!i) usage (_("Warning threshold must be float or float triplet!\n"));
  78. if(i != 2) {
  79. /* one or more numbers were given, so fill array with last
  80. * we got (most likely to NOT produce the least expected result) */
  81. for(n = i; n < 3; n++) th[n] = th[i];
  82. }
  83. }
  84. int
  85. main (int argc, char **argv)
  86. {
  87. int result;
  88. int i;
  89. long numcpus;
  90. double la[3] = { 0.0, 0.0, 0.0 }; /* NetBSD complains about unitialized arrays */
  91. #ifndef HAVE_GETLOADAVG
  92. char input_buffer[MAX_INPUT_BUFFER];
  93. # ifdef HAVE_PROC_LOADAVG
  94. FILE *fp;
  95. char *str, *next;
  96. # endif
  97. #endif
  98. setlocale (LC_ALL, "");
  99. bindtextdomain (PACKAGE, LOCALEDIR);
  100. textdomain (PACKAGE);
  101. setlocale(LC_NUMERIC, "POSIX");
  102. if (process_arguments (argc, argv) == ERROR)
  103. usage4 (_("Could not parse arguments"));
  104. #ifdef HAVE_GETLOADAVG
  105. result = getloadavg (la, 3);
  106. if (result != 3)
  107. return STATE_UNKNOWN;
  108. #else
  109. # ifdef HAVE_PROC_LOADAVG
  110. fp = fopen (PROC_LOADAVG, "r");
  111. if (fp == NULL) {
  112. printf (_("Error opening %s\n"), PROC_LOADAVG);
  113. return STATE_UNKNOWN;
  114. }
  115. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
  116. str = (char *)input_buffer;
  117. for(i = 0; i < 3; i++) {
  118. la[i] = strtod(str, &next);
  119. str = next;
  120. }
  121. }
  122. fclose (fp);
  123. # else
  124. child_process = spopen (PATH_TO_UPTIME);
  125. if (child_process == NULL) {
  126. printf (_("Error opening %s\n"), PATH_TO_UPTIME);
  127. return STATE_UNKNOWN;
  128. }
  129. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  130. if (child_stderr == NULL) {
  131. printf (_("Could not open stderr for %s\n"), PATH_TO_UPTIME);
  132. }
  133. fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process);
  134. sscanf (input_buffer, "%*[^l]load average: %lf, %lf, %lf", &la1, &la5, &la15);
  135. result = spclose (child_process);
  136. if (result) {
  137. printf (_("Error code %d returned in %s\n"), result, PATH_TO_UPTIME);
  138. return STATE_UNKNOWN;
  139. }
  140. # endif
  141. #endif
  142. if (take_into_account_cpus == 1) {
  143. if ((numcpus = GET_NUMBER_OF_CPUS()) > 0) {
  144. la[0] = la[0] / numcpus;
  145. la[1] = la[1] / numcpus;
  146. la[2] = la[2] / numcpus;
  147. }
  148. }
  149. if ((la[0] < 0.0) || (la[1] < 0.0) || (la[2] < 0.0)) {
  150. #ifdef HAVE_GETLOADAVG
  151. printf (_("Error in getloadavg()\n"));
  152. #else
  153. # ifdef HAVE_PROC_LOADAVG
  154. printf (_("Error processing %s\n"), PROC_LOADAVG);
  155. # else
  156. printf (_("Error processing %s\n"), PATH_TO_UPTIME);
  157. # endif
  158. #endif
  159. return STATE_UNKNOWN;
  160. }
  161. /* we got this far, so assume OK until we've measured */
  162. result = STATE_OK;
  163. asprintf(&status_line, _("load average: %.2f, %.2f, %.2f"), la1, la5, la15);
  164. for(i = 0; i < 3; i++) {
  165. if(la[i] > cload[i]) {
  166. result = STATE_CRITICAL;
  167. break;
  168. }
  169. else if(la[i] > wload[i]) result = STATE_WARNING;
  170. }
  171. printf("%s - %s|", state_text(result), status_line);
  172. for(i = 0; i < 3; i++)
  173. printf("load%d=%.3f;%.3f;%.3f;0; ", nums[i], la[i], wload[i], cload[i]);
  174. putchar('\n');
  175. return result;
  176. }
  177. /* process command-line arguments */
  178. static int
  179. process_arguments (int argc, char **argv)
  180. {
  181. int c = 0;
  182. int option = 0;
  183. static struct option longopts[] = {
  184. {"warning", required_argument, 0, 'w'},
  185. {"critical", required_argument, 0, 'c'},
  186. {"percpu", no_argument, 0, 'r'},
  187. {"version", no_argument, 0, 'V'},
  188. {"help", no_argument, 0, 'h'},
  189. {0, 0, 0, 0}
  190. };
  191. if (argc < 2)
  192. return ERROR;
  193. while (1) {
  194. c = getopt_long (argc, argv, "Vhrc:w:", longopts, &option);
  195. if (c == -1 || c == EOF)
  196. break;
  197. switch (c) {
  198. case 'w': /* warning time threshold */
  199. get_threshold(optarg, wload);
  200. break;
  201. case 'c': /* critical time threshold */
  202. get_threshold(optarg, cload);
  203. break;
  204. case 'r': /* Divide load average by number of CPUs */
  205. take_into_account_cpus = 1;
  206. break;
  207. case 'V': /* version */
  208. print_revision (progname, revision);
  209. exit (STATE_OK);
  210. case 'h': /* help */
  211. print_help ();
  212. exit (STATE_OK);
  213. case '?': /* help */
  214. usage5 ();
  215. }
  216. }
  217. c = optind;
  218. if (c == argc)
  219. return validate_arguments ();
  220. /* handle the case if both arguments are missing,
  221. * but not if only one is given without -c or -w flag */
  222. if(c - argc == 2) {
  223. get_threshold(argv[c++], wload);
  224. get_threshold(argv[c++], cload);
  225. }
  226. else if(c - argc == 1) {
  227. get_threshold(argv[c++], cload);
  228. }
  229. return validate_arguments ();
  230. }
  231. static int
  232. validate_arguments (void)
  233. {
  234. int i = 0;
  235. /* match cload first, as it will give the most friendly error message
  236. * if user hasn't given the -c switch properly */
  237. for(i = 0; i < 3; i++) {
  238. if(cload[i] < 0)
  239. die (STATE_UNKNOWN, _("Critical threshold for %d-minute load average is not specified\n"), nums[i]);
  240. if(wload[i] < 0)
  241. die (STATE_UNKNOWN, _("Warning threshold for %d-minute load average is not specified\n"), nums[i]);
  242. if(wload[i] > cload[i])
  243. die (STATE_UNKNOWN, _("Parameter inconsistency: %d-minute \"warning load\" is greater than \"critical load\"\n"), nums[i]);
  244. }
  245. return OK;
  246. }
  247. void
  248. print_help (void)
  249. {
  250. print_revision (progname, revision);
  251. printf ("Copyright (c) 1999 Felipe Gustavo de Almeida <galmeida@linux.ime.usp.br>\n");
  252. printf (COPYRIGHT, copyright, email);
  253. printf (_("This plugin tests the current system load average."));
  254. printf ("\n\n");
  255. print_usage ();
  256. printf (_(UT_HELP_VRSN));
  257. printf (" %s\n", "-w, --warning=WLOAD1,WLOAD5,WLOAD15");
  258. printf (" %s\n", _("Exit with WARNING status if load average exceeds WLOADn"));
  259. printf (" %s\n", "-c, --critical=CLOAD1,CLOAD5,CLOAD15");
  260. printf (" %s\n", _("Exit with CRITICAL status if load average exceed CLOADn"));
  261. printf (" %s\n", _("the load average format is the same used by \"uptime\" and \"w\""));
  262. printf (" %s\n", "-r, --percpu");
  263. printf (" %s\n", _("Divide the load averages by the number of CPUs (when possible)"));
  264. printf (_(UT_SUPPORT));
  265. }
  266. void
  267. print_usage (void)
  268. {
  269. printf (_("Usage:"));
  270. printf ("%s [-r] -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15\n", progname);
  271. }