summaryrefslogtreecommitdiffstats
path: root/web/attachments/147630-check_mysqlhealth.pl
blob: f0391d5d77983daeacdf3fe143a3ee42facae6ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/perl
# --
# Check the health of a mysql server.
#
# @author Peter Romianowski / optivo GmbH
# @version 1.0 2005-08-31
# --
use Getopt::Long;
use DBI;

# --
# Print out the usage message
# --
sub usage {
    print "usage: check_mysqlhealth.pl -H <host> -u <user> -p <password> \n";
    print "       Optional parameters:\n";
    print "       --port <port> \n";
    print "       --Cc <critical number of connections> \n";
    print "       --Wc <warning number of connections> \n";
    print "       --Ca <critical number of ACTIVE connections> \n";
    print "       --Wa <warning number of ACTIVE connections> \n";
    print "       --sql <an sql statement that will be executed and that must return at least one row>\n";
}

$|=1;

# --
# Parse arguments and read Configuration
# --
my ($host, $user, $password, $port, $criticalConnections, $warningConnections, $criticalActive, $warningActive, $sql);
GetOptions (
    'host=s' => \$host,
    'H=s' => \$host,
    'user=s' => \$user,
    'u=s' => \$user,
    'password=s' => \$password,
    'p:s' => \$password,
    'port=i' => \$port,
    'Cc=i' => \$criticalConnections,
    'Wc=i' => \$warningConnections,
    'Ca=i' => \$criticalActive,
    'Wa=i' => \$warningActive,
    'sql=s'=> \$sql
);

if (!$host || !$user) {
    usage();
    exit(1);
}

if (!$port) {
    $port = 3306;
}

my $totalTime = time();

# --
# Establish connection
# --
my $state = "OK";
my $dbh;
eval {
    $dbh = DBI->connect("DBI:mysql:host=$host;port=$port", $user, $password, {'RaiseError' => 1});
};

if ($@) {
    my $status = $@;
    print 'CRITICAL: Connect failed with reason ' . $status . "\n";
    exit 2;
}

# --
# Count active statements
# --
my $connections = 0;
my $active = 0;

eval {
    my $sth = $dbh->prepare("SHOW PROCESSLIST");
    $sth->execute();
    my $row;
    do {
        $row = $sth->fetchrow_hashref();
        if ($row) {
            if (!($row->{'Command'} =~ /Sleep/)) {
                $active++;
            }
            $connections++;
        }
        
    } while ($row);
};

if ($@) {
    my $status = $@;
    print 'CRITICAL: Error executing SHOW PROCESSLIST, reason ' . $status . "\n";
    exit 2;
}

# --
# Execute optional sql statement if given
# --
$sqlResult;
if ($sql) {
    eval {
        my $sth = $dbh->prepare($sql);
        $sth->execute();
        my @row = $sth->fetchrow();
        if (@row) {
            $sqlResult = join('|', @row);
        }
    };
    
    if ($@) {
        my $status = $@;
        print 'CRITICAL: Error executing statement "' . $sql . '", reason ' . $status . "\n";
        exit 2;
    }
    
}

# --
# Cleanup resources
# --
$dbh->disconnect();    

# --
# Check
# --
my $statusString = "$connections connections, $active active connections";
if ($criticalConnections && $criticalConnections < $connections) {
    print "CRITICAL: Number of connections higher than $criticalConnections ($statusString)\n";
    exit 2;
}
if ($criticalActive && $criticalActive < $active) {
    print "CRITICAL: Number of ACTIVE connections higher than $criticalActive ($statusString)\n";
    exit 2;
}
if ($warningConnections && $warningConnections < $connections) {
    print "WARNING: Number of connections higher than $warningConnections ($statusString)\n";
    exit 1;
}
if ($warningActive && $warningActive < $active) {
    print "WARNING: Number of ACTIVE connections higher than $warningActive ($statusString)\n";
    exit 1;
}
if ($sql) {
    if ($sqlResult) {
        $statusString = "Statement '$sql' returned: '$sqlResult', $statusString";
    } else {
        print "CRITICAL: Execution of statement '$sql' did not produce any result ($statusString)\n";
        exit 2;
    }
}

print "OK: $statusString\n";
exit 0;