LoginSignup
2
2

More than 5 years have passed since last update.

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

Last updated at Posted at 2016-07-26

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をインストールする。

sensu-install -p http

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

Checksを追加して監視する設定を書いていく。
60秒毎にlocalhost:3000を監視する。
subscribersClient 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":2output"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}}}

監視できた。

2
2
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
2
2