Home Directory Plugins Remote Access Count number of terminal server sessions

Search Exchange

Search All Sites

Nagios Live Webinars

Let our experts show you how Nagios can help your organization.

Contact Us

Phone: 1-888-NAGIOS-1
Email: sales@nagios.com

Login

Remember Me

Directory Tree

Count number of terminal server sessions

Rating
6 votes
Favoured:
0
Current Version
0.4
Last Release Date
2012-09-25
Compatible With
  • Nagios 1.x
  • Nagios 2.x
  • Nagios 3.x
Hits
129689
Files:
FileDescription
check_user_count.batThis version returns the perfdata as well as the users currently signed on.
Nagios CSP

Meet The New Nagios Core Services Platform

Built on over 25 years of monitoring experience, the Nagios Core Services Platform provides insightful monitoring dashboards, time-saving monitoring wizards, and unmatched ease of use. Use it for free indefinitely.

Monitoring Made Magically Better

  • Nagios Core on Overdrive
  • Powerful Monitoring Dashboards
  • Time-Saving Configuration Wizards
  • Open Source Powered Monitoring On Steroids
  • And So Much More!
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.
@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
Reviews (6)
byrkutsch, November 17, 2015
Edit the Line for Performance Data:
:end
ECHO %MS% Number of active sessions = %COUNT% ^| 'number'=%COUNT%
EXIT /b %EX%
byejo1974, May 8, 2013
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 briandent@redshift.com
REM - Original Version
REM -------------------------------------------
REM - Version 0.5 - By julienchpt@gmail.com
REM - ADD CRITICAL AND WARNING STATUS
REM -------------------------------------------
REM - Version 0.6 - By roberto.mereu@gmail.com
REM - ADD USERNAME LOGGED IN
REM -------------------------------------------
REM - Version 0.7 - By ejo1974@yahoo.com
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 briandent@redshift.com
REM - Original Version
REM -------------------------------------------
REM - Version 0.5 - By julienchpt@gmail.com
REM - ADD CRITICAL AND WARNING STATUS
REM -------------------------------------------
REM - Version 0.6 - By roberto.mereu@gmail.com
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 briandent@redshift.com
REM - Original Version
REM -------------------------------------------
REM - Version 0.5 - By julienchpt@gmail.com
REM - ADD CRITICAL AND WARNING STATUS
REM -------------------------------------------
REM - Version 0.6 - By roberto.mereu@gmail.com
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%
byNapsty, January 23, 2012
You can simply add perfdata to the output:

ECHO Number of active sessions = %COUNT%^|usersconnected=%COUNT%
byjulienchpt, January 17, 2012
2 of 2 people found this review helpful
You will find below this improving version with Critical and Warning status:

@ECHO OFF
REM -------------------------------------------
REM - Version 0.4 - By briandent@redshift.com
REM - Original Version
REM -------------------------------------------
REM - Version 0.5 - By julienchpt@gmail.com
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