check_load.c 7.5 KB

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