check_users.c 5.6 KB

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