Skip to content
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

event-handler: T3083: Add simple event-handler #1340

Merged
merged 7 commits into from
Jun 8, 2022
Merged

Conversation

sever-sever
Copy link
Member

@sever-sever sever-sever commented May 28, 2022

Change Summary

Event-handler allows executing a custom script when in logs it
detects configured "pattern"
A simple implementation

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Code style update (formatting, renaming)
  • Refactoring (no functional changes)
  • Migration from an old Vyatta component to vyos-1x, please link to related PR inside obsoleted component
  • Other (please describe):

Related Task(s)

Component(s) name

Proposed changes

How to test

set service event-handler event first filter pattern '.*ssh2.*'
set service event-handler event first script arguments '192.0.2.5'
set service event-handler event first script environment interface value 'eth0'
set service event-handler event first script path '/config/scripts/hello.sh'

cat hello.sh:

vyos@r14:~$ cat /config/scripts/hello.sh 
#!/usr/bin/env bash

echo "$(date) Hello world! From script, first arg ${1}" >> /tmp/hello.txt

Expect vyos-event-handle.service

vyos@r14:~$ sudo systemctl status vyos-event-handler.service
● vyos-event-handler.service - VyOS event handler
     Loaded: loaded (/lib/systemd/system/vyos-event-handler.service; disabled; vendor preset: enabled)
     Active: active (running) since Tue 2022-06-07 15:23:03 EEST; 1min 39s ago
   Main PID: 17489 (python3)
      Tasks: 1 (limit: 4695)
     Memory: 7.8M
        CPU: 48ms
     CGroup: /system.slice/vyos-event-handler.service
             └─17489 /usr/bin/python3 /usr/libexec/vyos/system/vyos-event-handler.py --config /run/vyos-event-handler.conf

Jun 07 15:23:03 r14 systemd[1]: Started VyOS event handler.
Jun 07 15:23:03 r14 vyos-event-handler[17489]: Started with configuration: {'first': {'filter': {'pattern': '.*ssh2.*'}, 'script': {'arguments': '192.0.2.5', 'environment': {'interface': {'value': 'eth0'}}, 'path': '/config/scripts/hello.sh'}}}
Jun 07 15:23:13 r14 vyos-event-handler[17489]: Pattern found: ".*ssh2.*", script executed: "/config/scripts/hello.sh 192.0.2.5"

Connect via ssh and check journalctl

...
Jun 07 15:23:13 r14 vyos-event-handler[17489]: Pattern found: ".*ssh2.*", script executed: "/config/scripts/hello.sh 192.0.2.5"
...

check cat /tmp/hello.txt

Tue 07 Jun 2022 03:23:13 PM EEST Hello world! From script, first arg 192.0.2.5

Checklist:

  • I have read the CONTRIBUTING document
  • I have linked this PR to one or more Phabricator Task(s)
  • I have run the components SMOKETESTS if applicable
  • My commit headlines contain a valid Task id
  • My change requires a change to the documentation
  • I have updated the documentation accordingly

@zdc
Copy link
Contributor

zdc commented May 30, 2022

Can it be extended by adding logging of received messages and running scripts? Most likely you need to exclude logs produced by the program itself to avoid endless loops.

Maybe you want to use required instead if len(argv)?

Also, you should switch to compiled regular expression objects. This reduces CPU usage a lot in systems with intensive logs.

Copy link
Member

@dmbaturin dmbaturin left a comment

Choose a reason for hiding this comment

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

I think the idea is sensible: reading the system log from journald is the only thing that doesn't open a huge can of worms with dying processes, disappearing log files and other unfortunate situations.

However, I think we should make it possible to watch for multiple patterns. I also left a few more ideas and suggestions there in the review.

src/helpers/vyos-event-handler.py Outdated Show resolved Hide resolved
interface-definitions/system-event-handler.xml.in Outdated Show resolved Hide resolved
interface-definitions/system-event-handler.xml.in Outdated Show resolved Hide resolved
interface-definitions/system-event-handler.xml.in Outdated Show resolved Hide resolved
@sever-sever sever-sever force-pushed the T3083 branch 3 times, most recently from e71e7c8 to 547f531 Compare June 3, 2022 12:25
@sever-sever sever-sever marked this pull request as draft June 3, 2022 17:24
@sever-sever sever-sever marked this pull request as ready for review June 5, 2022 08:03
@github-actions github-actions bot requested a review from dmbaturin June 5, 2022 08:03
@sever-sever sever-sever marked this pull request as draft June 6, 2022 06:27
Event-handler allows executing a custom script when in logs it
detects configured "pattern"
A simple implemenation

set system event-handler first pattern '.*ssh2.*'
set system event-handler first script '/config/scripts/hello.sh'
@sever-sever sever-sever marked this pull request as ready for review June 6, 2022 15:27
src/helpers/vyos-event-handler.py Outdated Show resolved Hide resolved
interface-definitions/system-event-handler.xml.in Outdated Show resolved Hide resolved
zdc and others added 2 commits June 6, 2022 21:33
* Removed dynamic generating for systemd unit
* Optimized configuration file deleting process
* Added exceptions handlers to event-handler script to protect service
from most obvious potential troubles
* Improved logging
* Moved pattern compilation outside a messages loop to avoid extra operations
* Added signal handlers for proper systemd integration
Move 'system event-handler' to 'service event-handler'
@sever-sever sever-sever marked this pull request as draft June 7, 2022 07:32
* Added the ability to filter by a syslog identifier
* Added the ability to pass arguments to a script
* Added the ability to pass preconfigured environment variables to a script
* A message that triggered a script is now passed in the `message` variable and
can be used in a script
* Replaced `call()` to `run()`, since stdout are not need to be printed
@sever-sever sever-sever marked this pull request as ready for review June 7, 2022 12:26
@github-actions github-actions bot requested a review from dmbaturin June 7, 2022 12:26
Before:
set service event-handler Foo
After:
set service event-handler event Foo
@c-po
Copy link
Member

c-po commented Jun 8, 2022

Lets merge it, play it, and improve it :)

@c-po c-po merged commit cfd958c into vyos:current Jun 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants