########################################################################
# Name: termusers.pl
# Version: 1.0
# Author: Brandon Kitchen
# Purpose: termusers.pl is an external script that is run by the Big
# Brother client on a Windows Terminal Servers. It counts
# the number of users and determines how many are active
# and disconnected users.
#
# Changelog:
# 10-13-2006: Updated to work with Xymon (GMJ)
# Removed laard-grapher.cgi links
########################################################################
use strict;
use Sys::Hostname;
# Constants
use constant RED_ALERT => 45;
use constant YELLOW_ALERT => 25;
use constant DEBUG => 0;
# Globals
use vars qw($total_users);
use vars qw($active_users $disc_users);
use vars qw($computer $service $line $userattr $bblogs);
use vars qw(@userinfo @query);
# This is the name of the column in the BB display.
$service = "termusers";
$bblogs = "c:\\Program Files\\BBWin\\tmp";
# Get the local system name.
$computer = hostname();
# Setup where output goes.
if ( DEBUG ) {
open(STATUS, ">$service") || die "$!: file $service";
} else {
open(STATUS, ">$bblogs\\$service") || die "$!: file $bblogs\\$service";
}
# Get the list of terminal users.
@query = `query user`;
# Setup the counters.
$total_users = 0;
$active_users = 0;
$disc_users = 0;
# Go through the list of users looking for different types (ie group1 vs group2)
for $line (@query) {
# Discard the header line.
next if $line =~ /USERNAME|^\s*$/;
$total_users++;
print "Total: $total_users\n" if DEBUG;
# Get rid of whitespace at the start of the line. Also fix
# the special case user (console) that starts with <.
if ( $line =~ /^\s+/ ) { $line =~ s/^\s+//g; }
if ( $line =~ /^\>/ ) { $line =~ s/^\>//g; }
# Split the line into tokens and start counting special users.
@userinfo = split /\s+/, $line;
if ($line =~ /Active/) {
$active_users++;
} elsif ($line =~ /Disc/) {
$disc_users++;
}
}
# Check the total_users count and alert as appropriate. Also alert red if
# any users in the Revoked OU have sessions on the system.
if ($total_users <= YELLOW_ALERT) {
print STATUS "green\n";
} elsif ($total_users <= RED_ALERT) {
print STATUS "yellow\n";
} else {
print STATUS "red\n";
}
print STATUS qq/There are $total_users terminal users on $computer.\n
Of the $total_users total users:\n
\t Active Sessions:\t$active_users
\tDisconnected Sessions:\t$disc_users
/;
# Send the list of users.
print STATUS "\n\n@query\n";
# All done now.
close(STATUS);