CHips L MINI SHELL

CHips L pro

Current Path : /proc/3/cwd/opt/zabbix_scripts/
Upload File :
Current File : //proc/3/cwd/opt/zabbix_scripts/check_mysql_connections

#!/usr/bin/perl

# Copyright (c) 2012 Jason Hancock <jsnbyh@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is furnished
# to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# This file is part of the nagios-mysql bundle that can be found
# at https://github.com/jasonhancock/nagios-mysql
#
# This plugin reports on connection statistics for MySQL. It takes warning
# and critical thresholds expressed as percents of the maximum number of
# connections allowed by the database server

use strict;
use warnings;
use Nagios::Plugin;
use DBI;

my $np = Nagios::Plugin->new(
    usage => "Usage: %s [-H|--host=<host> ] [ -t|--type=<type> ]",
    shortname => 'MySQL Connections',
);

$np->add_arg(
    spec => 'host|H=s',
    help => '-H, --host=hostname or IP',
    required => 1,
);


$np->add_arg(
    spec => 'port|P=s',
    help => '-P, --port=port',
    default => 3306,
);

$np->add_arg(
    spec => 'user|u=s',
    help => '-u, --user=username',
    required => 1,
);

$np->add_arg(
    spec => 'pass|p=s',
    help => '-p, --pass=password',
    required => 1,
);

$np->add_arg(
    spec => 'warning|w=s',
    help => '-w, --warning=percent',
    required => 1,
);

$np->add_arg(
    spec => 'critical|c=s',
    help => '-c, --critical=percent',
    required => 1,
);

$np->getopts;

# a hash for quick lookups
my %interests = (
    'Aborted_clients' => 1,
    'Aborted_connects' => 1,
    'Threads_connected' => 1,
    'Connections' => 1,
    'Max_used_connections' => 1
);

# The order in which we will output the stats
my @order = (
    'max_connections',
    'max_used_connections',
    'threads_connected',
    'aborted_clients',
    'aborted_connects',
    'connections'
);
    
my %data;

# Connect to the database
my $ds = sprintf('DBI:mysql::%s:%d', $np->opts->host, $np->opts->port);
my $dbh = DBI->connect($ds, $np->opts->user, $np->opts->pass, { PrintError => 0 }) or
    $np->nagios_exit('UNKNOWN', 'Unable to connect to database');

# Run the query
my $qh = $dbh->prepare('SHOW GLOBAL STATUS') or
    $np->nagios_exit('UNKNOWN', 'UNABLE to prepare query');
$qh->execute();
while(my ($name, $value) = $qh->fetchrow_array()) {
    $data{lc($name)} = $value if defined($interests{$name});
}
$qh->finish();

$qh = $dbh->prepare('SHOW GLOBAL VARIABLES LIKE \'max_connections\'') or
    $np->nagios_exit('UNKNOWN', 'UNABLE to prepare query');
$qh->execute();
while(my ($name, $value) = $qh->fetchrow_array()) {
    $data{lc($name)} = $value;
}
$qh->finish();
$dbh->disconnect();

foreach my $key(@order) {
    $np->add_perfdata(
        label => $key,
        value => $data{$key},
        uom => undef,
    );
}

my $code = $np->check_threshold(
    check => $data{'threads_connected'},
    warning => int($np->opts->warning * .01 * $data{'max_connections'}),
    critical => int($np->opts->critical * .01 * $data{'max_connections'}),
);

$np->nagios_exit(
    return_code => $code,
    message => sprintf('Connections used: %d maximum: %d',
        $data{'threads_connected'},
        $data{'max_connections'},
    ),
);

Copyright 2K16 - 2K18 Indonesian Hacker Rulez