check_users.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*****************************************************************************
  2. *
  3. * Nagios check_users plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 2000-2012 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 "utils.h"
  37. #include <utmpx.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 *perf;
  50. struct utmpx *putmpx;
  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. users = 0;
  60. /* get currently logged users from utmpx */
  61. setutxent ();
  62. while ((putmpx = getutxent ()) != NULL)
  63. if (putmpx->ut_type == USER_PROCESS)
  64. users++;
  65. endutxent ();
  66. /* check the user count against warning and critical thresholds */
  67. if (users > cusers)
  68. result = STATE_CRITICAL;
  69. else if (users > wusers)
  70. result = STATE_WARNING;
  71. else if (users >= 0)
  72. result = STATE_OK;
  73. if (result == STATE_UNKNOWN)
  74. printf ("%s\n", _("Unable to read output"));
  75. else {
  76. xasprintf (&perf, "%s", perfdata ("users", users, "",
  77. TRUE, wusers,
  78. TRUE, cusers,
  79. TRUE, 0,
  80. FALSE, 0));
  81. printf (_("USERS %s - %d users currently logged in |%s\n"), state_text (result),
  82. users, perf);
  83. }
  84. return result;
  85. }
  86. /* process command-line arguments */
  87. int
  88. process_arguments (int argc, char **argv)
  89. {
  90. int c;
  91. int option = 0;
  92. static struct option longopts[] = {
  93. {"critical", required_argument, 0, 'c'},
  94. {"warning", required_argument, 0, 'w'},
  95. {"version", no_argument, 0, 'V'},
  96. {"help", no_argument, 0, 'h'},
  97. {0, 0, 0, 0}
  98. };
  99. if (argc < 2)
  100. usage ("\n");
  101. while (1) {
  102. c = getopt_long (argc, argv, "+hVvc:w:", longopts, &option);
  103. if (c == -1 || c == EOF || c == 1)
  104. break;
  105. switch (c) {
  106. case '?': /* print short usage statement if args not parsable */
  107. usage5 ();
  108. case 'h': /* help */
  109. print_help ();
  110. exit (STATE_OK);
  111. case 'V': /* version */
  112. print_revision (progname, NP_VERSION);
  113. exit (STATE_OK);
  114. case 'c': /* critical */
  115. if (!is_intnonneg (optarg))
  116. usage4 (_("Critical threshold must be a positive integer"));
  117. else
  118. cusers = atoi (optarg);
  119. break;
  120. case 'w': /* warning */
  121. if (!is_intnonneg (optarg))
  122. usage4 (_("Warning threshold must be a positive integer"));
  123. else
  124. wusers = atoi (optarg);
  125. break;
  126. }
  127. }
  128. c = optind;
  129. if (wusers == -1 && argc > c) {
  130. if (is_intnonneg (argv[c]) == FALSE)
  131. usage4 (_("Warning threshold must be a positive integer"));
  132. else
  133. wusers = atoi (argv[c++]);
  134. }
  135. if (cusers == -1 && argc > c) {
  136. if (is_intnonneg (argv[c]) == FALSE)
  137. usage4 (_("Warning threshold must be a positive integer"));
  138. else
  139. cusers = atoi (argv[c]);
  140. }
  141. return OK;
  142. }
  143. void
  144. print_help (void)
  145. {
  146. print_revision (progname, NP_VERSION);
  147. printf ("Copyright (c) 1999 Ethan Galstad\n");
  148. printf (COPYRIGHT, copyright, email);
  149. printf ("%s\n", _("This plugin checks the number of users currently logged in on the local"));
  150. printf ("%s\n", _("system and generates an error if the number exceeds the thresholds specified."));
  151. printf ("\n\n");
  152. print_usage ();
  153. printf (UT_HELP_VRSN);
  154. printf (UT_EXTRA_OPTS);
  155. printf (" %s\n", "-w, --warning=INTEGER");
  156. printf (" %s\n", _("Set WARNING status if more than INTEGER users are logged in"));
  157. printf (" %s\n", "-c, --critical=INTEGER");
  158. printf (" %s\n", _("Set CRITICAL status if more than INTEGER users are logged in"));
  159. printf (UT_SUPPORT);
  160. }
  161. void
  162. print_usage (void)
  163. {
  164. printf ("%s\n", _("Usage:"));
  165. printf ("%s -w <users> -c <users>\n", progname);
  166. }