#!/usr/bin/perl 
##!/usr/bin/perl -wT

my $license_file = "/usr/local/flexlm/licenses/license.dat";

#use strict;
use POSIX qw(mktime);
use Getopt::Std;

################

my @monabrs = ( "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", 
    "sep", "oct", "nov", "dec" );
my $verbose = 0;

my( $line, @fields, $day, $monname, $year, $mon, $expires, $now, $rc,
    @warn_errors, @unknown_errors, @crit_errors, %opts, $ok_cnt,
    $linebuf );

getopts( 'f:v', \%opts );
$verbose++ if ( $opts{ v } );
$license_file = $opts{ f } if ( $opts{ f } );

if ( ! open( fH, $license_file ) ) { 
    print "Can't open license file: $!\n";
    exit -1;
    }

$now = time();
$linebuf = '';
$ok_cnt = 0;
while ( <fH> ) { 
    chomp;
    next if ( m/^\s*#|^\s*$/ );

    $linebuf .= $_;
    if ( $linebuf =~ m/\\$/ ) { 
	$linebuf =~ s/\\$//;
	next;
	}
    $line = $linebuf; 
    $linebuf = '';
    $verbose && print "<$line\n";

    @fields = split( /\s+/, $line );
    next if ( $fields[0] eq "SERVER" ); 
    next if ( $fields[0] eq "DAEMON" ); 
    next if ( $fields[0] eq "FEATURESET" ); 
    next if ( $fields[0] eq "PACKAGE" ); 
    next if ( $fields[0] eq "VENDOR" ); 
    next if ( $fields[0] eq "USE_SERVER" ); 

    if ( $fields[0] eq "FEATURE" || $fields[0] eq "INCREMENT" ) { 
	if ( $fields[4] eq "permanent" ) { 
	    # never expires
	    $ok_cnt++;
	    next;
	    }
	elsif ( $fields[4] !~ m/^(\d+)-(\w\w\w)-(\d+)$/ ) { 
	    $verbose && print "** can't parse date from line $.: $line\n"; 
	    push @unknown_errors, "can't parse date from line $.: $line"; 
	    next;
	    }
	$day = $1; $monname = $2; $year = $3;
	if ( $year eq "0000" || $year eq "0" ) {  
	    # never expires
	    $ok_cnt++;
	    next;
	    }
	for ( $mon = 0; $mon < 12 && $monname !~ m/^$monabrs[$mon]$/i; $mon++ )
	    { } 
	if ( $mon > 11 ) { 
	    $verbose && print "** can't parse date from line $.: $line\n"; 
	    push @unknown_errors, "can't parse date from line $.: $line"; 
	    next;
	    }
	if ( $year == 0 && $mon == 0 && $day == 1 ) {  
	    # never expires
	    $ok_cnt++;
	    next;
	    }
	elsif ( $year > 2038 ) {  
	    # essentially never expires
	    $ok_cnt++;
	    next;
	    }
	$year -= 1900 if ( $year > 1900 );
	$expires = mktime( 0, 0, 0, $day, $mon, $year );
	$verbose && printf "%s %s expires %s, %d days left\n", 
	    $fields[1], $fields[2], scalar( localtime( $expires ) ), 
	    ( $expires - $now ) / 86400;
	
	if ( $expires < ( $now - 30 * 86400 ) ) { 
	    # too old, ignore it
	    $verbose && print "$fields[1] $fields[2] expired more than 30 days ago\n"; 
	    next;
	    }
	elsif ( $expires < $now ) { 
	    print "$line\n    has expired \n"; 
	    push @crit_errors, "$fields[1] $fields[2] has expired as of $fields[4]"; 
	    next;
	    }
	elsif ( $expires < ( $now + 7 * 86400 ) ) { 
	    push @crit_errors, "$fields[1] $fields[2] will expire on $fields[4]"; 
	    next;
	    }
	elsif ( $expires < ( $now + 30 * 86400 ) ) { 
	    push @warn_errors, "$fields[1] $fields[2] will expire on $fields[4]"; 
	    next;
	    }
	else { 
	    $ok_cnt++;
	    }
	}

    else { 
	$verbose && print "** can't parse line $.: $line\n"; 
	push @unknown_errors, "can't parse line $.: $line"; 
	next;
	}

    }



$rc = 0;   # nagios ok exit code
$" = ", ";
if ( scalar( @crit_errors ) ) { 
    print "CRITICAL: @crit_errors ";
    $rc = 2;
    }
if ( scalar( @warn_errors ) ) { 
    print "; " if ( $rc != 0 );
    print "Warning: @warn_errors ";
    $rc = 1 if ( $rc == 0 );
    }
if ( scalar( @unknown_errors ) ) { 
    print "; " if ( $rc != 0 );
    print "Unknown: @unknown_errors ";
    $rc = -1 if ( $rc == 0 );
    }
elsif ( $rc == 0 ) { 
    print "$ok_cnt features or increments were ok"; 
    }
print "\n";
exit $rc;   

