4
0

check_users.c 5.2 KB

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