From 9ea1067cd247c88fb952f6607def94b92a53e2fb Mon Sep 17 00:00:00 2001 From: Tomaz Muraus Date: Mon, 23 Mar 2015 19:03:49 +0100 Subject: [PATCH 1/7] Update FileWatchSensor to utilize trigger parameters. --- contrib/linux/config.yaml | 2 -- contrib/linux/sensors/file_watch_sensor.py | 27 +++++++++++++------- contrib/linux/sensors/file_watch_sensor.yaml | 12 +++++++++ 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/contrib/linux/config.yaml b/contrib/linux/config.yaml index 2f9aca8029..e69de29bb2 100644 --- a/contrib/linux/config.yaml +++ b/contrib/linux/config.yaml @@ -1,2 +0,0 @@ -file_watch_sensor: - file_paths: diff --git a/contrib/linux/sensors/file_watch_sensor.py b/contrib/linux/sensors/file_watch_sensor.py index d350c87c0f..33fd068662 100644 --- a/contrib/linux/sensors/file_watch_sensor.py +++ b/contrib/linux/sensors/file_watch_sensor.py @@ -9,17 +9,14 @@ class FileWatchSensor(Sensor): def __init__(self, sensor_service, config=None): super(FileWatchSensor, self).__init__(sensor_service=sensor_service, config=config) - self._config = self._config['file_watch_sensor'] - - self._file_paths = self._config.get('file_paths', []) self._trigger_ref = 'linux.file_watch.line' + self._logger = self._sensor_service.get_logger(__name__) + + self._file_paths = [] # stores a list of file paths we are monitoring self._tail = None def setup(self): - if not self._file_paths: - raise ValueError('No file_paths configured to monitor') - - self._tail = Tail(filenames=self._file_paths) + self._tail = Tail(filenames=[]) self._tail.handler = self._handle_line self._tail.should_run = True @@ -36,13 +33,25 @@ def cleanup(self): pass def add_trigger(self, trigger): - pass + if trigger['type'] not in ['linux.file_watch.file_path']: + return + + file_path = trigger['parameters']['file_path'] + self._tail.add_file(filename=file_path) + + self._logger.info('Added file "%s"' % (file_path)) def update_trigger(self, trigger): pass def remove_trigger(self, trigger): - pass + if trigger['type'] not in ['linux.file_watch.file_path']: + return + + file_path = trigger['parameters']['file_path'] + self._tail.remove_file(filename=file_path) + + self._logger.info('Removed file "%s"' % (file_path)) def _handle_line(self, file_path, line): trigger = self._trigger_ref diff --git a/contrib/linux/sensors/file_watch_sensor.yaml b/contrib/linux/sensors/file_watch_sensor.yaml index fd445e156d..a6479deb00 100644 --- a/contrib/linux/sensors/file_watch_sensor.yaml +++ b/contrib/linux/sensors/file_watch_sensor.yaml @@ -4,6 +4,18 @@ entry_point: "file_watch_sensor.py" description: "Sensor which monitors files for new lines" trigger_types: + - + name: "file_watch.file_path" + description: "Trigger which represents a file path to be monitored" + parameters_schema: + type: "object" + properties: + file_path: + description: "Path to the file to monitor" + type: "string" + required: + - "file_path" + additionalProperties: false - name: "file_watch.line" description: "Trigger which indicates a new line has been detected" From b53f8b8e1cdd572a15790af7743720b0a1b05089 Mon Sep 17 00:00:00 2001 From: Lakshmi Kannan Date: Tue, 18 Apr 2017 07:42:55 -0700 Subject: [PATCH 2/7] Cleanup sensor YAML and enable sensor by default --- contrib/linux/sensors/file_watch_sensor.yaml | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/contrib/linux/sensors/file_watch_sensor.yaml b/contrib/linux/sensors/file_watch_sensor.yaml index a6479deb00..59182e9d42 100644 --- a/contrib/linux/sensors/file_watch_sensor.yaml +++ b/contrib/linux/sensors/file_watch_sensor.yaml @@ -1,23 +1,24 @@ --- class_name: "FileWatchSensor" - enabled: false + enabled: true entry_point: "file_watch_sensor.py" description: "Sensor which monitors files for new lines" trigger_types: - name: "file_watch.file_path" + pack: "linux" description: "Trigger which represents a file path to be monitored" parameters_schema: type: "object" properties: - file_path: + file_path: # User ``st2`` should have permissions to be able to read this file. description: "Path to the file to monitor" type: "string" - required: - - "file_path" + required: true additionalProperties: false - name: "file_watch.line" + pack: "linux" description: "Trigger which indicates a new line has been detected" payload_schema: type: "object" From 6837265d0d5f8949dc25666ae4f6fd80b3ba4e17 Mon Sep 17 00:00:00 2001 From: Lakshmi Kannan Date: Tue, 18 Apr 2017 07:43:22 -0700 Subject: [PATCH 3/7] Add a rule to watch a file (trigger type with parameters) --- .../examples/rules/sample_rule_file_watch.yaml | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 contrib/examples/rules/sample_rule_file_watch.yaml diff --git a/contrib/examples/rules/sample_rule_file_watch.yaml b/contrib/examples/rules/sample_rule_file_watch.yaml new file mode 100644 index 0000000000..c1b6643a49 --- /dev/null +++ b/contrib/examples/rules/sample_rule_file_watch.yaml @@ -0,0 +1,17 @@ +--- +name: sample_rule_file_watch +pack: "examples" +description: Sample rule custom trigger type - add a file to be watched by file_watch_sensor in linux pack. +enabled: false + +trigger: + parameters: + file_path: /var/log/dmesg + type: linux.file_watch.file_path + +criteria: {} + +action: + parameters: + cmd: echo "{{trigger}}" + ref: core.local From 40daf1a588ce8e5cdcbfb5fec19c2cbdf4da227c Mon Sep 17 00:00:00 2001 From: Lakshmi Kannan Date: Thu, 27 Apr 2017 11:26:20 -0700 Subject: [PATCH 4/7] Rewrite trigger type check in file_watch_sensor.py --- contrib/linux/sensors/file_watch_sensor.py | 6 ++++-- contrib/linux/sensors/file_watch_sensor.yaml | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/contrib/linux/sensors/file_watch_sensor.py b/contrib/linux/sensors/file_watch_sensor.py index 33fd068662..e24df26f2f 100644 --- a/contrib/linux/sensors/file_watch_sensor.py +++ b/contrib/linux/sensors/file_watch_sensor.py @@ -33,10 +33,12 @@ def cleanup(self): pass def add_trigger(self, trigger): - if trigger['type'] not in ['linux.file_watch.file_path']: + file_path = trigger['parameters'].get('file_path', None) + + if not file_path: + self._logger.error('Received trigger type without "file_path" field.') return - file_path = trigger['parameters']['file_path'] self._tail.add_file(filename=file_path) self._logger.info('Added file "%s"' % (file_path)) diff --git a/contrib/linux/sensors/file_watch_sensor.yaml b/contrib/linux/sensors/file_watch_sensor.yaml index 59182e9d42..fd095443e2 100644 --- a/contrib/linux/sensors/file_watch_sensor.yaml +++ b/contrib/linux/sensors/file_watch_sensor.yaml @@ -1,6 +1,6 @@ --- class_name: "FileWatchSensor" - enabled: true + enabled: false entry_point: "file_watch_sensor.py" description: "Sensor which monitors files for new lines" trigger_types: From e9c7f52abc0ae6565f8ffcc8a65502c0f6de8fba Mon Sep 17 00:00:00 2001 From: Lakshmi Kannan Date: Thu, 27 Apr 2017 11:26:33 -0700 Subject: [PATCH 5/7] Update CHANGELOG --- CHANGELOG.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index cf83f40c89..b2cbb0f970 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -41,6 +41,9 @@ in development ``TRIGGER_*`` RBAC permission constants (improvement) * Implement RBAC for webhooks get all and get one API endpoint. (improvement) * Add webhook payload to the Jinja render context when rendering Jinja variable inside rule criteria section +* Switch file_watch_sensor in Linux pack to use trigger type with parameters. Now you can add a + rule with `file_path` and sensor will pick up the `file_path` from the rule. A sample rule + is provided in contrib/examples/rules/sample_rule_file_watch.yaml. (improvement) 2.2.1 - April 3, 2017 --------------------- From 1b5a72a9169c84ab43315987f269d3c548abce16 Mon Sep 17 00:00:00 2001 From: Lakshmi Kannan Date: Thu, 27 Apr 2017 11:37:31 -0700 Subject: [PATCH 6/7] Update remove_trigger method --- contrib/linux/sensors/file_watch_sensor.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/contrib/linux/sensors/file_watch_sensor.py b/contrib/linux/sensors/file_watch_sensor.py index e24df26f2f..2970b7d351 100644 --- a/contrib/linux/sensors/file_watch_sensor.py +++ b/contrib/linux/sensors/file_watch_sensor.py @@ -47,7 +47,10 @@ def update_trigger(self, trigger): pass def remove_trigger(self, trigger): - if trigger['type'] not in ['linux.file_watch.file_path']: + file_path = trigger['parameters'].get('file_path', None) + + if not file_path: + self._logger.error('Received trigger type without "file_path" field.') return file_path = trigger['parameters']['file_path'] From f3425987fbae360119fbc93642dbae743e3445ef Mon Sep 17 00:00:00 2001 From: Lakshmi Kannan Date: Thu, 27 Apr 2017 11:42:59 -0700 Subject: [PATCH 7/7] Remove duplicate line in remove_trigger --- contrib/linux/sensors/file_watch_sensor.py | 1 - 1 file changed, 1 deletion(-) diff --git a/contrib/linux/sensors/file_watch_sensor.py b/contrib/linux/sensors/file_watch_sensor.py index 2970b7d351..49f624700a 100644 --- a/contrib/linux/sensors/file_watch_sensor.py +++ b/contrib/linux/sensors/file_watch_sensor.py @@ -53,7 +53,6 @@ def remove_trigger(self, trigger): self._logger.error('Received trigger type without "file_path" field.') return - file_path = trigger['parameters']['file_path'] self._tail.remove_file(filename=file_path) self._logger.info('Removed file "%s"' % (file_path))