File check_bs_crit.pl of Package monitoring-plugins-buildservice

#!/usr/bin/perl -w

use strict;
use warnings;
use Getopt::Long;

my $logdir     = "";
my $filespec = "";
my $warning   = "";
my $critical  = "";

GetOptions(
    "d|directory=s" => \$logdir,
    "f|filespec=s" => \$filespec,
    "w|warning=s"   => \$warning,
    "c|critical=s"  => \$critical,
);

my $output  = "";
my $line    = "";

my %status = ( 'OK'       => 0, 
               'WARNING'  => 1, 
               'CRITICAL' => 2, 
               'UNKNOWN'  => 3 
           );
my $exit_status = $status{OK}; 
my $found = 0;
my $foundwarn = 0;
my $foundcrit = 0;

usage("missing data") if ( !$logdir || !$filespec );
usage("missing warn or crit") if ( !$warning || !$critical );
usage() if ( $warning > $critical );

my $message;

my @files = glob ("$logdir/$filespec");
for my $logfile (@files) {
    next unless -e $logfile;
    $found++;
    open(LOG, $logfile);
    my @loglines = <LOG>;
    close(LOG);
    my $num_lines = $#loglines + 1;
    if ($num_lines >= $critical) {
	$foundcrit++;
	$message .= "CRITIAL: $logfile has $num_lines lines\n";
    } elsif ($num_lines >= $warning) {
	$foundwarn++;
	$message .= "WARNING: $logfile has $num_lines lines\n";
    }
}

if ($foundcrit) {
        $exit_status = $status{CRITICAL};
        $output .= "CRITICAL";
} elsif ($foundwarn) {
	$exit_status = $status{WARNING};
        $output .= "WARNING";
} else {
        $output .= "OK";
 }

$output .= sprintf( " | num_files=%d num_warn=%d num_crit=%d",
	$found, $foundwarn, $foundcrit);

print "$output\n";
print "\n$message" if $message;
exit( $exit_status );


sub usage {
    my ($mesg) = @_;

    print "PROBLEM: $mesg\n\n" if $mesg;

    print <<EOU;

    Usage: $0 -d <directory> -f <filespec> -w <warn> -c <critical>

    -d  --directory dir to search for files
    -f  --filespec  spec to search for files
    -w, --warning STRING
        max lines per file for warning level
    -c, --critical STRING
        max lines per file for critical level

EOU

    exit $status{UNKNOWN};
}