:: Version: 0.3 :: Date: Nov 05, 2021 :: Author: ivan.stetka@live.com :: Summary: This is a Nagios NCPA plugin to check RDP sessions (the number of users, the user name, and the computer name from which the user is connected). :: :: On windows host side copy plugin to folder 'C:\Program Files (x86)\Nagios\NCPA\plugins\' :: :: Nagios example: :: ./check_ncpa.py -H '' -t '' -M 'plugins/check_RDP_session.bat' -q "args=1 2 EN" @ECHO OFF SETLOCAL ENABLEDELAYEDEXPANSION SET exit=3 SET state=UNKNOWN: SET /a countu=0 SET /a countp=0 SET user= SET pc= SET plural= SET lang= IF [%1]==[] (ECHO CRITICAL: Missing warning threshold. && GOTO HELP) ELSE (SET warn=%1) IF [%1]==[/?] (GOTO HELP) ELSE (SET warn=%1) IF [%1]==[-h] (GOTO HELP) ELSE (SET warn=%1) IF [%2]==[] (ECHO CRITICAL: Missing critical threshold. && GOTO HELP) ELSE (SET crit=%2) IF [%3]==[] (ECHO CRITICAL: Missing language of netstat command. && GOTO HELP) IF [%3]==[EN] (SET lang="ESTA") IF [%3]==[CZ] (SET lang="NAV") IF [%4]==[] (SET par_net=^-a) ELSE (SET par_net=^-an) FOR /f "TOKENS=3" %%P IN ('netstat %par_net% ^|FIND %lang% ^|FIND ":3389"') DO ( SET /a countp+=1 IF !countp! == 1 (SET pc=%%P) ELSE ( SET pc=!pc!, %%P)) FOR /f "TOKENS=2" %%U IN ('query session ^|FIND "rdp-tcp#"') DO ( SET /a countu+=1 IF !countu! == 1 (SET user=%%U) ELSE (SET user=!user!, %%U)) IF !countu! GTR !countp! (ECHO CRITICAL: count of users is great then count of connected PCs, maybe problem with language of netstat command. && GOTO HELP) IF !countu! GEQ !crit! (SET exit=2 && SET state=CRITICAL: && GOTO FINISH) IF !countu! GEQ !warn! (SET exit=1 && SET state=WARNING: && GOTO FINISH) SET exit=0 && SET state=OK: :FINISH IF !countu! GTR 1 (SET plural=s) IF !countu! == 0 (ECHO %state% No RDP sessions. ^| Users=%countu%) ELSE (ECHO %state% logged %countu% user!plural! over RDP: !user!. Connected %countp% PC!plural!: !pc!. ^| Users=%countu%;%warn%;%crit%) EXIT /b %exit% :HELP ECHO H E L P: ECHO Nagios NCPA plugin to check RDP sessions (the number of users, the user name, and the computer name from which the user is connected). ECHO. ECHO Usage: check_RDP_sessions.bat $ARG1$ $ARG2$ $ARG3$ n ECHO Examples: check_RDP_sessions.bat 1 2 EN n ECHO Examples: check_RDP_sessions.bat 1 2 EN ECHO $ARG1$ # warning threshold of user count ECHO $ARG2$ # critical threshold of user count ECHO $ARG3$ # output language of netstat command [EN or CZ] ECHO n # optional parameter, recommended on a computer where DNS is not available, ECHO # displays addresses and port numbers in numerical form EXIT /b 2