RDP connections counter

Post support questions that relate to the Windows 2003/2000/XP operating systems.

Moderators: Developers, Moderators

Post Reply
ni5ni6
Posts: 9
Joined: Tue Mar 25, 2008 8:07 am

RDP connections counter

Post by ni5ni6 »

Hey cacti ppl,

I woud like to count connected RDP sessions to windows boxes. Therfore, I found similar perl script somewhere on this forum, did some modifications and got this at the end:

Code: Select all

$in_hostname = $ARGV[0] if defined $ARGV[0]; 
$in_version = $ARGV[1] if defined $ARGV[1]; 
$in_community = $ARGV[2] if defined $ARGV[2]; 
$in_port = $ARGV[3] if defined $ARGV[3]; 
$in_timeout = $ARGV[4] if defined $ARGV[4]; 

if ($in_version eq "2") 
{ 
$in_version = "2c"; 
} 

my $_cmd = "snmpnetstat -v $in_version -c $in_community -t $in_timeout -Cn -Cp tcp $in_hostname:$in_port | find \".3389\"";

# usage notes 
if ( 
( ! defined $in_hostname ) || 
( ! defined $in_version ) || 
( ! defined $in_community ) || 
( ! defined $in_port ) || 
( ! defined $in_timeout ) 
) {
print "usage:\n\n 
$0 <host> <snmp version> <snmp community> <snmp port> <snmp timeout>\n\n"; 
exit; 
}

my @_output = `$_cmd`; 

my $_estab = 0; 

foreach ( @_output ) { 
#print $_; 
$_estab++ if /ESTABLISHED/; 
} 

print "established:$_estab"; 
So, as you can see, I tried to read all TCP connections to the box and then filter them by the port number. RDP uses port 3389 so therefore script works really fine and I got the actual number that I need.

BUT, this script causes my poller to hang for 2-3 times during the day. which you can see at this post:
http://forums.cacti.net/viewtopic.php?t=26776

I am not any kind of perl expert, so if anyone know what's wrong with it, or has any other solution to track RDP connections, please let me know!

Cheers,
Nikola
User avatar
BSOD2600
Cacti Moderator
Posts: 12171
Joined: Sat May 08, 2004 12:44 pm
Location: USA

Post by BSOD2600 »

Well something in the script is obviously hanging up... Ideally, you should find out where in the code thats happening. You might consider throwing in some additional logic that will kill the script if it runs for X seconds. You'll have to look on google for some examples.

Otherwise, since the script runs fine most of the time, not much else you can do.
Fjugy
Posts: 5
Joined: Fri Feb 27, 2009 12:29 pm

Post by Fjugy »

Hi, not sure if you still need it but...

here is some corrected script

Code: Select all

$in_hostname = $ARGV[0] if defined $ARGV[0];
$in_version = $ARGV[1] if defined $ARGV[1];
$in_community = $ARGV[2] if defined $ARGV[2];
$in_port = $ARGV[3] if defined $ARGV[3];
$in_timeout = $ARGV[4] if defined $ARGV[4];

if ($in_version eq "2")
{
$in_version = "2c";
}

my $_cmd = "snmpnetstat -v $in_version -c $in_community -t $in_timeout -Cn -Cp tcp $in_hostname | grep \".$in_port\"";

# usage notes
if (
( ! defined $in_hostname ) ||
( ! defined $in_version ) ||
( ! defined $in_community ) ||
( ! defined $in_port ) ||
( ! defined $in_timeout )
) {
print "usage:\n\n
$0 <host> <snmp version> <snmp community> <snmp port> <snmp timeout>\n\n";
exit;
}

my @_output = `$_cmd`;

my $_estab = 0;

foreach ( @_output ) {
#print $_;
$_estab++ if /ESTABLISHED/;
}

print "established:$_estab";
yaxzone
Posts: 1
Joined: Fri Aug 28, 2015 6:02 pm

Re: RDP connections counter

Post by yaxzone »

I know this is old but I just stumbled on this post, so for anyone wanting to do the easiest way, user powershell:

Here is my quick function:

Code: Select all

# list of computer names
# this can be grabbed directly from AD
$tss = "ts1","ts2","ts3","ts4","ts5","ts6","ts7","ts8"

function Get-TSUsers {

	foreach ($ts in $tss) {
		$curTS = $ts.ToUpper()
		$UserSelect = query user /server:$ts
		$UserCount = ($UserSelect | findstr /v "USERNAME" | measure).Count
		$UserCountActive = ($UserSelect | findstr /v "USERNAME" | findstr 'Active' | measure).Count
		$UserCountDisc = ($UserSelect | findstr /v "USERNAME" | findstr 'Disc' | measure).Count
		"`n$curTS with Number of users: $UserCount - Active Sessions: $UserCountActive - Disconnect State: $UserCountDisc`n------------------------------------------------------------------------------------"
		$UserSelect 
	}

}

Enjoy!
User avatar
BSOD2600
Cacti Moderator
Posts: 12171
Joined: Sat May 08, 2004 12:44 pm
Location: USA

Re: RDP connections counter

Post by BSOD2600 »

yay for PS! however, I'd suggest a slight change so that the function accepts the computer name so it could be passed from Cacti... and you're going to need to work on the formatting of the results returned be correctly parsed by the poller.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest