使用以下方式管理警報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。 -
您必須定義觸發警報時您希望收到通知的通知管道。
有效的通知管道類型是
SLACK,PAGER_DUTY,VICTOROPS,WEBHOOK,和EMAIL。定義通知通道時,必須在監控實例中配置通道。
新增電子郵件通知管道時,您可以新增多個收件者。 您可以使用逗號分隔值。
當您定義 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 警報。有關更多信息,請參閱 多條件警報。
-
[
segmentby]:您可以透過配置來定義警報的範圍segmentedby部分。 預設值為ANY。 -
[
segment_condition]:當參數分段比如果已指定,則設定此欄位以確定何時觸發警報。 有效值為任何和全部。 -
[
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)