2020-08-12 04:10:41 +08:00
|
|
|
# Iptables Input Plugin
|
2016-07-06 05:15:54 +08:00
|
|
|
|
2022-08-10 00:57:31 +08:00
|
|
|
The iptables plugin gathers packets and bytes counters for rules within a set
|
|
|
|
|
of table and chain from the Linux's iptables firewall.
|
2016-07-06 05:15:54 +08:00
|
|
|
|
2022-06-08 05:37:08 +08:00
|
|
|
Rules are identified through associated comment. **Rules without comment are
|
2022-08-10 00:57:31 +08:00
|
|
|
ignored**. Indeed we need a unique ID for the rule and the rule number is not
|
|
|
|
|
a constant: it may vary when rules are inserted/deleted at start-up or by
|
2022-06-08 05:37:08 +08:00
|
|
|
automatic tools (interactive firewalls, fail2ban, ...). Also when the rule set
|
|
|
|
|
is becoming big (hundreds of lines) most people are interested in monitoring
|
|
|
|
|
only a small part of the rule set.
|
2017-03-02 17:58:26 +08:00
|
|
|
|
2022-06-08 05:37:08 +08:00
|
|
|
Before using this plugin **you must ensure that the rules you want to monitor
|
|
|
|
|
are named with a unique comment**. Comments are added using the `-m comment
|
|
|
|
|
--comment "my comment"` iptables options.
|
2016-07-06 05:15:54 +08:00
|
|
|
|
2022-06-08 05:37:08 +08:00
|
|
|
The iptables command requires CAP_NET_ADMIN and CAP_NET_RAW capabilities. You
|
|
|
|
|
have several options to grant telegraf to run iptables:
|
2016-07-06 05:15:54 +08:00
|
|
|
|
|
|
|
|
* Run telegraf as root. This is strongly discouraged.
|
2022-06-08 05:37:08 +08:00
|
|
|
* Configure systemd to run telegraf with CAP_NET_ADMIN and CAP_NET_RAW. This is
|
|
|
|
|
the simplest and recommended option.
|
2022-08-10 00:57:31 +08:00
|
|
|
* Configure sudo to grant telegraf to run iptables. This is the most
|
|
|
|
|
restrictive option, but require sudo setup.
|
2016-07-06 05:15:54 +08:00
|
|
|
|
2021-11-25 03:18:53 +08:00
|
|
|
## Using systemd capabilities
|
2016-07-06 05:15:54 +08:00
|
|
|
|
|
|
|
|
You may run `systemctl edit telegraf.service` and add the following:
|
|
|
|
|
|
2021-11-25 03:18:53 +08:00
|
|
|
```shell
|
2016-07-06 05:15:54 +08:00
|
|
|
[Service]
|
|
|
|
|
CapabilityBoundingSet=CAP_NET_RAW CAP_NET_ADMIN
|
|
|
|
|
AmbientCapabilities=CAP_NET_RAW CAP_NET_ADMIN
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-10 00:57:31 +08:00
|
|
|
Since telegraf will fork a process to run iptables, `AmbientCapabilities` is
|
|
|
|
|
required to transmit the capabilities bounding set to the forked process.
|
2016-07-06 05:15:54 +08:00
|
|
|
|
2021-11-25 03:18:53 +08:00
|
|
|
## Using sudo
|
2016-07-06 05:15:54 +08:00
|
|
|
|
2019-01-25 02:54:25 +08:00
|
|
|
You will need the following in your telegraf config:
|
2021-11-25 03:18:53 +08:00
|
|
|
|
2019-01-25 02:54:25 +08:00
|
|
|
```toml
|
|
|
|
|
[[inputs.iptables]]
|
|
|
|
|
use_sudo = true
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
You will also need to update your sudoers file:
|
2016-07-06 05:15:54 +08:00
|
|
|
|
2019-01-25 02:54:25 +08:00
|
|
|
```bash
|
|
|
|
|
$ visudo
|
|
|
|
|
# Add the following line:
|
|
|
|
|
Cmnd_Alias IPTABLESSHOW = /usr/bin/iptables -nvL *
|
|
|
|
|
telegraf ALL=(root) NOPASSWD: IPTABLESSHOW
|
|
|
|
|
Defaults!IPTABLESSHOW !logfile, !syslog, !pam_session
|
2016-07-06 05:15:54 +08:00
|
|
|
```
|
|
|
|
|
|
2021-11-25 03:18:53 +08:00
|
|
|
## Using IPtables lock feature
|
2017-02-01 22:37:18 +08:00
|
|
|
|
2022-06-08 05:37:08 +08:00
|
|
|
Defining multiple instances of this plugin in telegraf.conf can lead to
|
|
|
|
|
concurrent IPtables access resulting in "ERROR in input [inputs.iptables]: exit
|
|
|
|
|
status 4" messages in telegraf.log and missing metrics. Setting 'use_lock =
|
|
|
|
|
true' in the plugin configuration will run IPtables with the '-w' switch,
|
|
|
|
|
allowing a lock usage to prevent this error.
|
2017-02-01 22:37:18 +08:00
|
|
|
|
2022-10-27 03:58:36 +08:00
|
|
|
## Global configuration options <!-- @/docs/includes/plugin_config.md -->
|
|
|
|
|
|
|
|
|
|
In addition to the plugin-specific configuration settings, plugins support
|
|
|
|
|
additional global and plugin configuration settings. These settings are used to
|
|
|
|
|
modify metrics, tags, and field or create aliases and configure ordering, etc.
|
|
|
|
|
See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
|
|
|
|
|
|
2023-01-12 23:55:21 +08:00
|
|
|
[CONFIGURATION.md]: ../../../docs/CONFIGURATION.md#plugins
|
2022-10-27 03:58:36 +08:00
|
|
|
|
2021-11-25 03:18:53 +08:00
|
|
|
## Configuration
|
2016-07-06 05:15:54 +08:00
|
|
|
|
2022-05-24 21:49:47 +08:00
|
|
|
```toml @sample.conf
|
2022-04-08 06:01:21 +08:00
|
|
|
# Gather packets and bytes throughput from iptables
|
2023-03-02 05:21:14 +08:00
|
|
|
# This plugin ONLY supports Linux
|
2022-04-08 06:01:21 +08:00
|
|
|
[[inputs.iptables]]
|
|
|
|
|
## iptables require root access on most systems.
|
|
|
|
|
## Setting 'use_sudo' to true will make use of sudo to run iptables.
|
2022-08-10 00:57:31 +08:00
|
|
|
## Users must configure sudo to allow telegraf user to run iptables with
|
|
|
|
|
## no password.
|
2022-04-08 06:01:21 +08:00
|
|
|
## iptables can be restricted to only list command "iptables -nvL".
|
2016-07-06 05:15:54 +08:00
|
|
|
use_sudo = false
|
2022-04-08 06:01:21 +08:00
|
|
|
## Setting 'use_lock' to true runs iptables with the "-w" option.
|
2022-08-10 00:57:31 +08:00
|
|
|
## Adjust your sudo settings appropriately if using this option
|
|
|
|
|
## ("iptables -w 5 -nvl")
|
2017-02-01 22:37:18 +08:00
|
|
|
use_lock = false
|
2022-04-08 06:01:21 +08:00
|
|
|
## Define an alternate executable, such as "ip6tables". Default is "iptables".
|
2018-09-13 02:47:45 +08:00
|
|
|
# binary = "ip6tables"
|
2022-04-08 06:01:21 +08:00
|
|
|
## defines the table to monitor:
|
2016-07-06 05:15:54 +08:00
|
|
|
table = "filter"
|
2022-04-08 06:01:21 +08:00
|
|
|
## defines the chains to monitor.
|
|
|
|
|
## NOTE: iptables rules without a comment will not be monitored.
|
|
|
|
|
## Read the plugin documentation for more information.
|
2016-07-06 05:15:54 +08:00
|
|
|
chains = [ "INPUT" ]
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-09 18:04:41 +08:00
|
|
|
## Metrics
|
|
|
|
|
|
|
|
|
|
### Measurements & Fields
|
2016-07-06 05:15:54 +08:00
|
|
|
|
2021-11-25 03:18:53 +08:00
|
|
|
* iptables
|
|
|
|
|
* pkts (integer, count)
|
|
|
|
|
* bytes (integer, bytes)
|
2016-07-06 05:15:54 +08:00
|
|
|
|
2023-02-09 18:04:41 +08:00
|
|
|
### Tags
|
2016-07-06 05:15:54 +08:00
|
|
|
|
2021-11-25 03:18:53 +08:00
|
|
|
* All measurements have the following tags:
|
|
|
|
|
* table
|
|
|
|
|
* chain
|
|
|
|
|
* ruleid
|
2016-07-06 05:15:54 +08:00
|
|
|
|
|
|
|
|
The `ruleid` is the comment associated to the rule.
|
|
|
|
|
|
2021-11-25 03:18:53 +08:00
|
|
|
## Example Output
|
2016-07-06 05:15:54 +08:00
|
|
|
|
2023-04-04 19:43:49 +08:00
|
|
|
```shell
|
|
|
|
|
iptables -nvL INPUT
|
|
|
|
|
```
|
|
|
|
|
|
2021-11-25 03:18:53 +08:00
|
|
|
```text
|
2016-07-06 05:15:54 +08:00
|
|
|
Chain INPUT (policy DROP 0 packets, 0 bytes)
|
|
|
|
|
pkts bytes target prot opt in out source destination
|
|
|
|
|
100 1024 ACCEPT tcp -- * * 192.168.0.0/24 0.0.0.0/0 tcp dpt:22 /* ssh */
|
|
|
|
|
42 2048 ACCEPT tcp -- * * 192.168.0.0/24 0.0.0.0/0 tcp dpt:80 /* httpd */
|
|
|
|
|
```
|
|
|
|
|
|
2023-04-04 19:43:49 +08:00
|
|
|
```text
|
2016-07-06 05:15:54 +08:00
|
|
|
iptables,table=filter,chain=INPUT,ruleid=ssh pkts=100i,bytes=1024i 1453831884664956455
|
|
|
|
|
iptables,table=filter,chain=INPUT,ruleid=httpd pkts=42i,bytes=2048i 1453831884664956455
|
|
|
|
|
```
|