目的
監視の仕組みを独自で作成した際に問題となるのは、
外部のアラートの仕組みなどと連携する場合です。
ここでは、Pythonで作成した監視の仕組みとPagerDuryを
連携させるための例を説明します。
手順概要
- PagerDutyのIntegrationを設定
- Pythonのコードを記述
手順詳細
PagerDutyのIntergrationを設定
Serviceの作成
Configutaion
-> Services
と選択します。
Add New Services
ボタンをクリックします。
次は、ServiceとIntegrationの設定を進めます。
(Escalation Policyなどの設定は含めません)
- Serviceの名前を指定します
-
Use our API directry
を選択します -
Events API v2
を選択します -
Intagration
の名前を設定します
あとはよしなに。
Add Service
ボタンをクリックします。
先ほど作成したService
を選択し、Integration Key
を選びます。
Pythonのコードを記述
pypd - PagerDuty Python API Clientを利用します
pagerduty-api-python-clientのインストール
pip install pypd
コードを記述
単純にIncident
を生成するだけのコードです。
integration_keyには先ほど作成した。
PagerDutyとのIntegration Key
を指定します。
Incident生成のサンプルコード
import pypd
integration_key = 'hogehogehogehoge'
pypd.EventV2.create(data={
'routing_key': integration_key,
'event_action': 'trigger',
'payload': {
'summary': 'test',
'severity': 'error',
'source': 'test',
}
})
本コードをベースとして、任意のタイミングでPagerDutyでIncident
を生成することができます。
他にもIncident
の一覧を取得したりなど様々な作業をpypdを用いて実現できそうです。