Verified Commit 026480fd authored by Florian Pritz's avatar Florian Pritz
Browse files

WIP: Monitor mysql table stats

TODO this still needs to give zabbix SELECT permissions to the tables,
otherwise the table statistics are not available. Do we want zabbix to
be able to read everything? Not sure yet, leaning towards no.

I don't want to lose the other changes though so I'm commiting this here
for now.

Signed-off-by: Florian Pritz's avatarFlorian Pritz <>
parent b7ab03fa
......@@ -42,6 +42,8 @@
- name: configure zabbix-agent user
mysql_user: user={{zabbix_agent_mysql_user}} host=localhost password={{vault_mariadb_users.zabbix_agent}}
# TODO give zabbix-agent select permissions on all tablesse. maybe don't?
- name: install zabbix mysql config
template: dest=/etc/zabbix/ owner=zabbix-agent group=zabbix-agent mode=0600
......@@ -7,27 +7,30 @@ use JSON;
my $config = $ARGV[0];
my $mode = $ARGV[1];
my $user = $ARGV[2];
if (!($mode eq "userstat" or $mode eq "discover")) {
print STDERR "mode is not userstat or discover\n";
if ($mode eq "userstat" and not defined $user) {
print STDERR "userstat requires user to be defined\n";
my $db = DBI->connect("DBI:mysql:mysql_read_default_file=$config");
if ($mode eq "userstat") {
my $user = $ARGV[2] // die "user not defined";
$db->{FetchHashKeyName} = "NAME_lc";
print encode_json($db->selectrow_hashref("select * from information_schema.user_statistics where user = ?", undef, $user));
} elsif ($mode eq "tablestats") {
my $schema = $ARGV[2] // die "schema not defined";
my $table = $ARGV[3] // die "table not defined";
$db->{FetchHashKeyName} = "NAME_lc";
print encode_json($db->selectrow_hashref("select * from information_schema.table_statistics where table_schema = ? and table_name = ?", undef, $schema, $table));
} elsif ($mode eq "tablestats_discover") {
print encode_json({
data => $db->selectall_arrayref("select table_schema '{#SCHEMA}', table_name '{#TABLENAME}' from information_schema.table_statistics", {Slice=>{}})
} elsif ($mode eq "discover") {
print encode_json({
data => $db->selectall_arrayref("select user '{#USERNAME}' from information_schema.user_statistics", {Slice=>{}})
} else {
die "unhandeled mode";
......@@ -12,6 +12,8 @@ TLSPSKFile=/etc/zabbix/zabbix_agentd.psk,mysqladmin --defaults-file="/etc/zabbix/" ping|grep alive|wc -l
UserParameter=mysql.version,mysql --defaults-file="/etc/zabbix/" -V
UserParameter=mysql.status[*],echo "show global status where Variable_name='$1';" | mysql --defaults-file="/etc/zabbix/" -N | awk '{print $$2}'
UserParameter=mysql.tablestats[*],/usr/local/bin/ "/etc/zabbix/" tablestats '$1'
UserParameter=mysql.tablestats_discover,/usr/local/bin/ "/etc/zabbix/" tablestats_discover
UserParameter=mysql.userstats[*],/usr/local/bin/ "/etc/zabbix/" userstat '$1'
UserParameter=mysql.userstats_discover,/usr/local/bin/ "/etc/zabbix/" discover
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment