Lightweight replacement to both epylog and fail2ban.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.5 KiB

4 years ago
  1. # Writing custom modules
  2. Custom filters are Python files written in `/etc/pyruse/pyruse/filters/`.
  3. Custom actions are Python files written in `/etc/pyruse/pyruse/actions/`.
  4. Filters must define a class named `Filter` that extends Pyruse’s own `Filter` class from the `pyruse.base` namespace.
  5. By convention, a filter module name starts with `filter_`. A filter module looks like this:
  6. ```python
  7. from pyruse import base
  8. class Filter(base.Filter):
  9. def __init__(self, args):
  10. super().__init__()
  11. # get mandatory arguments with args["param_name"]
  12. # get optional arguments with args.get("param_name", default_value)
  13. # store in self.whatever the data that is needed at each run of filter below
  14. def filter(self, entry):
  15. # return true for the "then" link, or false for the "else" link
  16. return some_check(entry["a_field"], entry["another_field"])
  17. ```
  18. Actions must define a class named `Action` that extends Pyruse’s own `Action` class from the `pyruse.base` namespace.
  19. By convention, an action module name starts with `action_`. An action module looks like this:
  20. ```python
  21. from pyruse import base
  22. class Action(base.Action):
  23. def __init__(self, args):
  24. super().__init__()
  25. # get mandatory arguments with args["param_name"]
  26. # get optional arguments with args.get("param_name", default_value)
  27. # store in self.whatever the data that is needed at each run of act below
  28. def act(self, entry):
  29. # do whatever this action is supposed to do
  30. ```
  31. Some actions may need to restore a state at boot, or each time the main Pyruse program is restarted. The aim usually is to configure an external tool (firewall, etc.), based on files, or a database…
  32. In such cases:
  33. * The action’s constructor must be altered so that it does not fail if `args` is `None`:
  34. ```python
  35. def __init__(self, args):
  36. super().__init__()
  37. if args is None:
  38. return
  39. ```
  40. * A new `boot` method must be defined; it will get called at boot and this is where the wanted state shall be restored:
  41. ```python
  42. def boot(self):
  43. # do whatever must be done
  44. ```
  45. * Assuming the action is named `action_myModule`, the systemd unit `pyruse-boot@action_myModule.service` should be enabled. If this unit has dependencies, these must be declared before enabling the specific `pyruse-boot` service, by creating a drop-in with the dependencies, for example:
  46. ```
  47. # /etc/systemd/system/pyruse-boot@action_myModule.service.d/action_myModule.conf
  48. [Unit]
  49. Requires=iptables.service
  50. After=iptables.service
  51. ```