4
0

check_users.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /******************************************************************************
  2. *
  3. * Nagios check_users plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 2002-2006 nagios-plugins team
  7. *
  8. * Last Modified: $Date$
  9. *
  10. * Description:
  11. *
  12. * This file contains the check_users plugin
  13. *
  14. * License Information:
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  29. *
  30. * $Id$
  31. *
  32. *****************************************************************************/
  33. const char *progname = "check_users";
  34. const char *revision = "$Revision$";
  35. const char *copyright = "2000-2006";
  36. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  37. #include "common.h"
  38. #include "popen.h"
  39. #include "utils.h"
  40. #define possibly_set(a,b) ((a) == 0 ? (b) : 0)
  41. int process_arguments (int, char **);
  42. void print_help (void);
  43. void print_usage (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_UNKNOWN;
  51. char input_buffer[MAX_INPUT_BUFFER];
  52. char *perf;
  53. setlocale (LC_ALL, "");
  54. bindtextdomain (PACKAGE, LOCALEDIR);
  55. textdomain (PACKAGE);
  56. perf = strdup("");
  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. usage2 (_("Unknown argument"), optarg);
  128. case 'h': /* help */
  129. print_help ();
  130. exit (STATE_OK);
  131. case 'V': /* version */
  132. print_revision (progname, revision);
  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, revision);
  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 (" %s\n", "-w, --warning=INTEGER");
  175. printf (" %s\n", _("Set WARNING status if more than INTEGER users are logged in"));
  176. printf (" %s\n", "-c, --critical=INTEGER");
  177. printf (" %s\n", _("Set CRITICAL status if more than INTEGER users are logged in"));
  178. printf (_(UT_SUPPORT));
  179. }
  180. void
  181. print_usage (void)
  182. {
  183. printf (_("Usage:"));
  184. printf ("%s -w <users> -c <users>\n", progname);
  185. }