#!/usr/bin/perl
#
#Description:
#	Check the opmondb file size
#
#       This plugin must be executed using the command sudo.
#       Before run, insert the follow line at /etc/sudoers:
#       
#	opuser ALL= /usr/local/opmon/libexec/check_opmon_opmondb_size.pl, NOPASSWD: ALL
#
#Author:
#	Fernando Rocha (fernando.rocha@opservices.com.br)
#
#Version:
#	1.0:	Initial release

use strict;
use Getopt::Long;
use POSIX;
use File::Basename;

# Setting environment
$ENV{"USER"}="opuser";
$ENV{"HOME"}="/home/opuser";

# globlal variables
our $version = "1.0";
our $name = basename($0, ".pl");
our $path = "/usr/local/opmon/libexec/opservices";
our ($opt_version, $opt_help, $opt_debug);

our $opmondb_dir = "/var/tmp/opmondb";

sub main {
        # Get options
        getOptions();

        logger("----- INICIO DO SCRIPT -----");

	# Read the perfparse dir
	opendir(DIR,$opmondb_dir)
	or do {
		print "$opmondb_dir: $!\n";
		exit (2);
	};
	my @opmondb_files = readdir(DIR);
	closedir(DIR); 

	my $dir_size = 0;

	foreach my $db_file (@opmondb_files){
	
		#perfdata_service_bin_www_clion_net.ibd
		if ($db_file =~ /(.+)\.sql/){

			logger (":: $db_file");

			# get the DB size
			my @db_stat = stat ("$opmondb_dir/$db_file");
			my $db_size = $db_stat[7];

			logger ("\tFile: $db_file");
			logger ("\tSize: $db_size");

			$dir_size += $db_size;
		}
	
	}

	
	# convert to MB
	$dir_size = sprintf ("%.2f",($dir_size / 1024) / 1024);	
	
	logger (":: DIR SIZE");
	logger ("\tSize: $dir_size");

	# Compare the last size
	open (LOG,"$path/$name.size");
	my $last_size = <LOG>;
	close (LOG);

	logger ("\tOld Size: $last_size");

	logger ("-------FIM DO SCRIPT--------");

	if ($last_size >= $dir_size){
	        print "Critical: OpMonDB: $dir_size MB|size=$dir_size\MB;;;0;\n";
	        exit (2);
	}

	open(LOG,">$path/$name.size")
	 or do {
		print "$path/$name.size: $!\n";
		exit (2);
	 };
	print LOG $dir_size;
	close(LOG);
	
	print "Ok: OpMonDB: $dir_size MB|size=$dir_size\MB;;;0;\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,

        )or do {
                printUsage();
                exit();
        };

        if ($opt_version) {
                print "$name.pl: $version\n";
                exit();
        }

        if ($opt_help){
                printUsage();
                exit();
        }

        if (($opt_debug) and (($opt_debug < 1) or ($opt_debug > 2))){
                printUsage();
                exit();
        }

}
#--------------------------------------------------------------------------
sub printUsage {

       print <<EOB

Usage: $0 [OPTION]...
Verify the opmondb file size.

        -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;
