Skip to content

Instantly share code, notes, and snippets.

@ccooke
Created October 3, 2011 10:18
Show Gist options
  • Save ccooke/1258826 to your computer and use it in GitHub Desktop.
Save ccooke/1258826 to your computer and use it in GitHub Desktop.
Zabbix mysql plugin
# Plugin for zbxprox.pl, which is not yet released (2011-10-03)
{
'name' => "Mysql",
'config' => {
'host' => '127.0.0.1',
'user' => 'client',
'pass' => 'XXXXXXXXXXXXXXXXX',
'command' => "show status",
'cachetime' => 15
},
'translations' => {
'NULL' => 0,
'OFF' => 0,
'ON' => 1,
},
'cache' => {},
'last_update_success' => 0,
'template' => sub {
my ( $data, $template ) = @_;
$template->additem(
'description',
'key',
{
# options
'type' => 'Integer', # default: Integer
'multiplier' => 0, # default: Not present
# etc
}
);
},
'update' => sub {
my ( $self, $agents ) = @_;
my $command = sprintf(
"/usr/bin/mysql --batch --disable-column-names -h %s -u %s -p%s -e '%s'",
$self->{'config'}->{'host'},
$self->{'config'}->{'user'},
$self->{'config'}->{'pass'},
$self->{'config'}->{'command'}
);
my @data = split( "\n", `$command` );
my $retval = $? ;
my $signal = $? & 127;
if ( @data > 20 )
{
foreach my $sample ( @data )
{
my ( $key, $value ) = split( /\s+/, $sample );
if ( defined( $self->{'translations'}->{ $value } ) )
{
$value = $self->{'translations'}->{ $value };
}
$self->{'cache'}->{$key} = $value;
}
$self->{'cache'}->{'last_status'} = $?;
$self->{'cache'}->{'last_update'} = int(time());
$self->{'last_update_success'} = 1;
}
else
{
$self->{'last_update_success'} = 0;
}
return time() + $self->{'config'}->{'cachetime'};
},
'keys' => {
'mysql.agent' => sub {
my ( $self, $key, @params ) = @_;
return undef unless $self->{'last_update_success'};
return $self->{'cache'}->{$params[0]};
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment