diff --git a/roles/mariadb/tasks/main.yml b/roles/mariadb/tasks/main.yml index 29f72724a711033d4470387eabdde42b69ffdb26..8c7ec8360352027afb401b2e74883c128f59fc69 100644 --- a/roles/mariadb/tasks/main.yml +++ b/roles/mariadb/tasks/main.yml @@ -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: src=zabbix_agentd.my.cnf.j2 dest=/etc/zabbix/zabbix_agentd.my.cnf owner=zabbix-agent group=zabbix-agent mode=0600 diff --git a/roles/zabbix-agent/files/mysql-user-stats.pl b/roles/zabbix-agent/files/mysql-user-stats.pl index 0e17625c806680c764517b373cbe71397b9207a9..64d774a92bb18c551750c82dc1f581f429899120 100644 --- a/roles/zabbix-agent/files/mysql-user-stats.pl +++ b/roles/zabbix-agent/files/mysql-user-stats.pl @@ -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"; - die; -} - -if ($mode eq "userstat" and not defined $user) { - print STDERR "userstat requires user to be defined\n"; - die; -} 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"; } diff --git a/roles/zabbix-agent/templates/zabbix_agentd.conf b/roles/zabbix-agent/templates/zabbix_agentd.conf index 1f9e4d08a62341f7955f23508bed4ec1f96b314a..4e9724f60b1ab946aaeaeef2100e2ec0e9fa4687 100644 --- a/roles/zabbix-agent/templates/zabbix_agentd.conf +++ b/roles/zabbix-agent/templates/zabbix_agentd.conf @@ -12,6 +12,8 @@ TLSPSKFile=/etc/zabbix/zabbix_agentd.psk UserParameter=mysql.ping,mysqladmin --defaults-file="/etc/zabbix/zabbix_agentd.my.cnf" ping|grep alive|wc -l UserParameter=mysql.version,mysql --defaults-file="/etc/zabbix/zabbix_agentd.my.cnf" -V UserParameter=mysql.status[*],echo "show global status where Variable_name='$1';" | mysql --defaults-file="/etc/zabbix/zabbix_agentd.my.cnf" -N | awk '{print $$2}' +UserParameter=mysql.tablestats[*],/usr/local/bin/mysql-user-stats.pl "/etc/zabbix/zabbix_agentd.my.cnf" tablestats '$1' +UserParameter=mysql.tablestats_discover,/usr/local/bin/mysql-user-stats.pl "/etc/zabbix/zabbix_agentd.my.cnf" tablestats_discover UserParameter=mysql.userstats[*],/usr/local/bin/mysql-user-stats.pl "/etc/zabbix/zabbix_agentd.my.cnf" userstat '$1' UserParameter=mysql.userstats_discover,/usr/local/bin/mysql-user-stats.pl "/etc/zabbix/zabbix_agentd.my.cnf" discover