Skip to content

Commit

Permalink
support template_parm
Browse files Browse the repository at this point in the history
  • Loading branch information
v_connliu committed Sep 17, 2021
1 parent 92adbfa commit 479f1c3
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 10 deletions.
35 changes: 35 additions & 0 deletions docs/source/ruletypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://intl.cloud.tencent.com/document/api/382/40466#region-list>`_ or
`Global <https://cloud.tencent.com/document/api/382/52071#.E5.9C.B0.E5.9F.9F.E5.88.97.E8.A1.A8>`_) 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 <https://datatracker.ietf.org/doc/html/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
~~~~~~~
Expand Down
16 changes: 9 additions & 7 deletions elastalert/alerters/tencentsms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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__))

Expand All @@ -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")
Expand All @@ -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
}
3 changes: 2 additions & 1 deletion examples/rules/example_tencent_sms.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
tencentcloud-sdk-python>=3.0.484
jsonpointer>=2.1
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]
)

0 comments on commit 479f1c3

Please sign in to comment.