check_load.c 7.1 KB

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