orenoblog

エンジニアになりたいExcel方眼紙erの物語

monitのrecipe

pidファイルをチェックするタイプのmonit.confを一気に作りたいとき。

  • recipes/defualt.rb
service "monit" do
  action :nothing
end

common_process = ["ntpd", "sshd", "crond"]

common_process.each do |proc|
  template "/etc/monit.d/#{proc}.conf" do
    source "monit_template.conf.erb"
    owner "root"
    group "root"
    mode 0644
    notifies :restart, "service[monit]"
    variables ({
      :process_name => proc,
      :pidfile => "/var/run/#{proc}.pid",
      :program => "/etc/init.d/#{proc}",
      :restarts => 5,
      :cycles => 5
    })
  end
end
  • templates/default/monit_template.conf.erb
check process <%= @process_name %> with pidfile <%= @pidfile %>
    start program = "<%= @program %> start"
    stop program = "<%= @program %> stop"
    if <%= @restarts %> restarts within <%= @cycles %> cycles then timeout
  • knife solo cook実行後

プロセスごとのconfigファイルが展開される

  • crond.conf
check process crond with pidfile /var/run/crond.pid
    start program = "/etc/init.d/crond start"
    stop program = "/etc/init.d/crond stop"
    if 5 restarts within 5 cycles then timeout
  • ntpd.conf
check process ntpd with pidfile /var/run/ntpd.pid
    start program = "/etc/init.d/ntpd start"
    stop program = "/etc/init.d/ntpd stop"
    if 5 restarts within 5 cycles then timeout
  • /etc/monit.d/sshd.conf
check process sshd with pidfile /var/run/sshd.pid
    start program = "/etc/init.d/sshd start"
    stop program = "/etc/init.d/sshd stop"
    if 5 restarts within 5 cycles then timeout

楽ちん