check_users.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #define possibly_set(a,b) ((a) == 0 ? (b) : 0)
  22. int process_arguments (int, char **);
  23. void print_help (void);
  24. void print_usage (void);
  25. int wusers = -1;
  26. int cusers = -1;
  27. int
  28. main (int argc, char **argv)
  29. {
  30. int users = -1;
  31. int result = STATE_OK;
  32. char input_buffer[MAX_INPUT_BUFFER];
  33. if (process_arguments (argc, argv) == ERROR)
  34. usage (_("Could not parse arguments\n"));
  35. /* run the command */
  36. child_process = spopen (WHO_COMMAND);
  37. if (child_process == NULL) {
  38. printf (_("Could not open pipe: %s\n"), WHO_COMMAND);
  39. return STATE_UNKNOWN;
  40. }
  41. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  42. if (child_stderr == NULL)
  43. printf (_("Could not open stderr for %s\n"), WHO_COMMAND);
  44. users = 0;
  45. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  46. /* increment 'users' on all lines except total user count */
  47. if (input_buffer[0] != '#') {
  48. users++;
  49. continue;
  50. }
  51. /* get total logged in users */
  52. if (sscanf (input_buffer, _("# users=%d"), &users) == 1)
  53. break;
  54. }
  55. /* check STDERR */
  56. if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
  57. result = possibly_set (result, STATE_UNKNOWN);
  58. (void) fclose (child_stderr);
  59. /* close the pipe */
  60. if (spclose (child_process))
  61. result = possibly_set (result, STATE_UNKNOWN);
  62. /* else check the user count against warning and critical thresholds */
  63. if (users >= cusers)
  64. result = STATE_CRITICAL;
  65. else if (users >= wusers)
  66. result = STATE_WARNING;
  67. else if (users >= 0)
  68. result = STATE_OK;
  69. if (result == STATE_UNKNOWN)
  70. printf (_("Unable to read output\n"));
  71. else
  72. printf (_("USERS %s - %d users currently logged in\n"), state_text (result),
  73. users);
  74. return result;
  75. }
  76. /* process command-line arguments */
  77. int
  78. process_arguments (int argc, char **argv)
  79. {
  80. int c;
  81. int option = 0;
  82. static struct option longopts[] = {
  83. {"critical", required_argument, 0, 'c'},
  84. {"warning", required_argument, 0, 'w'},
  85. {"version", no_argument, 0, 'V'},
  86. {"help", no_argument, 0, 'h'},
  87. {0, 0, 0, 0}
  88. };
  89. if (argc < 2)
  90. usage ("\n");
  91. while (1) {
  92. c = getopt_long (argc, argv, "+hVvc:w:", longopts, &option);
  93. if (c == -1 || c == EOF || c == 1)
  94. break;
  95. switch (c) {
  96. case '?': /* print short usage statement if args not parsable */
  97. printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
  98. print_usage ();
  99. exit (STATE_UNKNOWN);
  100. case 'h': /* help */
  101. print_help ();
  102. exit (STATE_OK);
  103. case 'V': /* version */
  104. print_revision (progname, revision);
  105. exit (STATE_OK);
  106. case 'c': /* critical */
  107. if (!is_intnonneg (optarg))
  108. usage (_("Critical threshold must be a nonnegative integer\n"));
  109. else
  110. cusers = atoi (optarg);
  111. break;
  112. case 'w': /* warning */
  113. if (!is_intnonneg (optarg))
  114. usage (_("Warning threshold must be a nonnegative integer\n"));
  115. else
  116. wusers = atoi (optarg);
  117. break;
  118. }
  119. }
  120. c = optind;
  121. if (wusers == -1 && argc > c) {
  122. if (is_intnonneg (argv[c]) == FALSE)
  123. usage (_("Warning threshold must be a nonnegative integer\n"));
  124. else
  125. wusers = atoi (argv[c++]);
  126. }
  127. if (cusers == -1 && argc > c) {
  128. if (is_intnonneg (argv[c]) == FALSE)
  129. usage (_("Warning threshold must be a nonnegative integer\n"));
  130. else
  131. cusers = atoi (argv[c]);
  132. }
  133. return OK;
  134. }
  135. void
  136. print_help (void)
  137. {
  138. print_revision (progname, revision);
  139. printf (_("Copyright (c) 1999 Ethan Galstad\n"));
  140. printf (_(COPYRIGHT), copyright, email);
  141. printf (_("\
  142. This plugin checks the number of users currently logged in on the local\n\
  143. system and generates an error if the number exceeds the thresholds specified.\n"));
  144. print_usage ();
  145. printf (_(UT_HELP_VRSN));
  146. printf (_("\
  147. -w, --warning=INTEGER\n\
  148. Set WARNING status if more than INTEGER users are logged in\n\
  149. -c, --critical=INTEGER\n\
  150. Set CRITICAL status if more than INTEGER users are logged in\n"));
  151. printf (_(UT_SUPPORT));
  152. }
  153. void
  154. print_usage (void)
  155. {
  156. printf ("Usage: %s -w <users> -c <users>\n", progname);
  157. printf (_(UT_HLP_VRS), progname, progname);
  158. }