LoginSignup
2

More than 5 years have passed since last update.

StackStormでNew Relicと連携してみました

Last updated at Posted at 2017-01-06

image

はじめに

前回はAWSにStackStormをインストールしたので、今回はアプリケーション監視ツールであるNew Relicと連携できるようにしたいと思います。
StackStormには、Packと呼ばれるソフトウェアコンポーネントの追加機能があり、New RelicのPackも用意されています。今回はPackのインストールから使い方まで紹介します。

New Relicに関しては、下記記事を参考にしてください。
New RelicのAPMとINFRASTRUCTUREを使って監視してみました(前半)
New RelicのAPMとINFRASTRUCTUREを使って監視してみました(後半)

StackStomの設定

New Relic Packのインストール

下記st2コマンドでNewRelicのPackをインストールします。statusがsucceededになっていたら成功です。

$ st2 run packs.install packs=newrelic
......................
id: 586df3da897d4405b3b07f36
action.ref: packs.install
parameters:
  packs:
  - newrelic
status: succeeded
start_timestamp: 2017-01-05T07:20:58.022719Z
end_timestamp: 2017-01-05T07:23:13.100056Z
+--------------------------+-------------------------+--------------------------+-------------------------+------------------------------+
| id                       | status                  | task                     | action                  | start_timestamp              |
+--------------------------+-------------------------+--------------------------+-------------------------+------------------------------+
| 586df3da897d440342b8b6ba | succeeded (7s elapsed)  | download                 | packs.download          | Thu, 05 Jan 2017 07:20:58    |
|                          |                         |                          |                         | UTC                          |
| 586df3e2897d440342b8b6bd | succeeded (3s elapsed)  | virtualenv_prerun        | packs.virtualenv_prerun | Thu, 05 Jan 2017 07:21:06    |
|                          |                         |                          |                         | UTC                          |
| 586df3e5897d440342b8b6c0 | succeeded (56s elapsed) | setup_virtualenv         | packs.setup_virtualenv  | Thu, 05 Jan 2017 07:21:09    |
|                          |                         |                          |                         | UTC                          |
| 586df41e897d440342b8b6c4 | succeeded (63s elapsed) | reload                   | packs.load              | Thu, 05 Jan 2017 07:22:06    |
|                          |                         |                          |                         | UTC                          |
| 586df45e897d440342b8b6c8 | succeeded (2s elapsed)  | restart-sensor-container | packs.restart_component | Thu, 05 Jan 2017 07:23:10    |
|                          |                         |                          |                         | UTC                          |
+--------------------------+-------------------------+--------------------------+-------------------------+------------------------------+

New Relic Packの確認

1) センサー確認

$ st2 sensor list
+---------------------------------+----------+---------------------------------+---------+
| ref                             | pack     | description                     | enabled |
+---------------------------------+----------+---------------------------------+---------+
| newrelic.NewRelicHookSensor     | newrelic | Sensor which watches for alerts | True    |
|                                 |          | from NewRelic API.              |         |
| newrelic.LegacyNewRelicHookSens | newrelic | Sensor which watches for alerts | False   |
| or                              |          | from the NewRelic legacy API.   |         |
+---------------------------------+----------+---------------------------------+---------+

2)トリガー確認

$ st2 trigger list
+--------------------------------------+----------+-------------------------------------------+
| ref                                  | pack     | description                               |
+--------------------------------------+----------+-------------------------------------------+
| newrelic.WebAppAlertTrigger          | newrelic |                                           |
| newrelic.WebAppNormalTrigger         | newrelic |                                           |
| newrelic.ServerAlertTrigger          | newrelic |                                           |
| newrelic.ServerNormalTrigger         | newrelic |                                           |
+--------------------------------------+----------+-------------------------------------------+

3)アクション確認

$ st2 action list | grep new
+---------------------------------+----------+-------------------------------------------+
| ref                             | pack     | description                               |
+---------------------------------+----------+-------------------------------------------+
| newrelic.get_alerts             | newrelic | Get alerts for app.                       |
| newrelic.get_metric_data        | newrelic | Get metric data for metric.               |

New Relic Packの設定

1) 設定ファイルコピー
New RelicのPackは、下記フォルダーに保存されるので、

/opt/stackstorm/packs/newrelic/

サンプルファイルから設定ファイルを作成します

cd /opt/stackstorm/configs
cp /opt/stackstorm/packs/newrelic/newrelic.yaml.example ./newrelic.yaml

2) 設定ファイルの編集
デフォルトだとBase URLが、http://x.x.x.x:10001/st2/nrhook/になります

$vim ./newrelic.yaml
---
api_url: "https://api.newrelic.com/v2/"
api_key: "<New RelicのAPIキーを入力します>"
sensor_config:
    host: "0.0.0.0"
    port: 10001
    url: "/st2/nrhook/"
    normal_report_delay: 300

3) st2 再起動
設定を有効にするためにStackStormを再起動します

$ st2ctl reload

4) TCPポート起動確認
New RelicのSenserが起動すると、TCPの10001番でListenします

$ netstat -ln | grep 10001
tcp        0      0 0.0.0.0:10001           0.0.0.0:*               LISTEN

ルール作成

New Relicから送信されたアラート内容を、ファイル(st2.newrelic.out)に出力するルールを作成

1) APMアラート用ルール作成

$ cd /opt/stackstorm/packs/newrelic/rules/
$ vim webapp_notify.yaml
---
name: "webapp.notify"
pack: "newrelic"
description: "WebAppAlertTrigger Sample rule for newrelic."
enabled: true

trigger:
    type: "newrelic.WebAppAlertTrigger"

criteria: {}

action:
    ref: "core.local"
    parameters:
        cmd: "echo {{ trigger.alert }} >> ~/st2.newrelic.out"

2) Serversアラート用ルール作成

$ vim server_notify.yaml
---
name: "server.notify"
pack: "newrelic"
description: "ServerAlertTrigger Sample rule for newrelic."
enabled: true

trigger:
    type: "newrelic.ServerAlertTrigger"

criteria: {}

action:
    ref: "core.local"
    parameters:
        cmd: "echo {{ trigger.alert }} >> ~/st2.newrelic.out"

3)st2ルール作成

st2 rule create /opt/stackstorm/packs/newrelic/rules/webapp_notify.yaml
st2 rule create /opt/stackstorm/packs/newrelic/rules/server_notify.yaml

4)st2ルール確認

$ st2 rule list
+------------------------+----------+-----------------------------------------+---------+
| ref                    | pack     | description                             | enabled |
+------------------------+----------+-----------------------------------------+---------+
| newrelic.server.notify | newrelic | ServerAlertTrigger Sample rule for      | True    |
|                        |          | newrelic.                               |         |
| newrelic.webapp.notify | newrelic | WebAppAlertTrigger Sample rule for      | True    |
|                        |          | newrelic.                               |         |
+------------------------+----------+-----------------------------------------+---------+

動作確認

StackStomから自分宛(127.0.0.1)にテスト用のアラートを送信して確認します。

1) APMアラート確認用

curl -X POST \
    -H 'Content-Type:application/json; charset=UTF-8' \
    -d '{ 
    "current_state":"open",
    "details":"Apdex < .50 for at least 10 min",
    "severity":"CRITICAL",
    "event_type":"INCIDENT",
    "targets":[{
        "product": "APM", 
        "name": "TEST APM",
        "labels": {}, 
        "type": "Application", 
        "id": "123456789"
        }]
    }' \
http://127.0.0.1:10001/st2/nrhook/

2) Serversアラート確認用

curl -X POST \
    -H 'Content-Type:application/json; charset=UTF-8' \
    -d '{ 
    "current_state":"open",
    "details":"System load > 0.1 for at least 5 minutes",
    "severity":"CRITICAL",
    "event_type":"INCIDENT",
    "targets":[{
        "product": "SERVERS", 
        "name": "TEST Servers",
        "labels": {}, 
        "type": "Server", 
        "id": "123456789"
        }]
    }' \
http://127.0.0.1:10001/st2/nrhook/

3) アラート内容の確認

$ tail /home/stanley/st2.newrelic.out
{uevent_type: uINCIDENT, udetails: uApdex < .50 for at least 10 min, ucurrent_state: uopen, utargets: [{uproduct: uAPM, ulabels: {}, utype: uApplication, uname: uTEST APM, uid: u123456789}], useverity: uCRITICAL}
{uevent_type: uINCIDENT, udetails: uSystem load > 0.1 for at least 5 minutes, ucurrent_state: uopen, utargets: [{uproduct: uSERVERS, ulabels: {}, utype: uServer, uname: uTEST Servers, uid: u123456789}], useverity: uCRITICAL}

AWSの設定

1)セキュリテイ―グループの追加
外部(New Relic)からTCP:10001の接続ができるようセキュリテイ―グループを変更
New Relic DocumentのWebhook alerts項目にアラートの送信元IPが記載されているので、必要であれば送信元を制限します。

Webhook alerts
New Relic-generated webhooks for alert policies will be sent from an IP address in the 50.31.164.0/24 or 162.247.240.0/22 network block.

New Relic Alertsの設定

1)AlertsのNotification Channelの追加
image

2)Channelの設定
StackStomの待ち受けポート(10001/tcp)に対してWebhookの設定をします。
image

3)Alert Policyの作成

image

4)Alerts Conditionの作成
アラート条件を設定します
image

5)Channelの選択
先ほど作成したStackStomのWebhookのChannelを選択し追加します。
image

これでNew RelicからのAlertsをトリガーにStackStormで連携する事ができました。

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