使用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 警报。有关详细信息,请参阅For more information, see 多条件警报。
-
[
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)