Skip to content

Conversation

@lakshmi-kannan
Copy link
Contributor

Cherry-picked #1291

Then, I made the following changes:

  • I enabled the file_watch_sensor by default.
  • I edited the sensor YAML file to reflect pack name explicitly
  • I added a sample rule which would register a trigger with parameter (file_path). Sensor automatically picks up the file name from rule and starts to listen to file and emit lines from file.

To test, do the following:

  • Spin st2 up and place the linux pack from this branch to /opt/stackstorm/packs
  • Register contents of linux pack - st2ctl reload --register-all
  • Restart st2 so file_watch_sensor is run - st2ctl restart
  • Verify file_watch_sensor is running and enabled
(virtualenv)vagrant@st2dev /m/s/s/st2 ❯❯❯ ps auxww | grep file_watch | grep -v grep                                      tt_with_params ✭ ◼
vagrant  15142  0.1  2.0 111104 42520 pts/5    S+   07:19   0:04 /mnt/src/storm/st2/virtualenv/bin/python /mnt/src/storm/st2/st2reactor/st2reactor/container/sensor_wrapper.py --pack=linux --file-path=/opt/stackstorm/packs/linux/sensors/file_watch_sensor.py --class-name=FileWatchSensor --trigger-type-refs=linux.file_watch.file_path,linux.file_watch.line --parent-args=["--config-file", "/mnt/src/storm/st2/tools/../conf/st2.dev.conf"]
(virtualenv)vagrant@st2dev /m/s/s/st2 ❯❯❯
(virtualenv)vagrant@st2dev /m/s/s/st2 ❯❯❯ st2 sensor list --pack=linux                                                   tt_with_params ✭ ◼
+-----------------------+-------+-------------------------------------+---------+
| ref                   | pack  | description                         | enabled |
+-----------------------+-------+-------------------------------------+---------+
| linux.FileWatchSensor | linux | Sensor which monitors files for new | True    |
|                       |       | lines                               |         |
+-----------------------+-------+-------------------------------------+---------+
(virtualenv)vagrant@st2dev /m/s/s/st2 ❯❯❯
  • Now copy sample rule that will register the rule
(virtualenv)vagrant@st2dev /m/s/s/st2 ❯❯❯ st2 rule create /opt/stackstorm/packs/examples/rules/sample_rule_file_watch.yaml
+-------------+-----------------------------------------------------------+
| Property    | Value                                                     |
+-------------+-----------------------------------------------------------+
| id          | 58f620c6d9d7ed3af4554f4d                                  |
| name        | sample_rule_file_watch                                    |
| pack        | examples                                                  |
| description | Sample rule customer trigger type.                        |
| action      | {                                                         |
|             |     "ref": "core.local",                                  |
|             |     "parameters": {                                       |
|             |         "cmd": "echo "{{trigger}}""                       |
|             |     }                                                     |
|             | }                                                         |
| criteria    |                                                           |
| enabled     | False                                                     |
| ref         | examples.sample_rule_file_watch                           |
| tags        |                                                           |
| trigger     | {                                                         |
|             |     "type": "linux.file_watch.file_path",                 |
|             |     "ref": "linux.1c65e441-fd18-4217-bc53-8ac616a31074",  |
|             |     "parameters": {                                       |
|             |         "file_path": "/var/log/dmesg"                     |
|             |     }                                                     |
|             | }                                                         |
| type        | {                                                         |
|             |     "ref": "standard",                                    |
|             |     "parameters": {}                                      |
|             | }                                                         |
| uid         | rule:examples:sample_rule_file_watch                      |
+-------------+-----------------------------------------------------------+
(virtualenv)vagrant@st2dev /m/s/s/st2 ❯❯❯


def add_trigger(self, trigger):
pass
if trigger['type'] not in ['linux.file_watch.file_path']:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed on Slack - I prototyped it this way for easiness.

If we decide to go with this approach I would propose to move this filtering to the base sensor class so those trigger handler methods only get called with triggers which belong to that sensor (so no additional filtering needed in sensor class trigger handler methods).

Another, even better future optimization (better than this suggested later filtering in the base class) would be to use a more advanced message routing so each sensor class would only receive triggers it should receive. Problem with that approach is that it's not fully backward compatible and would need to research how expensive it is to use queue per sensor (probably not too expensive).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Kami I think the trigger check is not strictly required. We already filter in TriggerWatcher.

(virtualenv)vagrant@st2dev /m/s/s/st2 ❯❯❯ ps auxww | grep file_watch | grep -v grep                                      tt_with_params ✭ ◼
vagrant  18758  1.1  2.1 115520 45008 pts/4    S+   14:25   0:02 /mnt/src/storm/st2/virtualenv/bin/python /mnt/src/storm/st2/st2reactor/st2reactor/container/sensor_wrapper.py --pack=linux --file-path=/opt/stackstorm/packs/linux/sensors/file_watch_sensor.py --class-name=FileWatchSensor --trigger-type-refs=linux.file_watch.file_path,linux.file_watch.line --parent-args=["--config-file", "/mnt/src/storm/st2/tools/../conf/st2.dev.conf"]
(virtualenv)vagrant@st2dev /m/s/s/st2 ❯❯❯

Note we pass --trigger-type-refs to the sensor wrapper. This is then passed to TriggerWatcher
https://github.com/StackStorm/st2/blob/master/st2reactor/st2reactor/container/sensor_wrapper.py#L354

https://github.com/StackStorm/st2/blob/master/st2reactor/st2reactor/container/sensor_wrapper.py#L192

https://github.com/StackStorm/st2/blob/master/st2common/st2common/services/triggerwatcher.py#L93

So I thought you originally did it for safety measure.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call - this was probably added after my original trigger with parameters change was implemented so it looks like my comment is obsolete :)

In any case, would be good to double check and verify that is indeed the case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I verified and we are filtering right in TriggerWatcher and sensors don't see triggers that they don't register.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, in this case we should get rid of checks in the sensor code :)

@dzimine
Copy link

dzimine commented Apr 25, 2017

This looks right.
I don't see any draw-backs with this approach, if there are, pls list. Regardless, let's move forward.

To complete this PR, let's modify the line-watching sensor to take path as a trigger parameter, not a global parameter, too.

If we decide to go with this approach I would propose to move this filtering to the base sensor class so those trigger handler methods only get called with triggers which belong to that sensor (so no additional filtering needed in sensor class trigger handler methods).
👍

@lakshmi-kannan
Copy link
Contributor Author

To complete this PR, let's modify the line-watching sensor to take path as a trigger parameter, not a global parameter, too.

Dude, that's what this PR does. I am confused.

I don't see any draw-backs with this approach, if there are, pls list

I do see a drawback which is the sensor code is now responsible for some kind of scheduling handling the trigger types. In this particular case, this logic is hidden luckily https://github.com/StackStorm/st2/pull/3361/files#diff-414b51e67b8a124ee29083fc2bbce3a8R19. But it also is a poor example for people. We might want to add an example where we loop over the trigger types (may come from different rules) and do something.

@lakshmi-kannan lakshmi-kannan changed the title WIP: File watch sensor to utilize trigger with parameters File watch sensor to utilize trigger with parameters Apr 26, 2017
Copy link

@dzimine dzimine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

@dzimine
Copy link

dzimine commented Apr 26, 2017

Dude, that's what this PR does. I am confused.

I think you're adding file_watcher which takes parameters, but the line_watcher still relies on the global, no?

@Kami
Copy link
Member

Kami commented Apr 26, 2017

Let's please also update the changelog.

@Kami
Copy link
Member

Kami commented Apr 26, 2017

@dzimine

I think you're adding file_watcher which takes parameters, but the line_watcher still relies on the global, no?

Not exactly sure what you mean with line watcher? There is no such thing as line watcher (or am I missing something) - file watcher emits a trigger each time a change (new line) is detected on monitored file.

@lakshmi-kannan lakshmi-kannan merged commit c48c6c3 into StackStorm:master Apr 27, 2017
@lakshmi-kannan lakshmi-kannan deleted the tt_with_params branch April 27, 2017 20:10
Copy link

@dzimine dzimine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lakshmi-kannan there seem to be a mess between trigger references.

I don't quite get how you handle both .line and .file_pathtriggers.

Also, I am having troubles to make it work but likely because I'm in a rush and did something wrong.

self._logger.info('Removed file "%s"' % (file_path))

def _handle_line(self, file_path, line):
trigger = self._trigger_ref
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will it be always emitting 'linux.file_watch.line, see line 12?

trigger:
parameters:
file_path: /var/log/dmesg
type: linux.file_watch.file_path
Copy link

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 is linux.file_watch.file_path. Funniest is that it works.

-
name: "file_watch.line"
pack: "linux"
description: "Trigger which indicates a new line has been detected"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to add parameters_schema here, too

cognifloyd added a commit that referenced this pull request Jan 30, 2024
Pack config was removed for the linux pack in #3361 and #3475.
Remove the README note as it no longer applies.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants