check_load.c 8.5 KB

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