check_load.c 8.5 KB

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