check_users.c 5.6 KB

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