check_load.c 7.2 KB

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