summaryrefslogtreecommitdiffstats
path: root/web/attachments/147630-check_mysqlhealth.pl
diff options
context:
space:
mode:
Diffstat (limited to 'web/attachments/147630-check_mysqlhealth.pl')
-rw-r--r--web/attachments/147630-check_mysqlhealth.pl159
1 files changed, 159 insertions, 0 deletions
diff --git a/web/attachments/147630-check_mysqlhealth.pl b/web/attachments/147630-check_mysqlhealth.pl
new file mode 100644
index 0000000..f0391d5
--- /dev/null
+++ b/web/attachments/147630-check_mysqlhealth.pl
@@ -0,0 +1,159 @@
1#!/usr/bin/perl
2# --
3# Check the health of a mysql server.
4#
5# @author Peter Romianowski / optivo GmbH
6# @version 1.0 2005-08-31
7# --
8use Getopt::Long;
9use DBI;
10
11# --
12# Print out the usage message
13# --
14sub usage {
15 print "usage: check_mysqlhealth.pl -H <host> -u <user> -p <password> \n";
16 print " Optional parameters:\n";
17 print " --port <port> \n";
18 print " --Cc <critical number of connections> \n";
19 print " --Wc <warning number of connections> \n";
20 print " --Ca <critical number of ACTIVE connections> \n";
21 print " --Wa <warning number of ACTIVE connections> \n";
22 print " --sql <an sql statement that will be executed and that must return at least one row>\n";
23}
24
25$|=1;
26
27# --
28# Parse arguments and read Configuration
29# --
30my ($host, $user, $password, $port, $criticalConnections, $warningConnections, $criticalActive, $warningActive, $sql);
31GetOptions (
32 'host=s' => \$host,
33 'H=s' => \$host,
34 'user=s' => \$user,
35 'u=s' => \$user,
36 'password=s' => \$password,
37 'p:s' => \$password,
38 'port=i' => \$port,
39 'Cc=i' => \$criticalConnections,
40 'Wc=i' => \$warningConnections,
41 'Ca=i' => \$criticalActive,
42 'Wa=i' => \$warningActive,
43 'sql=s'=> \$sql
44);
45
46if (!$host || !$user) {
47 usage();
48 exit(1);
49}
50
51if (!$port) {
52 $port = 3306;
53}
54
55my $totalTime = time();
56
57# --
58# Establish connection
59# --
60my $state = "OK";
61my $dbh;
62eval {
63 $dbh = DBI->connect("DBI:mysql:host=$host;port=$port", $user, $password, {'RaiseError' => 1});
64};
65
66if ($@) {
67 my $status = $@;
68 print 'CRITICAL: Connect failed with reason ' . $status . "\n";
69 exit 2;
70}
71
72# --
73# Count active statements
74# --
75my $connections = 0;
76my $active = 0;
77
78eval {
79 my $sth = $dbh->prepare("SHOW PROCESSLIST");
80 $sth->execute();
81 my $row;
82 do {
83 $row = $sth->fetchrow_hashref();
84 if ($row) {
85 if (!($row->{'Command'} =~ /Sleep/)) {
86 $active++;
87 }
88 $connections++;
89 }
90
91 } while ($row);
92};
93
94if ($@) {
95 my $status = $@;
96 print 'CRITICAL: Error executing SHOW PROCESSLIST, reason ' . $status . "\n";
97 exit 2;
98}
99
100# --
101# Execute optional sql statement if given
102# --
103$sqlResult;
104if ($sql) {
105 eval {
106 my $sth = $dbh->prepare($sql);
107 $sth->execute();
108 my @row = $sth->fetchrow();
109 if (@row) {
110 $sqlResult = join('|', @row);
111 }
112 };
113
114 if ($@) {
115 my $status = $@;
116 print 'CRITICAL: Error executing statement "' . $sql . '", reason ' . $status . "\n";
117 exit 2;
118 }
119
120}
121
122# --
123# Cleanup resources
124# --
125$dbh->disconnect();
126
127# --
128# Check
129# --
130my $statusString = "$connections connections, $active active connections";
131if ($criticalConnections && $criticalConnections < $connections) {
132 print "CRITICAL: Number of connections higher than $criticalConnections ($statusString)\n";
133 exit 2;
134}
135if ($criticalActive && $criticalActive < $active) {
136 print "CRITICAL: Number of ACTIVE connections higher than $criticalActive ($statusString)\n";
137 exit 2;
138}
139if ($warningConnections && $warningConnections < $connections) {
140 print "WARNING: Number of connections higher than $warningConnections ($statusString)\n";
141 exit 1;
142}
143if ($warningActive && $warningActive < $active) {
144 print "WARNING: Number of ACTIVE connections higher than $warningActive ($statusString)\n";
145 exit 1;
146}
147if ($sql) {
148 if ($sqlResult) {
149 $statusString = "Statement '$sql' returned: '$sqlResult', $statusString";
150 } else {
151 print "CRITICAL: Execution of statement '$sql' did not produce any result ($statusString)\n";
152 exit 2;
153 }
154}
155
156print "OK: $statusString\n";
157exit 0;
158
159