check_users.c 4.9 KB

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