From f20de75884ee24ad7dd545c34807029397e79faa Mon Sep 17 00:00:00 2001 From: Daniel Berteaud Date: Mon, 31 Aug 2020 15:00:48 +0200 Subject: [PATCH] Add MySQL monitoring script and template --- zabbix_conf/mysql.conf | 7 + zabbix_scripts/check_mysql | 101 +++ zabbix_templates/Template_App_MySQL.xml | 1079 +++++++++++++++++++++++++++++++ 3 files changed, 1187 insertions(+) create mode 100644 zabbix_conf/mysql.conf create mode 100644 zabbix_scripts/check_mysql create mode 100644 zabbix_templates/Template_App_MySQL.xml diff --git a/zabbix_conf/mysql.conf b/zabbix_conf/mysql.conf new file mode 100644 index 0000000..5016a90 --- /dev/null +++ b/zabbix_conf/mysql.conf @@ -0,0 +1,7 @@ +# Type: Agent or Agent (active) +# You can also get all the stats in a json object by passing 'all', +# or retreive the value of a specific key +# Run the script without --what to get a list of available keys + +UserParameter=db.mysql[*],sudo /var/lib/zabbix/bin/check_mysql_sudo --host '$1' --port '$2' --user '$3' --password '$4' --defaults='$5' --what '$6' + diff --git a/zabbix_scripts/check_mysql b/zabbix_scripts/check_mysql new file mode 100644 index 0000000..2834120 --- /dev/null +++ b/zabbix_scripts/check_mysql @@ -0,0 +1,101 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use JSON; +use Data::Dumper; +use Getopt::Long; + +my $what = 'all'; +my $defaults = undef; +my $host = undef; +my $port = undef; +my $user = undef; +my $password = undef; +my $help = 0; +my $pretty = 0; +my $exit = 0; + +my $json = { + zbx_error => "none" +}; + +GetOptions( + "what=s" => \$what, + "help" => \$help, + "defaults=s" => \$defaults, + "host=s" => \$host, + "port=s" => \$port, + "user=s" => \$user, + "password=s" => \$password, + "pretty" => \$pretty +); + +# Basic input checks +if (defined $defaults and $defaults ne '' and not -e $defaults){ + $json->{zbx_error} = "File $defaults doesn't exist"; + $exit = 1; +} elsif (defined $host and $host ne '' and $host !~ m/^[\w\-\.]+$/){ + $json->{zbx_error} = "Bad value for --host"; + $exit = 1; +} elsif (defined $port and $port ne '' and ($port !~ m/^\d+$/ or $port lt 1 or $port gt 65535)){ + $json->{zbx_error} = "Bad value for --port"; + $exit = 1; +} elsif (defined $user and $user ne '' and $user !~ m/^[\w\-\.]+$/){ + $json->{zbx_error} = "Bad value for --user"; + $exit = 1; +} elsif (defined $password and $password ne '') { + # Just escape quotes as will protect the password with + $password =~ s/'/\\'/g; +} + +if ($help){ + print <<_EOF; +Usage: $0 [--what=key] [--help] [--pretty] + + * --what : if a key is given (eg --what=Bytes_received) will print only this value. + Else, all the stats are printed in a json format. + Run once without --what to get a list of available keys + + * --help : print this help and exit + + * --defaults : set the file from which mysql will read defaults + + * --host : set the hostname to connect to + + * --user : set the user to connect as + + * --password : set the password to use + + * --pretty : prints JSON in a pretty, human readable format. Has no use when --what is also given +_EOF + exit 0; +} + +if ($exit eq 0){ + my $opt = ""; + $opt .= " --defaults-file=$defaults" if (defined $defaults and $defaults ne ''); + $opt .= " --host=$host" if (defined $host and $host ne ''); + $opt .= " --user=$user" if (defined $user and $user ne ''); + $opt .= " --password='$password'" if (defined $password and $password ne ''); + my @status = qx(mysql $opt --batch --execute 'show global status;' 2>&1); + if ($? != 0){ + $exit = $?; + $json->{zbx_error} = join '', @status; + } else { + foreach (@status){ + chomp; + my ($key, $val) = split(/\t/, $_); + $json->{$key} = $val; + } + } +} + +if ($what ne 'all' and defined $json->{$what}){ + print $json->{$what} . "\n"; +} else { + print to_json($json, { pretty => $pretty }); +} + +exit $exit; + diff --git a/zabbix_templates/Template_App_MySQL.xml b/zabbix_templates/Template_App_MySQL.xml new file mode 100644 index 0000000..f863968 --- /dev/null +++ b/zabbix_templates/Template_App_MySQL.xml @@ -0,0 +1,1079 @@ + + + 5.0 + 2020-08-31T13:00:19Z + + + Templates + + + + + + + + MySQL : CPU Time + + + 1 + GRADIENT_LINE + F44336 + + Template_App_MySQL + db.mysql[Cpu_time] + + + + + + MySQL : InnoDB buffer pool + + + 1 + GRADIENT_LINE + EF5350 + + Template_App_MySQL + db.mysql[Innodb_buffer_pool_bytes_dirty] + + + + 2 + GRADIENT_LINE + FFCDD2 + + Template_App_MySQL + db.mysql[Innodb_buffer_pool_bytes_data] + + + + + + MySQL : Network + + + 1 + GRADIENT_LINE + 1A7C11 + + Template_App_MySQL + db.mysql[Bytes_sent] + + + + 2 + GRADIENT_LINE + F63100 + + Template_App_MySQL + db.mysql[Bytes_received] + + + + + + MySQL : Queries + STACKED + + + 1 + F63100 + + Template_App_MySQL + db.mysql[Com_prepare_sql] + + + + 2 + 1A7C11 + + Template_App_MySQL + db.mysql[Com_delete] + + + + 3 + C5E1A5 + + Template_App_MySQL + db.mysql[Com_select] + + + + 4 + FFE082 + + Template_App_MySQL + db.mysql[Com_update] + + + + 5 + BBDEFB + + Template_App_MySQL + db.mysql[Com_insert] + + + + 6 + 6C59DC + + Template_App_MySQL + db.mysql[Com_admin_commands] + + + + + + MySQL : slow and total queries + + + 1 + BOLD_LINE + 1A7C11 + + Template_App_MySQL + db.mysql[Queries] + + + + 2 + BOLD_LINE + F63100 + + Template_App_MySQL + db.mysql[Slow_queries] + + + + + +