diff options
author | vkalintiris <vasilis@netdata.cloud> | 2024-02-08 11:06:35 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-08 11:06:35 +0200 |
commit | e0ec10411bcfaeeaba331a93d5f3d96e23fa72e5 (patch) | |
tree | b371cfb03d8f276e8c64cfd042648053abfbd0a2 /collectors/python.d.plugin/fail2ban | |
parent | e9760b43f53442d9dbd22f5353872ae5e83f7c4c (diff) |
Move collectors/ under src/ (#16965)
Diffstat (limited to 'collectors/python.d.plugin/fail2ban')
l--------- | collectors/python.d.plugin/fail2ban/README.md | 1 | ||||
-rw-r--r-- | collectors/python.d.plugin/fail2ban/fail2ban.chart.py | 217 | ||||
-rw-r--r-- | collectors/python.d.plugin/fail2ban/fail2ban.conf | 68 | ||||
-rw-r--r-- | collectors/python.d.plugin/fail2ban/integrations/fail2ban.md | 209 | ||||
-rw-r--r-- | collectors/python.d.plugin/fail2ban/metadata.yaml | 200 |
5 files changed, 0 insertions, 695 deletions
diff --git a/collectors/python.d.plugin/fail2ban/README.md b/collectors/python.d.plugin/fail2ban/README.md deleted file mode 120000 index 642a8bcf53..0000000000 --- a/collectors/python.d.plugin/fail2ban/README.md +++ /dev/null @@ -1 +0,0 @@ -integrations/fail2ban.md
\ No newline at end of file diff --git a/collectors/python.d.plugin/fail2ban/fail2ban.chart.py b/collectors/python.d.plugin/fail2ban/fail2ban.chart.py deleted file mode 100644 index 76f6d92b4e..0000000000 --- a/collectors/python.d.plugin/fail2ban/fail2ban.chart.py +++ /dev/null @@ -1,217 +0,0 @@ -# -*- coding: utf-8 -*- -# Description: fail2ban log netdata python.d module -# Author: ilyam8 -# SPDX-License-Identifier: GPL-3.0-or-later - -import os -import re -from collections import defaultdict -from glob import glob - -from bases.FrameworkServices.LogService import LogService - -ORDER = [ - 'jails_failed_attempts', - 'jails_bans', - 'jails_banned_ips', -] - - -def charts(jails): - """ - Chart definitions creating - """ - - ch = { - ORDER[0]: { - 'options': [None, 'Failed attempts', 'attempts/s', 'failed attempts', 'fail2ban.failed_attempts', 'line'], - 'lines': [] - }, - ORDER[1]: { - 'options': [None, 'Bans', 'bans/s', 'bans', 'fail2ban.bans', 'line'], - 'lines': [] - }, - ORDER[2]: { - 'options': [None, 'Banned IP addresses (since the last restart of netdata)', 'ips', 'banned ips', - 'fail2ban.banned_ips', 'line'], - 'lines': [] - }, - } - for jail in jails: - dim = ['{0}_failed_attempts'.format(jail), jail, 'incremental'] - ch[ORDER[0]]['lines'].append(dim) - - dim = [jail, jail, 'incremental'] - ch[ORDER[1]]['lines'].append(dim) - - dim = ['{0}_in_jail'.format(jail), jail, 'absolute'] - ch[ORDER[2]]['lines'].append(dim) - - return ch - - -RE_JAILS = re.compile(r'\[([a-zA-Z0-9_-]+)\][^\[\]]+?enabled\s+= +(true|yes|false|no)') - -ACTION_BAN = 'Ban' -ACTION_UNBAN = 'Unban' -ACTION_RESTORE_BAN = 'Restore Ban' -ACTION_FOUND = 'Found' - -# Example: -# 2018-09-12 11:45:58,727 fail2ban.actions[25029]: WARNING [ssh] Found 203.0.113.1 -# 2018-09-12 11:45:58,727 fail2ban.actions[25029]: WARNING [ssh] Ban 203.0.113.1 -# 2018-09-12 11:45:58,727 fail2ban.actions[25029]: WARNING [ssh] Restore Ban 203.0.113.1 -# 2018-09-12 11:45:53,715 fail2ban.actions[25029]: WARNING [ssh] Unban 203.0.113.1 -RE_DATA = re.compile( - r'\[(?P<jail>[A-Za-z-_0-9]+)\] (?P<action>{0}|{1}|{2}|{3}) (?P<ip>[a-f0-9.:]+)'.format( - ACTION_BAN, ACTION_UNBAN, ACTION_RESTORE_BAN, ACTION_FOUND - ) -) - -DEFAULT_JAILS = [ - 'ssh', -] - - -class Service(LogService): - def __init__(self, configuration=None, name=None): - LogService.__init__(self, configuration=configuration, name=name) - self.order = ORDER - self.definitions = dict() - self.log_path = self.configuration.get('log_path', '/var/log/fail2ban.log') - self.conf_path = self.configuration.get('conf_path', '/etc/fail2ban/jail.local') - self.conf_dir = self.configuration.get('conf_dir', '/etc/fail2ban/jail.d/') - self.exclude = self.configuration.get('exclude', str()) - self.monitoring_jails = list() - self.banned_ips = defaultdict(set) - self.data = dict() - - def check(self): - """ - :return: bool - """ - if not self.conf_path.endswith(('.conf', '.local')): - self.error('{0} is a wrong conf path name, must be *.conf or *.local'.format(self.conf_path)) - return False - - if not os.access(self.log_path, os.R_OK): - self.error('{0} is not readable'.format(self.log_path)) - return False - - if os.path.getsize(self.log_path) == 0: - self.error('{0} is empty'.format(self.log_path)) - return False - - self.monitoring_jails = self.jails_auto_detection() - for jail in self.monitoring_jails: - self.data['{0}_failed_attempts'.format(jail)] = 0 - self.data[jail] = 0 - self.data['{0}_in_jail'.format(jail)] = 0 - - self.definitions = charts(self.monitoring_jails) - self.info('monitoring jails: {0}'.format(self.monitoring_jails)) - - return True - - def get_data(self): - """ - :return: dict - """ - raw = self._get_raw_data() - - if not raw: - return None if raw is None else self.data - - for row in raw: - match = RE_DATA.search(row) - - if not match: - continue - - match = match.groupdict() - - if match['jail'] not in self.monitoring_jails: - continue - - jail, action, ip = match['jail'], match['action'], match['ip'] - - if action == ACTION_FOUND: - self.data['{0}_failed_attempts'.format(jail)] += 1 - elif action in (ACTION_BAN, ACTION_RESTORE_BAN): - self.data[jail] += 1 - if ip not in self.banned_ips[jail]: - self.banned_ips[jail].add(ip) - self.data['{0}_in_jail'.format(jail)] += 1 - elif action == ACTION_UNBAN: - if ip in self.banned_ips[jail]: - self.banned_ips[jail].remove(ip) - self.data['{0}_in_jail'.format(jail)] -= 1 - - return self.data - - def get_files_from_dir(self, dir_path, suffix): - """ - :return: list - """ - if not os.path.isdir(dir_path): - self.error('{0} is not a directory'.format(dir_path)) - return list() - - return glob('{0}/*.{1}'.format(self.conf_dir, suffix)) - - def get_jails_from_file(self, file_path): - """ - :return: list - """ - if not os.access(file_path, os.R_OK): - self.error('{0} is not readable or not exist'.format(file_path)) - return list() - - with open(file_path, 'rt') as f: - lines = f.readlines() - raw = ' '.join(line for line in lines if line.startswith(('[', 'enabled'))) - - match = RE_JAILS.findall(raw) - # Result: [('ssh', 'true'), ('dropbear', 'true'), ('pam-generic', 'true'), ...] - - if not match: - self.debug('{0} parse failed'.format(file_path)) - return list() - - return match - - def jails_auto_detection(self): - """ - :return: list - - Parses jail configuration files. Returns list of enabled jails. - According man jail.conf parse order must be - * jail.conf - * jail.d/*.conf (in alphabetical order) - * jail.local - * jail.d/*.local (in alphabetical order) - """ - jails_files, all_jails, active_jails = list(), list(), list() - - jails_files.append('{0}.conf'.format(self.conf_path.rsplit('.')[0])) - jails_files.extend(self.get_files_from_dir(self.conf_dir, 'conf')) - jails_files.append('{0}.local'.format(self.conf_path.rsplit('.')[0])) - jails_files.extend(self.get_files_from_dir(self.conf_dir, 'local')) - - self.debug('config files to parse: {0}'.format(jails_files)) - - for f in jails_files: - all_jails.extend(self.get_jails_from_file(f)) - - exclude = self.exclude.split() - - for name, status in all_jails: - if name in exclude: - continue - - if status in ('true', 'yes') and name not in active_jails: - active_jails.append(name) - elif status in ('false', 'no') and name in active_jails: - active_jails.remove(name) - - return active_jails or DEFAULT_JAILS diff --git a/collectors/python.d.plugin/fail2ban/fail2ban.conf b/collectors/python.d.plugin/fail2ban/fail2ban.conf deleted file mode 100644 index a36436b510..0000000000 --- a/collectors/python.d.plugin/fail2ban/fail2ban.conf +++ /dev/null @@ -1,68 +0,0 @@ -# netdata python.d.plugin configuration for fail2ban -# -# This file is in YaML format. Generally the format is: -# -# name: value -# -# There are 2 sections: -# - global variables -# - one or more JOBS -# -# JOBS allow you to collect values from multiple sources. -# Each source will have its own set of charts. -# -# JOB parameters have to be indented (using spaces only, example below). - -# ---------------------------------------------------------------------- -# Global Variables -# These variables set the defaults for all JOBs, however each JOB -# may define its own, overriding the defaults. - -# update_every sets the default data collection frequency. -# If unset, the python.d.plugin default is used. -# update_every: 1 - -# priority controls the order of charts at the netdata dashboard. -# Lower numbers move the charts towards the top of the page. -# If unset, the default for python.d.plugin is used. -# priority: 60000 - -# penalty indicates whether to apply penalty to update_every in case of failures. -# Penalty will increase every 5 failed updates in a row. Maximum penalty is 10 minutes. -# penalty: yes - -# autodetection_retry sets the job re-check interval in seconds. -# The job is not deleted if check fails. -# Attempts to start the job are made once every autodetection_retry. -# This feature is disabled by default. -# autodetection_retry: 0 - -# ---------------------------------------------------------------------- -# JOBS (data collection sources) -# -# The default JOBS share the same *name*. JOBS with the same name -# are mutually exclusive. Only one of them will be allowed running at -# any time. This allows autodetection to try several alternatives and -# pick the one that works. -# -# Any number of jobs is supported. -# -# All python.d.plugin JOBS (for all its modules) support a set of -# predefined parameters. These are: -# -# job_name: -# name: myname # the JOB's name as it will appear at the -# # dashboard (by default is the job_name) -# # JOBs sharing a name are mutually exclusive -# update_every: 1 # the JOB's data collection frequency -# priority: 60000 # the JOB's order on the dashboard -# penalty: yes # the JOB's penalty -# autodetection_retry: 0 # the JOB's re-check interval in seconds -# -# Additionally to the above, fail2ban also supports the following: -# -# log_path: 'path to fail2ban.log' # Default: '/var/log/fail2ban.log' -# conf_path: 'path to jail.local/jail.conf' # Default: '/etc/fail2ban/jail.local' -# conf_dir: 'path to jail.d/' # Default: '/etc/fail2ban/jail.d/' -# exclude: 'jails you want to exclude from autodetection' # Default: none -#------------------------------------------------------------------------------------------------------------------ diff --git a/collectors/python.d.plugin/fail2ban/integrations/fail2ban.md b/collectors/python.d.plugin/fail2ban/integrations/fail2ban.md deleted file mode 100644 index a7116be5e4..0000000000 --- a/collectors/python.d.plugin/fail2ban/integrations/fail2ban.md +++ /dev/null @@ -1,209 +0,0 @@ -<!--startmeta -custom_edit_url: "https://github.com/netdata/netdata/edit/master/collectors/python.d.plugin/fail2ban/README.md" -meta_yaml: "https://github.com/netdata/netdata/edit/master/collectors/python.d.plugin/fail2ban/metadata.yaml" -sidebar_label: "Fail2ban" -learn_status: "Published" -learn_rel_path: "Data Collection/Authentication and Authorization" -most_popular: False -message: "DO NOT EDIT THIS FILE DIRECTLY, IT IS GENERATED BY THE COLLECTOR'S metadata.yaml FILE" -endmeta--> - -# Fail2ban - - -<img src="https://netdata.cloud/img/fail2ban.png" width="150"/> - - -Plugin: python.d.plugin -Module: fail2ban - -<img src="https://img.shields.io/badge/maintained%20by-Netdata-%2300ab44" /> - -## Overview - -Monitor Fail2ban performance for prime intrusion prevention operations. Monitor ban counts, jail statuses, and failed login attempts to ensure robust network security. - - -It collects metrics through reading the default log and configuration files of fail2ban. - - -This collector is supported on all platforms. - -This collector supports collecting metrics from multiple instances of this integration, including remote instances. - -The `fail2ban.log` file must be readable by the user `netdata`. - - change the file ownership and access permissions. - - update `/etc/logrotate.d/fail2ban`` to persist the changes after rotating the log file. - -To change the file ownership and access permissions, execute the following: - -```shell -sudo chown root:netdata /var/log/fail2ban.log -sudo chmod 640 /var/log/fail2ban.log -``` - -To persist the changes after rotating the log file, add `create 640 root netdata` to the `/etc/logrotate.d/fail2ban`: - -```shell -/var/log/fail2ban.log { - - weekly - rotate 4 - compress - - delaycompress - missingok - postrotate - fail2ban-client flushlogs 1>/dev/null - endscript - - # If fail2ban runs as non-root it still needs to have write access - # to logfiles. - # create 640 fail2ban adm - create 640 root netdata -} -``` - - -### Default Behavior - -#### Auto-Detection - -By default the collector will attempt to read log file at /var/log/fail2ban.log and conf file at /etc/fail2ban/jail.local. -If conf file is not found default jail is ssh. - - -#### Limits - -The default configuration for this integration does not impose any limits on data collection. - -#### Performance Impact - -The default configuration for this integration is not expected to impose a significant performance impact on the system. - - -## Metrics - -Metrics grouped by *scope*. - -The scope defines the instance that the metric belongs to. An instance is uniquely identified by a set of labels. - - - -### Per Fail2ban instance - -These metrics refer to the entire monitored application. - - -This scope has no labels. - -Metrics: - -| Metric | Dimensions | Unit | -|:------|:----------|:----| -| fail2ban.failed_attempts | a dimension per jail | attempts/s | -| fail2ban.bans | a dimension per jail | bans/s | -| fail2ban.banned_ips | a dimension per jail | ips | - - - -## Alerts - -There are no alerts configured by default for this integration. - - -## Setup - -### Prerequisites - -No action required. - -### Configuration - -#### File - -The configuration file name for this integration is `python.d/fail2ban.conf`. - - -You can edit the configuration file using the `edit-config` script from the -Netdata [config directory](https://github.com/netdata/netdata/blob/master/docs/configure/nodes.md#the-netdata-config-directory). - -```bash -cd /etc/netdata 2>/dev/null || cd /opt/netdata/etc/netdata -sudo ./edit-config python.d/fail2ban.conf -``` -#### Options - -There are 2 sections: - -* Global variables -* One or more JOBS that can define multiple different instances to monitor. - -The following options can be defined globally: priority, penalty, autodetection_retry, update_every, but can also be defined per JOB to override the global values. - -Additionally, the following collapsed table contains all the options that can be configured inside a JOB definition. - -Every configuration JOB starts with a `job_name` value which will appear in the dashboard, unless a `name` parameter is specified. - - -<details><summary>Config options</summary> - -| Name | Description | Default | Required | -|:----|:-----------|:-------|:--------:| -| log_path | path to fail2ban.log. | /var/log/fail2ban.log | no | -| conf_path | path to jail.local/jail.conf. | /etc/fail2ban/jail.local | no | -| conf_dir | path to jail.d/. | /etc/fail2ban/jail.d/ | no | -| exclude | jails you want to exclude from autodetection. | | no | -| update_every | Sets the default data collection frequency. | 1 | no | -| priority | Controls the order of charts at the netdata dashboard. | 60000 | no | -| autodetection_retry | Sets the job re-check interval in seconds. | 0 | no | -| penalty | Indicates whether to apply penalty to update_every in case of failures. | yes | no | -| name | Job name. This value will overwrite the `job_name` value. JOBS with the same name are mutually exclusive. Only one of them will be allowed running at any time. This allows autodetection to try several alternatives and pick the one that works. | | no | - -</details> - -#### Examples - -##### Basic - -A basic example configuration. - -```yaml -local: - log_path: '/var/log/fail2ban.log' - conf_path: '/etc/fail2ban/jail.local' - -``` - - -## Troubleshooting - -### Debug Mode - -To troubleshoot issues with the `fail2ban` collector, run the `python.d.plugin` with the debug option enabled. The output -should give you clues as to why the collector isn't working. - -- Navigate to the `plugins.d` directory, usually at `/usr/libexec/netdata/plugins.d/`. If that's not the case on - your system, open `netdata.conf` and look for the `plugins` setting under `[directories]`. - - ```bash - cd /usr/libexec/netdata/plugins.d/ - ``` - -- Switch to the `netdata` user. - - ```bash - sudo -u netdata -s - ``` - -- Run the `python.d.plugin` to debug the collector: - - ```bash - ./python.d.plugin fail2ban debug trace - ``` - -### Debug Mode - - - - diff --git a/collectors/python.d.plugin/fail2ban/metadata.yaml b/collectors/python.d.plugin/fail2ban/metadata.yaml deleted file mode 100644 index 61f762679c..0000000000 --- a/collectors/python.d.plugin/fail2ban/metadata.yaml +++ /dev/null @@ -1,200 +0,0 @@ -plugin_name: python.d.plugin -modules: - - meta: - plugin_name: python.d.plugin - module_name: fail2ban - monitored_instance: - name: Fail2ban - link: https://www.fail2ban.org/ - categories: - - data-collection.authentication-and-authorization - icon_filename: "fail2ban.png" - related_resources: - integrations: - list: [] - info_provided_to_referring_integrations: - description: "" - keywords: - - fail2ban - - security - - authentication - - authorization - most_popular: false - overview: - data_collection: - metrics_description: | - Monitor Fail2ban performance for prime intrusion prevention operations. Monitor ban counts, jail statuses, and failed login attempts to ensure robust network security. - method_description: | - It collects metrics through reading the default log and configuration files of fail2ban. - supported_platforms: - include: [] - exclude: [] - multi_instance: true - additional_permissions: - description: | - The `fail2ban.log` file must be readable by the user `netdata`. - - change the file ownership and access permissions. - - update `/etc/logrotate.d/fail2ban`` to persist the changes after rotating the log file. - - To change the file ownership and access permissions, execute the following: - - ```shell - sudo chown root:netdata /var/log/fail2ban.log - sudo chmod 640 /var/log/fail2ban.log - ``` - - To persist the changes after rotating the log file, add `create 640 root netdata` to the `/etc/logrotate.d/fail2ban`: - - ```shell - /var/log/fail2ban.log { - - weekly - rotate 4 - compress - - delaycompress - missingok - postrotate - fail2ban-client flushlogs 1>/dev/null - endscript - - # If fail2ban runs as non-root it still needs to have write access - # to logfiles. - # create 640 fail2ban adm - create 640 root netdata - } - ``` - default_behavior: - auto_detection: - description: | - By default the collector will attempt to read log file at /var/log/fail2ban.log and conf file at /etc/fail2ban/jail.local. - If conf file is not found default jail is ssh. - limits: - description: "" - performance_impact: - description: "" - setup: - prerequisites: - list: [] - configuration: - file: - name: python.d/fail2ban.conf - description: "" - options: - description: | - There are 2 sections: - - * Global variables - * One or more JOBS that can define multiple different instances to monitor. - - The following options can be defined globally: priority, penalty, autodetection_retry, update_every, but can also be defined per JOB to override the global values. - - Additionally, the following collapsed table contains all the options that can be configured inside a JOB definition. - - Every configuration JOB starts with a `job_name` value which will appear in the dashboard, unless a `name` parameter is specified. - folding: - title: Config options - enabled: true - list: - - name: log_path - description: path to fail2ban.log. - default_value: /var/log/fail2ban.log - required: false - - name: conf_path - description: path to jail.local/jail.conf. - default_value: /etc/fail2ban/jail.local - required: false - - name: conf_dir - description: path to jail.d/. - default_value: /etc/fail2ban/jail.d/ - required: false - - name: exclude - description: jails you want to exclude from autodetection. - default_value: "" - required: false - - name: update_every - description: Sets the default data collection frequency. - default_value: 1 - required: false - - name: priority - description: Controls the order of charts at the netdata dashboard. - default_value: 60000 - required: false - - name: autodetection_retry - description: Sets the job re-check interval in seconds. - default_value: 0 - required: false - - name: penalty - description: Indicates whether to apply penalty to update_every in case of failures. - default_value: yes - required: false - - name: name - description: Job name. This value will overwrite the `job_name` value. JOBS with the same name are mutually exclusive. Only one of them will be allowed running at any time. This allows autodetection to try several alternatives and pick the one that works. - default_value: "" - required: false - examples: - folding: - enabled: true - title: Config - list: - - name: Basic - folding: - enabled: false - description: A basic example configuration. - config: | - local: - log_path: '/var/log/fail2ban.log' - conf_path: '/etc/fail2ban/jail.local' - troubleshooting: - problems: - list: - - name: Debug Mode - description: | - To troubleshoot issues with the `fail2ban` module, run the `python.d.plugin` with the debug option enabled. - The output will give you the output of the data collection job or error messages on why the collector isn't working. - - First, navigate to your plugins directory, usually they are located under `/usr/libexec/netdata/plugins.d/`. If that's - not the case on your system, open `netdata.conf` and look for the setting `plugins directory`. Once you're in the - plugin's directory, switch to the `netdata` user. - - ```bash - cd /usr/libexec/netdata/plugins.d/ - sudo su -s /bin/bash netdata - ``` - - Now you can manually run the `fail2ban` module in debug mode: - - ```bash - ./python.d.plugin fail2ban debug trace - ``` - alerts: [] - metrics: - folding: - title: Metrics - enabled: false - description: "" - availability: [] - scopes: - - name: global - description: | - These metrics refer to the entire monitored application. - labels: [] - metrics: - - name: fail2ban.failed_attempts - description: Failed attempts - unit: "attempts/s" - chart_type: line - dimensions: - - name: a dimension per jail - - name: fail2ban.bans - description: Bans - unit: "bans/s" - chart_type: line - dimensions: - - name: a dimension per jail - - name: fail2ban.banned_ips - description: Banned IP addresses (since the last restart of netdata) - unit: "ips" - chart_type: line - dimensions: - - name: a dimension per jail |