このトピックでは、Python 用 Simple Log Service SDK を使用してアラートを管理する方法について説明し、サンプルコードを提供します。
前提条件
課金方法
アラート機能の場合、SMS メッセージと音声通話を使用してアラート通知を受信した場合にのみ課金されます。 詳細については、「Simple Log Service の価格」をご参照ください。
通知方法 | 説明 |
通知方法 | 説明 |
SMS メッセージ | SMS メッセージを使用して通知を受信すると、料金が発生します。 アラート通知を受信した回数に基づいて課金されます。 SMS メッセージが 70 文字を超える場合は、2 つのメッセージで送信される場合があります。 この場合、1 回だけ課金されます。 |
音声通話 | 音声通話を使用して通知を受信すると、料金が発生します。 アラート通知を受信した回数に基づいて課金されます。
|
アラートルールの管理
次のサンプルコードは、アラートルールを管理する方法の例を示しています。 サンプルコードのパラメーターの詳細については、「アラートルールのデータ構造」をご参照ください。
import os
from aliyun.log import LogClient
# Simple Log Service エンドポイント。
endpoint = 'cn-huhehaote.log.aliyuncs.com'
# 環境変数を構成します。 この例では、AccessKey ID と AccessKey シークレットは環境変数から取得されます。
accesskey_id = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accesskey_secret = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# Simple Log Service クライアントを作成します。
client = LogClient(endpoint, accesskey_id, accesskey_secret)
project = 'demo-alert'
alert_id = 'nginx-status-error'
def create_alert():
alert = {
'name': alert_id,
'displayName': 'Nginx ステータスエラー',
'type': 'Alert',
'state': 'Enabled',
'schedule': {
'type': 'FixedRate',
'interval': '1m'
},
'configuration': {
'version': '2.0',
'type': 'default',
'dashboard': 'internal-alert-analysis',
'queryList': [{
# アラートデータのタイプ。 有効な値:log、metric、および meta。 値 log は Logstore を指定します。 値 metric は Metricstore を指定します。 値 meta はリソースデータを指定します。
'storeType': 'log',
# サポートされているリージョン。
'region': 'cn-huhehaote',
# プロジェクトの名前。
'project': 'demo-alert',
# Logstore または Metricstore の名前。
'store': 'nginx-access-log',
# クエリ文。
'query': 'status >= 400 | select count(*) as cnt',
# 時間範囲のタイプ。
'timeSpanType': 'Truncated',
# 開始時刻。
'start': '-1m',
# 終了時刻。
'end': 'absolute',
# 専用 SQL を有効にするかどうかを指定します。
'powerSqlMode': 'auto'
}],
'groupConfiguration': {
'type': 'no_group',
'fields': []
},
'joinConfigurations': [],
'severityConfigurations': [{
'severity': 6,
'evalCondition': {
'condition': 'cnt > 0',
'countCondition': ''
}
}],
'labels': [{
'key': 'service',
'value': 'nginx'
}],
'annotations': [{
'key': 'title',
'value': 'Nginx ステータスエラー'
}, {
'key': 'desc',
'value': 'Nginx ステータスエラー、カウント:${cnt}'
}],
'autoAnnotation': True,
'sendResolved': False,
'threshold': 1,
'noDataFire': False,
'noDataSeverity': 6,
'policyConfiguration': {
'alertPolicyId': 'sls.builtin.dynamic',
'actionPolicyId': 'test-action-policy',
'repeatInterval': '1m',
'useDefault': False
}
}
}
res = client.create_alert(project, alert)
res.log_print()
def get_and_update_alert():
res = client.get_alert(project, alert_id)
res.log_print()
alert = res.get_body()
alert['configuration']['queryList'][0]['query'] = 'status >= 400 | select count(*) as cnt'
res = client.update_alert(project, alert)
res.log_print()
def enable_and_disable_alert():
res = client.disable_alert(project, alert_id)
res.log_print()
res = client.enable_alert(project, alert_id)
res.log_print()
def list_alerts():
res = client.list_alert(project, offset=0, size=100)
res.log_print()
def delete_alert():
res = client.delete_alert(project, alert_id)
res.log_print()
if __name__ == '__main__':
create_alert()
get_and_update_alert()
enable_and_disable_alert()
list_alerts()
delete_alert()
アラートリソースデータの管理
次のサンプルコードは、アラートリソースデータを管理する方法の例を示しています。 サンプルコードのパラメーターの詳細については、「アラートリソースデータのデータ構造」をご参照ください。