diff --git a/CHANGELOG.md b/CHANGELOG.md index c60147cc..66e7da49 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ The **patch** part changes incrmentally at each release. * postfix: add lines in /etc/.gitignore * nagios-nrpe: add "check_open_files" plugin * nagios-nrpe: mark plugins as executable +* mysql-oracle: new role to install MySQL 5.7 with Oracle packages ### Changed * elasticsearch: use ES_TMPDIR variable for custom tmpdir, (from `/etc/default/elasticsearch` instead of changing `/etc/elesticsearch/jvm.options`). diff --git a/mysql-oracle/.kitchen.yml b/mysql-oracle/.kitchen.yml new file mode 100644 index 00000000..b21cc3db --- /dev/null +++ b/mysql-oracle/.kitchen.yml @@ -0,0 +1,28 @@ +--- +driver: + name: docker + privileged: true + use_sudo: false + +provisioner: + name: ansible_playbook + hosts: test-kitchen + roles_path: ../ + ansible_verbose: true + require_ansible_source: false + require_chef_for_busser: false + idempotency_test: true + +platforms: + - name: debian + driver_config: + image: evolix/ansible:2.2.1 + +suites: + - name: default + provisioner: + name: ansible_playbook + playbook: ./tests/test.yml + +transport: + max_ssh_sessions: 6 diff --git a/mysql-oracle/README.md b/mysql-oracle/README.md new file mode 100644 index 00000000..5d09720e --- /dev/null +++ b/mysql-oracle/README.md @@ -0,0 +1,40 @@ +# mysql + +Install MySQL (from Oracle) + +## Tasks + +Tasks are extracted in several files, included in `tasks/main.yml` : + +* `packages.yml` : packages installation ; +* `users.yml` : replacement of `root` user by `mysqladmin` user ; +* `config.yml` : configurations ; +* `datadir.yml` : data directory customization ; +* `tmpdir.yml` : temporary directory customization ; +* `nrpe.yml` : `nrpe` user for Nagios checks ; +* `munin.yml` : Munin plugins ; +* `log2mail.yml` : log2mail patterns ; +* `utils.yml` : useful tools. + +## Available variables + +* `mysql_replace_root_with_mysqladmin`: switch from `root` to `mysqladmin` user or not ; +* `mysql_thread_cache_size`: number of threads for the cache ; +* `mysql_innodb_buffer_pool_size`: amount of RAM dedicated to InnoDB ; +* `mysql_custom_datadir`: custom datadir +* `mysql_custom_tmpdir`: custom tmpdir. +* `general_alert_email`: email address to send various alert messages (default: `root@localhost`). +* `log2mail_alert_email`: email address to send Log2mail messages to (default: `general_alert_email`). +* `general_scripts_dir`: general directory for scripts installation (default: `/usr/local/bin`). +* `mysql_scripts_dir`: email address to send Log2mail messages to (default: `general_scripts_dir`). +* `mysql_force_new_nrpe_password` : change the password for NRPE even if it exists already (default: `False`). + +NB : changing the _datadir_ location can be done multiple times, as long as it is not restored to the default initial location, (because a symlink is created and can't be switched back, yet). + +## Misc + +We use the `mysql-apt-config` package from Oracle to configure APT sources. It is used right from the role since there is no apparent stable URL to download it from Oracle servers. We should verify from time to time if a new version is available to update the .deb in the role. + +The MySQL debian package made by Oracle doesn't work with systemd out of the box. We've used the `mysql-systemd-start` script and the systemd unit made by Debian for the "mysql-5.7" package (currently only available for Sid). + +On Stretch, "mytop" is not packages anymore (only provided by "mariadb-client-10.1"). We're using the version from the "mytop" package (currently only available for Sid). diff --git a/mysql-oracle/defaults/main.yml b/mysql-oracle/defaults/main.yml new file mode 100644 index 00000000..e79fe036 --- /dev/null +++ b/mysql-oracle/defaults/main.yml @@ -0,0 +1,22 @@ +--- +general_alert_email: "root@localhost" +log2mail_alert_email: Null + +general_scripts_dir: "/usr/share/scripts" +mysql_scripts_dir: Null + +mysql_replace_root_with_mysqladmin: True + +mysql_custom_datadir: '' +mysql_custom_tmpdir: '' + +mysql_thread_cache_size: '{{ ansible_processor_cores }}' +mysql_innodb_buffer_pool_size: '{{ (ansible_memtotal_mb * 0.3) | int }}M' + +mysql_cron_optimize: True +mysql_cron_optimize_frequency: weekly + +mysql_cron_mysqltuner: True +mysql_cron_mysqltuner_frequency: monthly + +mysql_force_new_nrpe_password: False diff --git a/mysql-oracle/files/debian/mysql-server-5.7.mysql.service b/mysql-oracle/files/debian/mysql-server-5.7.mysql.service new file mode 100644 index 00000000..258d7f57 --- /dev/null +++ b/mysql-oracle/files/debian/mysql-server-5.7.mysql.service @@ -0,0 +1,22 @@ +# MySQL systemd service file + +[Unit] +Description=MySQL Community Server +After=network.target + +[Install] +WantedBy=multi-user.target + +[Service] +Type=forking +User=mysql +Group=mysql +PIDFile=/run/mysqld/mysqld.pid +PermissionsStartOnly=true +ExecStartPre=/usr/share/mysql/mysql-systemd-start pre +ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid +TimeoutSec=600 +Restart=on-failure +RuntimeDirectory=mysqld +RuntimeDirectoryMode=755 +LimitNOFILE=5000 diff --git a/mysql-oracle/files/debian/mysql-systemd-start b/mysql-oracle/files/debian/mysql-systemd-start new file mode 100755 index 00000000..946efb51 --- /dev/null +++ b/mysql-oracle/files/debian/mysql-systemd-start @@ -0,0 +1,56 @@ +#!/bin/bash +# +# Scripts to run by MySQL systemd service +# +# Needed argument: pre +# +# pre mode : try to perform sanity check for configuration, log, data + +# Read a config option from mysql. Note that this will only work if a config +# file actually specifies the option, so it's important to specify a default +# $1 is application (e.g. mysqld for server) +# $2 is option +# $3 is default value used if no config value is found +get_mysql_option() { + result=$(my_print_defaults "$1" | sed -n "s/^--$2=//p" | tail -n 1) + if [ -z "$result" ]; + then + result="$3" + fi + echo "$result" +} +sanity () { + if [ ! -r /etc/mysql/my.cnf ]; then + echo "MySQL configuration not found at /etc/mysql/my.cnf. Please create one." + exit 1 + fi + datadir=$(get_mysql_option mysqld datadir "/var/lib/mysql") + if [ ! -d "${datadir}" ] && [ ! -L "${datadir}" ]; then + echo "MySQL data dir not found at ${datadir}. Please create one." + exit 1 + fi + + if [ ! -d "${datadir}/mysql" ] && [ ! -L "${datadir}/mysql" ]; then + echo "MySQL system database not found in ${datadir}. Please run mysqld --initialize." + exit 1 + fi + + # Do a test start to make sure there are no configuration issues or similar + # mysqld --verbose --help will output a full listing of settings and plugins. + # To do so it needs to initialize the database, so it can be used as a test + # for whether or not the server can start. We redirect stdout to /dev/null so + # only the error messages are left. + result=0 + output=$(mysqld --verbose --help --innodb-read-only 2>&1 > /dev/null) || result=$? + if [ ! "$result" = "0" ]; then + echo "ERROR: Unable to start MySQL server:" >&2 + echo "$output" >&2 + echo "Please take a look at https://wiki.debian.org/Teams/MySQL/FAQ for tips on fixing common upgrade issues." >&2 + echo "Once the problem is resolved, restart the service." >&2 + exit 1 + fi +} + +case $1 in + "pre") sanity ;; +esac diff --git a/mysql-oracle/files/evolinux-defaults.cnf b/mysql-oracle/files/evolinux-defaults.cnf new file mode 100644 index 00000000..395ccac4 --- /dev/null +++ b/mysql-oracle/files/evolinux-defaults.cnf @@ -0,0 +1,62 @@ +[mysqld] + +###### Connexions +# Maximum de connexions concurrentes (defaut = 100)... provoque un "Too many connections" +max_connections = 250 +# Maximum de connexions en attente en cas de max_connections atteint (defaut = 50) +back_log = 100 +# Maximum d'erreurs avant de blacklister un hote +max_connect_errors = 10 +# Loguer les requetes trop longues +slow_query_log = 1 +slow_query_log_file = /var/log/mysql/mysql-slow.log +long_query_time = 10 + +###### Tailles +# Taille reservee au buffer des index MyIsam +# A ajuster selon les resultats +key_buffer_size = 512M +# Taille max des paquets envoyés/reçus … provoque un "Packet too large" +max_allowed_packet = 64M +# Taille de la memoire reserve pour un thread +thread_stack = 192K +# A mettre le nombre de threads CPU alloues pour MySQL +thread_cache_size = 1 +# Taille maximum des tables de type MEMORY +max_heap_table_size = 64M + +###### Cache +# max_connections x nbre max de tables dans une jointure (defaut = 64) +table_open_cache = 4096 +table_definition_cache = 4096 +# Taille max des requetes cachees (defaut = 1M) +query_cache_limit = 8M +# Taille reservee pour le cache (defaut = 0) +query_cache_size = 256M +# Type de requetes a cacher (defaut = 1 : tout peut etre cache) +query_cache_type = 1 +# Cache tables +max_heap_table_size = 128M +tmp_table_size = 128M + +###### InnoDB +# Si InnoDB n'est pas utilise... le desactiver +#skip-innodb +# En general, il est plus optimum d'avoir un fichier par table +innodb_file_per_table +# Taille memoire allouee pour le cache des datas et index +# A ajuster en fonction de sa RAM (si serveur dedie a MySQL, on peut aller jusqu'a 80%) +innodb_buffer_pool_size = 512M +# Nombre maximum de threads systeme concurents +innodb_thread_concurrency = 16 +# Ajuste la valeur des logs InnoDB +# (attention, il faut ensuite stopper MySQL et effacer les fichiers ib_logfile*) +#innodb_log_file_size = 128M +#innodb_log_files_in_group = 2 + +###### Misc +# charset utf8 par defaut +character-set-server=utf8 +collation-server=utf8_general_ci +# Patch MySQL 5.5.53 +secure-file-priv = "" diff --git a/mysql-oracle/files/my-add.sh b/mysql-oracle/files/my-add.sh new file mode 100755 index 00000000..b649a209 --- /dev/null +++ b/mysql-oracle/files/my-add.sh @@ -0,0 +1,160 @@ +#!/bin/sh + +usage() { + echo "Usage: $0 [ -d -u [-p ] [-f] ]" +} + +interactive() { + echo "Add an account / database in MySQL" + echo "Enter the name of the new database" + read db + + if ( is_db $db ); then + echo "Database $db already exist !" >&2 + exit 1 + fi + + echo "Enter account with all right on this new database" + echo "(you can use existant account)" + read user + + if ( is_user $user ); then + echo "Warning, account already exists, update password ? [N/y]" + read confirm + if [ "${confirm}" = "y" ] || [ "${confirm}" = "Y" ]; then + echo -n "Enter new password for existant MySQL account (empty for random): " + read password + if [ -z "${password}" ]; then + password=$(apg -n1) + fi + fi + else + echo -n "Enter new password for new MySQL account (empty for random): " + read password + if [ -z "${password}" ]; then + password=$(apg -n1) + fi + fi + mysql_add $db $user $password +} + +cli() { + while getopts ":d:u:p:f" opt; do + case "$opt" in + d) + db=$OPTARG + ;; + u) + user=$OPTARG + ;; + p) + password=$OPTARG + ;; + f) + force="true" + ;; + *) + usage + exit 1 + ;; + esac + done + shift $((OPTIND-1)) + + if [ -z "${db}" ]; then + usage + exit 1 + fi + + if [ -z "${user}" ]; then + usage + exit 1 + fi + + if ( is_db $db ); then + echo "Database $db already exist !" >&2 + exit 1 + fi + + if ( is_user $user ); then + if [ -z "${force}" ]; then + if [ -n "${password}" ]; then + echo "User $user already exist, update password with -f !" >&2 + exit 1 + fi + else + if [ -z "${password}" ]; then + password=$(apg -n1) + fi + fi + else + if [ -z "${password}" ]; then + password=$(apg -n1) + fi + fi + mysql_add $db $user $password >/dev/null +} + +is_db() { + db=$1 + mysql mysql -Ne "SHOW DATABASES;"|grep -q "^${db}$" + exit $? +} + +is_user() { + user=$1 + nb_user=$(mysql mysql -Ne "SELECT COUNT(User) from user WHERE User='${user}';") + if [ $nb_user -gt 0 ]; then + exit 0 + else + exit 1 + fi +} + +mysql_add() { + db=$1 + user=$2 + password=$3 + + echo -n "Create '${db}' database ..." + mysql -e "CREATE DATABASE ${db};" + if [ $? -eq 0 ]; then + echo "OK" + else + echo "KO" + exit 1 + fi + if [ -z "${password}" ]; then + password=$(mysql mysql -Ne "SELECT Password FROM user WHERE User='${user}' AND Host='localhost';") + echo -n "Grant '${user}' to '${db}' database ..." + mysql -e "GRANT ALL PRIVILEGES ON ${db}.* TO ${user}@localhost IDENTIFIED BY PASSWORD '${password}';" + grant=$? + else + echo -n "Grant '${user}' to '${db}' database with password '${password}' ..." + mysql -e "GRANT ALL PRIVILEGES ON ${db}.* TO ${user}@localhost IDENTIFIED BY '${password}';" + grant=$? + fi + if [ $grant -eq 0 ]; then + echo "OK" + else + echo "KO" + exit 1 + fi + echo -n "Flush Mysql privileges ..." + mysql -e "FLUSH PRIVILEGES;" + if [ $? -eq 0 ]; then + echo "OK" + else + echo "KO" + exit 1 + fi +} + +main() { + if [ $# = 0 ]; then + interactive + else + cli $@ + fi +} +main $@ diff --git a/mysql-oracle/files/mysql-apt-config_0.8.9-1_all.deb b/mysql-oracle/files/mysql-apt-config_0.8.9-1_all.deb new file mode 100644 index 00000000..f9cedd31 Binary files /dev/null and b/mysql-oracle/files/mysql-apt-config_0.8.9-1_all.deb differ diff --git a/mysql-oracle/files/mysql-optimize.sh b/mysql-oracle/files/mysql-optimize.sh new file mode 100644 index 00000000..0c89d74e --- /dev/null +++ b/mysql-oracle/files/mysql-optimize.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +# Do some weekly optimizations. +# Reset the cache to avoid fragmentation. +mysql --defaults-extra-file=/etc/mysql/debian.cnf -e "RESET QUERY CACHE;" diff --git a/mysql-oracle/files/mysqltuner.cron.sh b/mysql-oracle/files/mysqltuner.cron.sh new file mode 100644 index 00000000..5424aa90 --- /dev/null +++ b/mysql-oracle/files/mysqltuner.cron.sh @@ -0,0 +1,50 @@ +#!/bin/bash +set -e +export TERM=screen + +mem=$(free -m | grep Mem: | tr -s ' ' | cut -d ' ' -f2) +swap=$(free -m | grep Swap: | tr -s ' ' | cut -d ' ' -f2) +template=$(mktemp --tmpdir=/tmp evomysqltuner.XXX) +body=$(mktemp --tmpdir=/tmp evomysqltuner.XXX) +clientmail=$(grep EVOMAINTMAIL /etc/evomaintenance.cf | cut -d'=' -f2) +hostname=$(grep HOSTNAME /etc/evomaintenance.cf | cut -d'=' -f2) +hostname=${hostname%%.evolix.net} +# If hostname is composed with -, remove the first part. +if [[ $hostname =~ "-" ]]; then + hostname=$(echo $hostname | cut -d'-' -f2-) +fi + +# Remove temporary files on exit. +trap "rm $template $body" EXIT + +# Add port here if you have more than one instance! +instances="3306" +for instance in $instances; do + mysqltuner --port $instance --host 127.0.0.1 --forcemem $mem --forceswap $swap \ + | aha > /var/www/mysqlreport_${instance}.html + cat << EOT > $template +Content-Type: text/plain; charset="utf-8" +Reply-To: Équipe Evolix +From: Équipe Evolix +To: $clientmail +Subject: Rapport MySQL instance $instance pour votre serveur $hostname +EOT + cat << EOT > $body +Bonjour, + +Veuillez trouver ci-joint un rapport MySQL. +Celui-ci permet d'identifier aisément si des optimisations MySQL sont possibles. + +N'hésitez pas à nous indiquer par mail ou ticket quelles variables vous souhaiter +optimiser. + +Veuillez noter qu'il faudra redémarrer MySQL pour appliquer de nouveaux paramètres. + +Bien à vous, +-- +Rapport automatique Evolix +EOT + mutt -x -e 'set send_charset="utf-8"' -H $template \ + -a /var/www/mysqlreport_${instance}.html < $body +done +chmod 644 /var/www/mysqlreport*html diff --git a/mysql-oracle/files/mytop b/mysql-oracle/files/mytop new file mode 100755 index 00000000..1dd8820b --- /dev/null +++ b/mysql-oracle/files/mytop @@ -0,0 +1,2328 @@ +#!/usr/bin/perl -w +# +# $Id: mytop,v 1.91 2012/01/18 16:49:12 mgrennan Exp $ + +=pod + +=head1 NAME + +mytop - display MySQL server performance info like `top' + +=cut + +## most of the POD is at the bottom of the file + +use 5.005; +use strict; +use DBI; +use Getopt::Long; +use DBD::mysql; +use Socket; + +$main::VERSION = "1.91"; + +$|=1; +$0 = 'mytop'; + +my $WIN = ($^O eq 'MSWin32') ? 1 : 0; + +## Test for color support. + +eval { require Term::ANSIColor; }; + +my $HAS_COLOR = $@ ? 0 : 1; + +$HAS_COLOR = 0 if $WIN; + +## Test of Time::HiRes support + +eval { require Time::HiRes }; + +my $HAS_TIME = $@ ? 0 : 1; + +my $debug = 0; + +## Try to lower our priority (which, who, pri) + +setpriority(0,0,10) unless $WIN; + +## Prototypes + +sub Clear(); +sub GetData(); +sub GetQPS(); +sub FullQueryInfo($); +sub Explain($); +sub PrintTable(@); +sub PrintHelp(); +sub Sum(@); +sub commify($); +sub make_short($); +sub Hashes($); +sub Execute($); +sub StringOrRegex($); +sub GetInnoDBStatus(); +sub GetCmdSummary(); +sub GetShowVariables(); +sub GetShowStatus(); +sub cmd_s; +sub cmd_S; +sub cmd_q; +sub FindProg($); + +## Default Config Values + +my %config = ( + batchmode => 0, + color => 1, + db => 'test', + delay => 5, + filter_user => qr/.?/, + filter_db => qr/.?/, + filter_host => qr/.?/, + filter_state => qr/.?/, + header => 1, + help => 0, + host => 'localhost', + idle => 1, + long => 120, + long_nums => 0, + mode => 'top', + prompt => 0, + pass => '', + port => 3306, + resolve => 0, + slow => 10, # slow query time + socket => '', + sort => 1, # default or reverse sort ("s") + user => 'root' +); + +my %qcache = (); ## The query cache--used for full query info support. +my %ucache = (); ## The user cache--used for full killing by user +my %dbcache = (); ## The db cache. This should be merged at some point. +my %statcache = (); ## The show status cache for GetShowStatus() + +my (%STATUS, %OLD_STATUS); # header stuff. + +my $CLEAR = $WIN ? '': `clear`; + +## Term::ReadKey values + +my $RM_RESET = 0; +my $RM_NOBLKRD = 3; ## using 4 traps Ctrl-C :-( + +## Read the user's config file, if it exists. + +my $config = "$ENV{HOME}/.mytop"; + +if (-e $config) +{ + if (open CFG, "<$config") + { + while () + { + next if /^\s*$/; ## skip blanks + next if /^\s*#/; ## skip comments + + chomp; + + if (/(\S+)\s*=\s*(.*\S)/) + { + $config{lc $1} = $2 if exists $config{lc $1}; + } + } + close CFG; + } +} + +## Command-line args. + +use vars qw($opt_foo); + +Getopt::Long::Configure('no_ignore_case', 'bundling'); + +GetOptions( + "color!" => \$config{color}, + "user|u=s" => \$config{user}, + "pass|password|p=s" => \$config{pass}, + "database|db|d=s" => \$config{db}, + "host|h=s" => \$config{host}, + "port|P=i" => \$config{port}, + "socket|S=s" => \$config{socket}, + "delay|s=i" => \$config{delay}, + "batch|batchmode|b" => \$config{batchmode}, + "header!" => \$config{header}, + "idle|i!" => \$config{idle}, + "resolve|r!" => \$config{resolve}, + "prompt!" => \$config{prompt}, + "long=i" => \$config{long}, + "long_nums!" => \$config{long_nums}, + "mode|m=s" => \$config{mode}, + "slow=i" => \$config{slow}, + "sort=s" => \$config{sort} +); + +## User may have put the port with the host. + +if ($config{host} =~ s/:(\d+)$//) +{ + $config{port} = $1; +} + +## Don't use Term::ReadKey unless running interactively. + +if (not $config{batchmode}) +{ + require Term::ReadKey; + Term::ReadKey->import(); +} + +## User may want to disable color. + +if ($HAS_COLOR and not $config{color}) +{ + $HAS_COLOR = 0; +} + +if ($HAS_COLOR) +{ + import Term::ANSIColor ':constants'; +} +else +{ + *RESET = sub { }; + *YELLOW = sub { }; + *RED = sub { }; + *MAGENTA = sub { }; + *GREEN = sub { }; + *BLUE = sub { }; + *WHITE = sub { }; + *BOLD = sub { }; +} + +my $RESET = RESET() || ''; +my $YELLOW = YELLOW() || ''; +my $RED = RED() || ''; +my $MAGENTA = MAGENTA() || ''; +my $GREEN = GREEN() || ''; +my $BLUE = BLUE() || ''; +my $WHITE = WHITE() || ''; +my $BOLD = BOLD() || ''; + +## Connect + +my $dsn; + +## Socket takes precedence. + +$dsn ="DBI:mysql:database=$config{db};mysql_read_default_group=mytop;"; + +if ($config{socket} and -S $config{socket}) +{ + $dsn .= "mysql_socket=$config{socket}"; +} +else +{ + $dsn .= "host=$config{host};port=$config{port}"; +} + +if ($config{prompt}) +{ + print "Password: "; + ReadMode(2); + chomp($config{pass} = ); + ReadMode(0); + print "\n"; +} + +my $dbh = DBI->connect($dsn, $config{user}, $config{pass}, + { PrintError => 0 }); + +if (not ref $dbh) +{ + my $Error = <{Variable_name} eq "version") + { + $db_version = $_->{Value}; + $db_release = int(sprintf("%d",$db_version)); + next; + } + if ($_->{Variable_name} eq "have_query_cache") + { +# if ($_->{Value} eq 'YES') + if ($_->{Value} eq 'YES' or $_->{Value} eq 'DEMAND') # http://freshmeat.net/users/jerjones + { + $have_query_cache = 1; + } + else + { + $have_query_cache = 0; + } + next; + } +} + +######################################################################### +## +## The main loop +## +######################################################################### + +ReadMode($RM_NOBLKRD) unless $config{batchmode}; + +while (1) +{ + my $key; + + if ($config{mode} eq 'qps') + { + GetQPS(); + $key = ReadKey(1); + + next unless $key; + + if ($key =~ /t/i) + { + $config{mode} = 'top'; + } + if ($key =~ /q/) + { + cmd_q(); + } + next; + } + if ($config{mode} eq 'top') + { + GetData(); + last if $config{batchmode}; + $key = ReadKey($config{delay}); + next unless $key; + } + elsif ($config{mode} eq 'cmd') + { + GetCmdSummary(); + last if $config{batchmode}; + $key = ReadKey($config{delay}); + next unless $key; + } + elsif ($config{mode} eq 'innodb') + { + GetInnoDBStatus(); + last if $config{batchmode}; + $key = ReadKey($config{delay}); + next unless $key; + } + elsif ($config{mode} eq 'status') + { + GetShowStatus(); + last if $config{batchmode}; + $key = ReadKey($config{delay}); + next unless $key; + } + + ## + ## keystroke command processing (if we get this far) + ## + + if ($key eq '!') + { + Execute("stop slave"); + Execute("set global sql_slave_skip_counter=1"); + Execute("start slave"); + } + + # t - top + + if ($key =~ /t/i) + { + $config{mode} = 'top'; + } + + ## q - quit + + if ($key eq 'q') + { + cmd_q(); + } + + if ($key eq 'D') + { + require Data::Dumper; + print Data::Dumper::Dumper([\%config]); + ReadKey(0); + } + + ## l - change long running hightling + + if ($key eq 'l') + { + cmd_l(); + next; + } + + ## m - mode swtich to qps + + if ($key eq 'm') + { + $config{mode} = 'qps'; + Clear() unless $config{batchmode}; + print "Queries Per Second [hit q to exit this mode]\n"; + next; + } + + ## c - mode swtich to command summary + + if ($key eq 'c') + { + $config{mode} = 'cmd'; + Clear() unless $config{batchmode}; + print "Command Summary [hit q to exit this mode]\n"; + next; + } + + ## C - change Color on and off + + if ($key eq 'C') + { + if ( $HAS_COLOR ) + { + $HAS_COLOR = 0; + } + else + { + $HAS_COLOR = 1; + } + } + + ## s - seconds of delay + + if ($key eq 's') + { + cmd_s(); + next; + } + + if ($key eq 'S') + { + cmd_S(); + next; + } + + ## R - resolve hostnames + if ($key eq 'R') + { + if ($config{resolve}) + { + $config{resolve} = 0; + } + else + { + $config{resolve} = 1; + } + } + + ## t - username based filter + + if ($key eq 't') + { + ReadMode($RM_RESET); + print RED(), "Which state (blank for all, /.../ for regex): ", RESET(); + $config{filter_state} = StringOrRegex(ReadLine(0)); + ReadMode($RM_NOBLKRD); + next; + } + + ## u - username based filter + + if ($key eq 'u') + { + ReadMode($RM_RESET); + print RED(), "Which user (blank for all, /.../ for regex): ", RESET(); + $config{filter_user} = StringOrRegex(ReadLine(0)); + ReadMode($RM_NOBLKRD); + next; + } + + ## d - database name based filter + + if ($key eq 'd') + { + ReadMode($RM_RESET); + print RED(), "Which database (blank for all, /.../ for regex): ", + RESET(); + $config{filter_db} = StringOrRegex(ReadLine(0)); + ReadMode($RM_NOBLKRD); + next; + } + + ## h - hostname based filter + + if ($key eq 'h') + { + ReadMode($RM_RESET); + print RED(), "Which hostname (blank for all, /.../ for regex): ", + RESET(); + $config{filter_host} = StringOrRegex(ReadLine(0)); + ReadMode($RM_NOBLKRD); + next; + } + + ## E - Show full Replication Error + + if ($key eq 'E') + { + my($data) = Hashes('SHOW SLAVE STATUS'); + Clear(); + print "Error is: $data->{Last_Error}\n"; + print RED(), "-- paused. press any key to resume --", RESET(); + ReadKey(0); + next; + } + ## F - remove all filters + + if ($key eq 'F') + { + $config{filter_host} = qr/.?/; + $config{filter_db} = qr/.?/; + $config{filter_user} = qr/.?/; + $config{filter_state} = qr/.?/; + print RED(), "-- display unfiltered --", RESET(); + sleep 1; + next; + } + + ## p - pause + + if ($key eq 'p') + { + print RED(), "-- paused. press any key to resume --", RESET(); + ReadKey(0); + next; + } + + ## i - idle toggle + + if ($key =~ /i/) + { + if ($config{idle}) + { + $config{idle} = 0; + $config{sort} = 1; + print RED(), "-- idle (sleeping) processed filtered --", RESET(); + sleep 1; + } + else + { + $config{idle} = 1; + $config{sort} = 0; + print RED(), "-- idle (sleeping) processed unfiltered --", RESET(); + sleep 1; + } + } + + ## I - InnoDB status + + if ($key =~ 'I') + { + $config{mode} = 'innodb'; + Clear() unless $config{batchmode}; + print "InnoDB Status [hit q to exit this mode]\n"; + next; + } + + ## o - sort order + + if ($key =~ /o/) + { + if ($config{sort}) + { + $config{sort} = 0; + print RED(), "-- sort order reversed --", RESET(); + sleep 1; + } + else + { + $config{sort} = 1; + print RED(), "-- sort order reversed --", RESET(); + sleep 1; + } + } + + ## ? - help + + if ($key eq '?') + { + Clear(); + PrintHelp(); + ReadKey(0); + next; + } + + ## k - kill + + if ($key eq 'k') + { + ReadMode($RM_RESET); + + print RED(), "Thread id to kill: ", RESET(); + my $id = ReadLine(0); + + $id =~ s/\s//g; + + if ($id =~ /^\d+$/) + { + Execute("KILL $id"); + } + else + { + print RED(), "-- invalid thread id --", RESET(); + sleep 1; + } + + ReadMode($RM_NOBLKRD); + next; + } + + ## K - kill based on a username + if ($key =~ /K/) + { + ReadMode($RM_RESET); + + print RED(), "User to kill: ", RESET(); + my $user = ReadLine(0); + + $user =~ s/\s//g; + + if ($user =~ /^\S+$/) + { + for my $pid (keys %ucache) + { + next unless $ucache{$pid} eq $user; + Execute("KILL $pid"); + select(undef, undef, undef, 0.2); + } + } + else + { + print RED(), "-- invalid thread id --", RESET(); + sleep 1; + } + + ReadMode($RM_NOBLKRD); + } + + ## f - full info + + if ($key =~ /f/) + { + ReadMode($RM_RESET); + print RED(), "Full query for which thread id: ", RESET(); + my $id = ReadLine(0); + chomp $id; + FullQueryInfo($id); + ReadMode($RM_NOBLKRD); + print RED(), "-- paused. press any key to resume or (e) to explain --", + RESET(); + my $key = ReadKey(0); + + if ($key eq 'e') + { + Explain($id); + print RED(), "-- paused. press any key to resume --", RESET(); + ReadKey(0); + } + + next; + } + + ## e - explain + + if ($key =~ /e/) + { + ReadMode($RM_RESET); + print RED(), "Explain which query (id): ", RESET(); + my $id = ReadLine(0); + chomp $id; + Explain($id); + ReadMode($RM_NOBLKRD); + print RED(), "-- paused. press any key to resume --", RESET(); + ReadKey(0); + next; + } + + ## r - reset status counters + + if ($key =~ /r/) + { + Execute("FLUSH STATUS"); + print RED(), "-- counters reset --", RESET(); + sleep 1; + next; + } + + ## H - header toggle + + if ($key eq 'H') + { + if ($config{header}) + { + $config{header} = 0; + } + else + { + $config{header}++; + } + } + + ## # - magic debug key + + if ($key eq '#') + { + $debug = 1; + } + + if ($key eq 'V') + { + GetShowVariables(); + print RED(), "-- paused. press any key to resume --", RESET(); + ReadKey(0); + } + + if ($key eq 'S') + { + $config{mode} = 'status'; + } +} + +ReadMode($RM_RESET) unless $config{batchmode}; + +exit; + +####################################################################### + +sub Clear() +{ + if (not $WIN) + { + print "$CLEAR" + } + else + { + print "\n" x 90; ## dumb hack for now. Anyone know how to + ## clear the screen in dos window on a Win32 + ## system?? + } +} + +my $last_time; + +sub GetData() +{ + ## Get terminal info + my $now_time; + %qcache = (); ## recycle memory + %dbcache = (); + + my ($width, $height, $wpx, $hpx, $lines_left); + + if (not $config{batchmode}) + { + ($width, $height, $wpx, $hpx) = GetTerminalSize(); + $lines_left = $height - 2; + } + else + { + $height = 999_999; ## I hope you don't have more than that! + $lines_left = 999_999; + $width = 80; + } + + ## + ## Header stuff. + ## + if ($config{header}) + { + my @recs = ""; + if ( $db_release > 4 ) + { + @recs = Hashes("show global status"); + } + else + { + @recs = Hashes("show status"); + } + + ## if the server died or we lost connectivity + if (not @recs) + { + ReadMode($RM_RESET); + exit 1; + } + + ## get high-res or low-res time + my ($t_delta); + + if ($HAS_TIME) + { + $now_time = Time::HiRes::gettimeofday(); + } + else + { + $now_time = time; + } + + if ($last_time and $last_time != $now_time) + { + $t_delta = $now_time - $last_time; + } + + %OLD_STATUS = %STATUS; + + foreach my $ref (@recs) + { + my $key = $ref->{Variable_name}; + my $val = $ref->{Value}; + + $STATUS{$key} = $val; + } + + ## Compute Key Cache Hit Stats + + $STATUS{Key_read_requests} ||= 1; ## can't divide by zero next + + my $cache_hits_percent = (100-($STATUS{Key_reads}/$STATUS{Key_read_requests}) * 100); + $cache_hits_percent = sprintf("%2.2f",$cache_hits_percent); + + ## Query Cache info for <= Ver. 4.1 + ## + ## mysql> show status like 'qcache%'; + ## +-------------------------+----------+ + ## | Variable_name | Value | + ## +-------------------------+----------+ + ## | Qcache_queries_in_cache | 81 | + ## | Qcache_inserts | 4961668 | + ## | Qcache_hits | 1374170 | + ## | Qcache_not_cached | 5656249 | + ## | Qcache_free_memory | 33164800 | + ## | Qcache_free_blocks | 2 | + ## | Qcache_total_blocks | 168 | + ## +-------------------------+----------+ + ## + ## Query Cache info for => Ver. 5.0 + ## + ## mysql> show status like 'qcache%'; + ## +-------------------------+------------+ + ## | Variable_name | Value | + ## +-------------------------+------------+ + ## | Qcache_free_blocks | 37652 | + ## | Qcache_free_memory | 110289712 | + ## | Qcache_hits | 1460617356 | + ## | Qcache_inserts | 390563495 | + ## | Qcache_lowmem_prunes | 6414172 | + ## | Qcache_not_cached | 93002420 | + ## | Qcache_queries_in_cache | 66558 | + ## | Qcache_total_blocks | 192031 | + ## +-------------------------+------------+ + + my $query_cache_hits = 0; + my $query_cache_hits_per_sec = 0; + my $now_query_cache_hits_per_sec = 0; + + if ($have_query_cache) + { + $query_cache_hits = $STATUS{Qcache_hits}; + $query_cache_hits_per_sec = $STATUS{Qcache_hits} / $STATUS{Uptime}; + + if (defined $last_time and $last_time != $now_time) + { + my $q_delta = $STATUS{Qcache_hits} - $OLD_STATUS{Qcache_hits}; + $now_query_cache_hits_per_sec = sprintf "%.2f", $q_delta / $t_delta; + } + } + + open L, "; + close L; + chomp $l; + + $last_time = $now_time; + + ## Server Uptime in meaningful terms... + + my $time = $STATUS{Uptime}; + my ($d,$h,$m,$s) = (0, 0, 0, 0); + + $d += int($time / (60*60*24)); $time -= $d * (60*60*24); + $h += int($time / (60*60)); $time -= $h * (60*60); + $m += int($time / (60)); $time -= $m * (60); + $s += int($time); + + my $uptime = sprintf("%d+%02d:%02d:%02d", $d, $h, $m, $s); + + ## Queries per second... + + my $avg_queries_per_sec = sprintf("%.2f", $STATUS{Questions} / $STATUS{Uptime}); + my $num_queries = $STATUS{Questions}; + + my @t = localtime(time); + + my $current_time = sprintf "[%02d:%02d:%02d]", $t[2], $t[1], $t[0]; + + my $host_width = 75; + my $up_width = $width - $host_width; + + Clear() unless $config{batchmode}; + print RESET(); + + printf "%-${host_width}s%${up_width}s\n", + "MySQL on $config{host} ($db_version)", + "load $l up $uptime $current_time"; + $lines_left--; + + + printf " Queries: %-6s qps: %4.0f Slow: %7s Se/In/Up/De(%%): %02.0f/%02.0f/%02.0f/%02.0f \n", + make_short( $STATUS{Questions} ), # q total + $STATUS{Questions} / $STATUS{Uptime}, # qps, average + make_short( $STATUS{Slow_queries} ), # slow + + # hmm. a Qcache hit is really a select and should be counted. + 100 * ($STATUS{Com_select} + ($STATUS{Qcache_hits}||0) ) / $STATUS{Questions}, + 100 * ($STATUS{Com_insert} + $STATUS{Com_replace} ) / $STATUS{Questions}, + 100 * ($STATUS{Com_update} ) / $STATUS{Questions}, + 100 * $STATUS{Com_delete} / $STATUS{Questions}; + + $lines_left--; + + if ($t_delta) + { + my $q_diff = ( $STATUS{Questions} - $OLD_STATUS{Questions} ); +# print("q_diff: $STATUS{Questions} - $OLD_STATUS{Questions} / $t_delta = $q_diff\n"); + + printf(" Sorts: %5.0f qps now: %4.0f Slow qps: %3.1f Threads: %4.0f (%4.0f/%4.0f) %02.0f/%02.0f/%02.0f/%02.0f \n", + ( $STATUS{Sort_rows} - $OLD_STATUS{Sort_rows} ) / $t_delta, + ( $STATUS{Questions} - $OLD_STATUS{Questions} ) / $t_delta, + ( # slow now (qps) + ($STATUS{Slow_queries} ) ? + ( $STATUS{Slow_queries} - $OLD_STATUS{Slow_queries} ) / $t_delta : + 0 + ), + $STATUS{Threads_connected}, + $STATUS{Threads_running}, + $STATUS{Threads_cached}, + + (100 * ($STATUS{Com_select} - $OLD_STATUS{Com_select} + + ($STATUS{Qcache_hits}||0) - ($OLD_STATUS{Qcache_hits}||0) + ) ) / ($q_diff ), + (100 * ($STATUS{Com_insert} - $OLD_STATUS{Com_insert} + + $STATUS{Com_replace} - $OLD_STATUS{Com_replace} + ) ) / ($q_diff ), + (100 * ($STATUS{Com_update} - $OLD_STATUS{Com_update}) ) / ($q_diff ), + (100 * ($STATUS{Com_delete} - $OLD_STATUS{Com_delete}) ) / ($q_diff ), + ); + } + else + { + print "\n"; + } + $lines_left--; + + if ($have_query_cache and $STATUS{Com_select} and $query_cache_hits) + { + printf(" Cache Hits: %-5s Hits/s: %4.1f Hits now: %5.1f Ratio: ", + make_short($STATUS{Qcache_hits}), # cache hits + $STATUS{Qcache_hits} / $STATUS{Uptime}, # hits / sec + ($t_delta) ? ($STATUS{Qcache_hits} - $OLD_STATUS{Qcache_hits}) / $t_delta : 0, # Hits Now + ); + + my($Ratio) = 100 * ($STATUS{Qcache_hits}) / ($STATUS{Qcache_hits} + $STATUS{Com_select} ); + if ($HAS_COLOR) + { + print YELLOW() if ($Ratio < 80.0); + print RED() if ($Ratio < 50.0); + print MAGENTA() if ($Ratio < 20.0); + } + printf("%4.1f%% ",$Ratio); + if ($HAS_COLOR) + { + print RESET(); + } + + print " Ratio now: "; + my($Ratio_now) = ($t_delta) ? # ratio now + 100 * ($STATUS{Qcache_hits} - $OLD_STATUS{Qcache_hits} ) / + ( ($STATUS{Com_select} + $STATUS{Qcache_hits} - + ($OLD_STATUS{Qcache_hits} + $OLD_STATUS{Com_select}) + ) || 1) : 0; + if ($HAS_COLOR) + { + print GREEN() if ($Ratio_now => 80.0); + print YELLOW() if ($Ratio_now < 80.0); + print RED() if ($Ratio_now < 50.0); + print MAGENTA() if ($Ratio_now < 20.0); + } + printf("%4.1f%% \n",$Ratio_now); + if ($HAS_COLOR) + { + print RESET(); + } + } + $lines_left--; + + + printf(" Key Efficiency: %2.1f%% Bps in/out: %5s/%5s ", + $cache_hits_percent, + make_short($STATUS{Bytes_received} / $STATUS{Uptime} ), + make_short($STATUS{Bytes_sent} / $STATUS{Uptime})); + printf("Now in/out: %5s/%5s", + make_short(($STATUS{Bytes_received} - $OLD_STATUS{Bytes_received}) / $t_delta ), + make_short(($STATUS{Bytes_sent} - $OLD_STATUS{Bytes_sent}) / $t_delta )) + if ($t_delta); + print "\n"; + + $lines_left--; + + my($data) = Hashes('show global variables like "read_only"'); + if ($data->{Value} ne "OFF") + { + print RED() if ($HAS_COLOR) ; + print " ReadOnly"; + RESET() if ($HAS_COLOR); + } + + my($data) = Hashes('SHOW SLAVE STATUS'); + if (defined($data->{Master_Host})) + { + if (defined($data->{Seconds_Behind_Master})) + { + if ($HAS_COLOR) { + print GREEN(); + print YELLOW() if ($data->{Seconds_Behind_Master} > 60); + print MAGENTA() if ($data->{Seconds_Behind_Master} > 360); + } + } + print " Replication "; + print "IO:$data->{Slave_IO_Running} "; + print "SQL:$data->{Slave_SQL_Running} "; + print RESET() if ($HAS_COLOR); + + if (defined($data->{Seconds_Behind_Master})) + { + if ($HAS_COLOR) { + print GREEN(); + print YELLOW() if ($data->{Seconds_Behind_Master} > 60); + print MAGENTA() if ($data->{Seconds_Behind_Master} > 360); + } + print "Delay: $data->{Seconds_Behind_Master} sec."; + } else { + my $free = $width - 45; + my $Err = substr $data->{Last_Error},0 ,$free; + printf(" ERR: %-${free}s", $Err) if ( $Err ne "" ); + } + print WHITE() if ($HAS_COLOR); + print "\n"; + $lines_left--; + } + print "\n"; + } + + if (not $config{batchmode} and not $config{header}) + { + Clear(); + print RESET(); + } + + ## + ## Threads + ## + + #my $sz = $width - 52; + my @sz = (9, 9, 15, 10, 10, 6, 8); + my $used = scalar(@sz) + Sum(@sz); + my $free = $width - $used; + + print BOLD() if ($HAS_COLOR); + + printf "%9s %9s %15s %10s %10s %6s %8s %-${free}s\n", + 'Id','User','Host/IP','DB','Time', 'Cmd', 'State', 'Query'; + + print RESET() if ($HAS_COLOR); + + ## Id User Host DB + printf "%9s %9s %15s %10s %10s %6s %8s %-${free}s\n", + '--','----','-------','--','----', '---', '-----', '----------'; + + $lines_left -= 2; + + my $proc_cmd = "show full processlist"; + + my @data = Hashes($proc_cmd); + + foreach my $thread (@data) + { + last if not $lines_left; + + ## Drop Domain Name, unless it looks like an IP address. If + ## it's an IP, we'll strip the port number because it's rarely + ## interesting. + + my $is_ip = 0; + + if ($thread->{Host} =~ /^(\d{1,3}\.){3}(\d{1,3})(:\d+)?$/) + { + $thread->{Host} =~ s/:.*$//; + $is_ip = 1; + } + else + { + $thread->{Host} =~ s/^([^.]+).*/$1/; + } + + ## Otherwise, look up the IP (if resolve is set) and strip the + ## name + if ($is_ip and $config{resolve}) + { + $thread->{Host} =~ s/:\d+$//; +# my $host = $thread->{Host}; + my $host = gethostbyaddr(inet_aton($thread->{Host}), AF_INET); +# $host =~ s/^([^.]+).*/$1/; + $thread->{Host} = $host; + } + + ## Fix possible undefs + + $thread->{db} ||= ''; + $thread->{Info} ||= ''; + $thread->{Time} ||= 0 ; + $thread->{Id} ||= 0 ; + $thread->{User} ||= ''; + $thread->{Command} ||= ''; + $thread->{Host} ||= ''; + $thread->{State} ||= ""; + + ## alter double hyphen comments so they don't break + ## the query when newlines are removed - http://freshmeat.net/users/jerjones + $thread->{Info} =~ s~\s--(.*)$~ /* $1 */ ~mg; + + ## Normalize spaces -- mostly disabled for now. This can + ## break EXPLAIN if you try to explain a mangled query. It + ## may be re-enabled later as an option. + + ## leading space removal + $thread->{Info} =~ s/^\s*//; + + if (1) + { + ## remove newlines and carriage returns + $thread->{Info} =~ s/[\n\r]//g; + + ## collpase whitespace + $thread->{Info} =~ s/\s+/ /g; + } + + ## stow it in the cache + + $qcache{$thread->{Id}} = $thread->{Info}; + $dbcache{$thread->{Id}} = $thread->{db}; + $ucache{$thread->{Id}} = $thread->{User}; + + } + + ## Sort by idle time (closest thing to CPU usage I can think of). + + my @sorted; + + if (not $config{sort}) + { + @sorted = sort { $a->{Time} <=> $b->{Time} } @data + } + else + { + @sorted = sort { $b->{Time} <=> $a->{Time} } @data + } + + foreach my $thread (@sorted) + { + # Check to see if we can skip out. We skip out if we know the + # given line doesn't match. + + next if (($thread->{Command} eq "Sleep") + and + (not $config{idle})); + + next if (($thread->{Command} eq "Binlog Dump") + and + (not $config{idle})); + + next if (($thread->{Command} eq "Daemon") + and + (not $config{idle})); + + next if ($thread->{User} !~ $config{filter_user}); + next if ($thread->{db} !~ $config{filter_db}); + next if ($thread->{Host} !~ $config{filter_host}); + next if ($thread->{State} !~ $config{filter_state}); + + $thread->{State} = trim(sprintf("%8.8s",$thread->{State})); + + # Otherwise, print. + + my $smInfo; + + if ($thread->{Info}) + { + $smInfo = substr $thread->{Info}, 0, $free; + } +# if ($thread->{State}) +# { +# $smInfo = substr $thread->{State}, 0, $free; +# } + else + { + $smInfo = ""; + } + + if ($HAS_COLOR) + { + print YELLOW() if $thread->{Command} eq 'Query'; + print WHITE() if $thread->{Command} eq 'Sleep'; + print GREEN() if $thread->{Command} eq 'Connect'; + print BOLD() if $thread->{Time} > $config{slow}; + print MAGENTA() if $thread->{Time} > $config{long}; + } + + printf "%9d %9.9s %15.15s %10.10s %10d %6.6s %8.8s %-${free}.${free}s\n", + $thread->{Id}, $thread->{User}, $thread->{Host}, $thread->{db}, + $thread->{Time}, $thread->{Command}, $thread->{State}, $smInfo; + + print RESET() if $HAS_COLOR; + + $lines_left--; + + last if $lines_left == 0; + + } + +} + +########################################################################### + +my $questions; + +sub GetQPS() +{ + my($data) = Hashes('SHOW STATUS LIKE "Questions"'); + my $num = $data->{Value}; + + if (not defined $questions) ## first time? + { + $questions = $num; + return; + } + + my $qps = $num - $questions; + $questions = $num; + print "$qps\n"; +} + +########################################################################### + +sub GetQcacheSummary() +{ +} + +########################################################################### + +sub GetInnoDBStatus() +{ + if (not $config{pager}) + { + if (not $config{pager} = FindProg('less')) + { + $config{pager} = FindProg('more'); + } + } + + my @data = Hashes("SHOW INNODB STATUS"); + + open P, "|$config{pager}" or die "$!"; + print keys %{$data[0]}; + print $data[0]->{Status},"\n"; + close P; +} + +########################################################################### + +my %prev_data; + +sub GetCmdSummary() +{ + my ($width, $height, $wpx, $hpx, $lines_left); + + if (not $config{batchmode}) + { + ($width, $height, $wpx, $hpx) = GetTerminalSize(); + + $lines_left = $height - 2; + } + else + { + $height = 999_999; ## I hope you don't have more than that! + $lines_left = 999_999; + $width = 80; + } + + # Variable_name and Value pairs come back... + my @data = Hashes("SHOW STATUS LIKE 'Com_%'"); + my %cmd_data; + my %cmd_delta; + my %cmd_pct; + my %cmd_delta_pct; + my $total; + my $delta_total; + + for my $item (@data) + { + next unless $item->{Value}; + $item->{Variable_name} =~ s/^Com_//; + $item->{Variable_name} =~ s/_/ /g; + $cmd_data{$item->{Variable_name}} = $item->{Value}; + $total += $item->{Value}; + } + + ## Populate other stats + + for my $item (keys %cmd_data) + { + $cmd_delta{$item} = $cmd_data{$item} - + ($prev_data{$item} || $cmd_data{$item} - 1); + + $delta_total += $cmd_delta{$item}; + + $cmd_pct{$item} = int(($cmd_data{$item} / $total) * 100); + } + + for my $item (keys %cmd_data) + { + $cmd_delta_pct{$item} = int(($cmd_delta{$item} / $delta_total) * 100); + } + + + ## Display + + Clear() unless $config{batchmode}; + print RESET(); + printf "%18s %10s %4s | %5s %4s\n", 'Command', 'Total', 'Pct', 'Last', 'Pct'; + printf "%18s %10s %4s | %5s %4s\n", '-------', '-----', '---', '----', '---'; + $lines_left -= 2; + + for my $item (sort { $cmd_data{$b} <=> $cmd_data{$a} } keys %cmd_data) + { + printf "%18s %10d %4s | %5d %4s\n", + $item, + $cmd_data{$item}, + $cmd_pct{$item} . "%", + $cmd_delta{$item}, + $cmd_delta_pct{$item} . "%"; + + last if not $lines_left; + $lines_left -= 1; + } + + %prev_data = %cmd_data; +} + +########################################################################### + +sub GetShowVariables() +{ + if (not $config{pager}) + { + if (not $config{pager} = FindProg('less')) + { + $config{pager} = FindProg('more'); + } + } + + my @rows = Hashes("SHOW VARIABLES"); + + open P, "|$config{pager}" or die "$!"; + + for my $row (@rows) + { + my $name = $row->{Variable_name}; + my $value = $row->{Value}; + printf P "%32s: %s\n", $name, $value; + } + + close P; +} + +########################################################################### + +sub GetShowStatus() +{ + Clear() unless $config{batchmode}; + my @rows = Hashes("SHOW STATUS"); + + printf "%32s %10s %10s\n", 'Counter', 'Total', 'Change'; + printf "%32s %10s %10s\n", '-------', '-----', '------'; + + for my $row (@rows) + { + my $name = $row->{Variable_name}; + my $value = $row->{Value}; + my $old = $statcache{$name}; + my $delta = 0; + + next if $name =~ m/^Com_/; ## skip Com_ stats + next if $value =~ m/^[^0-9]*$/; ## skip non-numeric + + ## TODO: if Qcache is off, we should skip Qcache_ values + + if ($HAS_COLOR and defined $old and $old =~ /^\d/) + { + if ($value > $old) + { + print YELLOW(); + $delta = $value - $old; + } + elsif ($value < $old) + { + print RED(); + $delta = $value - $old; + } + + if (not $config{idle} and $value == $old) + { + # filter unchanging stats, maybe + print RESET(); + next; + } + } + + printf "%32s: %10s %10s\n", $name, $value, $delta; + print RESET() if $HAS_COLOR; + + $statcache{$name} = $value; + } + +} + +########################################################################### + +sub FullQueryInfo($) +{ + my $id = shift; + + if (not exists $qcache{$id} or not defined $qcache{$id}) + { + print "*** Invalid id. ***\n"; + return; + } + + my $sql = $qcache{$id}; + print $CLEAR; + print "Thread $id was executing following query:\n\n"; + print YELLOW(), $sql,"\n\n", RESET(); +} + +########################################################################### + +sub Explain($) +{ + my $id = shift; + + if (not exists $qcache{$id} or not defined $qcache{$id}) + { + print "*** Invalid id. ***\n"; + return; + } + + my $sql = $qcache{$id}; + my $db = $dbcache{$id}; + + Execute("USE $db"); + my @info = Hashes("EXPLAIN $sql"); + print $CLEAR; + print "EXPLAIN $sql:\n\n"; + PrintTable(@info); +} + +########################################################################### + +sub PrintTable(@) +{ + my $cnt = 1; + my @cols = qw(table type possible_keys key key_len ref rows Extra); + + for my $row (@_) + { + print "*** row $cnt ***\n"; + for my $key (@cols) + { + my $val = $row->{$key} || 'NULL'; + printf "%15s: %s\n", $key, $val; + } + $cnt++; + } +} + +########################################################################### + +sub StringOrRegex($) +{ + my $input = shift; + chomp $input; + if (defined $input) + { + # regex, strip /.../ and use via qr// + if ($input =~ m{^/} and $input =~ m{/$}) + { + $input =~ s{^/}{} if $config{filter_user}; + $input =~ s{/$}{} if $config{filter_user}; + $input = qr/$input/; + } + + + # reset to match anything + elsif ($input eq '') + { + $input = qr/.*/; + } + + # string, build a simple regex + else + { + $input = '^' . $input . '$'; + $input = qr/$input/; + } + } + + # reset to match anything + else + { + $input = qr/.*/; + } + return $input; +} + +########################################################################### + +sub cmd_l +{ + ReadMode($RM_RESET); + + print RED(), "Seconds for long queries: ", RESET(); + my $secs = ReadLine(0); + + if ($secs =~ /^\s*(\d+)/) + { + $config{long} = $1; + if ($config{long} < 1) + { + $config{long} = 1; + } + } + ReadMode($RM_NOBLKRD); +} + +sub cmd_s +{ + ReadMode($RM_RESET); + + print RED(), "Seconds of Delay: ", RESET(); + my $secs = ReadLine(0); + + if ($secs =~ /^\s*(\d+)/) + { + $config{delay} = $1; + if ($config{delay} < 1) + { + $config{delay} = 1; + } + } + ReadMode($RM_NOBLKRD); +} + +sub cmd_S +{ + ReadMode($RM_RESET); + + print RED(), "Seconds for Slow queries: ", RESET(); + my $secs = ReadLine(0); + + if ($secs =~ /^\s*(\d+)/) + { + $config{slow} = $1; + if ($config{slow} < 1) + { + $config{slow} = 1; + } + } + ReadMode($RM_NOBLKRD); +} + +sub cmd_q +{ + ReadMode($RM_RESET); + print "\n"; + exit; +} + +sub trim($) +{ + my $string = shift; + $string =~ s/^\s+//; + $string =~ s/\s+$//; + return $string; +} + +########################################################################### + +sub PrintHelp() +{ + my $help = qq[ +Help for mytop version $main::VERSION by Jeremy D. Zawodny <${YELLOW}Jeremy\@Zawodny.com${RESET}> + with updates by Mark Grennan <${YELLOW}mark\@grennan.com${RESET}> + + ? - display this screen + # - toggle short/long numbers (not yet implemented) + c - command summary view (based on Com_* counters) + C - turn color on and off + d - show only a specific database + e - explain the query that a thread is running + E - display current replication error + f - show full query info for a given thread + F - unFilter the dispaly + h - show only a specifc host's connections + H - toggle the mytop header + i - toggle the display of idle (sleeping) threads + I - show innodb status + k - kill a thread + p - pause the display + l - change long running queries hightlighing + m - switch [mode] to qps (queries/sec) scrolling view + o - reverse the sort order (toggle) + q - quit + r - reset the status counters (via FLUSH STATUS on your server) + R - change reverse IP lookup + s - change the delay between screen updates + S - change slow quiery hightlighting + t - switch to thread view (default) + u - show only a specific user + : - enter a command (not yet implemented) + ! - Skip an error that has stopped replications (at your own risk) + +${GREEN}http://jeremy.zawodny.com/mysql/mytop/${RESET} +${GREEN}http://www.mysqlfanboy.com/mytop/${RESET} +]; + + print $help; +} + +sub Sum(@) +{ + my $sum; + while (my $val = shift @_) { $sum += $val; } + return $sum; +} + +## A useful routine from perlfaq + +sub commify($) +{ + local $_ = shift; + return 0 unless defined $_; + 1 while s/^([-+]?\d+)(\d{3})/$1,$2/; + return $_; +} + +## Compact numeric representation (10,000 -> 10.0k) + +sub make_short($) +{ + my $number = shift; + return commify($number) if $config{long_nums}; + my $n = 0; + while ($number > 1_025) { $number /= 1024; $n++; }; + return sprintf "%.1f%s", $number, ('','k','M','G', 'T')[$n]; +} + + + +## Run a query and return the records has an array of hashes. + +sub Hashes($) +{ + my $sql = shift; + my @records; + + if (my $sth = Execute($sql)) + { + while (my $ref = $sth->fetchrow_hashref) + { + print "record\n" if $debug; + push @records, $ref; + } + } + return @records; +} + +## Execute an SQL query and return the statement handle. + +sub Execute($) +{ + my $sql = shift; + my $sth = $dbh->prepare($sql); + + if (not $sth) { ReadMode($RM_RESET); die $DBI::errstr; } + + my $ReturnCode = $sth->execute; + + if (not $ReturnCode) + { + if ($debug) + { + print "query failed\n"; + sleep 10; + } + return undef; + } + + return $sth; +} + +sub FindProg($) +{ + my $prog = shift; + my $found = undef; + my @search_dirs = ("/bin", "/usr/bin", "/usr/sbin", + "/usr/local/bin", "/usr/local/sbin"); + + for (@search_dirs) + { + my $loc = "$_/$prog"; + if (-e $loc) + { + $found = $loc; + last; + } + } + return $found; +} + +=pod + +=head1 SYNOPSIS + +B [options] + +=head1 AVAILABILITY + +The latest version of B is available from +http://jeremy.zawodny.com/mysql/mytop/ it B also be on CPAN as +well. + +=head1 REQUIREMENTS + +In order for B to function properly, you must have the +following: + + * Perl 5.005 or newer + * Getopt::Long + * DBI and DBD::mysql + * Term::ReadKey from CPAN + +Most systems are likely to have all of those installed--except for +Term::ReadKey. You will need to pick that up from the CPAN. You can +pick up Term::ReadKey here: + + http://search.cpan.org/search?dist=TermReadKey + +And you obviously need access to a MySQL server (version 3.22.x or +3.23.x) with the necessary security to run the I and +I commands. + +If you are a Windows user, using ActiveState's Perl, you can use PPM +(the Perl Package Manager) to install the MySQL and Term::ReadKey +modules. + +=head2 Optional Color Support + +In additon, if you want a color B (recommended), install +Term::ANSIColor from the CPAN: + + http://search.cpan.org/search?dist=ANSIColor + +Once you do, B will automatically use it. However, color is not +yet working on Windows. Patches welcome. :-) + +=head2 Optional Hi-Res Timing + +If you want B to provide more accurate real-time +queries-per-second statistics, install the Time::HiRes module from +CPAN. B will automatically notice that you have it and use it +rather than the standard timing mechanism. + +=head2 Platforms + +B is known to work on: + + * Linux (2.2.x, 2.4.x) + * FreeBSD (2.2, 3.x, 4.x) + * Mac OS X + * BSDI 4.x + * Solaris 2.x + * Windows NT 4.x (ActivePerl) + +If you find that it works on another platform, please let me +know. Given that it is all Perl code, I expect it to be rather +portable to Unix and Unix-like systems. Heck, it I even work on +Win32 systems. + +=head1 DESCRIPTION + +Help is always welcome in improving this software. Feel free to +contact the author (see L<"AUTHOR"> below) with bug reports, fixes, +suggestions, and comments. Additionally L<"BUGS"> will provide a list +of things this software is not able to do yet. + +Having said that, here are the details on how it works and what you can +do with it. + +=head2 The Basics + +B was inspired by the system monitoring tool B. I +routinely use B on Linux, FreeBSD, and Solaris. You are likely to +notice features from each of them here. + +B will connect to a MySQL server and periodically run the +I and I commands and attempt to +summarize the information from them in a useful format. + +=head2 The Display + +The B display screen is really broken into two parts. The top 4 +lines (header) contain summary information about your MySQL +server. For example, you might see something like: + +MySQL on localhost (4.0.13-log) up 1+11:13:00 [23:29:11] + Queries: 19.3M qps: 160 Slow: 1.0 Se/In/Up/De(%): 00/80/03/17 + qps now: 219 Slow qps: 0.0 Threads: 1 ( 1/ 16) 00/74/00/25 + Key Efficiency: 99.3% Bps in/out: 30.5k/162.8 Now in/out: 32.7k/ 3.3k + +The first line identifies the hostname of the server (localhost) and +the version of MySQL it is running. The right had side shows the +uptime of the MySQL server process in days+hours:minutes:seconds +format (much like FreeBSD's top) as well as the current time. + +The second line displays the total number of queries the server has +processed, the average number of queries per second, the number of +slow queries, and the percentage of Select, Insert, Update, and Delete +queries. + +The third real-time values. First is the number of queries per second, +then the number of slow queries, followed by query precentages (like +on the previous line). + +And the fourth line displays key buffer efficiency (how often keys are +read from the buffer rather than disk) and the number of bytes that +MySQL has sent and received, both over all and in the last cycle. + +You can toggle the header by hitting B when running B. + +The second part of the display lists as many threads as can fit on +screen. By default they are sorted according to their idle time (least +idle first). The display looks like: + + Id User Host Dbase Time Cmd Query or State + -- ---- ---- ----- ---- --- -------------- + 61 jzawodn localhost music 0 Query show processlist + +As you can see, the thread id, username, host from which the user is +connecting, database to which the user is connected, number of seconds +of idle time, the command the thread is executing, and the query info +are all displayed. + +Often times the query info is what you are really interested in, so it +is good to run B in an xterm that is wider than the normal 80 +columns if possible. + +The thread display color-codes the threads if you have installed color +support. The current color scheme only works well in a window with a +dark (like black) background. The colors are selected according to the +C column of the display: + + Query - Yellow + Sleep - White + Connect - Green + Slow - Bright + Long - Magenta + +Those are purely arbitrary and will be customizable in a future +release. If they annoy you just start B with the B<--nocolor> +flag or adjust your config file appropriately. + +=head2 Arguments + +B handles long and short command-line arguments. Not all +options have both long and short formats, however. The long arguments +have two dashes `--'. Short arguments only have one '-'. + +=over + +=item B<-u> or B<-user> username + +Username to use when logging in to the MySQL server. Default: ``root''. + +=item B<-p> or B<-pass> or B<-password> password + +Password to use when logging in to the MySQL server. Default: none. + +=item B<-h> or B<--host> hostname[:port] + +Hostname of the MySQL server. The hostname may be followed by an +option port number. Note that the port is specified separate from the +host when using a config file. Default: ``localhost''. + +=item B<--port> or B<-P> port + +If you're running MySQL on a non-standard port, use this to specify +the port number. Default: 3306. + +=item B<-s> or B<--delay> seconds + +How long between display refreshes. Default: 5 + +=item B<-d> or B<--db> or B<--database> database + +Use if you'd like B to connect to a specific database by +default. Default: ``test''. + +=item B<-b> or B<--batch> or B<--batchmode> + +In batch mode, mytop runs only once, does not clear the screen, and +places no limit on the number of lines it will print. This is suitable +for running periodically (perhaps from cron) to capture the +information into a file for later viewing. You might use batch mode in +a CGI script to occasionally display your MySQL server status on the +web. + +Default: unset. + +=item B<-S> or B<--socket> /path/to/socket + +If you're running B on the same host as MySQL, you may wish to +have it use the MySQL socket directly rather than a standard TCP/IP +connection. If you do,just specify one. + +Note that specifying a socket will make B ignore any host +and/or port that you might have specified. If the socket does not +exist (or the file specified is not a socket), this option will be +ignored and B will use the hostname and port number instead. + +Default: none. + +=item B<--header> or B<--noheader> + +Sepcify if you want the header to display or not. You can toggle this +with the B key while B is running. + +Default: header. + +=item B<--color> or B<--nocolor> + +Specify if you want a color display. This has no effect if you don't +have color support available. + +Default: If you have color support, B will try color unless you +tell it not to. + +=item B<-i> or B<--idle> or B<--noi> or B<--noidle> + +Specify if you want idle (sleeping) threads to appear in the list. If +sleeping threads are omitted, the default sorting order is reversed so +that the longest running queries appear at the top of the list. + +Default: idle. + +=item B<--prompt> or B<--noprompt> + +Specify if you want to be prompted to type in your database password. +This provides a little bit more security since it not only prevents +the password from viewable in a process list, but also doesn't require +the password to be stored in plain text in your ~/.mytop config file. +You will B be prompted if a password has not been specified in +your config file or through another command line option. + +Default: noprompt. + +=item B<--resolve> + +If you have skip-resolve set on MySQL (to keep it from doing a reverse +DNS lookup on each inbound connection), mytop can replace IP addresses +with hostnames but toggling this option. + +Default: noresolve + +=back + +Command-line arguments will always take precedence over config file +options. That happens because the config file is read I the +command-line arguments are applied. + +=head2 Config File + +Instead of always using bulky command-line parameters, you can also +use a config file in your home directory (C<~/.mytop>). If present, +B will read it automatically. It is read I any of your +command-line arguments are processed, so your command-line arguments +will override directives in the config file. + +Here is a sample config file C<~/.mytop> which implements the defaults +described above. + + user=root + pass= + host=localhost + db=test + delay=5 + port=3306 + slow=10 + socket= + batchmode=0 + header=1 + color=1 + idle=1 + long=120 + +Using a config file will help to ensure that your database password +isn't visible to users on the command-line. Just make sure that the +permissions on C<~/.mytop> are such that others cannot read it (unless +you want them to, of course). + +You may have white space on either side of the C<=> in lines of the +config file. + +=head2 Shortcut Keys + +The following keys perform various actions while B is +running. Those which have not been implemented are listed as +such. They are included to give the user idea of what is coming. + +=over + +=item B + +Display help. + +=item B + +Show "command counters" based on the Com_* values in SHOW STATUS. +This is a new feature. Feedback welcome. + +=item B + +Turn display color on and off. Default is on. + +=item B + +Show only threads connected to a particular database. + +=item B + +Given a thread id, display the entire query that thread was (and still +may be) running. + +=item B + +Disable all filtering (host, user, and db). + +=item B + +Only show queries from a particular host. + +=item B + +Toggle the header display. You can also specify either C or +C in your config file to set the default behavior. + +=item B + +Toggle the display of idle (sleeping) threads. If sleeping threads are +filtered, the default sorting order is reversed so that the longest +running queries appear at the top of the list. + +=item B + +Switch to InnoDB Status mode. The output of "SHOW INNODB STATUS" will +be displayed every cycle. In a future version, this may actually +summarize that data rather than producing raw output. + +=item B + +Kill a thread. + +=item B + +Toggle modes. Currently this switches from `top' mode to `qps' +(Queries Per Second Mode). In this mode, mytop will write out one +integer per second. The number written reflects the number of queries +executed by the server in the previous one second interval. + +More modes may be added in the future. + +=item B + +Reverse the default sort order. + +=item B

+ +Pause display. + +=item B + +Quit B + +=item B + +Reset the server's status counters via a I command. + +=item B + +Togle IP reverse lookup. Default is on. + +=item B + +Change the sleep time (number of seconds between display refreshes). + +=item B + +Set the number of seconds a query will need to run before it is +considered old and will be highlighted. + +=item B + +Show only threads owned by a giver user. + +=back + +The B key has a command-line counterpart: B<-s>. + +The B key has two command-line counterparts: B<-header> and +B<-noheader>. + +=head1 BUGS + +This is more of a BUGS + WishList. + +Some performance information is not available when talking to a +version 3.22.x MySQL server. Additional information (about threads +mostly) was added to the output of I in MySQL 3.23.x and +B makes use of it. If the information is not available, you +will simply see zeros where the real numbers should be. + +Simply running this program will increase your overall counters (such +as the number of queries run). But you may or may not view that as a +bug. + +B consumes too much CPU time when running (verified on older +versions of Linux and FreeBSD). It's likely a problem related to +Term::ReadKey. I haven't had time to investigate yet, so B now +automatically lowers its priority when you run it. You may also think +about running B on another workstation instead of your database +server. However, C on Solaris does B have this problem. +Newer versions of Linux and FreeBSD seem to have fixed this. + +You can't specify the maximum number of threads to list. If you have +many threads and a tall xterm, B will always try to display as +many as it can fit. + +The size of most of the columns in the display has a small maximum +width. If you have fairly long database/user/host names the display +may appear odd. I have no good idea as to how best to deal with that +yet. Suggestions are welcome. + +It'd be nice if you could just add B configuration directives +in your C file instead of having a separate config file. + +You should be able to specify the columns you'd like to see in the +display and the order in which they appear. If you only have one +username that connects to your database, it's probably not worth +having the User column appear, for example. + +=head1 AUTHOR + +mytop was developed and is maintained by Jeremy D. Zawodny +(Jeremy@Zawodny.com). + +If you wish to e-mail me regarding this software, B subscribe +to the B mailing list. See the B homepage for details. + +=head1 DISCLAIMER + +While I use this software in my job at Yahoo!, I am solely responsible +for it. Yahoo! does not necessarily support this software in any +way. It is merely a personal idea which happened to be very useful in +my job. + +=head1 RECRUITING + +If you hack Perl and grok MySQL, come work at Yahoo! Contact me for +details. Or just send me your resume. Er, unless we just had layoffs, +in which case we're not hiring. :-( + +=head1 SEE ALSO + +Please check the MySQL manual if you're not sure where some of the +output of B is coming from. + +=head1 COPYRIGHT + +Copyright (C) 2000-2010, Jeremy D. Zawodny. + +=head1 CREDITS + +Fix a bug. Add a feature. See your name here! + +Many thanks go to these fine folks: + +=over + +=item Mark Grennan (mark@grennan.com) www.linuxfangoy.com + +Added updates for MySQL 5.x. Added 'S' (slow) highlighting. +Added 'C' to turn on and off Color. Added 'l' command to change +color for long running queries. Fixed a few documentation issues. +Monitors Slave status. Added color to Queue hit ratio. +Added number of rows sorted per second. +Created release 1.7. + +=item Sami Ahlroos (sami@avis-net.de) + +Suggested the idle/noidle stuff. + +=item Jan Willamowius (jan@janhh.shnet.org) + +Mirnor bug report. Documentation fixes. + +=item Alex Osipov (alex@acky.net) + +Long command-line options, Unix socket support. + +=item Stephane Enten (tuf@grolier.fr) + +Suggested batch mode. + +=item Richard Ellerbrock (richarde@eskom.co.za) + +Bug reports and usability suggestions. + +=item William R. Mattil (wrm@newton.irngtx.tel.gte.com) + +Bug report about empty passwords not working. + +=item Benjamin Pflugmann (philemon@spin.de) + +Suggested -P command-line flag as well as other changes. + +=item Justin Mecham + +Suggested setting $0 to `mytop'. + +=item Thorsten Kunz + +Provided a fix for cases when we try remove the domain name from the +display even if it is actually an IP address. + +=item Sasha Pachev + +Provided the idea of real-time queries per second in the main display. + +=item Paul DuBois + +Pointed out some option-handling bugs. + +=item Mike Wexler + +Suggested that we don't mangle (normalize) whitespace in query info by +default. + +=item Mark Zweifel + +Make the --idle command-line argument negatable. + +=item Axel Schwenke + +Noticed the inccorect formula for query cache hit percentages in +version 1.2. + +=item Steven Roussey + +Supplied a patch to help filter binary junk in queries so that +terminals don't freak out. + +=item jon r. luini + +Supplied a patch that formed the basis for C<-prompt> support. Sean +Leach submitted a similar patch. + +=item Yogish Baliga + +Supplied a patch that formed the basis for C<-resolve> support. + +=item Per Andreas Buer + +Supplied an excellent patch to tidy up the top display. This includes +showing most values in short form, such as 10k rather than 10000. + +=back + +See the Changes file on the B distribution page for more +details on what has changed. + +=head1 LICENSE + +B is licensed under the GNU General Public License version +2. For the full license information, please visit +http://www.gnu.org/copyleft/gpl.html + +=cut + +__END__ diff --git a/mysql-oracle/handlers/main.yml b/mysql-oracle/handlers/main.yml new file mode 100644 index 00000000..80375330 --- /dev/null +++ b/mysql-oracle/handlers/main.yml @@ -0,0 +1,19 @@ +--- +- name: restart munin-node + service: + name: munin-node + state: restarted + +- name: restart nagios-nrpe-server + service: + name: nagios-nrpe-server + state: restarted + +- name: restart mysql + service: + name: mysql + state: restarted + +- name: reload systemd + command: systemctl daemon-reload + diff --git a/mysql-oracle/meta/main.yml b/mysql-oracle/meta/main.yml new file mode 100644 index 00000000..8cb59054 --- /dev/null +++ b/mysql-oracle/meta/main.yml @@ -0,0 +1,20 @@ +galaxy_info: + author: Evolix + description: Install and configure MySQL 5.7 (with Oracle packages) + + issue_tracker_url: https://forge.evolix.org/projects/ansible-roles/issues + + license: GPLv2 + + min_ansible_version: 2.2 + + platforms: + - name: Debian + versions: + - jessie + - stretch + +dependencies: [] + # List your role dependencies here, one per line. + # Be sure to remove the '[]' above if you add dependencies + # to this list. diff --git a/mysql-oracle/tasks/config.yml b/mysql-oracle/tasks/config.yml new file mode 100644 index 00000000..09131f92 --- /dev/null +++ b/mysql-oracle/tasks/config.yml @@ -0,0 +1,23 @@ +--- + +- name: "Copy MySQL defaults config file" + copy: + src: evolinux-defaults.cnf + dest: "/etc/mysql/conf.d/z-evolinux-defaults.cnf" + owner: root + group: root + mode: "0644" + force: yes + tags: + - mysql + +- name: "Copy MySQL custom config file" + template: + src: evolinux-custom.cnf.j2 + dest: "/etc/mysql/conf.d/zzz-evolinux-custom.cnf" + owner: root + group: root + mode: "0644" + force: no + tags: + - mysql diff --git a/mysql-oracle/tasks/datadir.yml b/mysql-oracle/tasks/datadir.yml new file mode 100644 index 00000000..28beb1ed --- /dev/null +++ b/mysql-oracle/tasks/datadir.yml @@ -0,0 +1,45 @@ +--- + +- block: + - name: "Is {{ mysql_custom_datadir }} present ?" + stat: + path: "{{ mysql_custom_datadir }}" + check_mode: no + register: mysql_custom_datadir_test + + - name: "read the real datadir" + command: readlink -f /var/lib/mysql + changed_when: False + check_mode: no + register: mysql_current_real_datadir_test + tags: + - mysql + when: mysql_custom_datadir != '' + +- block: + - name: MySQL is stopped + service: + name: mysql + state: stopped + + - name: Move MySQL datadir to {{ mysql_custom_datadir }} + command: mv {{ mysql_current_real_datadir_test.stdout }} {{ mysql_custom_datadir }} + args: + creates: "{{ mysql_custom_datadir }}" + + - name: Symlink {{ mysql_custom_datadir }} to /var/lib/mysql + file: + src: "{{ mysql_custom_datadir }}" + dest: '/var/lib/mysql' + state: link + + - name: MySQL is started + service: + name: mysql + state: started + tags: + - mysql + when: + - mysql_custom_datadir != '' + - mysql_custom_datadir != mysql_current_real_datadir_test.stdout + - not mysql_custom_datadir_test.stat.exists diff --git a/mysql-oracle/tasks/log2mail.yml b/mysql-oracle/tasks/log2mail.yml new file mode 100644 index 00000000..568b6649 --- /dev/null +++ b/mysql-oracle/tasks/log2mail.yml @@ -0,0 +1,22 @@ +--- + +- name: Is log2mail present ? + stat: + path: /etc/log2mail/config + check_mode: no + register: log2mail_config_dir + tags: + - mysql + - log2mail + +- name: Copy log2mail config + template: + src: log2mail.j2 + dest: /etc/log2mail/config/mysql.conf + owner: log2mail + group: adm + mode: "0640" + when: log2mail_config_dir.stat.exists + tags: + - mysql + - log2mail diff --git a/mysql-oracle/tasks/main.yml b/mysql-oracle/tasks/main.yml new file mode 100644 index 00000000..273960a9 --- /dev/null +++ b/mysql-oracle/tasks/main.yml @@ -0,0 +1,19 @@ +--- + +- include: packages.yml + +- include: users.yml + +- include: config.yml + +- include: datadir.yml + +- include: tmpdir.yml + +- include: nrpe.yml + +- include: munin.yml + +- include: log2mail.yml + +- include: utils.yml diff --git a/mysql-oracle/tasks/munin.yml b/mysql-oracle/tasks/munin.yml new file mode 100644 index 00000000..52b6eed4 --- /dev/null +++ b/mysql-oracle/tasks/munin.yml @@ -0,0 +1,64 @@ +--- + +- name: is Munin present ? + stat: + path: /etc/munin/plugin-conf.d/munin-node + check_mode: no + register: munin_node_plugins_config + tags: + - mysql + - munin + +- block: + - name: Install perl libraries for Munin + apt: + name: "{{ item }}" + state: present + with_items: + - libdbd-mysql-perl + - libcache-cache-perl + + - name: Enable core Munin plugins + file: + src: '/usr/share/munin/plugins/{{ item }}' + dest: /etc/munin/plugins/{{ item }} + state: link + with_items: + - mysql_bytes + - mysql_queries + - mysql_slowqueries + - mysql_threads + notify: restart munin-node + + - name: Enable contributed Munin plugins + file: + src: /usr/share/munin/plugins/mysql_ + dest: '/etc/munin/plugins/mysql_{{ item }}' + state: link + with_items: + - commands + - connections + - files_tables + - innodb_bpool + - innodb_bpool_act + - innodb_insert_buf + - innodb_io + - innodb_io_pend + - innodb_log + - innodb_rows + - innodb_semaphores + - myisam_indexes + - network_traffic + - qcache + - qcache_mem + - select_types + - slow + - sorts + - table_locks + - tmp_tables + notify: restart munin-node + + when: munin_node_plugins_config.stat.exists + tags: + - mysql + - munin diff --git a/mysql-oracle/tasks/nrpe.yml b/mysql-oracle/tasks/nrpe.yml new file mode 100644 index 00000000..c02fc007 --- /dev/null +++ b/mysql-oracle/tasks/nrpe.yml @@ -0,0 +1,55 @@ +--- + +- name: is NRPE present ? + stat: + path: /etc/nagios/nrpe.d/evolix.cfg + check_mode: no + register: nrpe_evolix_config + tags: + - mysql + - nrpe + +- name: NRPE user exists for MySQL ? + stat: + path: ~nagios/.my.cnf + check_mode: no + register: nrpe_my_cnf + tags: + - mysql + - nrpe + +- block: + - name: Create a password for NRPE + command: "apg -n 1 -m 16 -M lcN" + register: mysql_nrpe_password + check_mode: no + changed_when: False + + - name: Create nrpe user + mysql_user: + name: nrpe + password: '{{ mysql_nrpe_password.stdout }}' + priv: "*.*:REPLICATION CLIENT" + config_file: /root/.my.cnf + update_password: always + state: present + register: create_nrpe_user + + - name: Store credentials in nagios home + ini_file: + dest: "~nagios/.my.cnf" + owner: nagios + group: nagios + mode: "0600" + section: client + option: '{{ item.option }}' + value: '{{ item.value }}' + with_items: + - { option: 'user', value: 'nrpe' } + - { option: 'password', value: '{{ mysql_nrpe_password.stdout }}' } + when: create_nrpe_user.changed + + when: nrpe_evolix_config.stat.exists and (not nrpe_my_cnf.stat.exists or mysql_force_new_nrpe_password) + tags: + - mysql + - nrpe diff --git a/mysql-oracle/tasks/packages.yml b/mysql-oracle/tasks/packages.yml new file mode 100644 index 00000000..233e700b --- /dev/null +++ b/mysql-oracle/tasks/packages.yml @@ -0,0 +1,74 @@ +--- + +- name: MySQL APT config package is available + copy: + src: mysql-apt-config_0.8.9-1_all.deb + dest: /root/mysql-apt-config_0.8.9-1_all.deb + +- name: MySQL APT config package is installed + apt: + deb: /root/mysql-apt-config_0.8.9-1_all.deb + state: installed + register: mysql_apt_config_deb + +- name: APT cache is up-to-date + apt: + update_cache: yes + when: mysql_apt_config_deb | changed + +- name: Install MySQL packages + apt: + name: '{{ item }}' + update_cache: yes + state: present + with_items: + - mysql-server + - mysql-client + - libmysqlclient20 + tags: + - mysql + - packages + +- include_role: + name: remount-usr + +- name: mysql-systemd-start scripts is installed + copy: + src: debian/mysql-systemd-start + dest: /usr/share/mysql/mysql-systemd-start + mode: "0755" + owner: root + group: root + force: yes + +- name: systemd unit is installed + copy: + src: debian/mysql-server-5.7.mysql.service + dest: /etc/systemd/system/mysql.service + mode: "0755" + owner: root + group: root + force: yes + register: mysql_systemd_unit + +# - name: systemd daemon is reloaded +# systemd: +# daemon_reload: yes +# when: mysql_systemd_unit | changed + +- name: MySQL is started + service: + name: mysql + daemon_reload: yes + state: started + tags: + - mysql + - services + +- name: apg package is installed + apt: + name: apg + state: present + tags: + - mysql + - packages diff --git a/mysql-oracle/tasks/tmpdir.yml b/mysql-oracle/tasks/tmpdir.yml new file mode 100644 index 00000000..2c2c0d91 --- /dev/null +++ b/mysql-oracle/tasks/tmpdir.yml @@ -0,0 +1,23 @@ +--- + +- block: + - name: "Create {{ mysql_custom_tmpdir }}" + file: + path: "{{ mysql_custom_tmpdir }}" + owner: mysql + group: mysql + mode: "0700" + state: directory + tags: + - mysql + + - name: Configure tmpdir + ini_file: + dest: "/etc/mysql/conf.d/zzz-evolinux-custom.cnf" + section: mysqld + option: tmpdir + value: "{{ mysql_custom_tmpdir }}" + notify: restart mysql + tags: + - mysql + when: mysql_custom_tmpdir != '' diff --git a/mysql-oracle/tasks/users.yml b/mysql-oracle/tasks/users.yml new file mode 100644 index 00000000..bab76418 --- /dev/null +++ b/mysql-oracle/tasks/users.yml @@ -0,0 +1,89 @@ +--- + +# dependency for mysql_user and mysql_db +- name: python-mysqldb is installed (Ansible dependency) + apt: + name: python-mysqldb + state: present + tags: + - mysql + +- name: create a password for mysqladmin + command: "apg -n 1 -m 16 -M lcN" + register: mysql_admin_password + changed_when: False + tags: + - mysql + +- name: there is a mysqladmin user + mysql_user: + name: mysqladmin + password: '{{ mysql_admin_password.stdout }}' + priv: "*.*:ALL,GRANT" + update_password: on_create + state: present + config_file: "/etc/mysql/debian.cnf" + register: create_mysqladmin_user + tags: + - mysql + +- name: mysqladmin is the default user + ini_file: + dest: /root/.my.cnf + mode: "0600" + section: client + option: '{{ item.option }}' + value: '{{ item.value }}' + create: yes + with_items: + - { option: 'user', value: 'mysqladmin' } + - { option: password, value: '{{ mysql_admin_password.stdout }}' } + when: create_mysqladmin_user | changed + tags: + - mysql + + +- name: create a password for debian-sys-maint + command: "apg -n 1 -m 16 -M lcN" + register: mysql_debian_password + changed_when: False + tags: + - mysql + +- name: there is a debian-sys-maint user + mysql_user: + name: debian-sys-maint + password: '{{ mysql_debian_password.stdout }}' + priv: "*.*:ALL,GRANT" + update_password: on_create + state: present + config_file: "/root/.my.cnf" + register: create_debian_user + tags: + - mysql + +- name: store debian-sys-maint user credentials + ini_file: + dest: /etc/mysql/debian.cnf + mode: "0600" + section: "{{ item[0] }}" + option: '{{ item[1].option }}' + value: '{{ item[1].value }}' + create: yes + with_nested: + - [ "client", "mysql_upgrade" ] + - [ { option: 'user', value: 'debian-sys-maint' }, + { option: password, value: '{{ mysql_debian_password.stdout }}' } + ] + when: create_debian_user | changed + tags: + - mysql + +- name: remove root user + mysql_user: + name: root + host_all: yes + config_file: "/root/.my.cnf" + state: absent + tags: + - mysql diff --git a/mysql-oracle/tasks/utils.yml b/mysql-oracle/tasks/utils.yml new file mode 100644 index 00000000..4e0e4aa9 --- /dev/null +++ b/mysql-oracle/tasks/utils.yml @@ -0,0 +1,193 @@ +--- + +- name: Scripts directory exists + file: + dest: "{{ mysql_scripts_dir or general_scripts_dir | mandatory }}" + mode: "0700" + state: directory + tags: + - mysql + +# mytop + +- name: "mytop is installed (jessie)" + apt: + name: mytop + state: present + tags: + - packages + - mytop + - mysql + when: ansible_distribution_release == "jessie" + +# - name: "mysql-utilities are installed (Debian 9 or later)" +# apt: +# name: "{{ item }}" +# with_items: +# - mysql-utilities +# when: ansible_distribution_major_version | version_compare('9', '>=') + +- name: "mytop dependencies are installed (Debian 9 or later)" + apt: + name: "{{ item }}" + with_items: + - libconfig-inifiles-perl + - libdbd-mysql-perl + - libdbi-perl + - libterm-readkey-perl + - libtime-hires-perl + tags: + - packages + - mytop + - mysql + when: ansible_distribution_major_version | version_compare('9', '>=') + +- include_role: + name: remount-usr + tags: + - mytop + - mysql + +- name: "mytop is installed (Debian 9 or later)" + copy: + src: mytop + dest: /usr/local/bin/mytop + mode: "0755" + owner: root + group: staff + force: yes + tags: + - mytop + - mysql + when: ansible_distribution_major_version | version_compare('9', '>=') + +- name: Read debian-sys-maint password + shell: 'cat /etc/mysql/debian.cnf | grep -m1 "password = .*" | cut -d" " -f3' + register: mysql_debian_password + changed_when: False + check_mode: no + tags: + - mytop + - mysql + +- name: mytop configuration is copied + template: + src: mytop-config.j2 + dest: /root/.mytop + mode: "0600" + force: yes + tags: + - mytop + - mysql + +# mysqltuner + +- include_role: + name: remount-usr + tags: + - mysql + when: (mysql_scripts_dir or general_scripts_dir) | search ("/usr") + +- name: mysqltuner is installed + # copy: + # src: mysqltuner.pl + # dest: "{{ mysql_scripts_dir or general_scripts_dir | mandatory }}/mysqltuner.pl" + # mode: "0700" + apt: + name: mysqltuner + state: present + tags: + - mysql + - mysqltuner + +- name: aha is installed + apt: + name: aha + tags: + - mysql + +# automatic optimizations + +- include_role: + name: remount-usr + tags: + - mysql + when: (mysql_scripts_dir or general_scripts_dir) | search ("/usr") + +- name: mysql-optimize.sh is installed + copy: + src: mysql-optimize.sh + dest: "{{ mysql_scripts_dir or general_scripts_dir | mandatory }}/mysql-optimize.sh" + mode: "0700" + tags: + - mysql + +- name: "Cron dir for optimize is present" + file: + path: "/etc/cron.{{ mysql_cron_optimize_frequency | mandatory }}" + state: directory + mode: "0755" + owner: root + group: root + tags: + - mysql + +- name: "Enable cron to optimize MySQL" + file: + src: "{{ mysql_scripts_dir or general_scripts_dir | mandatory }}/mysql-optimize.sh" + dest: /etc/cron.{{ mysql_cron_optimize_frequency | mandatory }}/mysql-optimize.sh + state: link + when: mysql_cron_optimize | bool + tags: + - mysql + +- name: "Disable cron to optimize MySQL" + file: + dest: /etc/cron.{{ mysql_cron_optimize_frequency | mandatory }}/mysql-optimize.sh + state: absent + when: not (mysql_cron_optimize | bool) + tags: + - mysql + +- name: "Cron dir for mysqltuner is present" + file: + path: "/etc/cron.{{ mysql_cron_mysqltuner_frequency | mandatory }}" + state: directory + mode: "0755" + owner: root + group: root + tags: + - mysql + - mysqltuner + +- name: "Enable mysqltuner in cron" + copy: + src: mysqltuner.cron.sh + dest: /etc/cron.{{ mysql_cron_mysqltuner_frequency | mandatory }}/mysqltuner.sh + when: mysql_cron_mysqltuner | bool + tags: + - mysql + - mysqltuner + +- name: "Disable mysqltuner in cron" + file: + dest: /etc/cron.{{ mysql_cron_mysqltuner_frequency | mandatory }}/mysqltuner.sh + state: absent + when: not (mysql_cron_mysqltuner | bool) + tags: + - mysql + - mysqltuner + +# my-add.sh + +- include_role: + name: remount-usr + when: (mysql_scripts_dir or general_scripts_dir) | search ("/usr") + +- name: Install my-add.sh + copy: + src: my-add.sh + dest: "{{ mysql_scripts_dir or general_scripts_dir | mandatory }}/my-add.sh" + mode: "0700" + tags: + - mysql diff --git a/mysql-oracle/templates/evolinux-custom.cnf.j2 b/mysql-oracle/templates/evolinux-custom.cnf.j2 new file mode 100644 index 00000000..fa818eaf --- /dev/null +++ b/mysql-oracle/templates/evolinux-custom.cnf.j2 @@ -0,0 +1,4 @@ +[mysqld] +#bind-address = 0.0.0.0 +thread_cache_size = {{ mysql_thread_cache_size }} +innodb_buffer_pool_size = {{ mysql_innodb_buffer_pool_size }} diff --git a/mysql-oracle/templates/log2mail.j2 b/mysql-oracle/templates/log2mail.j2 new file mode 100644 index 00000000..8a1ad3c3 --- /dev/null +++ b/mysql-oracle/templates/log2mail.j2 @@ -0,0 +1,29 @@ +file = /var/log/syslog +pattern = "is marked as crashed and should be repaired" +mailto = {{ (log2mail_alert_email or general_alert_email) | mandatory }} +template = /etc/log2mail/mail + +file = /var/log/syslog +pattern = "init function returned error" +mailto = {{ (log2mail_alert_email or general_alert_email) | mandatory }} +template = /etc/log2mail/mail + +file = /var/log/syslog +pattern = "try to repair it" +mailto = {{ (log2mail_alert_email or general_alert_email) | mandatory }} +template = /etc/log2mail/mail + +file = /var/log/syslog +pattern = "InnoDB: Fatal error" +mailto = {{ (log2mail_alert_email or general_alert_email) | mandatory }} +template = /etc/log2mail/mail + +file = /var/log/syslog +pattern = "as a STORAGE ENGINE failed" +mailto = {{ (log2mail_alert_email or general_alert_email) | mandatory }} +template = /etc/log2mail/mail + +file = /var/log/syslog +pattern = "The total blob data length" +mailto = {{ (log2mail_alert_email or general_alert_email) | mandatory }} +template = /etc/log2mail/mail diff --git a/mysql-oracle/templates/mytop-config.j2 b/mysql-oracle/templates/mytop-config.j2 new file mode 100644 index 00000000..507ab99d --- /dev/null +++ b/mysql-oracle/templates/mytop-config.j2 @@ -0,0 +1,3 @@ +user = debian-sys-maint +pass = {{ mysql_debian_password.stdout }} +db = mysql diff --git a/mysql-oracle/tests/test.yml b/mysql-oracle/tests/test.yml new file mode 100644 index 00000000..d75783bf --- /dev/null +++ b/mysql-oracle/tests/test.yml @@ -0,0 +1,4 @@ +--- +- hosts: test-kitchen + roles: + - role: mysql-oracle