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
]: 경고가 트리거되는 시기를 정의하는 조건을 정의해야 합니다. 예를 들어, 모든 시스템의 모든 프로세스에 대해 CPU 경보를 확인하기 위해 이 매개변수를['host.mac', 'proc.name']
으로 설정할 수 있습니다.자세한 정보는 다중 조건 경보를 참조하십시오.
-
[
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)