check_load.c 11 KB

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