#!/usr/bin/perl 
# check_users_logged.pl - Nagios NRPE plugin to check logged users
#
# This program is distributed under the Artistic License.
#
# http://www.opensource.org/licenses/artistic-license.php
#
#Copyright 2008, Moshe Sharon, Centerity LTD. (http://www.centerity.com)

#!/usr/bin/perl -w


$warning = $ARGV[0];
$critical = $ARGV[1];

$WHOCOMMAND = "/usr/bin/who";

open ( WHO, "$WHOCOMMAND |" ) or die ("Can't open who command");

$count = 0;
while ( $line = <WHO> ) {

        chomp $line;
        ( $logged_users ) = (split (/\s+/, $line))[0];
        push (@logged, $logged_users);
        $count++;

}


if ( $critical && $critical < $count ) {
        $STATUS = "CRITICAL. ";
        $status = 2;
}
elsif ( $warning && $warning < $count ) {
        $STATUS = "WARNING. ";
        $status = 1;
}
else {
        $STATUS = "OK. ";
        $status = 0;
}


print $STATUS;
print "Logged Users [$count]: ";
print join (", ", @logged);
print "\n";
exit($status);