check_load.c 8.0 KB

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