From e6d2757a9b9752f88446f6c7c8cd94df7acf0140 Mon Sep 17 00:00:00 2001 From: Nikolay Stanchev <ns17@it-innovation.soton.ac.uk> Date: Thu, 27 Sep 2018 14:35:04 +0100 Subject: [PATCH] Updates the JSON message sent when an alert is trigerred to include the event identifier --- .../clmcservice/alertsapi/utilities.py | 28 ++++++++++++++++--- src/service/clmcservice/alertsapi/views.py | 2 +- .../TICKscript/deadman-template.tick | 4 ++- .../TICKscript/relative-template.tick | 4 ++- .../TICKscript/threshold-batch-template.tick | 3 +- .../TICKscript/threshold-stream-template.tick | 4 ++- 6 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/service/clmcservice/alertsapi/utilities.py b/src/service/clmcservice/alertsapi/utilities.py index 5a2f3af..eba22f2 100644 --- a/src/service/clmcservice/alertsapi/utilities.py +++ b/src/service/clmcservice/alertsapi/utilities.py @@ -153,7 +153,7 @@ class TICKScriptTemplateFiller: @staticmethod def _fill_threshold_batch_template_vars(db=None, measurement=None, field=None, influx_function=None, critical_value=None, - comparison_operator=None, alert_period=None, topic_id=None, where_clause=None, **kwargs): + comparison_operator=None, alert_period=None, topic_id=None, event_id=None, where_clause=None, **kwargs): """ Creates a dictionary object ready to be posted to kapacitor to create a "threshold" task from template. @@ -165,6 +165,7 @@ class TICKScriptTemplateFiller: :param comparison_operator: type of comparison :param alert_period: alert period to query influx :param topic_id: topic identifier + :param event_id: event identifier :param where_clause: (OPTIONAL) argument for filtering the influx query by tag values :return: a dictionary object ready to be posted to kapacitor to create a "threshold" task from template. @@ -200,6 +201,10 @@ class TICKScriptTemplateFiller: "topicID": { "type": "string", "value": topic_id + }, + "eventID": { + "type": "string", + "value": event_id } } @@ -213,7 +218,7 @@ class TICKScriptTemplateFiller: @staticmethod def _fill_threshold_stream_template_vars(db=None, measurement=None, field=None, critical_value=None, - comparison_operator=None, topic_id=None, where_clause=None, **kwargs): + comparison_operator=None, topic_id=None, event_id=None, where_clause=None, **kwargs): """ Creates a dictionary object ready to be posted to kapacitor to create a "threshold" task from template. @@ -225,6 +230,7 @@ class TICKScriptTemplateFiller: :param comparison_operator: type of comparison :param alert_period: alert period to query influx :param topic_id: topic identifier + :param event_id: event identifier :param where_clause: (OPTIONAL) argument for filtering the influx query by tag values :return: a dictionary object ready to be posted to kapacitor to create a "threshold" task from template. @@ -248,6 +254,10 @@ class TICKScriptTemplateFiller: "topicID": { "type": "string", "value": topic_id + }, + "eventID": { + "type": "string", + "value": event_id } } @@ -262,7 +272,7 @@ class TICKScriptTemplateFiller: @staticmethod def _fill_relative_template_vars(db=None, measurement=None, field=None, influx_function=None, critical_value=None, comparison_operator=None, - alert_period=None, topic_id=None, where_clause=None, **kwargs): + alert_period=None, topic_id=None, event_id=None, where_clause=None, **kwargs): """ Creates a dictionary object ready to be posted to kapacitor to create a "relative" task from template. @@ -274,6 +284,7 @@ class TICKScriptTemplateFiller: :param comparison_operator: type of comparison :param alert_period: alert period to use for relative comparison :param topic_id: topic identifier + :param event_id: event identifier :param where_clause: (OPTIONAL) argument for filtering the influx query by tag values :return: a dictionary object ready to be posted to kapacitor to create a "relative" task from template. @@ -309,6 +320,10 @@ class TICKScriptTemplateFiller: "topicID": { "type": "string", "value": topic_id + }, + "eventID": { + "type": "string", + "value": event_id } } @@ -321,7 +336,7 @@ class TICKScriptTemplateFiller: return template_vars @staticmethod - def _fill_deadman_template_vars(db=None, measurement=None, critical_value=None, alert_period=None, topic_id=None, where_clause=None, **kwargs): + def _fill_deadman_template_vars(db=None, measurement=None, critical_value=None, alert_period=None, topic_id=None, event_id=None, where_clause=None, **kwargs): """ Creates a dictionary object ready to be posted to kapacitor to create a "deadman" task from template. @@ -330,6 +345,7 @@ class TICKScriptTemplateFiller: :param critical_value: critical value to compare with :param alert_period: alert period to use for relative comparison :param topic_id: topic identifier + :param event_id: event identifier :param where_clause: (OPTIONAL) argument for filtering the influx query by tag values :return: a dictionary object ready to be posted to kapacitor to create a "deadman" task from template. @@ -355,6 +371,10 @@ class TICKScriptTemplateFiller: "topicID": { "type": "string", "value": topic_id + }, + "eventID": { + "type": "string", + "value": event_id } } diff --git a/src/service/clmcservice/alertsapi/views.py b/src/service/clmcservice/alertsapi/views.py index 027302e..74ce6e9 100644 --- a/src/service/clmcservice/alertsapi/views.py +++ b/src/service/clmcservice/alertsapi/views.py @@ -253,7 +253,7 @@ class AlertsConfigurationAPI(object): # all extracted properties from the trigger are passed, the TICKScriptTemplateFiller entry point then forwards those to the appropriate function for template filling template_vars = TICKScriptTemplateFiller.fill_template_vars(event_type, db=db, measurement=measurement, field=field, influx_function=influx_function, critical_value=critical_value, comparison_operator=comparison_operator, alert_period=alert_period, - topic_id=topic_id, where_clause=where_clause) + topic_id=topic_id, event_id=event_id, where_clause=where_clause) # create and activate alert task through the kapacitor HTTP API kapacitor_api_tasks_url = "http://{0}:{1}/kapacitor/v1/tasks".format(kapacitor_host, kapacitor_port) diff --git a/src/service/resources/TICKscript/deadman-template.tick b/src/service/resources/TICKscript/deadman-template.tick index 2392b77..5762d4a 100644 --- a/src/service/resources/TICKscript/deadman-template.tick +++ b/src/service/resources/TICKscript/deadman-template.tick @@ -14,6 +14,8 @@ var throughputThreshold float // alerts will trigger if data points reported du var topicID string +var eventID string // topicID is based on the event ID, but represents a hash value + stream | from() @@ -22,7 +24,7 @@ stream .measurement(measurement) .where(whereClause) | deadman(throughputThreshold, alertPeriod) - .id(topicID) + .id(eventID) .details('db=' + db + ',measurement=' + measurement) .message(messageValue) .topic(topicID) diff --git a/src/service/resources/TICKscript/relative-template.tick b/src/service/resources/TICKscript/relative-template.tick index 2363ef4..30ac95c 100644 --- a/src/service/resources/TICKscript/relative-template.tick +++ b/src/service/resources/TICKscript/relative-template.tick @@ -18,6 +18,8 @@ var alertPeriod duration var topicID string +var eventID string // topicID is based on the event ID, but represents a hash value + var current = batch |query('SELECT ' + influxFunction + '(' + field + ') AS value FROM "' + db + '"."' + rp + '"."' + measurement + '" WHERE ' + whereClause) @@ -39,7 +41,7 @@ past | eval(lambda: float("current.value" - "past.value")) .as('diff') | alert() - .id(topicID) + .id(eventID) .details('db=' + db + ',measurement=' + measurement) .crit(comparisonLambda) .message(messageValue) diff --git a/src/service/resources/TICKscript/threshold-batch-template.tick b/src/service/resources/TICKscript/threshold-batch-template.tick index 5518814..379a49c 100644 --- a/src/service/resources/TICKscript/threshold-batch-template.tick +++ b/src/service/resources/TICKscript/threshold-batch-template.tick @@ -18,13 +18,14 @@ var alertPeriod duration var topicID string +var eventID string // topicID is based on the event ID, but represents a hash value batch |query('SELECT ' + influxFunction + '(' + field + ') AS real_value FROM "' + db + '"."' + rp + '"."' + measurement + '" WHERE ' + whereClause) .period(alertPeriod) .every(alertPeriod) |alert() - .id(topicID) + .id(eventID) .details('db=' + db + ',measurement=' + measurement) .crit(comparisonLambda) .message(messageValue) diff --git a/src/service/resources/TICKscript/threshold-stream-template.tick b/src/service/resources/TICKscript/threshold-stream-template.tick index f0800dd..6ee92fd 100644 --- a/src/service/resources/TICKscript/threshold-stream-template.tick +++ b/src/service/resources/TICKscript/threshold-stream-template.tick @@ -12,6 +12,8 @@ var comparisonLambda lambda // comparison function e.g. "real_value" > 40 var topicID string +var eventID string // topicID is based on the event ID, but represents a hash value + stream | from() @@ -20,7 +22,7 @@ stream .measurement(measurement) .where(whereClause) | alert() - .id(topicID) + .id(eventID) .details('db=' + db + ',measurement=' + measurement) .crit(comparisonLambda) .message(messageValue) -- GitLab