概要
Datadogで、 AWS Health Integration がイベント発行したかのようなダミーのイベントを発行する方法についてまとめます。
背景
任意のAWS Healthイベントが発行された際に、期待通りにDatadogのモニターでアラートが発生するのか試したいモチベがあったため。
やり方についてざっくり解説
Datadogのカスタムイベント発行によって実現できます。
Datadogには複数のやり方で、カスタムイベントを発行することができますが、今回は Datadog API によるやり方にフォーカスします。
イベントの発行時に、 source_type_name を指定することで、本当にそのイベント発行者がイベント発行したかのように振舞うことができます。これによって、ダミーイベントが発行できます。
source_type_name の値は以下で確認できます。
ハンズオン
PythonでDatadog APIを叩くことで AWS Health Integration のダミーイベントを、発行してみます。
①イベントを発行したい Datadog Organization の APIKey, APPKey を用意します。
※今回はテストなので、スコープ制限は割愛。
②APIKey, APPKeyをそれぞれ環境変数に格納します。
export DD_API_KEY=<発行したAPIKey> export DD_APP_KEY=<発行したAPPKey>
③Pythonライブラリ: datadog-api-client-python をインストールします。
pip install datadog-api-client-python
④以下コードを実行環境に保存し、Datadogの Post an Event を叩きます。
"""
Post an event returns "OK" response
"""
from datadog_api_client import ApiClient, Configuration
from datadog_api_client.v1.api.events_api import EventsApi
from datadog_api_client.v1.model.event_create_request import EventCreateRequest
body = EventCreateRequest(
source_type_name='amazon health',
alert_type='error',
title="Example-Event: AWS_EC2_SIMPLIFIED_AUTO_RECOVERY_SUCCESS --- open",
text="A text message.",
tags=[
"test:ExampleEvent",
],
host='hogeInstance'
)
configuration = Configuration()
with ApiClient(configuration) as api_client:
api_instance = EventsApi(api_client)
response = api_instance.create_event(body=body)
print(response)
⑤レスポンスの event.url のリンクをブラウザで開くと発行されてイベントが見れます。
実際に以下のようなカスタムイベントが発行されました。

期待通り、AWS Health Integration から発行された場合に限りなく近い形となっています。
菅谷 歩 (記事一覧)