check_load.c 8.4 KB

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