# In the NSC.ini file:
# check_lmstat_cag=perl scripts\check_lmstat.pl -c $ARG1$ -w $ARG2$
# check_lmstat_mps=perl scripts\check_lmstat.pl -c $ARG1$ -w $ARG2$ -l mps
# And make sure you have allow_arguments=1 in the NRPE section
#
use Getopt::Long;

my ($cagi, $cagu, $mpsi, $mpsu, $cagp, $mpsp, $warn, $crit, $check);

$crit = 10;
$warn = 20;
$check = "cag";

Getopt::Long::Configure('bundling');
GetOptions(
        "l=s" => \$check, "check"       => \$check,
        "c=i" => \$crit, "critical"       => \$crit,
        "w=i" => \$warn, "warning"     => \$warn
        );


#open (INDATA, "type lmstat.out |");
open (INDATA, "\"C:\\Program Files\\Citrix\\Licensing\\LS\\lmstat\" -c \"C:\\Program Files\\Citrix\\Licensing\\MyFiles\\citrix_startup.lic\" -a |");
while (<INDATA>) {

	$LINE = $_;
	if ("$LINE" =~ /^Users of CAG_SSLVPN_CCU/) {
		@cag = ($LINE =~ /(\d+) licenses/g);
		$cagi = @cag[0];
		$cagu = @cag[1];
		$cagp = round(100-($cagu/$cagi*100));
	}
	if ("$LINE" =~ /^Users of MPS_ADV_CCU/) {
		@mps = ($LINE =~ /(\d+) licenses/g);
		$mpsi = @mps[0];
		$mpsu = @mps[1];
		$mpsp = round(100-($mpsu/$mpsi*100));
	}

}

close INDATA;

#print "$cagi, $cagu = $cagp\n";
#print "$mpsi, $mpsu = $mpsp\n";

if ( $check eq "cag") {
	if ( $cagp <= $crit) {
		print "Critical: $cagp% CAG licenses free|CAG_SSLVPN_CCU=$cagp;$warn;$crit;0";
		exit 2;
	} elsif ($cagp <= $warn) {
		print "Warning: $cagp% CAG licenses free|CAG_SSLVPN_CCU=$cagp;$warn;$crit;0";
		exit 1;
	} else {
		print "OK: $cagp% CAG licenses free|CAG_SSLVPN_CCU=$cagp;$warn;$crit;0";
		exit 0;
	}
} else {
	if ( $mpsp <= $crit) {
		print "Critical: $mpsp% MPS licenses free|MPS_ADV_CCU=$mpsp;$warn;$crit;0";
		exit 2;
	} elsif ($mpsp <= $warn) {
		print "Warning: $mpsp% MPS licenses free|MPS_ADV_CCU=$mpsp;$warn;$crit;0";
		exit 1;
	} else {
		print "OK: $mpsp% MPS licenses free|MPS_ADV_CCU=$mpsp;$warn;$crit;0";
		exit 0;
	}
}


sub round {
    my($number) = shift;
    return int($number + .5);
}

