Build precise queries to find exactly what you need
Press ESC to close
Nagios World Conference 2026:Â Sept. 14-17Â in St. Paul, MN |Â Learn More
Your review has been submitted and is pending approval.
Simple bat file to query the number of active sessions on a Windows Terminal Server. CALLING SEQUENCE: command[nrpe_nt_check_users]=c:nrpe_ntpluginscheck_user_count.bat Counts the number of lines returned containing 'rdp-tcp#', which are active sessions. Disconnected sessions are not counted as they are not active.
Current Version
0.4
Last Release Date
2012-09-25
Owner
Brian Dent
Download URL
https://exchange.nagios.org/wp-content/uploads/project-files/2011/05/check_user_count.bat
Compatible With
This version returns the perfdata as well as the users currently signed on.
@ECHO OFF REM CALLING SEQUENCE: command[nrpe_nt_check_users]=c:nrpe_ntpluginscheck_user_count.bat warninglevel criticallevel
SETLOCAL ENABLEDELAYEDEXPANSION SET /a COUNT=0 SET /a WARNING=%1 SET /a CRITICAL=%2 SET CURRENTUSERS=
REM PULL THE NAMES FROM THE QUERY AND APPEND THEM TO THE LIST FOR /F "TOKENS=1,2,3 DELIMS= " %%I IN ('query session ^| find "rdp-tcp#"') DO ( REM ECHO %%I %%J %%K SET /a COUNT+=1 IF !COUNT! == 1 ( SET CURRENTUSERS=%%J ) ELSE ( SET CURRENTUSERS=!CURRENTUSERS!,%%J ) )
REM INTOK = 0 REM INTWARNING = 1 REM INTCRITICAL = 2 REM INTERROR = 3 REM INTUNKNOWN = 3
IF %COUNT% GTR %CRITICAL% ( ECHO Critical! Number of active sessions = %COUNT%, Critical level is: %CRITICAL%^|'Active Users='%COUNT%;%WARNING%;%CRITICAL% EXIT 2 ) IF %COUNT% GTR %WARNING% ( ECHO Warning! Number of active sessions = %COUNT%, Warning level is: %WARNING%^|'Active Users='%COUNT%;%WARNING%;%CRITICAL% EXIT 1 )
ECHO Active sessions: %COUNT%, Warning: %WARNING%, Critical: %CRITICAL%, Users: %CURRENTUSERS%^|'Active Users='%COUNT%;%WARNING%;%CRITICAL% EXIT 0
Edit the Line for Performance Data: :end ECHO %MS% Number of active sessions = %COUNT% ^| 'number'=%COUNT% EXIT /b %EX%
Thanks for this code, it is very helpful for monitoring our RDP farm! In the code above I found 2 bugs: Introduced in version 0.6. In this version the checks for WARNING and CRITICAL were turned around. As a result the program is creating a warning when the warning level is reached and exits instead of checking if the critical level is reached. @Napsty: Your addon displays the count again instead of the usernames of the connected users, correct command: ECHO Number of active sessions = %COUNT%^|usersconnected=!USER! New code (based on version 0.6 and added unnumbered addons from lumezit and Napsty) and some personal changes: @ECHO OFF setlocal EnableDelayedExpansion REM ------------------------------------------- REM - Version 0.4 - By [email protected] REM - Original Version REM ------------------------------------------- REM - Version 0.5 - By [email protected] REM - ADD CRITICAL AND WARNING STATUS REM ------------------------------------------- REM - Version 0.6 - By [email protected] REM - ADD USERNAME LOGGED IN REM ------------------------------------------- REM - Version 0.7 - By [email protected] REM - Bugfix REM - Formatting changes REM ------------------------------------------- REM USAGE: check_user_count warn crit REM warn: Number of session before warning REM crit: Number of session before Critical REM Example: check_user_count 2 4 REM ------------------------------------------- REM CALLING SEQUENCE: REM command[nrpe_nt_check_users]=c:nrpe_ntpluginscheck_user_count.bat $ARG1$ $ARG2$ REM ------------------------------------------- set EX=0 set MS=OK SET /a COUNT=0 SET USER= FOR /f "TOKENS=1" %%i IN ('query session ^|find "rdp-tcp#"') DO SET /a COUNT+=1 FOR /f "TOKENS=2" %%G IN ('query session ^|find "rdp-tcp#"') DO ( call :subroutine %%G ) REM - CRITICAL (COUNT => $2) if %COUNT% GEQ %2 ( set EX=2 && set MS=CRITICAL && goto end ) REM - WARNING (COUNT => $1) if %COUNT% GEQ %1 ( set EX=1 && set MS=WARNING && goto end ) REM - NOT CRITICAL / WARNING set EX=0 set MS=OK goto end :subroutine SET USER=%USER% %1 GOTO :eof :end ECHO %MS% - Number of active sessions = %COUNT% ^| Logged Users = !USER! EXIT /b %EX%
The plugin does not return status data / perf data when everything is ok. it gets to :subroutine and then jumps to eof. This is what works for me (just added goto end before :subroutine) : @ECHO OFF setlocal EnableDelayedExpansion REM ------------------------------------------- REM - Version 0.4 - By [email protected] REM - Original Version REM ------------------------------------------- REM - Version 0.5 - By [email protected] REM - ADD CRITICAL AND WARNING STATUS REM ------------------------------------------- REM - Version 0.6 - By [email protected] REM - ADD USERNAME LOGGED IN REM ------------------------------------------- REM USAGE: check_user_count warn crit REM warn: Number of session before warning REM crit: Number of session before Critical REM Example: check_user_count 2 4 REM ------------------------------------------- REM CALLING SEQUENCE: REM command[nrpe_nt_check_users]=c:nrpe_ntpluginscheck_user_count.bat $ARG1$ $ARG2$ REM ------------------------------------------- set EX=0 set MS=OK SET /a COUNT=0 SET USER= FOR /f "TOKENS=1" %%i IN ('query session ^|find "rdp-tcp#"') DO SET /a COUNT+=1 FOR /f "TOKENS=2" %%G IN ('query session ^|find "rdp-tcp#"') DO ( call :subroutine %%G ) REM - WARNING (COUNT => $1) if %COUNT% GEQ %1 ( set EX=1 && set MS=WARNING && goto end ) REM - CRITICAL (COUNT => $2) if %COUNT% GEQ %2 ( set EX=2 && set MS=CRITICAL && goto end ) goto end :subroutine SET USER=%USER% %1 GOTO :eof :end ECHO %MS% Number of active sessions = %COUNT% Logged Users = !USER! ^| usersconnected=%COUNT% EXIT /b %EX%
I've just added the output for the username logged in. @ECHO OFF setlocal EnableDelayedExpansion REM ------------------------------------------- REM - Version 0.4 - By [email protected] REM - Original Version REM ------------------------------------------- REM - Version 0.5 - By [email protected] REM - ADD CRITICAL AND WARNING STATUS REM ------------------------------------------- REM - Version 0.6 - By [email protected] REM - ADD USERNAME LOGGED IN REM ------------------------------------------- REM USAGE: check_user_count warn crit REM warn: Number of session before warning REM crit: Number of session before Critical REM Example: check_user_count 2 4 REM ------------------------------------------- REM CALLING SEQUENCE: REM command[nrpe_nt_check_users]=c:nrpe_ntpluginscheck_user_count.bat $ARG1$ $ARG2$ REM ------------------------------------------- set EX=0 set MS=OK SET /a COUNT=0 SET USER= FOR /f "TOKENS=1" %%i IN ('query session ^|find "rdp-tcp#"') DO SET /a COUNT+=1 FOR /f "TOKENS=2" %%G IN ('query session ^|find "rdp-tcp#"') DO ( call :subroutine %%G ) REM - WARNING (COUNT => $1) if %COUNT% GEQ %1 ( set EX=1 && set MS=WARNING && goto end ) REM - CRITICAL (COUNT => $2) if %COUNT% GEQ %2 ( set EX=2 && set MS=CRITICAL && goto end ) :subroutine SET USER=%USER% %1 GOTO :eof :end ECHO %MS% Number of active sessions = %COUNT% Logged Users = !USER! EXIT /b %EX%
You can simply add perfdata to the output: ECHO Number of active sessions = %COUNT%^|usersconnected=%COUNT%
You will find below this improving version with Critical and Warning status: @ECHO OFF REM ------------------------------------------- REM - Version 0.4 - By [email protected] REM - Original Version REM ------------------------------------------- REM - Version 0.5 - By [email protected] REM - ADD CRITICAL AND WARNING STATUS REM ------------------------------------------- REM USAGE: check_user_count warn crit REM warn: Number of session before warning REM crit: Number of session before Critical REM Example: check_user_count 2 4 REM ------------------------------------------- REM CALLING SEQUENCE: REM command[nrpe_nt_check_users]=c:nrpe_ntpluginscheck_user_count.bat $ARG1$ $ARG2$ REM ------------------------------------------- set EX=0 set MS=OK SET /a COUNT=0 FOR /f "TOKENS=1 DELIMS= " %%G IN ('query session ^|find "rdp-tcp#"') DO SET /a COUNT+=1 REM - CRITICAL (COUNT => $2) if %COUNT% GEQ %2 ( set EX=2 && set MS=CRITICAL && goto end ) REM - WARNING (COUNT => $1) if %COUNT% GEQ %1 ( set EX=1 && set MS=WARNING && goto end ) :end ECHO %MS% Number of active sessions = %COUNT% EXIT /b %EX% Enjoy
You must be logged in to submit a review.
To:
From: