check_users.c 5.5 KB

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