/usr/share/munin/plugins/mysql_isam_space_ is in munin-plugins-extra 2.0.19-3ubuntu0.3.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 | #!/usr/bin/perl -w
# -*- perl -*-
=head1 NAME
mysql_isam_space_ - Wildcard plugin to monitor the percent of table space used
on isam and myisam tables on a mysql server.
=head1 CONFIGURATION
Configuration parameters for /etc/munin/PLUGIN,
if you need to override the defaults below:
[mysql_isam_space_*]
env.mysqlopts - Options to pass to mysql
env.statefile - where to put the statefile
env.ignore - Tables to ignore. Regex.
env.absolute - use tables sizes, not percent of maximum
=head2 DEFAULT CONFIGURATION
[mysql_isam_space_*]
env.mysqlopts
env.statefile $ENV{MUNIN_PLUGSTATE}/plugin-mysql_isam_space.state
env.ignore
env.absolute 0
=head1 AUTHOR
Unknown author
=head1 LICENSE
GPLv2
=head1 MAGIC MARKERS
=begin comment
These magic markers are used by munin-node-configure when installing
munin-node.
=end comment
#%# family=contrib
#%# capabilities=suggest autoconf
=cut
my $DB = `basename $0 | sed 's/^mysql_isam_space_//g' | tr '_' '-'` ;
chomp $DB;
my $STATEFILE = $ENV{'statefile'} || "$ENV{MUNIN_PLUGSTATE}/plugin-mysql_isam_space.state";
my $MYSQLSHOW = $ENV{'mysqlshow'} || `which mysqlshow`;
my $ABSOLUTE = $ENV{'absolute'} || 0;
my @mysql_opts = ();
chomp $MYSQLSHOW;
if (exists $ENV{'mysqlopts'})
{
@mysql_opts = split /\s+/, $ENV{'mysqlopts'};
}
my $tables = undef;
my $databases = undef;
&print_autoconf () if ($ARGV[0] and $ARGV[0] eq "autoconf");
if (&use_statefile)
{
($databases, $tables) = &read_statefile;
}
else
{
($databases, $tables) = &fetch_state_from_mysql;
}
&print_config ($tables) if ($ARGV[0] and $ARGV[0] eq "config");
&print_suggest ($databases, $tables) if ($ARGV[0] and $ARGV[0] eq "suggest");
foreach my $t (keys %{$tables})
{
printf ("%s.%s %f\n", $t, "value ", $tables->{$t});
}
sub print_config
{
my $tables = shift;
print "graph_title MySQL \"$DB\" isam/myisam table-space usage\n";
print "graph_args --base 1000\n";
print "graph_vlabel percent\n";
print "graph_category mysql\n";
foreach my $t (keys %{$tables})
{
my $label = $t;
if (length ($t) >= 20)
{
$label = sprintf ("...%17s", substr ($t, -17));
}
print $t, ".label $label\n";
print $t, ".warning 0:90\n";
print $t, ".critical 0:95\n";
}
exit 0;
}
sub read_statefile
{
my $tables = shift;
my $databases = undef;
# Read the statefile
open (IN, $STATEFILE) or die "Could not open \"$STATEFILE\" for reading: $!";
while (<IN>)
{
tr/æøå/eoa/;
if (/^$DB\.([^\s]+)\s+([\d\.]+)\s*$/)
{
$tables->{$1} = $2;
}
if (/^([^\.]+)\./)
{
if (! grep (/^$1$/, @{$databases}))
{
push (@{$databases}, $1);
}
}
}
close (IN);
return ($databases, $tables);
}
sub use_statefile
{
if (-f $STATEFILE)
{
if ((stat ($STATEFILE))[9] > (time - 3600))
{
return 1;
}
}
return 0;
}
sub fetch_state_from_mysql
{
my $tables = shift;
my $databases = ();
# Fork off mysqlshow
my $pid = open (IN, '-|');
if (! defined ($pid))
{ # Fork fail
die "Couldn't fork.";
}
elsif ($pid == 0)
{ # Child
exec ($MYSQLSHOW, @mysql_opts, "--status", "-v");
}
my @mysql_status = <IN>;
close (IN);
if ($? != 0)
{
exit 1;
}
foreach $line (@mysql_status)
{
$line =~ tr/æøå/eoa/;
if ($line =~ /^\|\s+([^\|\s]+)\s+\|\s+([^\|\s]+)\s+\|/)
{
next if ($1 eq "Databases");
next if ($2 eq "N/A");
if ($2 > 0)
{
push (@{$databases}, $1)
}
}
}
if ((! -e $STATEFILE) or (-f $STATEFILE))
{
&makedirfor ($STATEFILE);
open (OUT, ">$STATEFILE") or die "Could not open \"$STATEFILE\" for writing: $!";
}
else
{
die "Outfile exists and isn't a \"file\".";
}
foreach my $db (@{$databases})
{
# Fork off mysqlshow
my $pid = open (IN, '-|');
if (! defined ($pid))
{ # Fork fail
die "Couldn't fork.";
}
elsif ($pid == 0)
{ # Child
exec ($MYSQLSHOW, @mysql_opts, "--status", "-v", $db);
# Notreached.
}
my %index;
my $headerseen;
while (<IN>)
{
my @fields = split (/\s*\|\s*/);
next if @fields < 2; # Separator line
if (! $headerseen and $fields[1] eq "Name")
{ # Header line, grab field names
%index = map {($fields[$_], $_)} 0..$#fields;
}
else
{
# mysqlshow says many fun things sometimes, sanity-check
next if $fields[$index{Data_length}] eq "";
next if ($fields[$index{Max_data_length}] eq "") && ( ! $ABSOLUTE );
next if ($fields[$index{Max_data_length}] == 0) && ( ! $ABSOLUTE );
my $value =
$ABSOLUTE ? $fields[$index{Data_length}]
: (100*$fields[$index{Data_length}]/$fields[$index{Max_data_length}]);
printf OUT ("%s.%s %f\n", $db, $fields[1], $value);
$tables->{$fields[1]} = $value if $DB eq $db;
}
}
close (IN);
}
close (OUT);
return ($databases, $tables);
}
sub makedirfor
{
my $filename = shift;
(my $dir = $filename) =~ s/\/[^\/]+$//;
if ($dir ne $filename and ! -d $dir)
{
&makedirfor ($dir);
mkdir ($dir, 0700);
}
return $dir;
}
sub print_suggest
{
my ($databases, $tables) = @_;
foreach my $db (@{$databases})
{
print "$db\n";
}
exit 0;
}
sub print_autoconf
{
# First check that mysqlshow exists...
if (! -x $MYSQLSHOW)
{
print "no (mysqlshow not found)\n";
exit 0;
}
# Fork off mysqlshow
my $pid = open (IN, '-|');
if (! defined ($pid))
{ # Fork fail
die "Couldn't fork.";
}
elsif ($pid == 0)
{ # Child
close STDERR;
open (STDERR, ">&STDOUT");
exec ($MYSQLSHOW, @mysql_opts, "--status", "-v");
}
my @slurp = <IN>;
close (IN);
if ($? == 0)
{
print "yes\n";
exit 0;
}
elsif (grep (/Can't connect to local MySQL server/, @slurp))
{
print "no (could not connect to mysql)\n";
}
else
{
print "no\n";
}
exit 0;
}
# vim:syntax=perl
|