check_users.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. /* Parse extra opts if any */
  60. argv=np_extra_opts (&argc, argv, progname);
  61. if (process_arguments (argc, argv) == ERROR)
  62. usage4 (_("Could not parse arguments"));
  63. /* run the command */
  64. child_process = spopen (WHO_COMMAND);
  65. if (child_process == NULL) {
  66. printf (_("Could not open pipe: %s\n"), WHO_COMMAND);
  67. return STATE_UNKNOWN;
  68. }
  69. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  70. if (child_stderr == NULL)
  71. printf (_("Could not open stderr for %s\n"), WHO_COMMAND);
  72. users = 0;
  73. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  74. /* increment 'users' on all lines except total user count */
  75. if (input_buffer[0] != '#') {
  76. users++;
  77. continue;
  78. }
  79. /* get total logged in users */
  80. if (sscanf (input_buffer, _("# users=%d"), &users) == 1)
  81. break;
  82. }
  83. /* check STDERR */
  84. if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
  85. result = possibly_set (result, STATE_UNKNOWN);
  86. (void) fclose (child_stderr);
  87. /* close the pipe */
  88. if (spclose (child_process))
  89. result = possibly_set (result, STATE_UNKNOWN);
  90. /* else check the user count against warning and critical thresholds */
  91. if (users >= cusers)
  92. result = STATE_CRITICAL;
  93. else if (users >= wusers)
  94. result = STATE_WARNING;
  95. else if (users >= 0)
  96. result = STATE_OK;
  97. if (result == STATE_UNKNOWN)
  98. printf ("%s\n", _("Unable to read output"));
  99. else {
  100. asprintf(&perf, "%s", perfdata ("users", users, "",
  101. TRUE, wusers,
  102. TRUE, cusers,
  103. TRUE, 0,
  104. FALSE, 0));
  105. printf (_("USERS %s - %d users currently logged in |%s\n"), state_text (result),
  106. users, perf);
  107. }
  108. return result;
  109. }
  110. /* process command-line arguments */
  111. int
  112. process_arguments (int argc, char **argv)
  113. {
  114. int c;
  115. int option = 0;
  116. static struct option longopts[] = {
  117. {"critical", required_argument, 0, 'c'},
  118. {"warning", required_argument, 0, 'w'},
  119. {"version", no_argument, 0, 'V'},
  120. {"help", no_argument, 0, 'h'},
  121. {0, 0, 0, 0}
  122. };
  123. if (argc < 2)
  124. usage ("\n");
  125. while (1) {
  126. c = getopt_long (argc, argv, "+hVvc:w:", longopts, &option);
  127. if (c == -1 || c == EOF || c == 1)
  128. break;
  129. switch (c) {
  130. case '?': /* print short usage statement if args not parsable */
  131. usage5 ();
  132. case 'h': /* help */
  133. print_help ();
  134. exit (STATE_OK);
  135. case 'V': /* version */
  136. print_revision (progname, revision);
  137. exit (STATE_OK);
  138. case 'c': /* critical */
  139. if (!is_intnonneg (optarg))
  140. usage4 (_("Critical threshold must be a positive integer"));
  141. else
  142. cusers = atoi (optarg);
  143. break;
  144. case 'w': /* warning */
  145. if (!is_intnonneg (optarg))
  146. usage4 (_("Warning threshold must be a positive integer"));
  147. else
  148. wusers = atoi (optarg);
  149. break;
  150. }
  151. }
  152. c = optind;
  153. if (wusers == -1 && argc > c) {
  154. if (is_intnonneg (argv[c]) == FALSE)
  155. usage4 (_("Warning threshold must be a positive integer"));
  156. else
  157. wusers = atoi (argv[c++]);
  158. }
  159. if (cusers == -1 && argc > c) {
  160. if (is_intnonneg (argv[c]) == FALSE)
  161. usage4 (_("Warning threshold must be a positive integer"));
  162. else
  163. cusers = atoi (argv[c]);
  164. }
  165. return OK;
  166. }
  167. void
  168. print_help (void)
  169. {
  170. print_revision (progname, revision);
  171. printf ("Copyright (c) 1999 Ethan Galstad\n");
  172. printf (COPYRIGHT, copyright, email);
  173. printf ("%s\n", _("This plugin checks the number of users currently logged in on the local"));
  174. printf ("%s\n", _("system and generates an error if the number exceeds the thresholds specified."));
  175. printf ("\n\n");
  176. print_usage ();
  177. printf (_(UT_HELP_VRSN));
  178. printf (_(UT_EXTRA_OPTS));
  179. printf (" %s\n", "-w, --warning=INTEGER");
  180. printf (" %s\n", _("Set WARNING status if more than INTEGER users are logged in"));
  181. printf (" %s\n", "-c, --critical=INTEGER");
  182. printf (" %s\n", _("Set CRITICAL status if more than INTEGER users are logged in"));
  183. #ifdef NP_EXTRA_OPTS
  184. printf ("\n");
  185. printf ("%s\n", _("Notes:"));
  186. printf (_(UT_EXTRA_OPTS_NOTES));
  187. #endif
  188. printf (_(UT_SUPPORT));
  189. }
  190. void
  191. print_usage (void)
  192. {
  193. printf (_("Usage:"));
  194. printf ("%s -w <users> -c <users>\n", progname);
  195. }