check_users.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*****************************************************************************
  2. *
  3. * Nagios check_users plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 2000-2012 Nagios Plugins Development Team
  7. *
  8. * Description:
  9. *
  10. * This file contains the check_users plugin
  11. *
  12. * This plugin checks the number of users currently logged in on the local
  13. * system and generates an error if the number exceeds the thresholds
  14. * specified.
  15. *
  16. *
  17. * This program is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation, either version 3 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. *
  30. *
  31. *****************************************************************************/
  32. const char *progname = "check_users";
  33. const char *copyright = "2000-2007";
  34. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  35. #include "common.h"
  36. #include "utils.h"
  37. #if HAVE_UTMPX_H
  38. # include <utmpx.h>
  39. #else
  40. # include "popen.h"
  41. #endif
  42. #define possibly_set(a,b) ((a) == 0 ? (b) : 0)
  43. int process_arguments (int, char **);
  44. void print_help (void);
  45. void print_usage (void);
  46. int wusers = -1;
  47. int cusers = -1;
  48. int
  49. main (int argc, char **argv)
  50. {
  51. int users = -1;
  52. int result = STATE_UNKNOWN;
  53. char *perf;
  54. #if HAVE_UTMPX_H
  55. struct utmpx *putmpx;
  56. #else
  57. char input_buffer[MAX_INPUT_BUFFER];
  58. #endif
  59. setlocale (LC_ALL, "");
  60. bindtextdomain (PACKAGE, LOCALEDIR);
  61. textdomain (PACKAGE);
  62. perf = strdup ("");
  63. /* Parse extra opts if any */
  64. argv = np_extra_opts (&argc, argv, progname);
  65. if (process_arguments (argc, argv) == ERROR)
  66. usage4 (_("Could not parse arguments"));
  67. users = 0;
  68. #if HAVE_UTMPX_H
  69. /* get currently logged users from utmpx */
  70. setutxent ();
  71. while ((putmpx = getutxent ()) != NULL)
  72. if (putmpx->ut_type == USER_PROCESS)
  73. users++;
  74. endutxent ();
  75. #else
  76. /* run the command */
  77. child_process = spopen (WHO_COMMAND);
  78. if (child_process == NULL) {
  79. printf (_("Could not open pipe: %s\n"), WHO_COMMAND);
  80. return STATE_UNKNOWN;
  81. }
  82. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  83. if (child_stderr == NULL)
  84. printf (_("Could not open stderr for %s\n"), WHO_COMMAND);
  85. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  86. /* increment 'users' on all lines except total user count */
  87. if (input_buffer[0] != '#') {
  88. users++;
  89. continue;
  90. }
  91. /* get total logged in users */
  92. if (sscanf (input_buffer, _("# users=%d"), &users) == 1)
  93. break;
  94. }
  95. /* check STDERR */
  96. if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
  97. result = possibly_set (result, STATE_UNKNOWN);
  98. (void) fclose (child_stderr);
  99. /* close the pipe */
  100. if (spclose (child_process))
  101. result = possibly_set (result, STATE_UNKNOWN);
  102. #endif
  103. /* check the user count against warning and critical thresholds */
  104. if (users > cusers)
  105. result = STATE_CRITICAL;
  106. else if (users > wusers)
  107. result = STATE_WARNING;
  108. else if (users >= 0)
  109. result = STATE_OK;
  110. if (result == STATE_UNKNOWN)
  111. printf ("%s\n", _("Unable to read output"));
  112. else {
  113. xasprintf (&perf, "%s", perfdata ("users", users, "",
  114. TRUE, wusers,
  115. TRUE, cusers,
  116. TRUE, 0,
  117. FALSE, 0));
  118. printf (_("USERS %s - %d users currently logged in |%s\n"), state_text (result),
  119. users, perf);
  120. }
  121. return result;
  122. }
  123. /* process command-line arguments */
  124. int
  125. process_arguments (int argc, char **argv)
  126. {
  127. int c;
  128. int option = 0;
  129. static struct option longopts[] = {
  130. {"critical", required_argument, 0, 'c'},
  131. {"warning", required_argument, 0, 'w'},
  132. {"version", no_argument, 0, 'V'},
  133. {"help", no_argument, 0, 'h'},
  134. {0, 0, 0, 0}
  135. };
  136. if (argc < 2)
  137. usage ("\n");
  138. while (1) {
  139. c = getopt_long (argc, argv, "+hVvc:w:", longopts, &option);
  140. if (c == -1 || c == EOF || c == 1)
  141. break;
  142. switch (c) {
  143. case '?': /* print short usage statement if args not parsable */
  144. usage5 ();
  145. case 'h': /* help */
  146. print_help ();
  147. exit (STATE_OK);
  148. case 'V': /* version */
  149. print_revision (progname, NP_VERSION);
  150. exit (STATE_OK);
  151. case 'c': /* critical */
  152. if (!is_intnonneg (optarg))
  153. usage4 (_("Critical threshold must be a positive integer"));
  154. else
  155. cusers = atoi (optarg);
  156. break;
  157. case 'w': /* warning */
  158. if (!is_intnonneg (optarg))
  159. usage4 (_("Warning threshold must be a positive integer"));
  160. else
  161. wusers = atoi (optarg);
  162. break;
  163. }
  164. }
  165. c = optind;
  166. if (wusers == -1 && argc > c) {
  167. if (is_intnonneg (argv[c]) == FALSE)
  168. usage4 (_("Warning threshold must be a positive integer"));
  169. else
  170. wusers = atoi (argv[c++]);
  171. }
  172. if (cusers == -1 && argc > c) {
  173. if (is_intnonneg (argv[c]) == FALSE)
  174. usage4 (_("Warning threshold must be a positive integer"));
  175. else
  176. cusers = atoi (argv[c]);
  177. }
  178. return OK;
  179. }
  180. void
  181. print_help (void)
  182. {
  183. print_revision (progname, NP_VERSION);
  184. printf ("Copyright (c) 1999 Ethan Galstad\n");
  185. printf (COPYRIGHT, copyright, email);
  186. printf ("%s\n", _("This plugin checks the number of users currently logged in on the local"));
  187. printf ("%s\n", _("system and generates an error if the number exceeds the thresholds specified."));
  188. printf ("\n\n");
  189. print_usage ();
  190. printf (UT_HELP_VRSN);
  191. printf (UT_EXTRA_OPTS);
  192. printf (" %s\n", "-w, --warning=INTEGER");
  193. printf (" %s\n", _("Set WARNING status if more than INTEGER users are logged in"));
  194. printf (" %s\n", "-c, --critical=INTEGER");
  195. printf (" %s\n", _("Set CRITICAL status if more than INTEGER users are logged in"));
  196. printf (UT_SUPPORT);
  197. }
  198. void
  199. print_usage (void)
  200. {
  201. printf ("%s\n", _("Usage:"));
  202. printf ("%s -w <users> -c <users>\n", progname);
  203. }