check_load.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. # ifdef HAVE_PROC_LOADAVG
  94. FILE *fp;
  95. char *str, *next;
  96. # endif
  97. #endif
  98. setlocale (LC_ALL, "");
  99. bindtextdomain (PACKAGE, LOCALEDIR);
  100. textdomain (PACKAGE);
  101. setlocale(LC_NUMERIC, "POSIX");
  102. /* Parse extra opts if any */
  103. argv = np_extra_opts (&argc, argv, progname);
  104. if (process_arguments (argc, argv) == ERROR)
  105. usage4 (_("Could not parse arguments"));
  106. #ifdef HAVE_GETLOADAVG
  107. result = getloadavg (la, 3);
  108. if (result != 3)
  109. return STATE_UNKNOWN;
  110. #else
  111. # ifdef HAVE_PROC_LOADAVG
  112. fp = fopen (PROC_LOADAVG, "r");
  113. if (fp == NULL) {
  114. printf (_("Error opening %s\n"), PROC_LOADAVG);
  115. return STATE_UNKNOWN;
  116. }
  117. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
  118. str = (char *)input_buffer;
  119. for(i = 0; i < 3; i++) {
  120. la[i] = strtod(str, &next);
  121. str = next;
  122. }
  123. }
  124. fclose (fp);
  125. # else
  126. child_process = spopen (PATH_TO_UPTIME);
  127. if (child_process == NULL) {
  128. printf (_("Error opening %s\n"), PATH_TO_UPTIME);
  129. return STATE_UNKNOWN;
  130. }
  131. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  132. if (child_stderr == NULL) {
  133. printf (_("Could not open stderr for %s\n"), PATH_TO_UPTIME);
  134. }
  135. fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process);
  136. if(strstr(input_buffer, "load average:")) {
  137. sscanf (input_buffer, "%*[^l]load average: %lf, %lf, %lf", &la1, &la5, &la15);
  138. }
  139. else if(strstr(input_buffer, "load averages:")) {
  140. sscanf (input_buffer, "%*[^l]load averages: %lf, %lf, %lf", &la1, &la5, &la15);
  141. }
  142. else {
  143. printf (_("could not parse load %d from uptime: %s\n"), result, PATH_TO_UPTIME);
  144. return STATE_UNKNOWN;
  145. }
  146. result = spclose (child_process);
  147. if (result) {
  148. printf (_("Error code %d returned in %s\n"), result, PATH_TO_UPTIME);
  149. return STATE_UNKNOWN;
  150. }
  151. # endif
  152. #endif
  153. if (take_into_account_cpus == 1) {
  154. if ((numcpus = GET_NUMBER_OF_CPUS()) > 0) {
  155. la[0] = la[0] / numcpus;
  156. la[1] = la[1] / numcpus;
  157. la[2] = la[2] / numcpus;
  158. }
  159. }
  160. if ((la[0] < 0.0) || (la[1] < 0.0) || (la[2] < 0.0)) {
  161. #ifdef HAVE_GETLOADAVG
  162. printf (_("Error in getloadavg()\n"));
  163. #else
  164. # ifdef HAVE_PROC_LOADAVG
  165. printf (_("Error processing %s\n"), PROC_LOADAVG);
  166. # else
  167. printf (_("Error processing %s\n"), PATH_TO_UPTIME);
  168. # endif
  169. #endif
  170. return STATE_UNKNOWN;
  171. }
  172. /* we got this far, so assume OK until we've measured */
  173. result = STATE_OK;
  174. if (take_into_account_cpus == 1 && (numcpus = GET_NUMBER_OF_CPUS()) > 1){
  175. xasprintf(&status_line, _("load average per CPU: %.2f, %.2f, %.2f"), la1, la5, la15);
  176. }
  177. else
  178. xasprintf(&status_line, _("load average: %.2f, %.2f, %.2f"), la1, la5, la15);
  179. for(i = 0; i < 3; i++) {
  180. if(la[i] > cload[i]) {
  181. result = STATE_CRITICAL;
  182. break;
  183. }
  184. else if(la[i] > wload[i]) result = STATE_WARNING;
  185. }
  186. printf("%s - %s|", state_text(result), status_line);
  187. for(i = 0; i < 3; i++)
  188. printf("load%d=%.3f;%.3f;%.3f;0; ", nums[i], la[i], wload[i], cload[i]);
  189. putchar('\n');
  190. if (n_procs_to_show > 0) {
  191. print_top_consuming_processes();
  192. }
  193. return result;
  194. }
  195. /* process command-line arguments */
  196. static int
  197. process_arguments (int argc, char **argv)
  198. {
  199. int c = 0;
  200. int option = 0;
  201. static struct option longopts[] = {
  202. {"warning", required_argument, 0, 'w'},
  203. {"critical", required_argument, 0, 'c'},
  204. {"percpu", no_argument, 0, 'r'},
  205. {"version", no_argument, 0, 'V'},
  206. {"help", no_argument, 0, 'h'},
  207. {"procs-to-show", required_argument, 0, 'n'},
  208. {0, 0, 0, 0}
  209. };
  210. if (argc < 2)
  211. return ERROR;
  212. while (1) {
  213. c = getopt_long (argc, argv, "Vhrc:w:n:", longopts, &option);
  214. if (c == -1 || c == EOF)
  215. break;
  216. switch (c) {
  217. case 'w': /* warning time threshold */
  218. get_threshold(optarg, wload);
  219. break;
  220. case 'c': /* critical time threshold */
  221. get_threshold(optarg, cload);
  222. break;
  223. case 'r': /* Divide load average by number of CPUs */
  224. take_into_account_cpus = 1;
  225. break;
  226. case 'V': /* version */
  227. print_revision (progname, NP_VERSION);
  228. exit (STATE_OK);
  229. case 'h': /* help */
  230. print_help ();
  231. exit (STATE_OK);
  232. case 'n':
  233. n_procs_to_show = atoi(optarg);
  234. break;
  235. case '?': /* help */
  236. usage5 ();
  237. }
  238. }
  239. c = optind;
  240. if (c == argc)
  241. return validate_arguments ();
  242. /* handle the case if both arguments are missing,
  243. * but not if only one is given without -c or -w flag */
  244. if(c - argc == 2) {
  245. get_threshold(argv[c++], wload);
  246. get_threshold(argv[c++], cload);
  247. }
  248. else if(c - argc == 1) {
  249. get_threshold(argv[c++], cload);
  250. }
  251. return validate_arguments ();
  252. }
  253. static int
  254. validate_arguments (void)
  255. {
  256. int i = 0;
  257. /* match cload first, as it will give the most friendly error message
  258. * if user hasn't given the -c switch properly */
  259. for(i = 0; i < 3; i++) {
  260. if(cload[i] < 0)
  261. die (STATE_UNKNOWN, _("Critical threshold for %d-minute load average is not specified\n"), nums[i]);
  262. if(wload[i] < 0)
  263. die (STATE_UNKNOWN, _("Warning threshold for %d-minute load average is not specified\n"), nums[i]);
  264. if(wload[i] > cload[i])
  265. die (STATE_UNKNOWN, _("Parameter inconsistency: %d-minute \"warning load\" is greater than \"critical load\"\n"), nums[i]);
  266. }
  267. return OK;
  268. }
  269. void
  270. print_help (void)
  271. {
  272. print_revision (progname, NP_VERSION);
  273. printf ("Copyright (c) 1999 Felipe Gustavo de Almeida <galmeida@linux.ime.usp.br>\n");
  274. printf (COPYRIGHT, copyright, email);
  275. printf (_("This plugin tests the current system load average."));
  276. printf ("\n\n");
  277. print_usage ();
  278. printf (UT_HELP_VRSN);
  279. printf (UT_EXTRA_OPTS);
  280. printf (" %s\n", "-w, --warning=WLOAD1,WLOAD5,WLOAD15");
  281. printf (" %s\n", _("Exit with WARNING status if load average exceeds WLOADn"));
  282. printf (" %s\n", "-c, --critical=CLOAD1,CLOAD5,CLOAD15");
  283. printf (" %s\n", _("Exit with CRITICAL status if load average exceed CLOADn"));
  284. printf (" %s\n", _("the load average format is the same used by \"uptime\" and \"w\""));
  285. printf (" %s\n", "-r, --percpu");
  286. printf (" %s\n", _("Divide the load averages by the number of CPUs (when possible)"));
  287. printf (" %s\n", "-n, --procs-to-show=NUMBER_OF_PROCS");
  288. printf (" %s\n", _("Number of processes to show when printing the top consuming processes."));
  289. printf (" %s\n", _("NUMBER_OF_PROCS=0 disables this feature. Default value is 0"));
  290. printf (UT_SUPPORT);
  291. }
  292. void
  293. print_usage (void)
  294. {
  295. printf ("%s\n", _("Usage:"));
  296. printf ("%s [-r] -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15 [-n NUMBER_OF_PROCS]\n", progname);
  297. }
  298. #ifdef PS_USES_PROCPCPU
  299. int cmpstringp(const void *p1, const void *p2) {
  300. int procuid = 0;
  301. pid_t procpid = 0;
  302. pid_t procppid = 0;
  303. pid_t kthread_ppid = 0;
  304. int procvsz = 0;
  305. int procrss = 0;
  306. float procpcpu = 0;
  307. char procstat[8];
  308. #ifdef PS_USES_PROCETIME
  309. char procetime[MAX_INPUT_BUFFER] = { '\0' };
  310. #endif /* PS_USES_PROCETIME */
  311. char *procprog;
  312. char *proc_cgroup_hierarchy;
  313. int pos;
  314. sscanf (* (char * const *) p1, PS_FORMAT, PS_VARLIST);
  315. float procpcpu1 = procpcpu;
  316. sscanf (* (char * const *) p2, PS_FORMAT, PS_VARLIST);
  317. return procpcpu1 < procpcpu;
  318. }
  319. #endif /* PS_USES_PROCPCPU */
  320. static int print_top_consuming_processes() {
  321. int i = 0;
  322. struct output chld_out, chld_err;
  323. if(np_runcmd(PS_COMMAND, &chld_out, &chld_err, 0) != 0){
  324. fprintf(stderr, _("'%s' exited with non-zero status.\n"), PS_COMMAND);
  325. return STATE_UNKNOWN;
  326. }
  327. if (chld_out.lines < 2) {
  328. fprintf(stderr, _("some error occurred getting procs list.\n"));
  329. return STATE_UNKNOWN;
  330. }
  331. #ifdef PS_USES_PROCPCPU
  332. qsort(chld_out.line + 1, chld_out.lines - 1, sizeof(char*), cmpstringp);
  333. #endif /* PS_USES_PROCPCPU */
  334. int lines_to_show = chld_out.lines < (n_procs_to_show + 1)
  335. ? chld_out.lines : n_procs_to_show + 1;
  336. for (i = 0; i < lines_to_show; i += 1) {
  337. printf("%s\n", chld_out.line[i]);
  338. }
  339. return OK;
  340. }