check_users.c 5.2 KB

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