#!/usr/bin/perl
#Description:
#	Check the command state from /etc/cron.d/backup.
#	If the command line is commented it's disabled.
#
#Author:
#	Fernando Rocha (fernando.rocha@opservices.com.br)
#
#Version:
#	1.0: 20-03-2009	Initial release

use strict;
use Getopt::Long;
use POSIX;
use File::Basename;
use DBI;

# Setting environment
$ENV{"USER"}="opuser";
$ENV{"HOME"}="/home/opuser";

# globlal variables
our $version = "1.1";
our $name = basename($0, ".pl");
our $path = dirname($0);
our $temp_log = "$path/$name.log";
our ($opt_version, $opt_help, $opt_debug , $opt_cmd);

our $bkp_file = "/etc/cron.d/backup";

sub main {
        # Get options
        getOptions();

        logger("----- INICIO DO SCRIPT -----");

	# Read bkp file
	open(FILE,$bkp_file)
	or do {
		print "$bkp_file: $!\n";
		exit (2);
	};
	my @file_content = <FILE>;
	close(FILE); 

	logger("File Content:");
	logger("@file_content");

	my @cmds = grep(/^[^#].+$opt_cmd /, @file_content);

	logger("Results Found:");
	logger("@cmds");

	logger("-------FIM DO SCRIPT--------");

	if (not @cmds){
		print "Critical - $opt_cmd: Disabled\n";
		exit (2);
	}else{
	        print "Ok - $opt_cmd: Enabled\n";
                exit (0);
	}
}
#--------------------------------------------------------------------------

sub getOptions {  #command line options

        Getopt::Long::Configure('bundling');
        GetOptions(
                'v|version'             => \$opt_version,
                'h|help'                => \$opt_help,
                'd|debug=i'             => \$opt_debug,
                'c|command=s'           => \$opt_cmd,

        )or do {
                printUsage();
                exit();
        };

        if ($opt_version) {
                print "$name.pl: $version\n";
                exit();
        }

        if ($opt_help){
                printUsage();
                exit();
        }

        if (!$opt_cmd){
                printUsage();
                exit();
        }


        if (($opt_debug) and (($opt_debug < 1) or ($opt_debug > 2))){
                printUsage();
                exit();
        }

}
#--------------------------------------------------------------------------
sub printUsage {

       print <<EOB

Usage: $0 [OPTION]...
Check the command state from /etc/cron.d/backup.
If the command line is commented it's disabled.

Mandatory Options:
        -c, --command		Command name

Extra Options:

        -h, --help              Display this help and exit
        -v, --version           Output version information and exit

        -d, --debug     1 = Debug
                        2 = Generate log file

EOB

}
#-------------------------------------------------------------------------
sub logger {

        return (0) if (not defined $opt_debug);

        my $msg = shift (@_);
        my $log = "$path/$name.log";

        my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time)
;
        $wday++;
        $yday++;
        $mon++;
        $year+=1900;
        $isdst++;

        if ($opt_debug == 1){
            print "$msg\n";
        }else {
           open(LOG, ">>$log");
           printf LOG ("%02i/%02i/%i - %02i:%02i:%02i => %s\n",$mday,$mon,$year,$hour,$min,$sec,$msg);
           close(LOG);
        }

}
#-------------------------------------------------------------------------

&main;
