#!/usr/bin/perl
#
# $Id$
#
# Copyright 2018 ITEG IT-Engineers GmbH
#
# Licensed under the Apache License, Version 2.0 (the "License"), see LICENSE.txt
#

# computes the size of a mysql database querying tine information_schema's TABLES table


use strict;
use DBI;
use Getopt::Std;


$main::helpMessage='
 Syntax:
  check_mysql_conntime --help | options
  
 mysql connection options are:
  -h   hostname, defaults to empty, in which case the default socket is used
  -P   TCP port to connect to, defaults to empty, i.e. default port
  -S   specific socket name
  -D   database name, required

 mysql authentication options are:
  -u   username, required
  -p   password, required unless socket is use for connection and authentication

 performanceOptions are:
  -w   warning connection time in MBs, defaults to 10000
  -c   critical connection time treshhold in MBs, defaults to 20000
  -t   connection timeout in seconds, defaults to 15
';

$main::versionMessage="check_mysql_dbsize 0.3.1\n";

sub main::HELP_MESSAGE()    { print $main::helpMessage; }
sub main::VERSION_MESSAGE() { print $main::versionMessage; }


%main::opts=();
$Getopt::Std::STANDARD_HELP_VERSION=1;
getopts('c:D:h:p:P:qS:t:u:w:', \%main::opts);

my $mysqlHost="";
if ($main::opts{"h"}) {
  $mysqlHost = $main::opts{"h"};
}
my $mysqlPort=""; # 3306;
if ($main::opts{"P"}) {
  $mysqlPort = $main::opts{"P"};
}
my $mysqlSocket="";
if ($main::opts{"S"}) {
  $mysqlSocket = $main::opts{"S"};
  $mysqlHost = "";
}
my $mysqlDatabase = "";
if ($main::opts{"D"}) {
  $mysqlDatabase = $main::opts{"D"};
}

my $mysqlUser="";
if ($main::opts{"u"}) {
  $mysqlUser = $main::opts{"u"};
}
my $mysqlPassword="";
if ($main::opts{"p"}) {
  $mysqlPassword = $main::opts{"p"};
}

my $warningMBs=10000;
if ($main::opts{"w"}) {
  $warningMBs = $main::opts{"w"};
}
my $criticalMBs=20000;
if ($main::opts{"c"}) {
  $criticalMBs = $main::opts{"c"};
}
my $timeoutSeconds=15;
if ($main::opts{"t"}) {
  $timeoutSeconds = $main::opts{"t"};
}

#$main::verbosity=1;
#if ($main::opts{"d"}) {
#  $main::verbosity = 2;
#}
#if ($main::opts{"q"}) {
#  $main::verbosity = 0;
#}
#if ($main::opts{"v"}) {
#  $main::verbosity += 1;
#}

# nagios / icinga plugin standards
my $rc4ok = 0;
my $rc4warning = 1;
my $rc4critical = 2;
my $rc4unknown = 3;

# db specified?
if ($mysqlDatabase eq "") {
  print "UNKNOWN - No database name specified";
  exit $rc4unknown;
}

# connect to DB (always needed)
my $dbsource = "DBI:mysql:database=" . $mysqlDatabase;
if ($mysqlHost ne "") {
  $dbsource .= ":host=" . $mysqlHost;
  if ($mysqlPort ne "") {
    $dbsource .= ":port=" . $mysqlPort;
  }
}
if ($mysqlSocket ne "") {
  $dbsource .= ":mysql_socket=" . $mysqlSocket;
}
$dbsource .= ":mysql_connect_timeout=" . $timeoutSeconds;

my $dbh = DBI->connect($dbsource, $mysqlUser, $mysqlPassword); # or $dberr = $dbh->errstr();
if (not defined $dbh) {
  print "UNKNOWN - Unable to connect to ".$dbsource." as '".$mysqlUser."' ";
  if ($mysqlPassword eq "") {
    print "without password";
  } else {
    print "with password of length ".length($mysqlPassword);
  }
  print ".\n"; 
  #exit $rc4critical;
  exit $rc4unknown;
}

my $stmt = $dbh->prepare("SELECT SUM(data_length + index_length) FROM information_schema.TABLES WHERE table_schema='" . $mysqlDatabase . "'");

$stmt->execute();

my @curRow = $stmt->fetchrow_array();
if (not defined $curRow[0]) {
  print "UNKNOWN - Unable to query size of database ".$mysqlDatabase." as '".$mysqlUser."' (no result)";
  #exit $rc4critical;
  exit $rc4unknown;
}
my $dbSizeBytes = $curRow[0];
if (0 + $dbSizeBytes < 1) {
  print "UNKNOWN - Unable to query size of database ".$mysqlDatabase." as '".$mysqlUser."' (invalid result)";
  #exit $rc4critical;
  exit $rc4unknown;
}
my $dbSizeMBs = $dbSizeBytes / 1048576;

$stmt->finish();

$dbh->disconnect();

my $perfData = "db_size=".$dbSizeMBs."MB;".$warningMBs.";".$criticalMBs.";;";
if ($dbSizeMBs >= $criticalMBs) {
  print "CRITICAL - db_size: ".$dbSizeMBs." MB|".$perfData."\n";
  exit $rc4critical;
}
if ($dbSizeMBs >= $warningMBs) {
  print "WARNING - db_size: ".$dbSizeMBs." MB|".$perfData."\n";
  exit $rc4warning;
}
print "OK - db_size: ".$dbSizeMBs." MB|".$perfData."\n";
exit $rc4ok;

#exit $rc4unknown;

