-
-
Notifications
You must be signed in to change notification settings - Fork 782
File watch sensor to utilize trigger with parameters #3361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
lakshmi-kannan
merged 7 commits into
StackStorm:master
from
lakshmi-kannan:tt_with_params
Apr 27, 2017
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9ea1067
Update FileWatchSensor to utilize trigger parameters.
Kami b53f8b8
Cleanup sensor YAML and enable sensor by default
6837265
Add a rule to watch a file (trigger type with parameters)
40daf1a
Rewrite trigger type check in file_watch_sensor.py
e9c7f52
Update CHANGELOG
1b5a72a
Update remove_trigger method
f342598
Remove duplicate line in remove_trigger
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +0,0 @@ | ||
| file_watch_sensor: | ||
| file_paths: | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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,29 @@ def cleanup(self): | |
| pass | ||
|
|
||
| def add_trigger(self, trigger): | ||
| pass | ||
| file_path = trigger['parameters'].get('file_path', None) | ||
|
|
||
| if not file_path: | ||
| self._logger.error('Received trigger type without "file_path" field.') | ||
| return | ||
|
|
||
| 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 | ||
| file_path = trigger['parameters'].get('file_path', None) | ||
|
|
||
| if not file_path: | ||
| self._logger.error('Received trigger type without "file_path" field.') | ||
| return | ||
|
|
||
| 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will it be always emitting |
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,8 +4,21 @@ | |
| 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: # User ``st2`` should have permissions to be able to read this file. | ||
| description: "Path to the file to monitor" | ||
| type: "string" | ||
| required: true | ||
| additionalProperties: false | ||
| - | ||
| name: "file_watch.line" | ||
| pack: "linux" | ||
| description: "Trigger which indicates a new line has been detected" | ||
| payload_schema: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need to add |
||
| type: "object" | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The emitted trigger is
linux.file_watch.line(I checked the logs, too) and here it islinux.file_watch.file_path. Funniest is that it works.