From 479f1c345225ef4326898daa3f5c1642ca886d7b Mon Sep 17 00:00:00 2001 From: v_connliu Date: Fri, 17 Sep 2021 20:56:34 +0800 Subject: [PATCH] support template_parm --- docs/source/ruletypes.rst | 35 +++++++++++++++++++++++++ elastalert/alerters/tencentsms.py | 16 ++++++----- examples/rules/example_tencent_sms.yaml | 3 ++- requirements.txt | 3 ++- setup.py | 3 ++- 5 files changed, 50 insertions(+), 10 deletions(-) diff --git a/docs/source/ruletypes.rst b/docs/source/ruletypes.rst index 33cc5794..4bdee7e5 100644 --- a/docs/source/ruletypes.rst +++ b/docs/source/ruletypes.rst @@ -2840,6 +2840,41 @@ Note: this parameter is required for Mainland China SMS. ``tencent_sms_region``: Region parameter, which is used to identify the region(`Mainland China `_ or `Global `_) to which the data you want to work with belongs. +``tencent_sms_template_parm``: The number of template parameters needs to be consistent with the number of variables of the template corresponding to TemplateId. +this value format by `rfc6901 `_ + +.. code-block:: json + + { + "_index" : "tmec" + "_type" : "fluentd", + "_id" : "PeXLrnsBvusb3d0w6dUl", + "_score" : 1.0, + "_source" : { + "kubernetes" : { + "host" : "9.134.191.187", + "pod_id" : "66ba4e5a-1ad2-4655-9a8e-cffb6b942559", + "labels" : { + "release" : "nginx", + "pod-template-hash" : "6bd96d6f74" + }, + "namespace_name" : "app", + "pod_name" : "app.nginx-6bd96d6f74-2ts4x" + }, + "time" : "2021-09-04T03:13:24.192875Z", + "message" : "2021-09-03T14:34:08+0000|INFO|vector eps : 192.168.0.2:10000,", + } + } + + +.. code-block:: yaml + + tencent_sms_template_id: "1123835" + tencent_sms_template_parm: + - "/kubernetes/pod_name" + + + TheHive ~~~~~~~ diff --git a/elastalert/alerters/tencentsms.py b/elastalert/alerters/tencentsms.py index b1fd6473..89bd1d40 100644 --- a/elastalert/alerters/tencentsms.py +++ b/elastalert/alerters/tencentsms.py @@ -6,6 +6,7 @@ from tencentcloud.common.profile.http_profile import HttpProfile from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException from tencentcloud.sms.v20210111 import sms_client, models +from jsonpointer import resolve_pointer class TencentSMSAlerter(Alerter): @@ -34,7 +35,7 @@ def __init__(self, *args): self.tencent_sms_region = self.rule.get('tencent_sms_region', 'ap-guangzhou') self.tencent_sms_sign_name = self.rule.get('tencent_sms_sign_name') # this parameter is required for Mainland China SMS. self.tencent_sms_template_id = self.rule.get('tencent_sms_template_id') - # self.tencent_sms_template_parm = self.rule.get('tencent_sms_template_parm', []) + self.tencent_sms_template_parm = self.rule.get('tencent_sms_template_parm', []) # Alert is called def alert(self, matches): @@ -110,11 +111,9 @@ def alert(self, matches): # Template parameters. If there are no template parameters, leave it empty req.TemplateParamSet = [] - for item in matches: - for key, val in item.items(): - if key.startswith('_'): - continue - req.TemplateParamSet.append(f'{key}:{val}') + esData = matches[0] + for key in self.tencent_sms_template_parm: + req.TemplateParamSet.append(resolve_pointer(esData, key)) elastalert_logger.debug("SendSms request :%s", json.dumps(req.__dict__)) @@ -123,6 +122,9 @@ def alert(self, matches): resp = client.SendSms(req) # A string return packet in JSON format is outputted elastalert_logger.debug("SendSms response :%s", resp.to_json_string()) + for item in resp.SendStatusSet: + if item.Code != "Ok": + raise EAException(json.dumps(item.__dict__)) except TencentCloudSDKException as e: raise EAException("Error posting to TencentSMS: %s" % e) elastalert_logger.info("Alert sent to TencentSMS") @@ -133,5 +135,5 @@ def alert(self, matches): def get_info(self): return { 'type': 'tencent sms', - 'to_number':self.tencent_sms_to_number + 'to_number': self.tencent_sms_to_number } diff --git a/examples/rules/example_tencent_sms.yaml b/examples/rules/example_tencent_sms.yaml index dc3336b2..9108af06 100755 --- a/examples/rules/example_tencent_sms.yaml +++ b/examples/rules/example_tencent_sms.yaml @@ -61,4 +61,5 @@ tencent_sms_to_number: tencent_sms_region: "ap-guangzhou" tencent_sms_sign_name: "tencent" tencent_sms_template_id: "1123835" - +tencent_sms_template_parm: + - "/kubernetes/pod_name" diff --git a/requirements.txt b/requirements.txt index ae53bd2b..b3884b29 100644 --- a/requirements.txt +++ b/requirements.txt @@ -21,4 +21,5 @@ texttable>=0.8.8 statsd-tags==3.2.1.post1 twilio>=6.0.0,<6.58 tzlocal<3.0 -tencentcloud-sdk-python>=3.0.484 \ No newline at end of file +tencentcloud-sdk-python>=3.0.484 +jsonpointer>=2.1 \ No newline at end of file diff --git a/setup.py b/setup.py index 0f952b0c..097b98e3 100644 --- a/setup.py +++ b/setup.py @@ -48,6 +48,7 @@ 'cffi>=1.11.5', 'statsd-tags==3.2.1.post1', 'tzlocal<3.0', - 'tencentcloud-sdk-python>=3.0.484' + 'tencentcloud-sdk-python>=3.0.484', + 'jsonpointer>=2.1' ] )