diff options
-rw-r--r-- | README | 57 | ||||
-rw-r--r-- | files/scripts/CentOS/setmysqlpass.sh | 20 | ||||
-rw-r--r-- | files/scripts/Debian/setmysqlpass.sh | 21 | ||||
-rw-r--r-- | manifests/conf.pp | 37 | ||||
-rw-r--r-- | manifests/server.pp | 5 | ||||
-rw-r--r-- | manifests/server/cron/optimize.pp | 12 | ||||
-rw-r--r-- | manifests/server/munin/debian.pp | 56 | ||||
-rw-r--r-- | templates/conf.erb | 9 |
8 files changed, 176 insertions, 41 deletions
@@ -18,6 +18,13 @@ installed, specifically it must have nagios::plugin::deploy functionality. You will need to have activated storedconfigs on the puppetmaster. +You need to ensure that the PATH environment variable contains the appropriate +directories else the root password will not be set correctly to the requested +value. Since the variable is empty by default when running puppet, you need to +have something similar to this somewhere in your manifests: + + Exec { path => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' } + Special Notes ============= @@ -48,6 +55,36 @@ grant tables. The my.cnf file will installed from one of many possible places, see manifests/server/base.pp for possible locations for managing this. +Configuration snippets +---------------------- + +To make managing mysql configuration easier, you can use the define +mysql::conf. Note, though that there currently is only the Debian default +configuration file that includes files in /etc/mysql/conf.d/. + +For example: + +mysql::conf { 'test': + ensure => present, + section => 'mysqld', + config => { + table_cache => '15000', + skip_slave => '', + something => '""', + } +} + +The above example shows two possibilities for empty values. + + * If a value only has an empty value in the hash passed to the config + parameter, that will define a boolean option in mysql by simply mentioning + the option name with no equal sign. So in the above, you'd have a line that + contains only "skip_slave". + + * If you need to declare a variable with an empty value (e.g. with the equal + sign), you can use two quotes as the option's value. In the above example, + you'd have a line that looks like "something=". + Backups ------- @@ -63,6 +100,16 @@ Optimizing tables If you wish mysql to periodically optimize tables, set the "$mysql_optimize_cron = true" variable before you include mysql::server. +By default, time of execution for the optimization script will be randomly +chosen (and will stay consistant for a server) any day between midnight and +7:00 AM. If you wish to force at least one value, you can use the following +parameters to the mysql::server class (all values are used directly as a +cronjob value so they should be set within cron value space): + +* optimize_day => sets the day of the week (integer value) during which the script will run. +* optimize_hour => sets the hour at which the optimization script will run. +* optimize_minute => sets the minute in the hour at which the script will run. + Munin ----- @@ -78,8 +125,8 @@ Debian's /etc/mysql/debian.cnf. Nagios ------ -If you wish nagios to check mysql, you should set the variable "use_nagios" to -"true" in hiera along with the "nagios_check_mysql" variable to "true". A +If you wish nagios to check mysql, you can set the variable "manage_nagios" to +"true" in hiera along with the "nagios_check_mysql" variable to "true". A password for the nagios mysql user will be created for you with reduced privileges used only for nagios checks. This will be what you passed as nagios_password_hash to mysql::server and should be a mysql md5 hash. These should be set before you @@ -94,7 +141,11 @@ mysql::server::nagios::check_health { [ 'connection-time', 'uptime', 'threads-co See the files/nagios/check_mysql_health script for the various mysql health checks that you can perform. Additionally, see the define "check_health" in -manifests/server/nagios.pp for various options that you can pass to check_health. +manifests/server/nagios.pp for various options that you can pass to check_health. + +Note that if you need to use some specific logic to decide whether or not to +create a nagios service check, you should set $manage_nagios to false, and +include mysql::server::nagios from within your own manifests. Firewall -------- diff --git a/files/scripts/CentOS/setmysqlpass.sh b/files/scripts/CentOS/setmysqlpass.sh index b84aa7a..6876cb9 100644 --- a/files/scripts/CentOS/setmysqlpass.sh +++ b/files/scripts/CentOS/setmysqlpass.sh @@ -2,6 +2,26 @@ test -f /root/.my.cnf || exit 1 +must_have () +{ + # Here, using "which" would not be appropriate since it also depends on + # PATH being set correctly. The type builtin command is unaffected by the + # environment. + type $1 >/dev/null + if [ $? -ne 0 ]; then + echo "Command '$1' not found, did you correctly set PATH ? Its current value is: $PATH" >&2 + exit 1 + fi +} + +# Since this script is doing something rather unsafe with the database, we want +# to be really careful to have all the necessary tools before doing anything so +# that we don't end up in an inconsistent state. +must_have sleep +must_have mysql +must_have killall +must_have chown + rootpw=$(grep password /root/.my.cnf | sed -e 's/^[^=]*= *\(.*\) */\1/') /usr/bin/mysqladmin -uroot --password="${rootpw}" status > /dev/null && echo "Nothing to do as the password already works" && exit 0 diff --git a/files/scripts/Debian/setmysqlpass.sh b/files/scripts/Debian/setmysqlpass.sh index 3a3e336..3de2781 100644 --- a/files/scripts/Debian/setmysqlpass.sh +++ b/files/scripts/Debian/setmysqlpass.sh @@ -2,6 +2,27 @@ test -f /root/.my.cnf || exit 1 +must_have () +{ + # Here, using "which" would not be appropriate since it also depends on + # PATH being set correctly. The type builtin command is unaffected by the + # environment. + type $1 >/dev/null + if [ $? -ne 0 ]; then + echo "Command '$1' not found, did you correctly set PATH ? Its current value is: $PATH" >&2 + exit 1 + fi +} + +# Since this script is doing something rather unsafe with the database, we want +# to be really careful to have all the necessary tools before doing anything so +# that we don't end up in an inconsistent state. +must_have sleep +must_have mysql +must_have killall +must_have ls +must_have chown + rootpw=$(grep password /root/.my.cnf | sed -e 's/^[^=]*= *\(.*\) */\1/') /usr/bin/mysqladmin -uroot --password="${rootpw}" status > /dev/null && echo "Nothing to do as the password already works" && exit 0 diff --git a/manifests/conf.pp b/manifests/conf.pp new file mode 100644 index 0000000..f9cbeb3 --- /dev/null +++ b/manifests/conf.pp @@ -0,0 +1,37 @@ +# $config needs to be a hash of key => value pairs. +# +# values in config are output as key = value, except when the value is empty; +# then just key is output. if you need to output an empty value in the form +# key = value, then you can specify empty quotes as the value (see example). +# +# mysql::conf { 'test': +# ensure => present, +# section => 'mysqld', +# config => { +# table_cache => '15000', +# skip_slave => '', +# something => '""', +# } +# } +# +# This will generate the following contents: +# [mysqld] +# skip_slave +# something = "" +# table_cache = 15000 +# +define mysql::conf ( + $section, + $config, + $ensure = present +) { + + include mysql::server::base + + file { "/etc/mysql/conf.d/${name}.cnf": + ensure => $ensure, + content => template('mysql/conf.erb'), + notify => Service['mysql'], + } + +} diff --git a/manifests/server.pp b/manifests/server.pp index a03dd7b..5a34b1c 100644 --- a/manifests/server.pp +++ b/manifests/server.pp @@ -8,6 +8,9 @@ class mysql::server ( $nagios_password_hash = 'absent', $backup_cron = false, $optimize_cron = false, + $optimize_hour = fqdn_rand(7), + $optimize_minute = fqdn_rand(60), + $optimize_day = fqdn_rand(7), $backup_dir = '/var/backups/mysql', $manage_backup_dir = true, $nagios_notcp = false @@ -20,7 +23,7 @@ class mysql::server ( } if $manage_munin and $::mysql_exists == 'true' { - if $munin_password == 'absent' { + if $munin_password == 'absent' and $::operatingsystem != debian { fail('need to set the munin password') } case $::operatingsystem { diff --git a/manifests/server/cron/optimize.pp b/manifests/server/cron/optimize.pp index d1d0257..5d4fa98 100644 --- a/manifests/server/cron/optimize.pp +++ b/manifests/server/cron/optimize.pp @@ -1,5 +1,9 @@ # optimize mysql databases regurarely -class mysql::server::cron::optimize { +class mysql::server::cron::optimize ( + $optimize_hour, + $optimize_minute, + $optimize_day +) { file { 'mysql_optimize_script': path => '/usr/local/sbin/optimize_mysql_tables.rb', @@ -12,9 +16,9 @@ class mysql::server::cron::optimize { cron { 'mysql_optimize_cron': command => '/usr/local/sbin/optimize_mysql_tables.rb', user => 'root', - minute => 40, - hour => 6, - weekday => 7, + minute => $optimize_minute, + hour => $optimize_hour, + weekday => $optimize_day, require => [ Exec['mysql_set_rootpw'], File['mysql_root_cnf'], File['mysql_optimize_script'] ], diff --git a/manifests/server/munin/debian.pp b/manifests/server/munin/debian.pp index 9ff7863..c7ae961 100644 --- a/manifests/server/munin/debian.pp +++ b/manifests/server/munin/debian.pp @@ -1,35 +1,25 @@ # debian way of calling plugins -class mysql::server::munin::debian inherits mysql::server::munin::default { - Munin::Plugin['mysql_bytes']{ - config => "user root\nenv.mysqlopts --defaults-file=/etc/mysql/debian.cnf", - require => Package['mysql'], - } - Munin::Plugin['mysql_queries']{ - config => "user root\nenv.mysqlopts --defaults-file=/etc/mysql/debian.cnf", - require => Package['mysql'], - } - Munin::Plugin['mysql_slowqueries']{ - config => "user root\nenv.mysqlopts --defaults-file=/etc/mysql/debian.cnf", - require => Package['mysql'], - } - Munin::Plugin['mysql_threads']{ - config => "user root\nenv.mysqlopts --defaults-file=/etc/mysql/debian.cnf", - require => Package['mysql'], - } - Munin::Plugin::Deploy['mysql_connections']{ - config => "user root\nenv.mysqlopts --defaults-file=/etc/mysql/debian.cnf", - require => Package['mysql'], - } - Munin::Plugin::Deploy['mysql_qcache']{ - config => "user root\nenv.mysqlopts --defaults-file=/etc/mysql/debian.cnf", - require => Package['mysql'], - } - Munin::Plugin::Deploy['mysql_cache_mem']{ - config => "user root\nenv.mysqlopts --defaults-file=/etc/mysql/debian.cnf", - require => Package['mysql'], - } - Munin::Plugin::Deploy['mysql_size_all']{ - config => "user root\nenv.mysqlopts --defaults-file=/etc/mysql/debian.cnf", - require => Package['mysql'], - } +class mysql::server::munin::debian { + + munin::plugin { + [mysql_queries, mysql_slowqueries, mysql_bytes, mysql_threads]: + config => "user root\nenv.mysqlopts --defaults-file=/etc/mysql/debian.cnf", + } + + munin::plugin::deploy{ + 'mysql_connections': + source => 'mysql/munin/mysql_connections'; + 'mysql_qcache': + source => 'mysql/munin/mysql_qcache'; + 'mysql_qcache_mem': + source => 'mysql/munin/mysql_qcache_mem'; + 'mysql_size_all': + source => 'mysql/munin/mysql_size_all'; + } + + Munin::Plugin::Deploy[ [ 'mysql_connections', 'mysql_qcache', 'mysql_qcache_mem', 'mysql_size_all' ] ] { + config => "user root\nenv.mysqlopts --defaults-file=/etc/mysql/debian.cnf", + require => Package['mysql'], + } + } diff --git a/templates/conf.erb b/templates/conf.erb new file mode 100644 index 0000000..c7e332a --- /dev/null +++ b/templates/conf.erb @@ -0,0 +1,9 @@ +# THIS FILE IS MANAGED BY PUPPET +[<%= @section -%>] +<% @config.each do |key, value| -%> +<% if value != '' -%> +<%= key -%> = <%= value %> +<% else -%> +<%= key %> +<% end -%> +<% end %> |