LoginSignup
6
3

More than 5 years have passed since last update.

PythonからPagerDutyのIncidentを生成する

Posted at

目的

監視の仕組みを独自で作成した際に問題となるのは、
外部のアラートの仕組みなどと連携する場合です。

ここでは、Pythonで作成した監視の仕組みとPagerDuryを
連携させるための例を説明します。

手順概要

  1. PagerDutyのIntegrationを設定
  2. Pythonのコードを記述

手順詳細

PagerDutyのIntergrationを設定

Serviceの作成

Configutaion -> Services と選択します。

image.png

Add New Servicesボタンをクリックします。

image.png

次は、ServiceとIntegrationの設定を進めます。
(Escalation Policyなどの設定は含めません)

image.png

  1. Serviceの名前を指定します
  2. Use our API directryを選択します
  3. Events API v2を選択します
  4. Intagrationの名前を設定します

あとはよしなに。
Add Service ボタンをクリックします。

image.png

先ほど作成したServiceを選択し、Integration Keyを選びます。

image.png

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を用いて実現できそうです。

6
3
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
3