LoginSignup
6
6

More than 5 years have passed since last update.

ChefのRoleを使ってPostfixを設定しつつmonitで監視

Last updated at Posted at 2012-12-26

この記事は最終更新から1年以上経過しています。 気をつけてね。

Chef活用の話、ミドルウェアのインストール&設定&監視を構築して維持する。

使うCookbooks

Postfixはopscodeのcookbook[postfix]knife cookbook siteで持ってこれる奴を使う。

MonitはOpscodeコミュニティに公開しているmonit_binのCookbookを使う。

監視設定はcookbook[monit_bin]内のLWRPに設定しているので、monit_bin[services]をどこでもいいからrunlistに加えておく必要がある。

Roleの例

Postfixを設定しつつ、monitでプロセス監視を行うRoleはこのようになる。

postfix_client.json
{
  "name": "postfix_client"
  "description": "settings for postfix as sattelite",
  "json_class": "Chef::Role",
  "default_attributes": {
    "monit": {
      "process": {
        "targets": {
          "postfix": {                                                                                                       
            "type": "pid",
            "pidfile": "/var/spool/postfix/pid/master.pid",
            "start_program": "/usr/sbin/service postfix start",
            "stop_program": "/usr/sbin/service postfix stop",
            "policies": [
              "if failed host 127.0.0.1 port 25 protocol smtp timeout 30 seconds then restart",
              "if 2 restarts within 3 cycles then timeout"
            ]
          }
        }
      }
    }
  },
  "override_attributes": {
    "postfix": {
      "relayhost": "smtp.example.jp",
      "aliases": {
        "root": [
          "sawanoboly@example.jp"
        ]
      },
      "mail_type": "client",
      "smtpd_use_tls": "no"
    }
  },
  "chef_type": "role",
  "run_list": [
    "recipe[postfix::default]",
    "recipe[postfix::default]"
  ],
  "env_run_lists": {
  }
}

targets以下のmonit用コンフィグはよそのRoleとマージされて複数のTargetにすることができる。

attributesについて

サンプルではpostfixがoverrideでmonitの分をdefaultにしている。
Environmentなど既存のattributesにマージする場合はdefault, 上書きしてしまいたい時にOverrideを使っている。

例えばpostfixのrootへのaliasesのアレイはdefaultだと他とマージされて複数になるケースがあるがoverrideだとここの"root": ["sawanoboly@example.jp"]だけが使用される。

出来上がるmonitのコンフィグ

もらったattributesを使ってテンプレートからビルドされる。

postfix(monit_config)
## Generated by Chef for test01.example.jp

check process postfix with pidfile /var/spool/postfix/pid/master.pid
  start program = "/usr/sbin/service postfix start"
  stop program  = "/usr/sbin/service postfix stop"
  if failed host 127.0.0.1 port 25 protocol smtp timeout 30 seconds then restart
  if 2 restarts within 3 cycles then timeout

設定の追加や更新があったらmonitも勝手にReloadされて手間いらず。

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6