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

# checks the time it takes mysql to accept a connection


use strict;
use DBI;
use Getopt::Std;
use Time::HiRes qw[gettimeofday tv_interval];


$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, defaults to information_schema

 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 milliseconds, defaults to 100
  -c   critical connection time treshhold in milliseconds, defaults to 200
  -t   connection timeout in seconds, defaults to 15
';

$main::versionMessage="check_mysql_conntime 0.3.0\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 = "information_schema";
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 $warningMillis=100;
if ($main::opts{"w"}) {
  $warningMillis = $main::opts{"w"};
}
my $criticalMillis=200;
if ($main::opts{"c"}) {
  $criticalMillis = $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;

# 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 $time1 = int(gettimeofday()*1000);
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 $time2 = int(gettimeofday()*1000);
#my $durationMillis = int(tv_interval($time1, $time2) * 1000);
my $durationMillis = $time2 - $time1;

#if ($main::verbosity > 1) {
#  print "DEBUG: Time 1: ".$time1."\n";
#  print "DEBUG: Time 2: ".$time2."\n";
#  print "DEBUG: Duration Millis: ".$durationMillis."\n";
#}

$dbh->disconnect();

my $perfData = "connection_time=".$durationMillis."ms;".$warningMillis.";".$criticalMillis.";;";
if ($durationMillis >= $criticalMillis) {
  print "CRITICAL - ".$durationMillis." ms to connect|".$perfData."\n";
  exit $rc4critical;
}
if ($durationMillis >= $warningMillis) {
  print "WARNING - ".$durationMillis." ms to connect|".$perfData."\n";
  exit $rc4warning;
}
print "OK - ".$durationMillis." ms to connect|".$perfData."\n";
exit $rc4ok;

