【Sensu】Ubuntu14.04にsensuをインストールする（設定編）

sensuを簡単な設定で動かすところまで。

# RabbitMQ

vhostを作成する

``` 
sudo rabbitmqctl add_vhost /sensu
```

sensu用のRabbitMQユーザーを作成する

``` 
sudo rabbitmqctl add_user sensu secret
sudo rabbitmqctl set_permissions -p /sensu sensu ".*" ".*" ".*"
```

# 設定ファイル

`/etc/sensu/config.json`か`/etc/sensu/conf.d/`の下にJSONで設定を書くことができる。ファイルを細かく分割することも可能。

インストールした時に`/etc/sensu/config.json.example`というのがあったので動かしてみる。

```
mv /etc/sensu/config.json.example /etc/sensu/config.json
```

``` /etc/sensu/config.json
{
  "rabbitmq": {
    "host": "localhost",
    "port": 5672,
    "user": "sensu",
    "password": "sensu",
    "vhost": "/sensu"
  },
  "redis": {
    "host": "localhost",
    "port": 6379
  },
  "api": {
    "host": "localhost",
    "port": 4567
  },
  "handlers": {
    "default": {
      "type": "set",
      "handlers": [
        "stdout"
      ]
    },
    "stdout": {
      "type": "pipe",
      "command": "cat"
    }
  },
  "checks": {
    "test": {
      "command": "echo -n OK",
      "subscribers": [
        "test"
      ],
      "interval": 60
    }
  },
  "client": {
    "name": "localhost",
    "address": "127.0.0.1",
    "subscriptions": [
      "test"
    ]
  }
}
```

`sensu-server`, `sensu-client`, `sensu-api`をリスタートする。

`sensu-api`を叩いて動いているか確認します。

```
$ curl http://localhost:4567/info | jq . 

{
  "sensu": {
    "version": "0.25.5"
  },
  "transport": {
    "keepalives": {
      "messages": 0,
      "consumers": 1
    },
    "results": {
      "messages": 0,
      "consumers": 1
    },
    "connected": true
  },
  "redis": {
    "connected": true
  }
}
```

ちゃんと返ってきた。
あと、`/var/log/sensu/sensu-client.log`, `/var/log/sensu/sensu-server.log`もでてた。

```/var/log/sensu-client.log
{"timestamp":"2016-07-23T18:08:26.828593+0000","level":"info","message":"received check request","check":{"command":"echo -n OK","name":"test","issued":1469297306}}
{"timestamp":"2016-07-23T18:08:26.830864+0000","level":"info","message":"publishing check result","payload":{"client":"localhost","check":{"command":"echo -n OK","name":"test","issued":1469297306,"subscribers":["test"],"interval":60,"executed":1469297306,"duration":0.001,"output":"OK","status":0}}}
```

# 監視項目を追加する

Checksを追加して監視項目を設定する。
Railsで作ったアプリケーションを監視してみる。

```
rails new sample
cd sample
bundle exec rails s -b 0.0.0.0
```

[sensu-plugins-http](https://github.com/sensu-plugins/sensu-plugins-http)をインストールする。


```
sensu-install -p http
```

`/etc/sensu/conf.d`に`check-sensu-website.json`という新しい設定ファイルを追加する。

[Checks](https://sensuapp.org/docs/0.25/reference/checks.html)を追加して監視する設定を書いていく。
60秒毎に`localhost:3000`を監視する。
`subscribers`は[Client subscriptions](https://sensuapp.org/docs/0.25/reference/clients.html#client-subscriptions)と対応していないとダメ。`/etc/sensu/config.json`のclientがtestなのでtestにする。

```/etc/sensu/conf.d/check-sensu-website.json
{
  "checks": {
    "sensu-website": {
      "command": "check-http.rb -u http://localhost:3000",
      "subscribers": [
        "test"
      ],
      "interval": 60
    }
  }
}
```

sensuをリスタートする

```
sudo service sensu-server restart
sudo service sensu-client restart
sudo service sensu-api restart
```

`/var/log/sensu-client.log`を見てみる。
`"name":"sensu-website"`のログが出ているのを確認。`"status":0`なのでRailsは正常に動いている。

```/var/log/sensu-client.log
{"timestamp":"2016-07-23T18:07:02.436898+0000","level":"info","message":"received check request","check":{"command":"check-http.rb -u http://localhost:3000","name":"sensu-website","issued":1469297222}}
{"timestamp":"2016-07-23T18:07:02.641105+0000","level":"info","message":"publishing check result","payload":{"client":"localhost","check":{"command":"check-http.rb -u http://localhost:3000","name":"sensu-website","issued":1469297222,"subscribers":["test"],"interval":60,"executed":1469297222,"duration":0.203,"output":"CheckHttp OK: 200, 14935 bytes\n","status":0}}}
```

Railsを止めてみる。
ログを見てみると``"status":2`で`output`も`"CheckHttp CRITICAL: Request error: Failed to open TCP connection to localhost:3000 (Connection refused - connect(2) for \"localhost\" port 3000)\n"`になって`localhost:3000`にアクセスできないことが検知できてる。

```/var/log/sensu-client.log
{"timestamp":"2016-07-23T18:29:03.132764+0000","level":"info","message":"received check request","check":{"command":"check-http.rb -u http://localhost:3000","name":"sensu-website","issued":1469298543}}
{"timestamp":"2016-07-23T18:29:03.331503+0000","level":"info","message":"publishing check result","payload":{"client":"localhost","check":{"command":"check-http.rb -u http://localhost:3000","name":"sensu-website","issued":1469298543,"subscribers":["test"],"interval":60,"executed":1469298543,"duration":0.198,"output":"CheckHttp CRITICAL: Request error: Failed to open TCP connection to localhost:3000 (Connection refused - connect(2) for \"localhost\" port 3000)\n","status":2}}}
```

監視できた。