check_users.c 5.3 KB

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