Python クライアントを使用したアラートの管理
アラートはIBM Cloud Monitoringインスタンスを使用してMonitoringPythonクライアント。
Python クライアントの使用方法については、Python クライアントの使用を参照してください。
アラートの作成 (POST)
次のコードは、アラートを作成するために使用できる Python スクリプトの構造を示しています。
# Reference the Python client
from sdcclient import IbmAuthHelper, SdMonitorClient
# Add the monitoring instance information that is required for authentication
URL = <MONITORING-ENDPOINT>
APIKEY = <IAM_APIKEY>
GUID = <GUID>
ibm_headers = IbmAuthHelper.get_headers(URL, APIKEY, GUID)
# Instantiate the Python client
sdclient = SdMonitorClient(sdc_url=URL, custom_headers=ibm_headers)
# Add the notification channels to send an alert when the alert is triggered
notify_channels = [
{
'type': 'SLACK',
'channel': '<SLACK_CHANNEL_NAME>'
},
{
'type': 'EMAIL',
'emailRecipients': [
'user1@ibm.com', 'user2@ibm.com'
]
}
]
# Get the IDs of the notification channels that you have configured
res = sdclient.get_notification_ids(notify_channels)
if not res[0]:
print("Failed to fetch notification channel ID's")
notification_channel_ids = res
# Create and define the alert details
res = sdclient.create_alert(
name=<ALERT_NAME>,
description=<ALERT_DESCRIPTION>,
severity=<SEVERITY>,
for_atleast_s=<FOR_ATLEAST_S>,
condition=<CONDITION>,
segmentby=<SEGMENTBY>,
segment_condition=<SEGMENT_CONDITION>,
user_filter=<USER_FILTER>,
notify=<NOTIFICATION_CHANNEL_IDS>,
enabled=<ENABLED>,
annotations=<ANNOTATIONS>,
alert_obj=<ALERT_OBJ>
)
if not res[0]:
print("Alert creation failed")
Python スクリプトを作成する場合は、以下の情報を考慮してください。
-
以下の情報を含める必要があります:
<MONITORING-ENDPOINT>
、<IAM_APIKEY>
、 そして<GUID>
これらのデータは、監視インスタンスでリクエストを認証するために必要です。 モニター・インスタンスの情報を取得するには、IAM を使用してユーザー ID またはサービス ID を認証するを参照してください。 -
アラートがトリガーされたときの通知に使用する通知チャネルを定義する必要があります。
有効な通知チャネルのタイプは、
SLACK
、PAGER_DUTY
、VICTOROPS
、WEBHOOK
、およびEMAIL
です。通知チャネルを定義する際、チャネルをモニタリング・インスタンスで構成する必要があります。
E メール通知チャネルを追加する際、複数の宛先を追加できます。 値は、コンマを使用して区切ります。
Slackチャンネルを定義するときは、
<SLACK_CHANNEL_NAME>
チャンネル名を入力します。 記号#
をチャネルの名前に含める必要があります。例えば、#my_monitoring_alert_channel
のようにします。
アラートを構成する場合、次のセクションに入力します。
-
[
name
そしてdescription
]: アラート名には、次の文字列を置き換えて一意の名前を定義する必要があります。<ALERT_NAME>
を置き換えて説明を追加することもできます。<ALERT_DESCRIPTION>
。 -
[
severity
]: アラートの重大度を定義するには、<SEVERITY>
番号付き。 有効な値は、0
、1
、2
、3
、4
、5
、6
、および7
です。 -
[
for_atleast_s
]: アラートがトリガーされる前に条件が満たされる連続秒数を定義する必要があります。 交換する<FOR_ATLEAST_S>
秒数で。 -
[
condition
]: アラートがいつトリガーされるかを定義する条件を定義する必要があります。 例えば、このパラメーターを['host.mac', 'proc.name']
に設定して、各マシンのすべてのプロセスの CPU アラートを確認できます。詳しくは、Multi-Condition Alerts を参照してください。
-
[
segmentby
]: アラートの範囲を定義するには、segmentedby
セクション。 デフォルト値はANY
です。 -
[
segment_condition
]: パラメータセグメント別が指定されている場合は、このフィールドを設定してアラートがいつトリガーされるかを決定します。 有効な値は、ANY および ALL です。 -
[
user_filter
]: 通知が送信されるタイミングを示すフィルターを定義できます。 例えば、プロセスの名前が条件を満たしている場合にのみ通知を受け取るようにする場合は、この項目を定義します。 -
[
notify
]: アラートで生成する通知の種類を定義できます。 この入力内容を、定義済みのチャネルの通知 ID に設定します。 -
[
enabled
]: アラートの作成時にアラートのステータスを設定できます。 デフォルトでは、アラートは有効になり、入力内容はtrue
に設定されます。 作成時に有効にしない場合は、false
に設定します。 -
[
annotations
]: アラートに関連付けることができるカスタム プロパティを追加できます。 -
[
alert_obj
]: 個々のパラメータを指定する代わりに、アラート オブジェクトを添付することができます。
アラートの更新 (PUT)
既存のアラートを更新するには、そのアラートの ID が必要です。
次のコードは、アラートを更新するために使用できる Python スクリプトの構造を示しています。
# Reference the Python client
from sdcclient import IbmAuthHelper, SdMonitorClient
# Add the monitoring instance information that is required for authentication
URL = <MONITORING-ENDPOINT>
APIKEY = <IAM_APIKEY>
GUID = <GUID>
ibm_headers = IbmAuthHelper.get_headers(URL, APIKEY, GUID)
# Instantiate the Python client
sdclient = SdMonitorClient(sdc_url=URL, custom_headers=ibm_headers)
res = sdclient.get_alerts()
if not res[0]:
print("Failed to fetch existing alerts")
alert_found = False
for alert in res['alerts']:
if alert['name'] == alert_name:
alert_found = True
if 'notificationChannelIds' in alert:
alert['notificationChannelIds'] = alert['notificationChannelIds'][0:-1]
update_txt = '(changed by update_alert)'
if alert['description'][-len(update_txt):] != update_txt:
alert['description'] = alert['description'] + update_txt
alert['timespan'] = alert['timespan'] * 2 # Note: Expressed in seconds * 1000000
res_update = sdclient.update_alert(alert)
if not res_update:
print("Alert update failed")
if not alert_found:
print('Alert to be updated not found')
アラートの削除 (DELETE)
既存のアラートを削除するには、そのアラートの ID が必要です。
次のコードは、アラートを削除するために使用できる Python スクリプトの構造を示しています。
# Reference the Python client
from sdcclient import IbmAuthHelper, SdMonitorClient
# Add the monitoring instance information that is required for authentication
URL = <MONITORING-ENDPOINT>
APIKEY = <IAM_APIKEY>
GUID = <GUID>
ibm_headers = IbmAuthHelper.get_headers(URL, APIKEY, GUID)
# Instantiate the Python client
sdclient = SdMonitorClient(sdc_url=URL, custom_headers=ibm_headers)
res = sdclient.get_alerts()
if not res[0]:
print("Failed to fetch existing alerts")
for alert in res['alerts']:
if alert['name'] == alert_name:
print("Deleting alert")
res = sdclient.delete_alert(alert)
if not res:
print("Alert deletion failed")
すべてのユーザー・アラートの取得 (GET)
次のコードは、すべてのアラートに関する情報を取得するために使用できる Python スクリプトの構造を示しています。
# Reference the Python client
from sdcclient import IbmAuthHelper, SdMonitorClient
# Add the monitoring instance information that is required for authentication
URL = <MONITORING-ENDPOINT>
APIKEY = <IAM_APIKEY>
GUID = <GUID>
ibm_headers = IbmAuthHelper.get_headers(URL, APIKEY, GUID)
# Instantiate the Python client
sdclient = SdMonitorClient(sdc_url=URL, custom_headers=ibm_headers)
json_res = sdclient.get_alerts()
print(json_res)