check_users.c 5.1 KB

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