check_users.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*****************************************************************************
  2. *
  3. * Nagios check_users plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 2000-2014 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-2014";
  34. const char *email = "devel@nagios-plugins.org";
  35. #include "common.h"
  36. #include "utils.h"
  37. #if HAVE_WTSAPI32_H
  38. # include <windows.h>
  39. # include <wtsapi32.h>
  40. # undef ERROR
  41. # define ERROR -1
  42. #elif HAVE_UTMPX_H
  43. # include <utmpx.h>
  44. #else
  45. # include "popen.h"
  46. #endif
  47. #define possibly_set(a,b) ((a) == 0 ? (b) : 0)
  48. int process_arguments (int, char **);
  49. void print_help (void);
  50. void print_usage (void);
  51. int wusers = -1;
  52. int cusers = -1;
  53. int
  54. main (int argc, char **argv)
  55. {
  56. int users = -1;
  57. int result = STATE_UNKNOWN;
  58. char *perf;
  59. #if HAVE_WTSAPI32_H
  60. WTS_SESSION_INFO *wtsinfo;
  61. DWORD wtscount;
  62. DWORD index;
  63. #elif HAVE_UTMPX_H
  64. struct utmpx *putmpx;
  65. #else
  66. char input_buffer[MAX_INPUT_BUFFER];
  67. #endif
  68. setlocale (LC_ALL, "");
  69. bindtextdomain (PACKAGE, LOCALEDIR);
  70. textdomain (PACKAGE);
  71. perf = strdup ("");
  72. /* Parse extra opts if any */
  73. argv = np_extra_opts (&argc, argv, progname);
  74. if (process_arguments (argc, argv) == ERROR)
  75. usage4 (_("Could not parse arguments"));
  76. users = 0;
  77. #if HAVE_WTSAPI32_H
  78. if (!WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE,
  79. 0, 1, &wtsinfo, &wtscount)) {
  80. printf(_("Could not enumerate RD sessions: %d\n"), GetLastError());
  81. return STATE_UNKNOWN;
  82. }
  83. for (index = 0; index < wtscount; index++) {
  84. LPTSTR username;
  85. DWORD size;
  86. int len;
  87. if (!WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE,
  88. wtsinfo[index].SessionId, WTSUserName, &username, &size))
  89. continue;
  90. len = lstrlen(username);
  91. WTSFreeMemory(username);
  92. if (len == 0)
  93. continue;
  94. if (wtsinfo[index].State == WTSActive ||
  95. wtsinfo[index].State == WTSDisconnected)
  96. users++;
  97. }
  98. WTSFreeMemory(wtsinfo);
  99. #elif HAVE_UTMPX_H
  100. /* get currently logged users from utmpx */
  101. setutxent ();
  102. while ((putmpx = getutxent ()) != NULL)
  103. if (putmpx->ut_type == USER_PROCESS)
  104. users++;
  105. endutxent ();
  106. #else
  107. /* run the command */
  108. child_process = spopen (WHO_COMMAND);
  109. if (child_process == NULL) {
  110. printf (_("Could not open pipe: %s\n"), WHO_COMMAND);
  111. return STATE_UNKNOWN;
  112. }
  113. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  114. if (child_stderr == NULL)
  115. printf (_("Could not open stderr for %s\n"), WHO_COMMAND);
  116. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  117. /* increment 'users' on all lines except total user count */
  118. if (input_buffer[0] != '#') {
  119. users++;
  120. continue;
  121. }
  122. /* get total logged in users */
  123. if (sscanf (input_buffer, _("# users=%d"), &users) == 1)
  124. break;
  125. }
  126. /* check STDERR */
  127. if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
  128. result = possibly_set (result, STATE_UNKNOWN);
  129. (void) fclose (child_stderr);
  130. /* close the pipe */
  131. if (spclose (child_process))
  132. result = possibly_set (result, STATE_UNKNOWN);
  133. #endif
  134. /* check the user count against warning and critical thresholds */
  135. if (users > cusers)
  136. result = STATE_CRITICAL;
  137. else if (users > wusers)
  138. result = STATE_WARNING;
  139. else if (users >= 0)
  140. result = STATE_OK;
  141. if (result == STATE_UNKNOWN)
  142. printf ("%s\n", _("Unable to read output"));
  143. else {
  144. xasprintf (&perf, "%s", perfdata ("users", users, "",
  145. TRUE, wusers,
  146. TRUE, cusers,
  147. TRUE, 0,
  148. FALSE, 0));
  149. printf (_("USERS %s - %d users currently logged in |%s\n"), state_text (result),
  150. users, perf);
  151. }
  152. return result;
  153. }
  154. /* process command-line arguments */
  155. int
  156. process_arguments (int argc, char **argv)
  157. {
  158. int c;
  159. int option = 0;
  160. static struct option longopts[] = {
  161. {"critical", required_argument, 0, 'c'},
  162. {"warning", required_argument, 0, 'w'},
  163. {"version", no_argument, 0, 'V'},
  164. {"help", no_argument, 0, 'h'},
  165. {0, 0, 0, 0}
  166. };
  167. if (argc < 2)
  168. usage ("\n");
  169. while (1) {
  170. c = getopt_long (argc, argv, "+hVvc:w:", longopts, &option);
  171. if (c == -1 || c == EOF || c == 1)
  172. break;
  173. switch (c) {
  174. case '?': /* print short usage statement if args not parsable */
  175. usage5 ();
  176. case 'h': /* help */
  177. print_help ();
  178. exit (STATE_OK);
  179. case 'V': /* version */
  180. print_revision (progname, NP_VERSION);
  181. exit (STATE_OK);
  182. case 'c': /* critical */
  183. if (!is_intnonneg (optarg))
  184. usage4 (_("Critical threshold must be a positive integer"));
  185. else
  186. cusers = atoi (optarg);
  187. break;
  188. case 'w': /* warning */
  189. if (!is_intnonneg (optarg))
  190. usage4 (_("Warning threshold must be a positive integer"));
  191. else
  192. wusers = atoi (optarg);
  193. break;
  194. }
  195. }
  196. c = optind;
  197. if (wusers == -1 && argc > c) {
  198. if (is_intnonneg (argv[c]) == FALSE)
  199. usage4 (_("Warning threshold must be a positive integer"));
  200. else
  201. wusers = atoi (argv[c++]);
  202. }
  203. if (cusers == -1 && argc > c) {
  204. if (is_intnonneg (argv[c]) == FALSE)
  205. usage4 (_("Warning threshold must be a positive integer"));
  206. else
  207. cusers = atoi (argv[c]);
  208. }
  209. return OK;
  210. }
  211. void
  212. print_help (void)
  213. {
  214. print_revision (progname, NP_VERSION);
  215. printf ("Copyright (c) 1999 Ethan Galstad\n");
  216. printf (COPYRIGHT, copyright, email);
  217. printf ("%s\n", _("This plugin checks the number of users currently logged in on the local"));
  218. printf ("%s\n", _("system and generates an error if the number exceeds the thresholds specified."));
  219. printf ("\n\n");
  220. print_usage ();
  221. printf (UT_HELP_VRSN);
  222. printf (UT_EXTRA_OPTS);
  223. printf (" %s\n", "-w, --warning=INTEGER");
  224. printf (" %s\n", _("Set WARNING status if more than INTEGER users are logged in"));
  225. printf (" %s\n", "-c, --critical=INTEGER");
  226. printf (" %s\n", _("Set CRITICAL status if more than INTEGER users are logged in"));
  227. printf (UT_SUPPORT);
  228. }
  229. void
  230. print_usage (void)
  231. {
  232. printf ("%s\n", _("Usage:"));
  233. printf ("%s -w <users> -c <users>\n", progname);
  234. }