check_users.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*****************************************************************************
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  13. *****************************************************************************/
  14. const char *progname = "check_users";
  15. const char *revision = "$Revision$";
  16. const char *copyright = "2000-2003";
  17. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  18. #include "common.h"
  19. #include "popen.h"
  20. #include "utils.h"
  21. void
  22. print_usage (void)
  23. {
  24. printf ("Usage: %s -w <users> -c <users>\n", progname);
  25. printf (_(UT_HLP_VRS), progname, progname);
  26. }
  27. void
  28. print_help (void)
  29. {
  30. print_revision (progname, revision);
  31. printf (_("Copyright (c) 1999 Ethan Galstad\n"));
  32. printf (_(COPYRIGHT), copyright, email);
  33. printf (_("\
  34. This plugin checks the number of users currently logged in on the local\n\
  35. system and generates an error if the number exceeds the thresholds specified.\n"));
  36. print_usage ();
  37. printf (_(UT_HELP_VRSN));
  38. printf (_("\
  39. -w, --warning=INTEGER\n\
  40. Set WARNING status if more than INTEGER users are logged in\n\
  41. -c, --critical=INTEGER\n\
  42. Set CRITICAL status if more than INTEGER users are logged in\n"));
  43. printf (_(UT_SUPPORT));
  44. }
  45. #define possibly_set(a,b) ((a) == 0 ? (b) : 0)
  46. int process_arguments (int, char **);
  47. void print_usage (void);
  48. void print_help (void);
  49. int wusers = -1;
  50. int cusers = -1;
  51. int
  52. main (int argc, char **argv)
  53. {
  54. int users = -1;
  55. int result = STATE_OK;
  56. char input_buffer[MAX_INPUT_BUFFER];
  57. if (process_arguments (argc, argv) == ERROR)
  58. usage (_("Could not parse arguments\n"));
  59. /* run the command */
  60. child_process = spopen (WHO_COMMAND);
  61. if (child_process == NULL) {
  62. printf (_("Could not open pipe: %s\n"), WHO_COMMAND);
  63. return STATE_UNKNOWN;
  64. }
  65. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  66. if (child_stderr == NULL)
  67. printf (_("Could not open stderr for %s\n"), WHO_COMMAND);
  68. users = 0;
  69. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  70. /* increment 'users' on all lines except total user count */
  71. if (input_buffer[0] != '#') {
  72. users++;
  73. continue;
  74. }
  75. /* get total logged in users */
  76. if (sscanf (input_buffer, _("# users=%d"), &users) == 1)
  77. break;
  78. }
  79. /* check STDERR */
  80. if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
  81. result = possibly_set (result, STATE_UNKNOWN);
  82. (void) fclose (child_stderr);
  83. /* close the pipe */
  84. if (spclose (child_process))
  85. result = possibly_set (result, STATE_UNKNOWN);
  86. /* else check the user count against warning and critical thresholds */
  87. if (users >= cusers)
  88. result = STATE_CRITICAL;
  89. else if (users >= wusers)
  90. result = STATE_WARNING;
  91. else if (users >= 0)
  92. result = STATE_OK;
  93. if (result == STATE_UNKNOWN)
  94. printf (_("Unable to read output\n"));
  95. else
  96. printf (_("USERS %s - %d users currently logged in\n"), state_text (result),
  97. users);
  98. return result;
  99. }
  100. /* process command-line arguments */
  101. int
  102. process_arguments (int argc, char **argv)
  103. {
  104. int c;
  105. int option_index = 0;
  106. static struct option long_options[] = {
  107. {"critical", required_argument, 0, 'c'},
  108. {"warning", required_argument, 0, 'w'},
  109. {"version", no_argument, 0, 'V'},
  110. {"help", no_argument, 0, 'h'},
  111. {0, 0, 0, 0}
  112. };
  113. if (argc < 2)
  114. usage ("\n");
  115. while (1) {
  116. c = getopt_long (argc, argv, "+hVvc:w:", long_options, &option_index);
  117. if (c == -1 || c == EOF || c == 1)
  118. break;
  119. switch (c) {
  120. case '?': /* print short usage statement if args not parsable */
  121. printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
  122. print_usage ();
  123. exit (STATE_UNKNOWN);
  124. case 'h': /* help */
  125. print_help ();
  126. exit (STATE_OK);
  127. case 'V': /* version */
  128. print_revision (progname, revision);
  129. exit (STATE_OK);
  130. case 'c': /* critical */
  131. if (!is_intnonneg (optarg))
  132. usage (_("Critical threshold must be a nonnegative integer\n"));
  133. cusers = atoi (optarg);
  134. break;
  135. case 'w': /* warning */
  136. if (!is_intnonneg (optarg))
  137. usage (_("Warning threshold must be a nonnegative integer\n"));
  138. wusers = atoi (optarg);
  139. break;
  140. }
  141. }
  142. c = optind;
  143. if (wusers == -1 && argc > c) {
  144. if (is_intnonneg (argv[c]) == FALSE)
  145. usage (_("Warning threshold must be a nonnegative integer\n"));
  146. wusers = atoi (argv[c++]);
  147. }
  148. if (cusers == -1 && argc > c) {
  149. if (is_intnonneg (argv[c]) == FALSE)
  150. usage (_("Warning threshold must be a nonnegative integer\n"));
  151. cusers = atoi (argv[c]);
  152. }
  153. return OK;
  154. }