| 1 |
#!/usr/bin/perl |
|---|
| 2 |
# |
|---|
| 3 |
# mysql_cachememory - written by Paul Dixon (paul@elphin.com) |
|---|
| 4 |
# adapted from mysql_queries by Per Andreas Buer |
|---|
| 5 |
# |
|---|
| 6 |
# This munin plugin graphs the total available memory for the |
|---|
| 7 |
# mysql query cache, show the proportion that has been used |
|---|
| 8 |
# |
|---|
| 9 |
# Parameters: |
|---|
| 10 |
# |
|---|
| 11 |
# config |
|---|
| 12 |
# autoconf |
|---|
| 13 |
# |
|---|
| 14 |
# Configuration variables |
|---|
| 15 |
# |
|---|
| 16 |
# mysqlopts - Options to pass to mysql |
|---|
| 17 |
# mysqladmin - Override location of mysqladmin |
|---|
| 18 |
# |
|---|
| 19 |
#%# family=auto |
|---|
| 20 |
#%# capabilities=autoconf |
|---|
| 21 |
|
|---|
| 22 |
use strict; |
|---|
| 23 |
|
|---|
| 24 |
my $MYSQLADMIN = $ENV{mysqladmin} || "mysqladmin"; |
|---|
| 25 |
my $COMMAND = "$MYSQLADMIN $ENV{mysqlopts} extended-status variables"; |
|---|
| 26 |
|
|---|
| 27 |
#status variables we want to fetch... |
|---|
| 28 |
my %mysql_status = ( |
|---|
| 29 |
"Qcache_free_memory" => 0, |
|---|
| 30 |
"query_cache_size" => 0, |
|---|
| 31 |
); |
|---|
| 32 |
|
|---|
| 33 |
my $arg = shift(); |
|---|
| 34 |
|
|---|
| 35 |
if ($arg eq 'config') { |
|---|
| 36 |
print_config(); |
|---|
| 37 |
exit(); |
|---|
| 38 |
} elsif ($arg eq 'autoconf') { |
|---|
| 39 |
unless (test_service() ) { |
|---|
| 40 |
print "yes\n"; |
|---|
| 41 |
} else { |
|---|
| 42 |
print "no\n"; |
|---|
| 43 |
} |
|---|
| 44 |
exit; |
|---|
| 45 |
} |
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
#no special argument to process, let's gather our data! |
|---|
| 49 |
|
|---|
| 50 |
open(SERVICE, "$COMMAND |") |
|---|
| 51 |
or die("Coult not execute '$COMMAND': $!"); |
|---|
| 52 |
|
|---|
| 53 |
while (<SERVICE>) { |
|---|
| 54 |
my ($k, $v) = (m/(\w+).*?(\d+(?:\.\d+)?)/); |
|---|
| 55 |
next unless ($k); |
|---|
| 56 |
if (exists $mysql_status{$k} ) { |
|---|
| 57 |
$mysql_status{$k}=$v; |
|---|
| 58 |
} |
|---|
| 59 |
} |
|---|
| 60 |
|
|---|
| 61 |
close(SERVICE); |
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
my $mb_size = $mysql_status{'query_cache_size'}/(1024*1024); |
|---|
| 65 |
my $mb_free = $mysql_status{'Qcache_free_memory'}/(1024*1024); |
|---|
| 66 |
my $mb_used = $mb_size - $mb_free; |
|---|
| 67 |
|
|---|
| 68 |
print "free.value $mb_free\n"; |
|---|
| 69 |
print "used.value $mb_used\n"; |
|---|
| 70 |
|
|---|
| 71 |
exit; |
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
sub print_config { |
|---|
| 76 |
|
|---|
| 77 |
my $num = 0; |
|---|
| 78 |
|
|---|
| 79 |
print("graph_title MySQL Query Cache Memory |
|---|
| 80 |
graph_args -l 0 |
|---|
| 81 |
graph_vlabel MB |
|---|
| 82 |
graph_category mysql |
|---|
| 83 |
graph_info This graph shows the size of the cache and the amount being used |
|---|
| 84 |
used.label Used memory (MB) |
|---|
| 85 |
used.type GAUGE |
|---|
| 86 |
used.draw AREA |
|---|
| 87 |
free.label Free memory (MB) |
|---|
| 88 |
free.type GAUGE |
|---|
| 89 |
free.draw STACK |
|---|
| 90 |
"); |
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
} |
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
sub test_service { |
|---|
| 97 |
|
|---|
| 98 |
my $return = 1; |
|---|
| 99 |
|
|---|
| 100 |
system ("$MYSQLADMIN --version >/dev/null 2>/dev/null"); |
|---|
| 101 |
if ($? == 0) |
|---|
| 102 |
{ |
|---|
| 103 |
system ("$COMMAND >/dev/null 2>/dev/null"); |
|---|
| 104 |
if ($? == 0) |
|---|
| 105 |
{ |
|---|
| 106 |
print "yes\n"; |
|---|
| 107 |
$return = 0; |
|---|
| 108 |
} |
|---|
| 109 |
else |
|---|
| 110 |
{ |
|---|
| 111 |
print "no (could not connect to mysql)\n"; |
|---|
| 112 |
} |
|---|
| 113 |
} |
|---|
| 114 |
else |
|---|
| 115 |
{ |
|---|
| 116 |
print "no (mysqadmin not found)\n"; |
|---|
| 117 |
} |
|---|
| 118 |
exit $return; |
|---|
| 119 |
} |
|---|