blob: 6413c24dc1a915240b973a6b261d743dc5ad2da8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
(event-handler)=
# Event Handler
## Event Handler Technology Overview
Event handler allows you to execute scripts when a string that matches
a regex or a regex with a service name appears in journald logs. You
can pass variables, arguments, and a full matching string to the script.
## How to configure Event Handler
> [1. Create an event handler](#create-an-event-handler)
>
> [2. Add regex to the script](#add-regex-to-the-script)
>
> [3. Add a full path to the script](#add-a-full-path-to-the-script)
>
> [4. Add optional parameters](#add-optional-parameters)
## Event Handler Configuration Steps
### 1. Create an event handler
```{cfgcmd} set service event-handler event \<event-handler name\>
This is an optional command because the event handler will be
automatically created after any of the next commands.
```
### 2. Add regex to the script
```{cfgcmd} set service event-handler event \<event-handler name\> filter pattern \<regex\>
This is a mandatory command. Sets regular expression to match
against log string message.
:::{note}
The regular expression matches if and only if the entire
string matches the pattern.
:::
```
### 3. Add a full path to the script
```{cfgcmd} set service event-handler event \<event-handler name\> script path \<path to script\>
This is a mandatory command. Sets the full path to the script.
The script file must be executable.
```
### 4. Add optional parameters
```{cfgcmd} set service event-handler event \<event-handler name\> filter syslog-identifier \<syslogid name\>
This is an optional command. Filters log messages by
syslog-identifier.
```
```{cfgcmd} set service event-handler event \<event-handler name\> script environment \<env name\> value \<env value\>
This is an optional command. Adds environment and its value to
the script. Use separate commands for each environment.
One implicit environment exists.
* ``message``: Full message that has triggered the script.
```
```{cfgcmd} set service event-handler event \<event-handler name\> script arguments \<arguments\>
This is an optional command. Adds arguments to the script.
Arguments must be separated by spaces.
:::{note}
We don't recommend using arguments. Using environment variables is preferable.
:::
```
## Example
Event handler that monitors the state of interface eth0.
```none
set service event-handler event INTERFACE_STATE_DOWN filter pattern '.*eth0.*,RUNNING,.*->.*'
set service event-handler event INTERFACE_STATE_DOWN filter syslog-identifier 'netplugd'
set service event-handler event INTERFACE_STATE_DOWN script environment interface_action value 'down'
set service event-handler event INTERFACE_STATE_DOWN script environment interface_name value 'eth0'
set service event-handler event INTERFACE_STATE_DOWN script path '/config/scripts/eventhandler.py'
```
Event handler script
```none
#!/usr/bin/env python3
#
# VyOS event-handler script example
from os import environ
import subprocess
from sys import exit
# Perform actions according to requirements
def process_event() -> None:
# Get variables
message_text = environ.get('message')
interface_name = environ.get('interface_name')
interface_action = environ.get('interface_action')
# Print the message that triggered this script
print(f'Logged message: {message_text}')
# Prepare a command to run
command = f'sudo ip link set {interface_name} {interface_action}'.split()
# Execute a command
subprocess.run(command)
if __name__ == '__main__':
try:
# Run script actions and exit
process_event()
exit(0)
except Exception as err:
# Exit properly in case if something in the script goes wrong
print(f'Error running script: {err}')
exit(1)
```
|