4
0

check_users.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. char *warning_range = NULL;
  52. char *critical_range = NULL;
  53. thresholds *thlds = NULL;
  54. int
  55. main (int argc, char **argv)
  56. {
  57. int users = -1;
  58. int result = STATE_UNKNOWN;
  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. /* Parse extra opts if any */
  72. argv = np_extra_opts (&argc, argv, progname);
  73. if (process_arguments (argc, argv) == ERROR)
  74. usage4 (_("Could not parse arguments"));
  75. users = 0;
  76. #if HAVE_WTSAPI32_H
  77. if (!WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE,
  78. 0, 1, &wtsinfo, &wtscount)) {
  79. printf(_("Could not enumerate RD sessions: %d\n"), GetLastError());
  80. return STATE_UNKNOWN;
  81. }
  82. for (index = 0; index < wtscount; index++) {
  83. LPTSTR username;
  84. DWORD size;
  85. int len;
  86. if (!WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE,
  87. wtsinfo[index].SessionId, WTSUserName, &username, &size))
  88. continue;
  89. len = lstrlen(username);
  90. WTSFreeMemory(username);
  91. if (len == 0)
  92. continue;
  93. if (wtsinfo[index].State == WTSActive ||
  94. wtsinfo[index].State == WTSDisconnected)
  95. users++;
  96. }
  97. WTSFreeMemory(wtsinfo);
  98. #elif HAVE_UTMPX_H
  99. /* get currently logged users from utmpx */
  100. setutxent ();
  101. while ((putmpx = getutxent ()) != NULL)
  102. if (putmpx->ut_type == USER_PROCESS)
  103. users++;
  104. endutxent ();
  105. #else
  106. /* run the command */
  107. child_process = spopen (WHO_COMMAND);
  108. if (child_process == NULL) {
  109. printf (_("Could not open pipe: %s\n"), WHO_COMMAND);
  110. return STATE_UNKNOWN;
  111. }
  112. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  113. if (child_stderr == NULL)
  114. printf (_("Could not open stderr for %s\n"), WHO_COMMAND);
  115. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  116. /* increment 'users' on all lines except total user count */
  117. if (input_buffer[0] != '#') {
  118. users++;
  119. continue;
  120. }
  121. /* get total logged in users */
  122. if (sscanf (input_buffer, _("# users=%d"), &users) == 1)
  123. break;
  124. }
  125. /* check STDERR */
  126. if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
  127. result = possibly_set (result, STATE_UNKNOWN);
  128. (void) fclose (child_stderr);
  129. /* close the pipe */
  130. if (spclose (child_process))
  131. result = possibly_set (result, STATE_UNKNOWN);
  132. #endif
  133. /* check the user count against warning and critical thresholds */
  134. result = get_status((double)users, thlds);
  135. if (result == STATE_UNKNOWN)
  136. printf ("%s\n", _("Unable to read output"));
  137. else {
  138. printf (_("USERS %s - %d users currently logged in |%s\n"),
  139. state_text(result), users,
  140. sperfdata_int("users", users, "", warning_range,
  141. critical_range, TRUE, 0, FALSE, 0));
  142. }
  143. return result;
  144. }
  145. /* process command-line arguments */
  146. int
  147. process_arguments (int argc, char **argv)
  148. {
  149. int c;
  150. int option = 0;
  151. static struct option longopts[] = {
  152. {"critical", required_argument, 0, 'c'},
  153. {"warning", required_argument, 0, 'w'},
  154. {"version", no_argument, 0, 'V'},
  155. {"help", no_argument, 0, 'h'},
  156. {0, 0, 0, 0}
  157. };
  158. if (argc < 2)
  159. usage ("\n");
  160. while (1) {
  161. c = getopt_long (argc, argv, "+hVvc:w:", longopts, &option);
  162. if (c == -1 || c == EOF || c == 1)
  163. break;
  164. switch (c) {
  165. case '?': /* print short usage statement if args not parsable */
  166. usage5 ();
  167. case 'h': /* help */
  168. print_help ();
  169. exit (STATE_OK);
  170. case 'V': /* version */
  171. print_revision (progname, NP_VERSION);
  172. exit (STATE_OK);
  173. case 'c': /* critical */
  174. critical_range = optarg;
  175. break;
  176. case 'w': /* warning */
  177. warning_range = optarg;
  178. break;
  179. }
  180. }
  181. c = optind;
  182. if (warning_range == NULL && argc > c)
  183. warning_range = argv[c++];
  184. if (critical_range == NULL && argc > c)
  185. critical_range = argv[c++];
  186. /* this will abort in case of invalid ranges */
  187. set_thresholds (&thlds, warning_range, critical_range);
  188. if (!thlds->warning || thlds->warning->end < 0)
  189. usage4 (_("Warning threshold must be zero or greater"));
  190. if (!thlds->critical || thlds->critical->end < 0)
  191. usage4 (_("Critical threshold must be zero or greater"));
  192. return OK;
  193. }
  194. void
  195. print_help (void)
  196. {
  197. print_revision (progname, NP_VERSION);
  198. printf ("Copyright (c) 1999 Ethan Galstad\n");
  199. printf (COPYRIGHT, copyright, email);
  200. printf ("%s\n", _("This plugin checks the number of users currently logged in on the local"));
  201. printf ("%s\n", _("system and generates an error if the number exceeds the thresholds specified."));
  202. printf ("\n\n");
  203. print_usage ();
  204. printf (UT_HELP_VRSN);
  205. printf (UT_EXTRA_OPTS);
  206. printf (" %s\n", "-w, --warning=INTEGER");
  207. printf (" %s\n", _("Set WARNING status if more than INTEGER users are logged in"));
  208. printf (" %s\n", "-c, --critical=INTEGER");
  209. printf (" %s\n", _("Set CRITICAL status if more than INTEGER users are logged in"));
  210. printf (UT_SUPPORT);
  211. }
  212. void
  213. print_usage (void)
  214. {
  215. printf ("%s\n", _("Usage:"));
  216. printf ("%s -w <users> -c <users>\n", progname);
  217. }