check_load.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 "runcmd.h"
  35. #include "utils.h"
  36. #include "popen.h"
  37. #ifdef HAVE_SYS_LOADAVG_H
  38. #include <sys/loadavg.h>
  39. #endif
  40. /* needed for compilation under NetBSD, as suggested by Andy Doran */
  41. #ifndef LOADAVG_1MIN
  42. #define LOADAVG_1MIN 0
  43. #define LOADAVG_5MIN 1
  44. #define LOADAVG_15MIN 2
  45. #endif /* !defined LOADAVG_1MIN */
  46. static int process_arguments (int argc, char **argv);
  47. static int validate_arguments (void);
  48. void print_help (void);
  49. void print_usage (void);
  50. static int print_top_consuming_processes();
  51. static int n_procs_to_show = 0;
  52. /* strictly for pretty-print usage in loops */
  53. static const int nums[3] = { 1, 5, 15 };
  54. /* provide some fairly sane defaults */
  55. double wload[3] = { 0.0, 0.0, 0.0 };
  56. double cload[3] = { 0.0, 0.0, 0.0 };
  57. #define la1 la[0]
  58. #define la5 la[1]
  59. #define la15 la[2]
  60. char *status_line;
  61. int take_into_account_cpus = 0;
  62. static void
  63. get_threshold(char *arg, double *th)
  64. {
  65. size_t i, n;
  66. int valid = 0;
  67. char *str = arg, *p;
  68. n = strlen(arg);
  69. for(i = 0; i < 3; i++) {
  70. th[i] = strtod(str, &p);
  71. if(p == str) break;
  72. valid = 1;
  73. str = p + 1;
  74. if(n <= (size_t)(str - arg)) break;
  75. }
  76. /* empty argument or non-floatish, so warn about it and die */
  77. if(!i && !valid) usage (_("Warning threshold must be float or float triplet!\n"));
  78. if(i != 2) {
  79. /* one or more numbers were given, so fill array with last
  80. * we got (most likely to NOT produce the least expected result) */
  81. for(n = i; n < 3; n++) th[n] = th[i];
  82. }
  83. }
  84. int
  85. main (int argc, char **argv)
  86. {
  87. int result;
  88. int i;
  89. long numcpus;
  90. double la[3] = { 0.0, 0.0, 0.0 }; /* NetBSD complains about uninitialized arrays */
  91. #ifndef HAVE_GETLOADAVG
  92. char input_buffer[MAX_INPUT_BUFFER];
  93. #endif
  94. setlocale (LC_ALL, "");
  95. bindtextdomain (PACKAGE, LOCALEDIR);
  96. textdomain (PACKAGE);
  97. setlocale(LC_NUMERIC, "POSIX");
  98. /* Parse extra opts if any */
  99. argv = np_extra_opts (&argc, argv, progname);
  100. if (process_arguments (argc, argv) == ERROR)
  101. usage4 (_("Could not parse arguments"));
  102. #ifdef HAVE_GETLOADAVG
  103. result = getloadavg (la, 3);
  104. if (result != 3)
  105. return STATE_UNKNOWN;
  106. #else
  107. child_process = spopen (PATH_TO_UPTIME);
  108. if (child_process == NULL) {
  109. printf (_("Error opening %s\n"), PATH_TO_UPTIME);
  110. return STATE_UNKNOWN;
  111. }
  112. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  113. if (child_stderr == NULL) {
  114. printf (_("Could not open stderr for %s\n"), PATH_TO_UPTIME);
  115. }
  116. fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process);
  117. if(strstr(input_buffer, "load average:")) {
  118. sscanf (input_buffer, "%*[^l]load average: %lf, %lf, %lf", &la1, &la5, &la15);
  119. }
  120. else if(strstr(input_buffer, "load averages:")) {
  121. sscanf (input_buffer, "%*[^l]load averages: %lf, %lf, %lf", &la1, &la5, &la15);
  122. }
  123. else {
  124. printf (_("could not parse load %d from uptime: %s\n"), result, PATH_TO_UPTIME);
  125. return STATE_UNKNOWN;
  126. }
  127. result = spclose (child_process);
  128. if (result) {
  129. printf (_("Error code %d returned in %s\n"), result, PATH_TO_UPTIME);
  130. return STATE_UNKNOWN;
  131. }
  132. #endif
  133. if (take_into_account_cpus == 1) {
  134. if ((numcpus = GET_NUMBER_OF_CPUS()) > 0) {
  135. la[0] = la[0] / numcpus;
  136. la[1] = la[1] / numcpus;
  137. la[2] = la[2] / numcpus;
  138. }
  139. }
  140. if ((la[0] < 0.0) || (la[1] < 0.0) || (la[2] < 0.0)) {
  141. #ifdef HAVE_GETLOADAVG
  142. printf (_("Error in getloadavg()\n"));
  143. #else
  144. printf (_("Error processing %s\n"), PATH_TO_UPTIME);
  145. #endif
  146. return STATE_UNKNOWN;
  147. }
  148. /* we got this far, so assume OK until we've measured */
  149. result = STATE_OK;
  150. if (take_into_account_cpus == 1 && (numcpus = GET_NUMBER_OF_CPUS()) > 1){
  151. xasprintf(&status_line, _("load average per CPU: %.2f, %.2f, %.2f"), la1, la5, la15);
  152. }
  153. else
  154. xasprintf(&status_line, _("load average: %.2f, %.2f, %.2f"), la1, la5, la15);
  155. for(i = 0; i < 3; i++) {
  156. if(la[i] > cload[i]) {
  157. result = STATE_CRITICAL;
  158. break;
  159. }
  160. else if(la[i] > wload[i]) result = STATE_WARNING;
  161. }
  162. printf("%s - %s|", state_text(result), status_line);
  163. for(i = 0; i < 3; i++)
  164. printf("load%d=%.3f;%.3f;%.3f;0; ", nums[i], la[i], wload[i], cload[i]);
  165. putchar('\n');
  166. if (n_procs_to_show > 0) {
  167. print_top_consuming_processes();
  168. }
  169. return result;
  170. }
  171. /* process command-line arguments */
  172. static int
  173. process_arguments (int argc, char **argv)
  174. {
  175. int c = 0;
  176. int option = 0;
  177. static struct option longopts[] = {
  178. {"warning", required_argument, 0, 'w'},
  179. {"critical", required_argument, 0, 'c'},
  180. {"percpu", no_argument, 0, 'r'},
  181. {"version", no_argument, 0, 'V'},
  182. {"help", no_argument, 0, 'h'},
  183. {"procs-to-show", required_argument, 0, 'n'},
  184. {0, 0, 0, 0}
  185. };
  186. if (argc < 2)
  187. return ERROR;
  188. while (1) {
  189. c = getopt_long (argc, argv, "Vhrc:w:n:", longopts, &option);
  190. if (c == -1 || c == EOF)
  191. break;
  192. switch (c) {
  193. case 'w': /* warning time threshold */
  194. get_threshold(optarg, wload);
  195. break;
  196. case 'c': /* critical time threshold */
  197. get_threshold(optarg, cload);
  198. break;
  199. case 'r': /* Divide load average by number of CPUs */
  200. take_into_account_cpus = 1;
  201. break;
  202. case 'V': /* version */
  203. print_revision (progname, NP_VERSION);
  204. exit (STATE_OK);
  205. case 'h': /* help */
  206. print_help ();
  207. exit (STATE_OK);
  208. case 'n':
  209. n_procs_to_show = atoi(optarg);
  210. break;
  211. case '?': /* help */
  212. usage5 ();
  213. }
  214. }
  215. c = optind;
  216. if (c == argc)
  217. return validate_arguments ();
  218. /* handle the case if both arguments are missing,
  219. * but not if only one is given without -c or -w flag */
  220. if(c - argc == 2) {
  221. get_threshold(argv[c++], wload);
  222. get_threshold(argv[c++], cload);
  223. }
  224. else if(c - argc == 1) {
  225. get_threshold(argv[c++], cload);
  226. }
  227. return validate_arguments ();
  228. }
  229. static int
  230. validate_arguments (void)
  231. {
  232. int i = 0;
  233. /* match cload first, as it will give the most friendly error message
  234. * if user hasn't given the -c switch properly */
  235. for(i = 0; i < 3; i++) {
  236. if(cload[i] < 0)
  237. die (STATE_UNKNOWN, _("Critical threshold for %d-minute load average is not specified\n"), nums[i]);
  238. if(wload[i] < 0)
  239. die (STATE_UNKNOWN, _("Warning threshold for %d-minute load average is not specified\n"), nums[i]);
  240. if(wload[i] > cload[i])
  241. die (STATE_UNKNOWN, _("Parameter inconsistency: %d-minute \"warning load\" is greater than \"critical load\"\n"), nums[i]);
  242. }
  243. return OK;
  244. }
  245. void
  246. print_help (void)
  247. {
  248. print_revision (progname, NP_VERSION);
  249. printf ("Copyright (c) 1999 Felipe Gustavo de Almeida <galmeida@linux.ime.usp.br>\n");
  250. printf (COPYRIGHT, copyright, email);
  251. printf (_("This plugin tests the current system load average."));
  252. printf ("\n\n");
  253. print_usage ();
  254. printf (UT_HELP_VRSN);
  255. printf (UT_EXTRA_OPTS);
  256. printf (" %s\n", "-w, --warning=WLOAD1,WLOAD5,WLOAD15");
  257. printf (" %s\n", _("Exit with WARNING status if load average exceeds WLOADn"));
  258. printf (" %s\n", "-c, --critical=CLOAD1,CLOAD5,CLOAD15");
  259. printf (" %s\n", _("Exit with CRITICAL status if load average exceed CLOADn"));
  260. printf (" %s\n", _("the load average format is the same used by \"uptime\" and \"w\""));
  261. printf (" %s\n", "-r, --percpu");
  262. printf (" %s\n", _("Divide the load averages by the number of CPUs (when possible)"));
  263. printf (" %s\n", "-n, --procs-to-show=NUMBER_OF_PROCS");
  264. printf (" %s\n", _("Number of processes to show when printing the top consuming processes."));
  265. printf (" %s\n", _("NUMBER_OF_PROCS=0 disables this feature. Default value is 0"));
  266. printf (UT_SUPPORT);
  267. }
  268. void
  269. print_usage (void)
  270. {
  271. printf ("%s\n", _("Usage:"));
  272. printf ("%s [-r] -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15 [-n NUMBER_OF_PROCS]\n", progname);
  273. }
  274. #ifdef PS_USES_PROCPCPU
  275. int cmpstringp(const void *p1, const void *p2) {
  276. int procuid = 0;
  277. pid_t procpid = 0;
  278. pid_t procppid = 0;
  279. pid_t kthread_ppid = 0;
  280. int procvsz = 0;
  281. int procrss = 0;
  282. float procpcpu = 0;
  283. char procstat[8];
  284. #ifdef PS_USES_PROCETIME
  285. char procetime[MAX_INPUT_BUFFER] = { '\0' };
  286. #endif /* PS_USES_PROCETIME */
  287. char *procprog;
  288. char *proc_cgroup_hierarchy;
  289. int pos;
  290. sscanf (* (char * const *) p1, PS_FORMAT, PS_VARLIST);
  291. float procpcpu1 = procpcpu;
  292. sscanf (* (char * const *) p2, PS_FORMAT, PS_VARLIST);
  293. return procpcpu1 < procpcpu;
  294. }
  295. #endif /* PS_USES_PROCPCPU */
  296. static int print_top_consuming_processes() {
  297. int i = 0;
  298. struct output chld_out, chld_err;
  299. if(np_runcmd(PS_COMMAND, &chld_out, &chld_err, 0) != 0){
  300. fprintf(stderr, _("'%s' exited with non-zero status.\n"), PS_COMMAND);
  301. return STATE_UNKNOWN;
  302. }
  303. if (chld_out.lines < 2) {
  304. fprintf(stderr, _("some error occurred getting procs list.\n"));
  305. return STATE_UNKNOWN;
  306. }
  307. #ifdef PS_USES_PROCPCPU
  308. qsort(chld_out.line + 1, chld_out.lines - 1, sizeof(char*), cmpstringp);
  309. #endif /* PS_USES_PROCPCPU */
  310. int lines_to_show = chld_out.lines < (n_procs_to_show + 1)
  311. ? chld_out.lines : n_procs_to_show + 1;
  312. for (i = 0; i < lines_to_show; i += 1) {
  313. printf("%s\n", chld_out.line[i]);
  314. }
  315. return OK;
  316. }