Thank you. This is Komiya.
Continuing from the previous article, we will introduce the recipe for the procedure of munin, zabbix. That's all for this article.
At the end of the article we will describe how to apply the recipe.
・Munin recipe
# cd /opt/src/rpms
# mkdir -p /root/chef-repo/site-cookbooks/munin/files/default/rpms
# mkdir -p /root/chef-repo/site-cookbooks/munin/files/default/var/www/html/munin
# mkdir /root/chef-repo/site-cookbooks/munin/files/default/etc/munin/plugin-conf.d
# cp -p /etc/munin/munin.conf /root/chef-repo/site-cookbooks/munin/files/default/etc/munin/
# scp -Cp xxx-web01:/etc/munin/munin-node.conf /root/chef-repo/site-cookbooks/munin/files/default/etc/munin/
# cp -p /var/www/html/munin/.htaccess /root/chef-repo/site-cookbooks/munin/files/default/var/www/html/munin/
# cp -p /etc/munin/plugin-conf.d/munin-node /root/chef-repo/site-cookbooks/munin/files/default/etc/munin/plugin-conf.d/
# tar cf /root/chef-repo/site-cookbooks/munin/files/default/rpms/munin-node-rpm.tar ./munin-node-rpm/
# tar tf /root/chef-repo/site-cookbooks/munin/files/default/rpms/munin-node-rpm.tar ./munin-node-rpm/
# tar cf /root/chef-repo/site-cookbooks/munin/files/default/rpms/munin-serv-rpm.tar ./munin-serv-rpm/
# tar tf /root/chef-repo/site-cookbooks/munin/files/default/rpms/munin-serv-rpm.tar ./munin-serv-rpm/
# cd /root/chef-repo/site-cookbooks/munin/recipes
# touch munin-node.rb munin-server.rb munin-node-db.rb munin-node-web.rb
# vi munin-node.rb
filename = "munin-node-rpm.tar"
cookbook_file "/tmp/#{filename}" do
source "rpms/#{filename}"
mode 0644
end
package "perl-DBI" do
not_if "rpm -qa|grep perl-DBI"
action :install
end
script "install_munin-node" do
not_if 'ls /etc/munin/munin-node.conf'
interpreter "bash"
user "root"
code <-EOL
cd /tmp
tar xf /tmp/#{filename}
rpm -i /tmp/munin-node-rpm/perl-Dig*
rpm -i /tmp/munin-node-rpm/perl-{C*,H*,I*,L*,N*,X*,li*}
rpm -i /tmp/munin-node-rpm/perl-DBD-Pg*
rpm -i /tmp/munin-node-rpm/munin-*
cp -p /etc/munin/munin-node.conf{,.org}
EOL
end
cookbook_file "/etc/munin/munin-node.conf" do
source "etc/munin/munin-node.conf"
mode 0644
end
script "link_plugins" do
not_if 'ls /etc/munin/plugins/tcp'
interpreter "bash"
user "root"
code <-EOL rm="" -f="" tc/munin/plugins/*="" ln="" -s="" sr/share/munin/plugins/cpu="" tc/munin/plugins/cpu="" ln="" -s="" sr/share/munin/plugins/if_err_="" tc/munin/plugins/if_err_eth0="" ln="" -s="" sr/share/munin/plugins/if_="" tc/munin/plugins/if_eth0="" ln="" -s="" sr/share/munin/plugins/iostat="" tc/munin/plugins/iostat="" ln="" -s="" sr/share/munin/plugins/load="" tc/munin/plugins/load="" ln="" -s="" sr/share/munin/plugins/memory="" tc/munin/plugins/memory="" ln="" -s="" sr/share/munin/plugins/tcp="" tc/munin/plugins/tcp="" ln="" -s="" sr/share/munin/plugins/df="" tc/munin/plugins/df="" eol="" end="" service="" "munin-node"="" do="" supports="" :status=""> true, :restart => true
action [ :enable, :start ]
end
# vi munin-server.rb
filename = "munin-serv-rpm.tar"
cookbook_file "/tmp/#{filename}" do
source "rpms/#{filename}"
mode 0644
end
script "install_munin-serv" do
not_if 'ls /etc/munin/munin.conf'
interpreter "bash"
user "root"
code <-EOL tar="" xf="" mp/#{filename}="" rpm="" -i="" mp/munin-serv-rpm/*="" cp="" -p="" tc/munin/munin.conf{,.org}="" eol="" end="" cookbook_file="" "/etc/munin/munin.conf"="" do="" source="" "etc/munin/munin.conf"="" mode="" 0644="" end="" service="" "munin-node"="" do="" supports="" :status=""> true, :restart => true
action [ :enable, :start ]
end
cookbook_file "/var/www/html/munin/.htaccess" do
source "var/www/html/munin/.htaccess"
mode 0644
end
# vi munin-node-web.rb
script "link_plugins_web" do
not_if 'ls /etc/munin/plugins/apache_accesses'
interpreter "bash"
user "root"
code <-EOL ln="" -s="" sr/share/munin/plugins/apache_accesses="" tc/munin/plugins/apache_accesses="" ln="" -s="" sr/share/munin/plugins/apache_processes="" tc/munin/plugins/apache_processes="" eol="" end="" service="" "munin-node"="" do="" supports="" :status=""> true, :restart => true
action [ :enable, :restart ]
end
# vi munin-node-db.rb
script "link_plugins_db" do
not_if 'ls /etc/munin/plugins/mysql_threads'
interpreter "bash"
user "root"
code <-EOL ln="" -s="" sr/share/munin/plugins/mysql_queries="" tc/munin/plugins/mysql_queries="" ln="" -s="" sr/share/munin/plugins/mysql_slowqueries="" tc/munin/plugins/mysql_slowqueries="" ln="" -s="" sr/share/munin/plugins/mysql_threads="" tc/munin/plugins/mysql_threads="" eol="" end="" cookbook_file="" "/etc/munin/plugin-conf.d/munin-node"="" do="" source="" "etc/munin/plugin-conf.d/munin-node"="" mode="" 0644="" end="" service="" "munin-node"="" do="" supports="" :status=""> true, :restart => true
action [ :enable, :restart ]
end
I'm wondering if there is a way to execute different commands for each role or do it in one recipe. Can it be done with if?
I didn't know how to do that this time, so I made separate recipes for web and db.
The order in which the plugins are applied to the role is that if you don't do munin-node.rb first, all plugins will be deleted first. (Recipes are executed from top to bottom)
Zabbix recipe
・zabbix-agent
# mkdir -p /root/chef-repo/site-cookbooks/zabbix/files/default/opt/bin
# mkdir -p /root/chef-repo/site-cookbooks/zabbix/templates/default/etc/zabbix
# mkdir -p /root/chef-repo/site-cookbooks/zabbix/files/default/{etc,rpms}
# cd /opt/src/rpms
# tar cf /root/chef-repo/site-cookbooks/zabbix/files/default/rpms/zabbix-rpm.tar ./zabbix-rpm/
# tar tf /root/chef-repo/site-cookbooks/zabbix/files/default/rpms/zabbix-rpm.tar ./zabbix-rpm/
# scp -Cp xxx-web01:/etc/zabbix/zabbix_agentd.conf /root/chef-repo/site-cookbooks/zabbix/templates/default/etc/zabbix/
# cp -p /opt/bin/mem_monitor.sh /root/chef-repo/site-cookbooks/zabbix/files/default/opt/bin/
# cd /root/chef-repo/site-cookbooks/zabbix/recipes
# touch zabbix-agent.rb zabbix-proxy.rb
# vi zabbix-agent.rb
filename = "zabbix-rpm.tar"
cookbook_file "/tmp/#{filename}" do
source "rpms/#{filename}"
mode 0644
end
script "install_zabbix_agent" do
not_if 'ls /etc/zabbix/zabbix_agentd.conf'
interpreter "bash"
user "root"
code <-EOL tar="" xf="" mp/#{filename}="" rpm="" -i="" mp/zabbix-rpm/repo-rpm/*.rpm="" ln="" -sf="" sr/share/zabbix-jp-release/zabbix-jp-1.8.repo="" tc/alternatives/zabbix-jp-release="" rpm="" -i="" mp/zabbix-rpm/*.rpm="" cp="" -p="" tc/zabbix/zabbix_agentd.conf{,.org}="" eol="" end="" template="" '/etc/zabbix/zabbix_agentd.conf'="" do="" owner="" 'root'="" group="" 'root'="" source="" 'etc/zabbix/zabbix_agentd.conf'="" end="" service="" "zabbix-agent"="" do="" supports="" :status=""> true, :restart => true, :reload => true
action [ :enable, :start ]
end
cookbook_file "/opt/bin/mem_monitor.sh" do
source "opt/bin/mem_monitor.sh"
mode 0755
end
zabbix_agentd.conf has a place to write the hostname and IP address, so I put it in the template and put the node value in ohai.
Make sure you get the price with ohai
# ohai ipaddress
[
"10.0.0.93"
]
# ohai hostname
[
"xxx-stg01"
]
# vi /root/chef-repo/site-cookbooks/zabbix/templates/default/etc/zabbix/zabbix_agentd.conf
# diff /etc/zabbix/zabbix_agentd.conf.org /root/chef-repo/site-cookbooks/zabbix/templates/default/etc/zabbix/zabbix_agentd.conf
63c63
< # EnableRemoteCommands=0
---
> EnableRemoteCommands=1
86c86
< Server=127.0.0.1
---
> Server=<%= node['ipaddress'] %>
109c109
< Hostname=Zabbix server
---
> Hostname=<%= node['hostname'] %>
134c134
< ListenIP=127.0.0.1
---
> ListenIP=0.0.0.0
265a266,268
>
> ##memory
> UserParameter=mem.puse,/opt/bin/mem_monitor.sh
・zabbix-proxy
Run mysql for zabbix separately on 4406
# mkdir -p /root/chef-repo/site-cookbooks/zabbix/files/default/etc/init.d
# cp -p /etc/init.d/mysqld_4406 /root/chef-repo/site-cookbooks/zabbix/files/default/etc/init.d/
# cp -p /etc/my_4406.cnf /root/chef-repo/site-cookbooks/zabbix/files/default/etc/
# mkdir -p /root/chef-repo/site-cookbooks/zabbix/files/default/etc/zabbix
# cp -p /etc/zabbix/zabbix_proxy.conf /root/chef-repo/site-cookbooks/zabbix/files/default/etc/zabbix/
# vi zabbix-proxy.rb
cookbook_file "/etc/init.d/mysqld_4406" do
source "etc/init.d/mysqld_4406"
mode 0755
end
directory '/var/lib/mysql_4406/' do
owner 'mysql'
group 'mysql'
mode '0755'
action :create
end
cookbook_file "/etc/my_4406.cnf" do
source "etc/my_4406.cnf"
mode 0644
end
service "mysqld_4406" do
supports :status => true, :restart => true, :reload => :true
action [ :enable, :start ]
end
data_bag1 = Chef::EncryptedDataBagItem.load('mysqluser','root')
data_bag2 = Chef::EncryptedDataBagItem.load('mysqluser','zabbix')
ROOT_PASSWORD = data_bag1['pass']
ZABBIX_PASSWORD = data_bag2['pass']
socket='--socket=/var/lib/mysql_4406/mysql.sock'
filename=zabbix-jp-release-5-6.noarch.rpm
script "install_zabbix-proxy" do
not_if 'ls /etc/init.d/zabbix-proxy'
interpreter "bash"
user "root"
code <-EOL
wget http://www.zabbix.jp/binaries/relatedpkgs/rhel5/x86_64/#{filename}
rpm -Uvh #{filename}
yum -y install --enablerepo=zabbix-jp zabbix-proxy zabbix-proxy-mysql
/usr/bin/mysqladmin -u root #{socket} password #{ROOT_PASSWORD}
mysql -u root #{socket} -p#{ROOT_PASSWORD} -e "drop database test;"
mysql -u root #{socket} -p#{ROOT_PASSWORD} -e "create database zabbix_proxy;"
mysql -u root #{socket} -p#{ROOT_PASSWORD} -e "grant all privileges on zabbix_proxy.* to zabbix@localhost identified by "#{ZABBIX_PASSWORD}";"
mysql -u root #{socket} -p#{ROOT_PASSWORD} -e "DELETE FROM mysql.user WHERE password='';"
mysql -u root #{socket} -p#{ROOT_PASSWORD} -e "flush privileges;"
mysql -u zabbix #{socket} -p#{ZABBIX_PASSWORD} zabbix_proxy < /usr/share/doc/zabbix-proxy-1.8.16/schema/mysql.sql
mysql -u zabbix #{socket} -p#{ZABBIX_PASSWORD} zabbix_proxy < /usr/share/doc/zabbix-proxy-1.8.16/data/data.sql
mysql -u zabbix #{socket} -p#{ZABBIX_PASSWORD} zabbix_proxy < /usr/share/doc/zabbix-proxy-1.8.16/data/images_mysql.sql
EOL
end
service "zabbix-proxy" do
supports :status => true, :restart => true, :reload => :true
action [ :enable, :start ]
end
cookbook_file "/etc/zabbix/zabbix_proxy.conf" do
source "etc/zabbix/zabbix_proxy.conf"
mode 0644
notifies :reload, 'service[zabbix-proxy]'
end
Syntax Test for Cookbooks
# knife cookbook test zabbix
# knife cookbook test munin
・How to apply recipes
If you want to assign the entire recipe required for a single node (the recipe will be applied from the predefined node and role)
# knife solo cook 10.0.0.240
If you want to test individually (e.g. when you want to quickly check after partial correction)
# knife solo cook 10.0.0.240 -o base_setting::sysctl,httpd::httpd-server
If you use vagrant, you will be given vagrant provision and provision (recipe applied) in the place where the vagrantfile is located.
In the case of chef-solo, applying recipes with knife commands does not seem to be possible in parallel, so if necessary, it is like spinning for and executing them in order.
It took about 2 minutes for the webserver and 5 minutes for the dbserver to apply the recipe I created this time. The test with serverspec was 30 seconds. (t1.micro instance)
Next time, I will introduce the test with serverspec at the end.
Please look forward to it. </-EOL></-EOL></-EOL></-EOL></-EOL>