diff options
Diffstat (limited to 'web/attachments/147630-check_mysqlhealth.pl')
| -rw-r--r-- | web/attachments/147630-check_mysqlhealth.pl | 159 |
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 | # -- | ||
| 8 | use Getopt::Long; | ||
| 9 | use DBI; | ||
| 10 | |||
| 11 | # -- | ||
| 12 | # Print out the usage message | ||
| 13 | # -- | ||
| 14 | sub 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 | # -- | ||
| 30 | my ($host, $user, $password, $port, $criticalConnections, $warningConnections, $criticalActive, $warningActive, $sql); | ||
| 31 | GetOptions ( | ||
| 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 | |||
| 46 | if (!$host || !$user) { | ||
| 47 | usage(); | ||
| 48 | exit(1); | ||
| 49 | } | ||
| 50 | |||
| 51 | if (!$port) { | ||
| 52 | $port = 3306; | ||
| 53 | } | ||
| 54 | |||
| 55 | my $totalTime = time(); | ||
| 56 | |||
| 57 | # -- | ||
| 58 | # Establish connection | ||
| 59 | # -- | ||
| 60 | my $state = "OK"; | ||
| 61 | my $dbh; | ||
| 62 | eval { | ||
| 63 | $dbh = DBI->connect("DBI:mysql:host=$host;port=$port", $user, $password, {'RaiseError' => 1}); | ||
| 64 | }; | ||
| 65 | |||
| 66 | if ($@) { | ||
| 67 | my $status = $@; | ||
| 68 | print 'CRITICAL: Connect failed with reason ' . $status . "\n"; | ||
| 69 | exit 2; | ||
| 70 | } | ||
| 71 | |||
| 72 | # -- | ||
| 73 | # Count active statements | ||
| 74 | # -- | ||
| 75 | my $connections = 0; | ||
| 76 | my $active = 0; | ||
| 77 | |||
| 78 | eval { | ||
| 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 | |||
| 94 | if ($@) { | ||
| 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; | ||
| 104 | if ($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 | # -- | ||
| 130 | my $statusString = "$connections connections, $active active connections"; | ||
| 131 | if ($criticalConnections && $criticalConnections < $connections) { | ||
| 132 | print "CRITICAL: Number of connections higher than $criticalConnections ($statusString)\n"; | ||
| 133 | exit 2; | ||
| 134 | } | ||
| 135 | if ($criticalActive && $criticalActive < $active) { | ||
| 136 | print "CRITICAL: Number of ACTIVE connections higher than $criticalActive ($statusString)\n"; | ||
| 137 | exit 2; | ||
| 138 | } | ||
| 139 | if ($warningConnections && $warningConnections < $connections) { | ||
| 140 | print "WARNING: Number of connections higher than $warningConnections ($statusString)\n"; | ||
| 141 | exit 1; | ||
| 142 | } | ||
| 143 | if ($warningActive && $warningActive < $active) { | ||
| 144 | print "WARNING: Number of ACTIVE connections higher than $warningActive ($statusString)\n"; | ||
| 145 | exit 1; | ||
| 146 | } | ||
| 147 | if ($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 | |||
| 156 | print "OK: $statusString\n"; | ||
| 157 | exit 0; | ||
| 158 | |||
| 159 | |||
