4
0

check_load.c 10 KB

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