From 4ffc2ea14e4ca0db54934cd79551446528bf3c26 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Wed, 11 Feb 2015 22:38:22 +0000 Subject: [PATCH] Convert postgresql_munin to an LWRP --- cookbooks/postgresql/.foodcritic | 1 - .../definitions/postgresql_munin.rb | 94 ------------------- cookbooks/postgresql/providers/munin.rb | 58 ++++++++++++ cookbooks/postgresql/resources/munin.rb | 29 ++++++ 4 files changed, 87 insertions(+), 95 deletions(-) delete mode 100644 cookbooks/postgresql/definitions/postgresql_munin.rb create mode 100644 cookbooks/postgresql/providers/munin.rb create mode 100644 cookbooks/postgresql/resources/munin.rb diff --git a/cookbooks/postgresql/.foodcritic b/cookbooks/postgresql/.foodcritic index e9a7c9233..3907cff00 100644 --- a/cookbooks/postgresql/.foodcritic +++ b/cookbooks/postgresql/.foodcritic @@ -1,2 +1 @@ ~FC001 -~FC015 diff --git a/cookbooks/postgresql/definitions/postgresql_munin.rb b/cookbooks/postgresql/definitions/postgresql_munin.rb deleted file mode 100644 index 3b6056394..000000000 --- a/cookbooks/postgresql/definitions/postgresql_munin.rb +++ /dev/null @@ -1,94 +0,0 @@ -# -# Cookbook Name:: postgresql -# Definition:: postgresql_munin -# -# Copyright 2012, OpenStreetMap Foundation -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -define :postgresql_munin, :action => :create do - cluster = params[:cluster] - suffix = cluster.tr("/", ":") - database = params[:database] - - if node[:postgresql][:clusters] && node[:postgresql][:clusters][cluster] - munin_plugin "postgres_cache_#{database}:#{suffix}" do - action params[:action] - target "postgres_cache_" - conf "munin.erb" - conf_cookbook "postgresql" - conf_variables :port => node[:postgresql][:clusters][cluster][:port] - end - - munin_plugin "postgres_connections_#{database}:#{suffix}" do - action params[:action] - target "postgres_connections_" - conf "munin.erb" - conf_cookbook "postgresql" - conf_variables :port => node[:postgresql][:clusters][cluster][:port] - end - - munin_plugin "postgres_locks_#{database}:#{suffix}" do - action params[:action] - target "postgres_locks_" - conf "munin.erb" - conf_cookbook "postgresql" - conf_variables :port => node[:postgresql][:clusters][cluster][:port] - end - - munin_plugin "postgres_querylength_#{database}:#{suffix}" do - action params[:action] - target "postgres_querylength_" - conf "munin.erb" - conf_cookbook "postgresql" - conf_variables :port => node[:postgresql][:clusters][cluster][:port] - end - - munin_plugin "postgres_scans_#{database}:#{suffix}" do - action params[:action] - target "postgres_scans_" - conf "munin.erb" - conf_cookbook "postgresql" - conf_variables :port => node[:postgresql][:clusters][cluster][:port] - end - - munin_plugin "postgres_size_#{database}:#{suffix}" do - action params[:action] - target "postgres_size_" - conf "munin.erb" - conf_cookbook "postgresql" - conf_variables :port => node[:postgresql][:clusters][cluster][:port] - end - - munin_plugin "postgres_transactions_#{database}:#{suffix}" do - action params[:action] - target "postgres_transactions_" - conf "munin.erb" - conf_cookbook "postgresql" - conf_variables :port => node[:postgresql][:clusters][cluster][:port] - end - - munin_plugin "postgres_tuples_#{database}:#{suffix}" do - action params[:action] - target "postgres_tuples_" - conf "munin.erb" - conf_cookbook "postgresql" - conf_variables :port => node[:postgresql][:clusters][cluster][:port] - end - else - log "Postgres cluster #{cluster} not found" do - level :warn - end - end -end diff --git a/cookbooks/postgresql/providers/munin.rb b/cookbooks/postgresql/providers/munin.rb new file mode 100644 index 000000000..986c11f44 --- /dev/null +++ b/cookbooks/postgresql/providers/munin.rb @@ -0,0 +1,58 @@ +# +# Cookbook Name:: postgresql +# Provider:: postgresql_munin +# +# Copyright 2015, OpenStreetMap Foundation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +def whyrun_supported? + true +end + +use_inline_resources + +action :create do + cluster = node[:postgresql][:clusters] && node[:postgresql][:clusters][new_resource.cluster] + database = new_resource.database + + if cluster + %w(cache connections locks querylength scans size transactions tuples).each do |plugin| + munin_plugin "postgres_#{plugin}_#{database}:#{suffix}" do + target "postgres_#{plugin}_" + conf "munin.erb" + conf_cookbook "postgresql" + conf_variables :port => cluster[:port] + restart_munin false + end + end + else + Chef::Log.info "Postgres cluster #{new_resource.cluster} not found" + end +end + +action :delete do + database = new_resource.database + + %w(cache connections locks querylength scans size transactions tuples).each do |plugin| + munin_plugin "postgres_#{plugin}_#{database}:#{suffix}" do + action :delete + restart_munin false + end + end +end + +def suffix + new_resource.cluster.tr("/", ":") +end diff --git a/cookbooks/postgresql/resources/munin.rb b/cookbooks/postgresql/resources/munin.rb new file mode 100644 index 000000000..aa4f8c8e6 --- /dev/null +++ b/cookbooks/postgresql/resources/munin.rb @@ -0,0 +1,29 @@ +# +# Cookbook Name:: postgresql +# Resource:: postgresql_munin +# +# Copyright 2015, OpenStreetMap Foundation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +actions :create, :delete +default_action :create + +attribute :name, :kind_of => String, :name_attribute => true +attribute :cluster, :kind_of => String, :required => true +attribute :database, :kind_of => String, :required => true + +def after_created + notifies :restart, "service[munin-node]" +end -- 2.43.2