check_users.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /******************************************************************************
  2. *
  3. * CHECK_USERS.C
  4. *
  5. * Program: Current users plugin for Nagios
  6. * License: GPL
  7. * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
  8. *
  9. * Last Modified: $Date$
  10. * Modifications:
  11. *
  12. * 1999-11-17 Karl DeBisschop
  13. * - check stderr and status from spoen/spclose
  14. * - reformat commenst to fit 80-cahr screen
  15. * - set default result to STATE_UNKNOWN
  16. * - initialize users at -1, eliminate 'found' variable
  17. *
  18. * Command line: CHECK_USERS <wusers> <cusers>
  19. *
  20. * Description:
  21. *
  22. * This plugin will use the /usr/bin/who command to check the number
  23. * of users currently logged into the system. If number of logged in
  24. * user exceeds the number specified by the <cusers> option, a
  25. * STATE_CRITICAL is return. It it exceeds <wusers>, a STATE_WARNING
  26. * is returned. Errors reading the output from the who command result
  27. * in a STATE_UNKNOWN error.
  28. *
  29. * License Information:
  30. *
  31. * This program is free software; you can redistribute it and/or modify
  32. * it under the terms of the GNU General Public License as published by
  33. * the Free Software Foundation; either version 2 of the License, or
  34. * (at your option) any later version.
  35. *
  36. * This program is distributed in the hope that it will be useful,
  37. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  38. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  39. * GNU General Public License for more details.
  40. *
  41. * You should have received a copy of the GNU General Public License
  42. * along with this program; if not, write to the Free Software
  43. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  44. *
  45. *****************************************************************************/
  46. #include "common.h"
  47. #include "popen.h"
  48. #include "utils.h"
  49. #define PROGNAME "check_users"
  50. #define possibly_set(a,b) ((a) == 0 ? (b) : 0)
  51. int process_arguments (int, char **);
  52. int call_getopt (int, char **);
  53. void print_usage (void);
  54. void print_help (void);
  55. int wusers = -1;
  56. int cusers = -1;
  57. int
  58. main (int argc, char **argv)
  59. {
  60. int users = -1;
  61. int result = STATE_OK;
  62. char input_buffer[MAX_INPUT_BUFFER];
  63. if (process_arguments (argc, argv) == ERROR)
  64. usage ("Could not parse arguments\n");
  65. /* run the command */
  66. child_process = spopen (WHO_COMMAND);
  67. if (child_process == NULL) {
  68. printf ("Could not open pipe: %s\n", WHO_COMMAND);
  69. return STATE_UNKNOWN;
  70. }
  71. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  72. if (child_stderr == NULL)
  73. printf ("Could not open stderr for %s\n", WHO_COMMAND);
  74. users = 0;
  75. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  76. /* increment 'users' on all lines except total user count */
  77. if (input_buffer[0] != '#') {
  78. users++;
  79. continue;
  80. }
  81. /* get total logged in users */
  82. if (sscanf (input_buffer, "# users=%d", &users) == 1)
  83. break;
  84. }
  85. /* check STDERR */
  86. if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
  87. result = possibly_set (result, STATE_UNKNOWN);
  88. (void) fclose (child_stderr);
  89. /* close the pipe */
  90. if (spclose (child_process))
  91. result = possibly_set (result, STATE_UNKNOWN);
  92. /* else check the user count against warning and critical thresholds */
  93. if (users >= cusers)
  94. result = STATE_CRITICAL;
  95. else if (users >= wusers)
  96. result = STATE_WARNING;
  97. else if (users >= 0)
  98. result = STATE_OK;
  99. if (result == STATE_UNKNOWN)
  100. printf ("Unable to read output\n");
  101. else
  102. printf ("USERS %s - %d users currently logged in\n", state_text (result),
  103. users);
  104. return result;
  105. }
  106. /* process command-line arguments */
  107. int
  108. process_arguments (int argc, char **argv)
  109. {
  110. int c;
  111. if (argc < 2)
  112. usage ("\n");
  113. c = 0;
  114. while ((c += call_getopt (argc - c, &argv[c])) < argc) {
  115. if (is_option (argv[c]))
  116. continue;
  117. if (wusers == -1 && argc > c) {
  118. if (is_intnonneg (argv[c]) == FALSE)
  119. usage ("Warning threshold must be a nonnegative integer\n");
  120. wusers = atoi (argv[c]);
  121. }
  122. else if (cusers == -1 && argc > c) {
  123. if (is_intnonneg (argv[c]) == FALSE)
  124. usage ("Warning threshold must be a nonnegative integer\n");
  125. cusers = atoi (argv[c]);
  126. }
  127. }
  128. return OK;
  129. }
  130. int
  131. call_getopt (int argc, char **argv)
  132. {
  133. int c, i = 0;
  134. #ifdef HAVE_GETOPT_H
  135. int option_index = 0;
  136. static struct option long_options[] = {
  137. {"critical", required_argument, 0, 'c'},
  138. {"warning", required_argument, 0, 'w'},
  139. {"version", no_argument, 0, 'V'},
  140. {"help", no_argument, 0, 'h'},
  141. {0, 0, 0, 0}
  142. };
  143. #endif
  144. while (1) {
  145. #ifdef HAVE_GETOPT_H
  146. c = getopt_long (argc, argv, "+hVvc:w:", long_options, &option_index);
  147. #else
  148. c = getopt (argc, argv, "+hVvc:w:");
  149. #endif
  150. i++;
  151. if (c == -1 || c == EOF || c == 1)
  152. break;
  153. switch (c) {
  154. case 'c':
  155. case 'w':
  156. i++;
  157. }
  158. switch (c) {
  159. case '?': /* print short usage statement if args not parsable */
  160. printf ("%s: Unknown argument: %s\n\n", my_basename (argv[0]), optarg);
  161. print_usage ();
  162. exit (STATE_UNKNOWN);
  163. case 'h': /* help */
  164. print_help ();
  165. exit (STATE_OK);
  166. case 'V': /* version */
  167. print_revision (my_basename (argv[0]), "$Revision$");
  168. exit (STATE_OK);
  169. case 'c': /* critical */
  170. if (!is_intnonneg (optarg))
  171. usage ("Critical threshold must be a nonnegative integer\n");
  172. cusers = atoi (optarg);
  173. break;
  174. case 'w': /* warning */
  175. if (!is_intnonneg (optarg))
  176. usage ("Warning threshold must be a nonnegative integer\n");
  177. wusers = atoi (optarg);
  178. break;
  179. }
  180. }
  181. return i;
  182. }
  183. void
  184. print_usage (void)
  185. {
  186. printf ("Usage: %s -w <users> -c <users>\n", PROGNAME);
  187. }
  188. void
  189. print_help (void)
  190. {
  191. print_revision (PROGNAME, "$Revision$");
  192. printf
  193. ("Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)\n\n"
  194. "This plugin checks the number of users currently logged in on the local\n"
  195. "system and generates an error if the number exceeds the thresholds specified.\n");
  196. print_usage ();
  197. printf
  198. ("Options:\n"
  199. " -w, --warning=INTEGER\n"
  200. " Set WARNING status if more than INTEGER users are logged in\n"
  201. " -c, --critical=INTEGER\n"
  202. " Set CRITICAL status if more than INTEGER users are logged in\n"
  203. " -h, --help\n"
  204. " Print detailed help screen\n"
  205. " -V, --version\n" " Print version information\n");
  206. }