#!/usr/bin/perl
# 
# run lmstat and check output for errors 

$ignore_pattern = 'ARCGIS';   # ignore daemons that match this

$cmd = "/opt/SUNWste/bin/lmstat";
if ( ! open( pH, "$cmd |" ) ) { 
    print "can't run $cmd: $!\n";
    exit -1;   # nagios unknown code
    }

undef @crit_errors;
#chomp( $daemon_host = `/usr/bin/hostname` );
$nservers = 0;
$ndaemons = 0;
$nignored = 0;

while ( <pH> ) { 
    if ( m/Copyright|Flexible License Manager status|License server status|License file/i ) {  
	next;
	}
    elsif ( m/^([\w\d\.\-]+):\s*license server +(\S+)\s+(\S.*)/i ) { 
	$server = $1;
	$status = $2;
	$version = $3;
	if ( $status ne "UP" ) { 
	    push @crit_errors, "$server $status";
	    next;
	    }
	$nservers++
	}
    elsif ( m/Vendor daemon status \(on (\S+)\):/i ) { 
	$daemon_host = $1;
	}
    elsif ( m/^\s*([^:]+):\s*(\S+)\s*(\s.*)/i ) { 
	$daemon = $1;
	$status = $2;
	$version = $3;
	if ( $status ne "UP" ) { 
	    if ( $daemon =~ m/$ignore_pattern/io ) { 
		$nignored++;
		next;
		}
	    push @crit_errors, "$daemon $status $version";
	    next;
	    }
	$ndaemons++;
	}
    }
close pH;

if ( $nservers <= 0 ) { 
    push @crit_errors, "no license servers found in lmstat output";
    }
if ( $ndaemons <= 0 ) { 
    push @crit_errors, "no daemons found in lmstat output";
    }

$rc = 0;
$" = ", ";
if ( $#crit_errors >= 0 ) { 
    print "CRITICAL - @crit_errors";
    $rc = 2;
    }
else { 
    print "OK - $nservers servers up, $ndaemons daemons up, $nignored ignored";
    $rc = 0;
    }
print "\n";
exit $rc;


