You've already forked prometheus-fail2ban-exporter
Compare commits
41 Commits
Author | SHA1 | Date | |
---|---|---|---|
e0531694dd | |||
c2bc99afc2 | |||
497e2ff692 | |||
157e065369 | |||
b397a51cf8 | |||
4be463a7c8 | |||
7932ccbe23 | |||
6bbdd7a0a6 | |||
c208c8e97d | |||
f18bd78d4e | |||
61a8a58754 | |||
4f2d8d9079 | |||
f2cd6ebb7b | |||
695447a4c2 | |||
5363cad4ce | |||
1a660ab046 | |||
4c122609b8 | |||
b268f8654c | |||
025347b7ca | |||
56730c8774 | |||
bb5c15de1b | |||
60e6365e1f | |||
7cdaf1ebd1 | |||
9ccad42342 | |||
3591582b61 | |||
0b6a941b38 | |||
d8ce799223 | |||
5a107cc547 | |||
351d3344f7 | |||
1e29f6bfbf | |||
038aeaaafd | |||
94ee6cac4e | |||
51c1157d21 | |||
84b9d02068 | |||
a1a0aa03a4 | |||
22a165da3e | |||
964fbfd0f8 | |||
03f5084020 | |||
911736cee4 | |||
fba9ee2809 | |||
d9f1ee33c8 |
@ -1,21 +1,26 @@
|
|||||||
image: golang:latest
|
image: golang:latest
|
||||||
|
|
||||||
before_script:
|
before_script:
|
||||||
- make install-deps
|
- make go/dependencies
|
||||||
|
|
||||||
stages:
|
stages:
|
||||||
- test
|
- test
|
||||||
- build
|
- build
|
||||||
|
|
||||||
|
dependencies:
|
||||||
|
stage: test
|
||||||
|
script:
|
||||||
|
- make go/checkDependencies
|
||||||
|
|
||||||
format:
|
format:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- make format
|
- make go/checkFmt
|
||||||
|
|
||||||
test:
|
test:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- make test
|
- make go/test
|
||||||
|
|
||||||
build:
|
build:
|
||||||
stage: build
|
stage: build
|
||||||
@ -31,7 +36,7 @@ build:
|
|||||||
- dist/checksums.txt
|
- dist/checksums.txt
|
||||||
expire_in: 1 day
|
expire_in: 1 day
|
||||||
|
|
||||||
docker-gitlab:
|
docker/gitlab:
|
||||||
stage: build
|
stage: build
|
||||||
only:
|
only:
|
||||||
- main
|
- main
|
||||||
@ -44,6 +49,6 @@ docker-gitlab:
|
|||||||
- apk add make
|
- apk add make
|
||||||
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
|
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
|
||||||
script:
|
script:
|
||||||
- make docker/build-latest
|
- make docker/build/latest
|
||||||
- make docker/build-tag
|
- make docker/build/tag
|
||||||
- docker push registry.gitlab.com/hectorjsmith/fail2ban-prometheus-exporter
|
- docker push registry.gitlab.com/hectorjsmith/fail2ban-prometheus-exporter
|
||||||
|
@ -2,13 +2,12 @@
|
|||||||
# Make sure to check the documentation at http://goreleaser.com
|
# Make sure to check the documentation at http://goreleaser.com
|
||||||
before:
|
before:
|
||||||
hooks:
|
hooks:
|
||||||
- make install-deps
|
- make go/dependencies
|
||||||
builds:
|
builds:
|
||||||
-
|
-
|
||||||
dir: src
|
dir: src
|
||||||
goos:
|
goos:
|
||||||
- linux
|
- linux
|
||||||
- windows
|
|
||||||
- darwin
|
- darwin
|
||||||
goarch:
|
goarch:
|
||||||
- amd64
|
- amd64
|
||||||
|
38
CHANGELOG.md
38
CHANGELOG.md
@ -5,6 +5,40 @@ All notable changes to this project will be documented in this file.
|
|||||||
The format is based on [Keep a Changelog], and this project adheres to [Semantic Versioning].
|
The format is based on [Keep a Changelog], and this project adheres to [Semantic Versioning].
|
||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
*Nothing yet*
|
||||||
|
|
||||||
|
## [0.5.0] - 2021-12-21
|
||||||
|
*Remove deprecated code & support python2*
|
||||||
|
|
||||||
|
### Added
|
||||||
|
- (b397a51) feat: sample grafana dashboard ([#15](https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/issues/15))
|
||||||
|
- (c208c8e) feat: add listen address parameter - thanks [@private-creator](https://gitlab.com/private-creator)!
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
- (7932ccb) fix: support python2 fail2ban ([#14](https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/issues/14)) - thanks [@private-creator](https://gitlab.com/private-creator)!
|
||||||
|
|
||||||
|
### BREAKING CHANGE
|
||||||
|
- Remove `-db` CLI flag
|
||||||
|
- Remove `f2b_errors{type="db"}` metric
|
||||||
|
|
||||||
|
## [0.4.0] - 2021-10-18
|
||||||
|
*Add new fail2ban config metrics*
|
||||||
|
|
||||||
|
### Added
|
||||||
|
- (56730c8) feat: add new jail config metrics
|
||||||
|
- (5a107cc) feat: support for textfile metrics ([#13](https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/issues/13))
|
||||||
|
|
||||||
|
### Removed
|
||||||
|
- (b268f86) remove: database-based metrics
|
||||||
|
- (0b6a941) remove: windows builds
|
||||||
|
|
||||||
|
## [0.3.0] - 2021-09-27
|
||||||
|
*Export new version metrics ([#12](https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/issues/12))*
|
||||||
|
|
||||||
|
### Added
|
||||||
|
- (3c9a005) feat: render basic html page at root url
|
||||||
|
- (22a165d) feat: improve startup logging
|
||||||
|
- (fba9ee2) feat: export new version metric ([#12](https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/issues/12))
|
||||||
|
|
||||||
## [0.2.0] - 2021-08-31
|
## [0.2.0] - 2021-08-31
|
||||||
*Collect metrics through fail2ban socket - based on [#11](https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/issues/11)*
|
*Collect metrics through fail2ban socket - based on [#11](https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/issues/11)*
|
||||||
@ -39,6 +73,7 @@ The format is based on [Keep a Changelog], and this project adheres to [Semantic
|
|||||||
- (0842419) fix: compile tool without cgo_enabled flag
|
- (0842419) fix: compile tool without cgo_enabled flag
|
||||||
|
|
||||||
## 0.0.0 - 2021-02-05
|
## 0.0.0 - 2021-02-05
|
||||||
|
*Repository creation*
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@ -47,3 +82,6 @@ The format is based on [Keep a Changelog], and this project adheres to [Semantic
|
|||||||
[Unreleased]: https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/compare/0.1.0...main
|
[Unreleased]: https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/compare/0.1.0...main
|
||||||
[0.1.0]: https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/compare/0.0.0...0.1.0
|
[0.1.0]: https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/compare/0.0.0...0.1.0
|
||||||
[0.2.0]: https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/compare/0.1.0...0.2.0
|
[0.2.0]: https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/compare/0.1.0...0.2.0
|
||||||
|
[0.3.0]: https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/compare/0.2.0...0.3.0
|
||||||
|
[0.4.0]: https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/compare/0.3.0...0.4.0
|
||||||
|
[0.5.0]: https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/compare/0.4.0...0.5.0
|
||||||
|
26
Makefile
26
Makefile
@ -1,20 +1,22 @@
|
|||||||
install-deps:
|
go/dependencies:
|
||||||
cd src/ && go mod download
|
cd src/ && go mod download
|
||||||
|
|
||||||
# Standard go test
|
|
||||||
test:
|
|
||||||
cd src/ && go test ./... -v -race
|
|
||||||
|
|
||||||
# Make sure no unnecessary dependencies are present
|
# Make sure no unnecessary dependencies are present
|
||||||
go-mod-tidy:
|
go/checkDependencies:
|
||||||
cd src/ && go mod tidy -v
|
cd src/ && go mod tidy -v
|
||||||
git diff-index --quiet HEAD
|
git diff-index --quiet HEAD
|
||||||
|
|
||||||
format:
|
# Standard go test
|
||||||
cd src/ && go fmt $(go list ./... | grep -v /vendor/)
|
go/test:
|
||||||
cd src/ && go vet $(go list ./... | grep -v /vendor/)
|
cd src/ && go test ./... -v -race
|
||||||
|
|
||||||
generateChangelog:
|
go/fmt:
|
||||||
|
cd src/ && go fmt ./...
|
||||||
|
|
||||||
|
go/checkFmt:
|
||||||
|
test -z $(shell gofmt -l .)
|
||||||
|
|
||||||
|
docs/genChangelog:
|
||||||
./tools/git-chglog_linux_amd64 --config tools/chglog/config.yml 0.0.0.. > CHANGELOG_gen.md
|
./tools/git-chglog_linux_amd64 --config tools/chglog/config.yml 0.0.0.. > CHANGELOG_gen.md
|
||||||
|
|
||||||
build/snapshot:
|
build/snapshot:
|
||||||
@ -27,8 +29,8 @@ build/docker:
|
|||||||
cd src/ && go build -o exporter \
|
cd src/ && go build -o exporter \
|
||||||
-ldflags '-X main.version=$(shell git describe --tags) -X main.commit=${shell git rev-parse HEAD} -X "main.date=${shell date --rfc-3339=seconds}" -X main.builtBy=docker' exporter.go
|
-ldflags '-X main.version=$(shell git describe --tags) -X main.commit=${shell git rev-parse HEAD} -X "main.date=${shell date --rfc-3339=seconds}" -X main.builtBy=docker' exporter.go
|
||||||
|
|
||||||
docker/build-latest:
|
docker/build/latest:
|
||||||
docker build -t registry.gitlab.com/hectorjsmith/fail2ban-prometheus-exporter:latest .
|
docker build -t registry.gitlab.com/hectorjsmith/fail2ban-prometheus-exporter:latest .
|
||||||
|
|
||||||
docker/build-tag:
|
docker/build/tag:
|
||||||
docker build -t registry.gitlab.com/hectorjsmith/fail2ban-prometheus-exporter:$(shell git describe --tags) .
|
docker build -t registry.gitlab.com/hectorjsmith/fail2ban-prometheus-exporter:$(shell git describe --tags) .
|
||||||
|
111
README.md
111
README.md
@ -9,27 +9,22 @@ Go tool to collect and export metrics on Fail2Ban
|
|||||||
4. Metrics
|
4. Metrics
|
||||||
|
|
||||||
## 1. Introduction
|
## 1. Introduction
|
||||||
The exporter can collect metrics from 2 locations: the fail2ban server socket and the fail2ban server database.
|
This exporter collects metrics from a running fail2ban instance.
|
||||||
|
|
||||||
Once the exporter is running, metrics are available at `localhost:9191/metrics`.
|
Once the exporter is running, metrics are available at `localhost:9191/metrics`.
|
||||||
|
|
||||||
(The default port is `9191` but can be modified with the `-port` flag)
|
(The default port is `9191` but can be modified with the `-port` flag)
|
||||||
|
|
||||||
### 1.1. Socket
|
The exporter communicates with the fail2ban server over its socket.
|
||||||
The recommended way to run the exporter is to point it at the fail2ban server socket.
|
This allows the data collected by the exporter to always align with the output of the `fail2ban-client`.
|
||||||
This allows the exporter to communicate with the server the same way `fail2ban-client` does and ensures the metrics it collects align with the values reported by `fail2ban-client status <jail>`.
|
|
||||||
|
|
||||||
The default path to the socket is: `/var/run/fail2ban/fail2ban.sock`
|
The default location of the socket is: `/var/run/fail2ban/fail2ban.sock`
|
||||||
|
|
||||||
### 1.2. Deprecated: Database
|
## 1.1. Grafana
|
||||||
The original way to collect metrics is to read them from the fail2ban database.
|
|
||||||
This has now been deprecated in favour of using the socket.
|
|
||||||
The reason being that database metrics do not always align with the output of `fail2ban-client status <jail>` and cause confusion.
|
|
||||||
See [#11](https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/-/issues/11) for more details.
|
|
||||||
|
|
||||||
If necessary, these metrics can still be exported by providing the database path to the exporter.
|
The metrics exported by this tool are compatible with Prometheus and Grafana. A sample grafana dashboard can be found in the `grafana.json` file. Just import the contents of this file into a new Grafana dashboard to get started.
|
||||||
|
|
||||||
The default path to the fail2ban database is: `/var/lib/fail2ban/fail2ban.sqlite3`
|
*(Sample dashboard is compatible with Grafana `8.2.1` and above)*
|
||||||
|
|
||||||
## 2. Running the Exporter
|
## 2. Running the Exporter
|
||||||
|
|
||||||
@ -44,14 +39,18 @@ See the [releases page](https://gitlab.com/hectorjsmith/fail2ban-prometheus-expo
|
|||||||
```
|
```
|
||||||
$ fail2ban-prometheus-exporter -h
|
$ fail2ban-prometheus-exporter -h
|
||||||
|
|
||||||
-db string
|
-web.listen-address string
|
||||||
path to the fail2ban sqlite database (deprecated)
|
address to use for metrics server (default 0.0.0.0)
|
||||||
-port int
|
-port int
|
||||||
port to use for the metrics server (default 9191)
|
port to use for the metrics server (default 9191)
|
||||||
-socket string
|
-socket string
|
||||||
path to the fail2ban server socket
|
path to the fail2ban server socket
|
||||||
-version
|
-version
|
||||||
show version info and exit
|
show version info and exit
|
||||||
|
-collector.textfile
|
||||||
|
enable the textfile collector
|
||||||
|
-collector.textfile.directory string
|
||||||
|
directory to read text files with metrics from
|
||||||
```
|
```
|
||||||
|
|
||||||
**Example**
|
**Example**
|
||||||
@ -60,7 +59,7 @@ $ fail2ban-prometheus-exporter -h
|
|||||||
fail2ban-prometheus-exporter -socket /var/run/fail2ban/fail2ban.sock -port 9191
|
fail2ban-prometheus-exporter -socket /var/run/fail2ban/fail2ban.sock -port 9191
|
||||||
```
|
```
|
||||||
|
|
||||||
Note that the exporter will need read access to the fail2ban socket or database.
|
Note that the exporter will need read access to the fail2ban socket.
|
||||||
|
|
||||||
### 2.1. Compile from Source
|
### 2.1. Compile from Source
|
||||||
|
|
||||||
@ -71,7 +70,7 @@ Run `go mod download` to download all necessary dependencies before running the
|
|||||||
|
|
||||||
## 3. Running in Docker
|
## 3. Running in Docker
|
||||||
|
|
||||||
If use of docker is desired, an official docker image is available on the Gitlab container registry.
|
An official docker image is available on the Gitlab container registry.
|
||||||
Use it by pulling the following image:
|
Use it by pulling the following image:
|
||||||
|
|
||||||
```
|
```
|
||||||
@ -83,11 +82,10 @@ See the [registry page](https://gitlab.com/hectorjsmith/fail2ban-prometheus-expo
|
|||||||
|
|
||||||
### 3.1. Volumes
|
### 3.1. Volumes
|
||||||
|
|
||||||
The docker image is designed to run by mounting either the fail2ban sqlite3 database of the fail2ban run folder.
|
The docker image is designed to run by mounting the fail2ban run folder.
|
||||||
- The database should be mounted at: `/app/fail2ban.sqlite3`
|
The run folder should be mounted in the container at: `/var/run/fail2ban`.
|
||||||
- The run folder should be mounted at: `/var/run/fail2ban`
|
|
||||||
|
|
||||||
Both paths can be mounted with readonly (`ro`) permissions.
|
The folder can be mounted with read-only (`ro`) permissions.
|
||||||
|
|
||||||
**NOTE:** While it is possible to mount the `fail2ban.sock` file directly, it is recommended to mount the parent folder instead.
|
**NOTE:** While it is possible to mount the `fail2ban.sock` file directly, it is recommended to mount the parent folder instead.
|
||||||
The `.sock` file is deleted by fail2ban on shutdown and re-created on startup and this causes problems for the docker mount.
|
The `.sock` file is deleted by fail2ban on shutdown and re-created on startup and this causes problems for the docker mount.
|
||||||
@ -100,7 +98,6 @@ Use the following command to run the exporter as a docker container.
|
|||||||
```
|
```
|
||||||
docker run -d \
|
docker run -d \
|
||||||
--name "fail2ban-exporter" \
|
--name "fail2ban-exporter" \
|
||||||
-v /var/lib/fail2ban/fail2ban.sqlite3:/app/fail2ban.sqlite3:ro \
|
|
||||||
-v /var/run/fail2ban:/var/run/fail2ban:ro \
|
-v /var/run/fail2ban:/var/run/fail2ban:ro \
|
||||||
-p "9191:9191" \
|
-p "9191:9191" \
|
||||||
registry.gitlab.com/hectorjsmith/fail2ban-prometheus-exporter:latest
|
registry.gitlab.com/hectorjsmith/fail2ban-prometheus-exporter:latest
|
||||||
@ -116,7 +113,6 @@ services:
|
|||||||
exporter:
|
exporter:
|
||||||
image: registry.gitlab.com/hectorjsmith/fail2ban-prometheus-exporter:latest
|
image: registry.gitlab.com/hectorjsmith/fail2ban-prometheus-exporter:latest
|
||||||
volumes:
|
volumes:
|
||||||
- /var/lib/fail2ban/fail2ban.sqlite3:/app/fail2ban.sqlite3:ro
|
|
||||||
- /var/run/fail2ban/:/var/run/fail2ban:ro
|
- /var/run/fail2ban/:/var/run/fail2ban:ro
|
||||||
ports:
|
ports:
|
||||||
- "9191:9191"
|
- "9191:9191"
|
||||||
@ -137,7 +133,7 @@ In this configuration, there will be two jails - one for IPs banned from the SSH
|
|||||||
This tool exports several metrics *per jail*, meaning that it is possible to track how many IPs are being banned in each jail as well as the overall total.
|
This tool exports several metrics *per jail*, meaning that it is possible to track how many IPs are being banned in each jail as well as the overall total.
|
||||||
This can be useful to track what services are seeing more failed logins.
|
This can be useful to track what services are seeing more failed logins.
|
||||||
|
|
||||||
### 4.1. Socket-based Metrics
|
### 4.1. Fail2Ban Metrics
|
||||||
|
|
||||||
These are the metrics exported by reading data from the fail2ban server socket.
|
These are the metrics exported by reading data from the fail2ban server socket.
|
||||||
All metrics are prefixed with `f2b_`.
|
All metrics are prefixed with `f2b_`.
|
||||||
@ -145,7 +141,6 @@ All metrics are prefixed with `f2b_`.
|
|||||||
Exposed metrics:
|
Exposed metrics:
|
||||||
* `up` - Returns 1 if the fail2ban server is up and connection succeeds
|
* `up` - Returns 1 if the fail2ban server is up and connection succeeds
|
||||||
* `errors` - Number of errors since startup
|
* `errors` - Number of errors since startup
|
||||||
* `db` - Errors connecting to the database
|
|
||||||
* `socket_conn` - Errors connecting to the fail2ban socket (e.g. connection refused)
|
* `socket_conn` - Errors connecting to the fail2ban socket (e.g. connection refused)
|
||||||
* `socket_req` - Errors sending requests to the fail2ban server (e.g. invalid responses)
|
* `socket_req` - Errors sending requests to the fail2ban server (e.g. invalid responses)
|
||||||
* `jail_count` - Number of jails configured in fail2ban
|
* `jail_count` - Number of jails configured in fail2ban
|
||||||
@ -153,13 +148,16 @@ Exposed metrics:
|
|||||||
* `jail_banned_total` (per jail) - Total number of banned IPs since fail2ban startup (includes expired bans)
|
* `jail_banned_total` (per jail) - Total number of banned IPs since fail2ban startup (includes expired bans)
|
||||||
* `jail_failed_current` (per jail) - Number of current failures
|
* `jail_failed_current` (per jail) - Number of current failures
|
||||||
* `jail_failed_total` (per jail) - Total number of failures since fail2ban startup
|
* `jail_failed_total` (per jail) - Total number of failures since fail2ban startup
|
||||||
|
* `jail_config_ban_time` (per jail) - How long an IP is banned for in this jail (in seconds)
|
||||||
|
* `jail_config_find_time` (per jail) - How far back the filter will look for failures in this jail (in seconds)
|
||||||
|
* `jail_config_max_retry` (per jail) - The max number of failures allowed before banning an IP in this jail
|
||||||
|
* `version` - Version string of the exporter and fail2ban
|
||||||
|
|
||||||
**Sample**
|
**Sample**
|
||||||
|
|
||||||
```
|
```
|
||||||
# HELP f2b_errors Number of errors found since startup
|
# HELP f2b_errors Number of errors found since startup
|
||||||
# TYPE f2b_errors counter
|
# TYPE f2b_errors counter
|
||||||
f2b_errors{type="db"} 0
|
|
||||||
f2b_errors{type="socket_conn"} 0
|
f2b_errors{type="socket_conn"} 0
|
||||||
f2b_errors{type="socket_req"} 0
|
f2b_errors{type="socket_req"} 0
|
||||||
# HELP f2b_jail_banned_current Number of IPs currently banned in this jail
|
# HELP f2b_jail_banned_current Number of IPs currently banned in this jail
|
||||||
@ -181,9 +179,24 @@ f2b_jail_failed_current{jail="sshd"} 6
|
|||||||
# TYPE f2b_jail_failed_total gauge
|
# TYPE f2b_jail_failed_total gauge
|
||||||
f2b_jail_failed_total{jail="recidive"} 7
|
f2b_jail_failed_total{jail="recidive"} 7
|
||||||
f2b_jail_failed_total{jail="sshd"} 125
|
f2b_jail_failed_total{jail="sshd"} 125
|
||||||
|
# HELP f2b_config_jail_ban_time How long an IP is banned for in this jail (in seconds)
|
||||||
|
# TYPE f2b_config_jail_ban_time gauge
|
||||||
|
f2b_config_jail_ban_time{jail="recidive"} 604800
|
||||||
|
f2b_config_jail_ban_time{jail="sshd"} 600
|
||||||
|
# HELP f2b_config_jail_find_time How far back will the filter look for failures in this jail (in seconds)
|
||||||
|
# TYPE f2b_config_jail_find_time gauge
|
||||||
|
f2b_config_jail_find_time{jail="recidive"} 86400
|
||||||
|
f2b_config_jail_find_time{jail="sshd"} 600
|
||||||
|
# HELP f2b_config_jail_max_retries The number of failures allowed until the IP is banned by this jail
|
||||||
|
# TYPE f2b_config_jail_max_retries gauge
|
||||||
|
f2b_config_jail_max_retries{jail="recidive"} 5
|
||||||
|
f2b_config_jail_max_retries{jail="sshd"} 5
|
||||||
# HELP f2b_up Check if the fail2ban server is up
|
# HELP f2b_up Check if the fail2ban server is up
|
||||||
# TYPE f2b_up gauge
|
# TYPE f2b_up gauge
|
||||||
f2b_up 1
|
f2b_up 1
|
||||||
|
# HELP f2b_version Version of the exporter and fail2ban server
|
||||||
|
# TYPE f2b_version gauge
|
||||||
|
f2b_version{exporter="0.3.0",fail2ban="0.11.1"} 1
|
||||||
```
|
```
|
||||||
|
|
||||||
The metrics above correspond to the matching fields in the `fail2ban-client status <jail>` command:
|
The metrics above correspond to the matching fields in the `fail2ban-client status <jail>` command:
|
||||||
@ -198,45 +211,23 @@ Status for the jail: sshd|- Filter
|
|||||||
`- Banned IP list: ...
|
`- Banned IP list: ...
|
||||||
```
|
```
|
||||||
|
|
||||||
### 4.2. Database Metrics (deprecated)
|
### 4.2. Textfile Metrics
|
||||||
|
|
||||||
These are the original metrics exported by the initial release of the exporter.
|
For more flexibility the exporter also allows exporting metrics collected from a text file.
|
||||||
They are all based on the data stored in the fail2ban sqlite3 database.
|
|
||||||
|
|
||||||
*These metrics are deprecated and will be removed in a future release.*
|
To enable textfile metrics:
|
||||||
|
1. Enable the collector with `-collector.textfile=true`
|
||||||
|
2. Provide the directory to read files from with the `-collector.textfile.directory` flag
|
||||||
|
|
||||||
All metrics are prefixed with `fail2ban_`.
|
Metrics collected from these files will be exposed directly alongside the other metrics without any additional processing.
|
||||||
|
This means that it is the responsibility of the file creator to ensure the format is correct.
|
||||||
|
|
||||||
Exposed metrics:
|
By exporting textfile metrics an extra metric is also exported with an error count for each file:
|
||||||
* `up` - Returns 1 if the service is up
|
|
||||||
* `errors` - Returns the number of errors found since startup
|
|
||||||
* `enabled_jails` - Returns 1 for each jail that is enabled, 0 if disabled.
|
|
||||||
* `bad_ips` (per jail)
|
|
||||||
* A *bad IP* is defined as an IP that has been banned at least once in the past
|
|
||||||
* Bad IPs are counted per jail
|
|
||||||
* `banned_ips` (per jail)
|
|
||||||
* A *banned IP* is defined as an IP that is currently banned on the firewall
|
|
||||||
* Banned IPs are counted per jail
|
|
||||||
|
|
||||||
**Sample**
|
|
||||||
|
|
||||||
```
|
```
|
||||||
# HELP fail2ban_bad_ips (Deprecated) Number of bad IPs stored in the database (per jail).
|
# HELP textfile_error Checks for errors while reading text files
|
||||||
# TYPE fail2ban_bad_ips gauge
|
# TYPE textfile_error gauge
|
||||||
fail2ban_bad_ips{jail="recidive"} 0
|
textfile_error{path="file.prom"} 0
|
||||||
fail2ban_bad_ips{jail="sshd"} 0
|
|
||||||
# HELP fail2ban_banned_ips (Deprecated) Number of banned IPs stored in the database (per jail).
|
|
||||||
# TYPE fail2ban_banned_ips gauge
|
|
||||||
fail2ban_banned_ips{jail="recidive"} 0
|
|
||||||
fail2ban_banned_ips{jail="sshd"} 0
|
|
||||||
# HELP fail2ban_enabled_jails (Deprecated) Enabled jails.
|
|
||||||
# TYPE fail2ban_enabled_jails gauge
|
|
||||||
fail2ban_enabled_jails{jail="recidive"} 1
|
|
||||||
fail2ban_enabled_jails{jail="sshd"} 1
|
|
||||||
# HELP fail2ban_errors (Deprecated) Number of errors found since startup.
|
|
||||||
# TYPE fail2ban_errors counter
|
|
||||||
fail2ban_errors{type="db"} 0
|
|
||||||
# HELP fail2ban_up (Deprecated) Was the last fail2ban query successful.
|
|
||||||
# TYPE fail2ban_up gauge
|
|
||||||
fail2ban_up 1
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
**NOTE:** Any file not ending with `.prom` will be ignored.
|
||||||
|
@ -3,19 +3,18 @@
|
|||||||
# Print version to logs for debugging purposes
|
# Print version to logs for debugging purposes
|
||||||
/app/fail2ban-prometheus-exporter -version
|
/app/fail2ban-prometheus-exporter -version
|
||||||
|
|
||||||
db_path=/app/fail2ban.sqlite3
|
|
||||||
socket_path=/var/run/fail2ban/fail2ban.sock
|
socket_path=/var/run/fail2ban/fail2ban.sock
|
||||||
|
textfile_dir=/app/textfile/
|
||||||
|
textfile_enabled=false
|
||||||
|
|
||||||
# Blank out the file paths if they do not exist - a hacky way to only use these files if they were mounted into the container.
|
# Enable textfile metrics if the folder exists (i.e. was mounted by docker)
|
||||||
if [ ! -f "$db_path" ]; then
|
if [ -d $textfile_dir ]; then
|
||||||
db_path=""
|
textfile_enabled=true
|
||||||
fi
|
|
||||||
if [ ! -S "$socket_path" ]; then
|
|
||||||
socket_path=""
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Start the exporter (use exec to support graceful shutdown)
|
# Start the exporter (use exec to support graceful shutdown)
|
||||||
# Inspired by: https://akomljen.com/stopping-docker-containers-gracefully/
|
# Inspired by: https://akomljen.com/stopping-docker-containers-gracefully/
|
||||||
exec /app/fail2ban-prometheus-exporter \
|
exec /app/fail2ban-prometheus-exporter \
|
||||||
-db "$db_path" \
|
-socket "$socket_path" \
|
||||||
-socket "$socket_path"
|
-collector.textfile=$textfile_enabled \
|
||||||
|
-collector.textfile.directory="$textfile_dir"
|
||||||
|
718
grafana.json
Normal file
718
grafana.json
Normal file
@ -0,0 +1,718 @@
|
|||||||
|
{
|
||||||
|
"__inputs": [],
|
||||||
|
"__requires": [
|
||||||
|
{
|
||||||
|
"type": "grafana",
|
||||||
|
"id": "grafana",
|
||||||
|
"name": "Grafana",
|
||||||
|
"version": "8.2.1"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "panel",
|
||||||
|
"id": "table",
|
||||||
|
"name": "Table",
|
||||||
|
"version": ""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "panel",
|
||||||
|
"id": "timeseries",
|
||||||
|
"name": "Time series",
|
||||||
|
"version": ""
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"annotations": {
|
||||||
|
"list": [
|
||||||
|
{
|
||||||
|
"builtIn": 1,
|
||||||
|
"datasource": "-- Grafana --",
|
||||||
|
"enable": true,
|
||||||
|
"hide": true,
|
||||||
|
"iconColor": "rgba(0, 211, 255, 1)",
|
||||||
|
"name": "Annotations & Alerts",
|
||||||
|
"target": {
|
||||||
|
"limit": 100,
|
||||||
|
"matchAny": false,
|
||||||
|
"tags": [],
|
||||||
|
"type": "dashboard"
|
||||||
|
},
|
||||||
|
"type": "dashboard"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"editable": true,
|
||||||
|
"fiscalYearStartMonth": 0,
|
||||||
|
"gnetId": null,
|
||||||
|
"graphTooltip": 2,
|
||||||
|
"id": null,
|
||||||
|
"links": [],
|
||||||
|
"liveNow": false,
|
||||||
|
"panels": [
|
||||||
|
{
|
||||||
|
"datasource": null,
|
||||||
|
"description": "",
|
||||||
|
"fieldConfig": {
|
||||||
|
"defaults": {
|
||||||
|
"color": {
|
||||||
|
"mode": "thresholds"
|
||||||
|
},
|
||||||
|
"custom": {
|
||||||
|
"align": "auto",
|
||||||
|
"displayMode": "auto"
|
||||||
|
},
|
||||||
|
"mappings": [],
|
||||||
|
"thresholds": {
|
||||||
|
"mode": "absolute",
|
||||||
|
"steps": [
|
||||||
|
{
|
||||||
|
"color": "green",
|
||||||
|
"value": null
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"overrides": [
|
||||||
|
{
|
||||||
|
"matcher": {
|
||||||
|
"id": "byRegexp",
|
||||||
|
"options": ".*Time"
|
||||||
|
},
|
||||||
|
"properties": [
|
||||||
|
{
|
||||||
|
"id": "unit",
|
||||||
|
"value": "s"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"gridPos": {
|
||||||
|
"h": 6,
|
||||||
|
"w": 24,
|
||||||
|
"x": 0,
|
||||||
|
"y": 0
|
||||||
|
},
|
||||||
|
"id": 206,
|
||||||
|
"options": {
|
||||||
|
"showHeader": true
|
||||||
|
},
|
||||||
|
"pluginVersion": "8.2.1",
|
||||||
|
"targets": [
|
||||||
|
{
|
||||||
|
"exemplar": true,
|
||||||
|
"expr": "f2b_config_jail_max_retries",
|
||||||
|
"format": "table",
|
||||||
|
"instant": true,
|
||||||
|
"interval": "",
|
||||||
|
"legendFormat": "{{jail}}",
|
||||||
|
"refId": "A"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"exemplar": true,
|
||||||
|
"expr": "f2b_config_jail_ban_time",
|
||||||
|
"format": "table",
|
||||||
|
"hide": false,
|
||||||
|
"instant": true,
|
||||||
|
"interval": "",
|
||||||
|
"legendFormat": "{{jail}}",
|
||||||
|
"refId": "B"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"exemplar": true,
|
||||||
|
"expr": "f2b_config_jail_find_time",
|
||||||
|
"format": "table",
|
||||||
|
"hide": false,
|
||||||
|
"instant": true,
|
||||||
|
"interval": "",
|
||||||
|
"legendFormat": "{{jail}}",
|
||||||
|
"refId": "C"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"title": "F2B Config",
|
||||||
|
"transformations": [
|
||||||
|
{
|
||||||
|
"id": "merge",
|
||||||
|
"options": {}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "groupBy",
|
||||||
|
"options": {
|
||||||
|
"fields": {
|
||||||
|
"Value #A": {
|
||||||
|
"aggregations": [
|
||||||
|
"lastNotNull"
|
||||||
|
],
|
||||||
|
"operation": "aggregate"
|
||||||
|
},
|
||||||
|
"Value #B": {
|
||||||
|
"aggregations": [
|
||||||
|
"lastNotNull"
|
||||||
|
],
|
||||||
|
"operation": "aggregate"
|
||||||
|
},
|
||||||
|
"Value #C": {
|
||||||
|
"aggregations": [
|
||||||
|
"lastNotNull"
|
||||||
|
],
|
||||||
|
"operation": "aggregate"
|
||||||
|
},
|
||||||
|
"jail": {
|
||||||
|
"aggregations": [],
|
||||||
|
"operation": "groupby"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "organize",
|
||||||
|
"options": {
|
||||||
|
"excludeByName": {},
|
||||||
|
"indexByName": {},
|
||||||
|
"renameByName": {
|
||||||
|
"Value #A (lastNotNull)": "Max Retries",
|
||||||
|
"Value #B (lastNotNull)": "Ban Time",
|
||||||
|
"Value #C (lastNotNull)": "Find Time",
|
||||||
|
"jail": "Jail"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"transparent": true,
|
||||||
|
"type": "table"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"datasource": null,
|
||||||
|
"description": "",
|
||||||
|
"fieldConfig": {
|
||||||
|
"defaults": {
|
||||||
|
"color": {
|
||||||
|
"mode": "palette-classic"
|
||||||
|
},
|
||||||
|
"custom": {
|
||||||
|
"axisLabel": "",
|
||||||
|
"axisPlacement": "auto",
|
||||||
|
"barAlignment": 0,
|
||||||
|
"drawStyle": "line",
|
||||||
|
"fillOpacity": 10,
|
||||||
|
"gradientMode": "none",
|
||||||
|
"hideFrom": {
|
||||||
|
"legend": false,
|
||||||
|
"tooltip": false,
|
||||||
|
"viz": false
|
||||||
|
},
|
||||||
|
"lineInterpolation": "linear",
|
||||||
|
"lineWidth": 1,
|
||||||
|
"pointSize": 5,
|
||||||
|
"scaleDistribution": {
|
||||||
|
"type": "linear"
|
||||||
|
},
|
||||||
|
"showPoints": "never",
|
||||||
|
"spanNulls": true,
|
||||||
|
"stacking": {
|
||||||
|
"group": "A",
|
||||||
|
"mode": "none"
|
||||||
|
},
|
||||||
|
"thresholdsStyle": {
|
||||||
|
"mode": "off"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"mappings": [],
|
||||||
|
"min": 0,
|
||||||
|
"thresholds": {
|
||||||
|
"mode": "absolute",
|
||||||
|
"steps": [
|
||||||
|
{
|
||||||
|
"color": "green",
|
||||||
|
"value": null
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"unit": "short"
|
||||||
|
},
|
||||||
|
"overrides": []
|
||||||
|
},
|
||||||
|
"gridPos": {
|
||||||
|
"h": 8,
|
||||||
|
"w": 12,
|
||||||
|
"x": 0,
|
||||||
|
"y": 6
|
||||||
|
},
|
||||||
|
"id": 190,
|
||||||
|
"options": {
|
||||||
|
"legend": {
|
||||||
|
"calcs": [
|
||||||
|
"lastNotNull"
|
||||||
|
],
|
||||||
|
"displayMode": "table",
|
||||||
|
"placement": "right"
|
||||||
|
},
|
||||||
|
"tooltip": {
|
||||||
|
"mode": "single"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"pluginVersion": "8.2.1",
|
||||||
|
"targets": [
|
||||||
|
{
|
||||||
|
"exemplar": true,
|
||||||
|
"expr": "f2b_jail_failed_total",
|
||||||
|
"hide": false,
|
||||||
|
"interval": "",
|
||||||
|
"legendFormat": "{{jail}}",
|
||||||
|
"refId": "A"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"timeFrom": null,
|
||||||
|
"timeShift": null,
|
||||||
|
"title": "Fail2Ban Failures (Total)",
|
||||||
|
"transparent": true,
|
||||||
|
"type": "timeseries"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"datasource": null,
|
||||||
|
"description": "",
|
||||||
|
"fieldConfig": {
|
||||||
|
"defaults": {
|
||||||
|
"color": {
|
||||||
|
"mode": "palette-classic"
|
||||||
|
},
|
||||||
|
"custom": {
|
||||||
|
"axisLabel": "",
|
||||||
|
"axisPlacement": "auto",
|
||||||
|
"barAlignment": 0,
|
||||||
|
"drawStyle": "line",
|
||||||
|
"fillOpacity": 10,
|
||||||
|
"gradientMode": "none",
|
||||||
|
"hideFrom": {
|
||||||
|
"legend": false,
|
||||||
|
"tooltip": false,
|
||||||
|
"viz": false
|
||||||
|
},
|
||||||
|
"lineInterpolation": "linear",
|
||||||
|
"lineWidth": 1,
|
||||||
|
"pointSize": 5,
|
||||||
|
"scaleDistribution": {
|
||||||
|
"type": "linear"
|
||||||
|
},
|
||||||
|
"showPoints": "never",
|
||||||
|
"spanNulls": true,
|
||||||
|
"stacking": {
|
||||||
|
"group": "A",
|
||||||
|
"mode": "none"
|
||||||
|
},
|
||||||
|
"thresholdsStyle": {
|
||||||
|
"mode": "off"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"mappings": [],
|
||||||
|
"min": 0,
|
||||||
|
"thresholds": {
|
||||||
|
"mode": "absolute",
|
||||||
|
"steps": [
|
||||||
|
{
|
||||||
|
"color": "green",
|
||||||
|
"value": null
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"unit": "short"
|
||||||
|
},
|
||||||
|
"overrides": []
|
||||||
|
},
|
||||||
|
"gridPos": {
|
||||||
|
"h": 8,
|
||||||
|
"w": 12,
|
||||||
|
"x": 12,
|
||||||
|
"y": 6
|
||||||
|
},
|
||||||
|
"id": 191,
|
||||||
|
"options": {
|
||||||
|
"legend": {
|
||||||
|
"calcs": [
|
||||||
|
"lastNotNull"
|
||||||
|
],
|
||||||
|
"displayMode": "table",
|
||||||
|
"placement": "right"
|
||||||
|
},
|
||||||
|
"tooltip": {
|
||||||
|
"mode": "single"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"pluginVersion": "8.2.1",
|
||||||
|
"targets": [
|
||||||
|
{
|
||||||
|
"exemplar": true,
|
||||||
|
"expr": "f2b_jail_banned_total",
|
||||||
|
"interval": "",
|
||||||
|
"legendFormat": "{{jail}}",
|
||||||
|
"refId": "A"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"timeFrom": null,
|
||||||
|
"timeShift": null,
|
||||||
|
"title": "Fail2Ban Bans (Total)",
|
||||||
|
"transparent": true,
|
||||||
|
"type": "timeseries"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"datasource": null,
|
||||||
|
"description": "",
|
||||||
|
"fieldConfig": {
|
||||||
|
"defaults": {
|
||||||
|
"color": {
|
||||||
|
"mode": "palette-classic"
|
||||||
|
},
|
||||||
|
"custom": {
|
||||||
|
"axisLabel": "",
|
||||||
|
"axisPlacement": "auto",
|
||||||
|
"barAlignment": 0,
|
||||||
|
"drawStyle": "line",
|
||||||
|
"fillOpacity": 10,
|
||||||
|
"gradientMode": "none",
|
||||||
|
"hideFrom": {
|
||||||
|
"legend": false,
|
||||||
|
"tooltip": false,
|
||||||
|
"viz": false
|
||||||
|
},
|
||||||
|
"lineInterpolation": "linear",
|
||||||
|
"lineWidth": 1,
|
||||||
|
"pointSize": 5,
|
||||||
|
"scaleDistribution": {
|
||||||
|
"type": "linear"
|
||||||
|
},
|
||||||
|
"showPoints": "never",
|
||||||
|
"spanNulls": true,
|
||||||
|
"stacking": {
|
||||||
|
"group": "A",
|
||||||
|
"mode": "none"
|
||||||
|
},
|
||||||
|
"thresholdsStyle": {
|
||||||
|
"mode": "off"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"mappings": [],
|
||||||
|
"min": 0,
|
||||||
|
"thresholds": {
|
||||||
|
"mode": "absolute",
|
||||||
|
"steps": [
|
||||||
|
{
|
||||||
|
"color": "green",
|
||||||
|
"value": null
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"unit": "short"
|
||||||
|
},
|
||||||
|
"overrides": []
|
||||||
|
},
|
||||||
|
"gridPos": {
|
||||||
|
"h": 8,
|
||||||
|
"w": 12,
|
||||||
|
"x": 0,
|
||||||
|
"y": 14
|
||||||
|
},
|
||||||
|
"id": 208,
|
||||||
|
"options": {
|
||||||
|
"legend": {
|
||||||
|
"calcs": [
|
||||||
|
"lastNotNull"
|
||||||
|
],
|
||||||
|
"displayMode": "table",
|
||||||
|
"placement": "right"
|
||||||
|
},
|
||||||
|
"tooltip": {
|
||||||
|
"mode": "single"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"pluginVersion": "8.2.1",
|
||||||
|
"targets": [
|
||||||
|
{
|
||||||
|
"exemplar": true,
|
||||||
|
"expr": "f2b_jail_failed_current",
|
||||||
|
"interval": "",
|
||||||
|
"legendFormat": "{{jail}}",
|
||||||
|
"refId": "A"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"timeFrom": null,
|
||||||
|
"timeShift": null,
|
||||||
|
"title": "Fail2Ban Failures (Current)",
|
||||||
|
"transparent": true,
|
||||||
|
"type": "timeseries"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"datasource": null,
|
||||||
|
"description": "",
|
||||||
|
"fieldConfig": {
|
||||||
|
"defaults": {
|
||||||
|
"color": {
|
||||||
|
"mode": "palette-classic"
|
||||||
|
},
|
||||||
|
"custom": {
|
||||||
|
"axisLabel": "",
|
||||||
|
"axisPlacement": "auto",
|
||||||
|
"barAlignment": 0,
|
||||||
|
"drawStyle": "line",
|
||||||
|
"fillOpacity": 10,
|
||||||
|
"gradientMode": "none",
|
||||||
|
"hideFrom": {
|
||||||
|
"legend": false,
|
||||||
|
"tooltip": false,
|
||||||
|
"viz": false
|
||||||
|
},
|
||||||
|
"lineInterpolation": "linear",
|
||||||
|
"lineWidth": 1,
|
||||||
|
"pointSize": 5,
|
||||||
|
"scaleDistribution": {
|
||||||
|
"type": "linear"
|
||||||
|
},
|
||||||
|
"showPoints": "never",
|
||||||
|
"spanNulls": true,
|
||||||
|
"stacking": {
|
||||||
|
"group": "A",
|
||||||
|
"mode": "none"
|
||||||
|
},
|
||||||
|
"thresholdsStyle": {
|
||||||
|
"mode": "off"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"mappings": [],
|
||||||
|
"min": 0,
|
||||||
|
"thresholds": {
|
||||||
|
"mode": "absolute",
|
||||||
|
"steps": [
|
||||||
|
{
|
||||||
|
"color": "green",
|
||||||
|
"value": null
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"unit": "short"
|
||||||
|
},
|
||||||
|
"overrides": []
|
||||||
|
},
|
||||||
|
"gridPos": {
|
||||||
|
"h": 8,
|
||||||
|
"w": 12,
|
||||||
|
"x": 12,
|
||||||
|
"y": 14
|
||||||
|
},
|
||||||
|
"id": 209,
|
||||||
|
"options": {
|
||||||
|
"legend": {
|
||||||
|
"calcs": [
|
||||||
|
"lastNotNull"
|
||||||
|
],
|
||||||
|
"displayMode": "table",
|
||||||
|
"placement": "right"
|
||||||
|
},
|
||||||
|
"tooltip": {
|
||||||
|
"mode": "single"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"pluginVersion": "8.2.1",
|
||||||
|
"targets": [
|
||||||
|
{
|
||||||
|
"exemplar": true,
|
||||||
|
"expr": "f2b_jail_banned_current",
|
||||||
|
"interval": "",
|
||||||
|
"legendFormat": "{{jail}}",
|
||||||
|
"refId": "A"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"timeFrom": null,
|
||||||
|
"timeShift": null,
|
||||||
|
"title": "Fail2Ban Bans (Current)",
|
||||||
|
"transparent": true,
|
||||||
|
"type": "timeseries"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"datasource": null,
|
||||||
|
"description": "",
|
||||||
|
"fieldConfig": {
|
||||||
|
"defaults": {
|
||||||
|
"color": {
|
||||||
|
"mode": "palette-classic"
|
||||||
|
},
|
||||||
|
"custom": {
|
||||||
|
"axisLabel": "",
|
||||||
|
"axisPlacement": "auto",
|
||||||
|
"barAlignment": 0,
|
||||||
|
"drawStyle": "line",
|
||||||
|
"fillOpacity": 10,
|
||||||
|
"gradientMode": "none",
|
||||||
|
"hideFrom": {
|
||||||
|
"legend": false,
|
||||||
|
"tooltip": false,
|
||||||
|
"viz": false
|
||||||
|
},
|
||||||
|
"lineInterpolation": "linear",
|
||||||
|
"lineWidth": 1,
|
||||||
|
"pointSize": 5,
|
||||||
|
"scaleDistribution": {
|
||||||
|
"type": "linear"
|
||||||
|
},
|
||||||
|
"showPoints": "never",
|
||||||
|
"spanNulls": true,
|
||||||
|
"stacking": {
|
||||||
|
"group": "A",
|
||||||
|
"mode": "none"
|
||||||
|
},
|
||||||
|
"thresholdsStyle": {
|
||||||
|
"mode": "off"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"mappings": [],
|
||||||
|
"max": 1,
|
||||||
|
"min": 0,
|
||||||
|
"thresholds": {
|
||||||
|
"mode": "absolute",
|
||||||
|
"steps": [
|
||||||
|
{
|
||||||
|
"color": "green",
|
||||||
|
"value": null
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"unit": "short"
|
||||||
|
},
|
||||||
|
"overrides": []
|
||||||
|
},
|
||||||
|
"gridPos": {
|
||||||
|
"h": 5,
|
||||||
|
"w": 12,
|
||||||
|
"x": 0,
|
||||||
|
"y": 22
|
||||||
|
},
|
||||||
|
"id": 203,
|
||||||
|
"options": {
|
||||||
|
"legend": {
|
||||||
|
"calcs": [
|
||||||
|
"lastNotNull"
|
||||||
|
],
|
||||||
|
"displayMode": "table",
|
||||||
|
"placement": "right"
|
||||||
|
},
|
||||||
|
"tooltip": {
|
||||||
|
"mode": "single"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"pluginVersion": "8.2.1",
|
||||||
|
"targets": [
|
||||||
|
{
|
||||||
|
"exemplar": true,
|
||||||
|
"expr": "f2b_up",
|
||||||
|
"interval": "",
|
||||||
|
"legendFormat": "Up",
|
||||||
|
"refId": "A"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"timeFrom": null,
|
||||||
|
"timeShift": null,
|
||||||
|
"title": "Fail2Ban Up",
|
||||||
|
"transparent": true,
|
||||||
|
"type": "timeseries"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"datasource": null,
|
||||||
|
"description": "",
|
||||||
|
"fieldConfig": {
|
||||||
|
"defaults": {
|
||||||
|
"color": {
|
||||||
|
"mode": "palette-classic"
|
||||||
|
},
|
||||||
|
"custom": {
|
||||||
|
"axisLabel": "",
|
||||||
|
"axisPlacement": "auto",
|
||||||
|
"barAlignment": 0,
|
||||||
|
"drawStyle": "line",
|
||||||
|
"fillOpacity": 10,
|
||||||
|
"gradientMode": "none",
|
||||||
|
"hideFrom": {
|
||||||
|
"legend": false,
|
||||||
|
"tooltip": false,
|
||||||
|
"viz": false
|
||||||
|
},
|
||||||
|
"lineInterpolation": "linear",
|
||||||
|
"lineWidth": 1,
|
||||||
|
"pointSize": 5,
|
||||||
|
"scaleDistribution": {
|
||||||
|
"type": "linear"
|
||||||
|
},
|
||||||
|
"showPoints": "never",
|
||||||
|
"spanNulls": true,
|
||||||
|
"stacking": {
|
||||||
|
"group": "A",
|
||||||
|
"mode": "none"
|
||||||
|
},
|
||||||
|
"thresholdsStyle": {
|
||||||
|
"mode": "off"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"mappings": [],
|
||||||
|
"max": 1,
|
||||||
|
"min": 0,
|
||||||
|
"thresholds": {
|
||||||
|
"mode": "absolute",
|
||||||
|
"steps": [
|
||||||
|
{
|
||||||
|
"color": "green",
|
||||||
|
"value": null
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"unit": "short"
|
||||||
|
},
|
||||||
|
"overrides": []
|
||||||
|
},
|
||||||
|
"gridPos": {
|
||||||
|
"h": 5,
|
||||||
|
"w": 12,
|
||||||
|
"x": 12,
|
||||||
|
"y": 22
|
||||||
|
},
|
||||||
|
"id": 204,
|
||||||
|
"options": {
|
||||||
|
"legend": {
|
||||||
|
"calcs": [
|
||||||
|
"lastNotNull"
|
||||||
|
],
|
||||||
|
"displayMode": "table",
|
||||||
|
"placement": "right"
|
||||||
|
},
|
||||||
|
"tooltip": {
|
||||||
|
"mode": "single"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"pluginVersion": "8.2.1",
|
||||||
|
"targets": [
|
||||||
|
{
|
||||||
|
"exemplar": true,
|
||||||
|
"expr": "f2b_errors",
|
||||||
|
"interval": "",
|
||||||
|
"legendFormat": "{{type}}",
|
||||||
|
"refId": "A"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"timeFrom": null,
|
||||||
|
"timeShift": null,
|
||||||
|
"title": "Fail2Ban Exporter Errors",
|
||||||
|
"transparent": true,
|
||||||
|
"type": "timeseries"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"refresh": "30s",
|
||||||
|
"schemaVersion": 31,
|
||||||
|
"style": "dark",
|
||||||
|
"tags": [],
|
||||||
|
"templating": {
|
||||||
|
"list": []
|
||||||
|
},
|
||||||
|
"time": {
|
||||||
|
"from": "now-6h",
|
||||||
|
"to": "now"
|
||||||
|
},
|
||||||
|
"timepicker": {},
|
||||||
|
"timezone": "",
|
||||||
|
"title": "F2B",
|
||||||
|
"uid": "cTkH9AT7z",
|
||||||
|
"version": 13
|
||||||
|
}
|
@ -12,18 +12,22 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type AppSettings struct {
|
type AppSettings struct {
|
||||||
VersionMode bool
|
VersionMode bool
|
||||||
MetricsPort int
|
MetricsAddress string
|
||||||
Fail2BanDbPath string
|
MetricsPort int
|
||||||
Fail2BanSocketPath string
|
Fail2BanSocketPath string
|
||||||
|
FileCollectorPath string
|
||||||
|
FileCollectorEnabled bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func Parse() *AppSettings {
|
func Parse() *AppSettings {
|
||||||
appSettings := &AppSettings{}
|
appSettings := &AppSettings{}
|
||||||
flag.BoolVar(&appSettings.VersionMode, "version", false, "show version info and exit")
|
flag.BoolVar(&appSettings.VersionMode, "version", false, "show version info and exit")
|
||||||
|
flag.StringVar(&appSettings.MetricsAddress, "web.listen-address", "0.0.0.0", "address to use for the metrics server")
|
||||||
flag.IntVar(&appSettings.MetricsPort, "port", 9191, "port to use for the metrics server")
|
flag.IntVar(&appSettings.MetricsPort, "port", 9191, "port to use for the metrics server")
|
||||||
flag.StringVar(&appSettings.Fail2BanDbPath, "db", "", "path to the fail2ban sqlite database (deprecated)")
|
|
||||||
flag.StringVar(&appSettings.Fail2BanSocketPath, "socket", "", "path to the fail2ban server socket")
|
flag.StringVar(&appSettings.Fail2BanSocketPath, "socket", "", "path to the fail2ban server socket")
|
||||||
|
flag.BoolVar(&appSettings.FileCollectorEnabled, "collector.textfile", false, "enable the textfile collector")
|
||||||
|
flag.StringVar(&appSettings.FileCollectorPath, "collector.textfile.directory", "", "directory to read text files with metrics from")
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
appSettings.validateFlags()
|
appSettings.validateFlags()
|
||||||
@ -33,8 +37,8 @@ func Parse() *AppSettings {
|
|||||||
func (settings *AppSettings) validateFlags() {
|
func (settings *AppSettings) validateFlags() {
|
||||||
var flagsValid = true
|
var flagsValid = true
|
||||||
if !settings.VersionMode {
|
if !settings.VersionMode {
|
||||||
if settings.Fail2BanDbPath == "" && settings.Fail2BanSocketPath == "" {
|
if settings.Fail2BanSocketPath == "" {
|
||||||
fmt.Println("at least one of the following flags must be provided: 'db', 'socket'")
|
fmt.Println("fail2ban socket path must not be blank")
|
||||||
flagsValid = false
|
flagsValid = false
|
||||||
}
|
}
|
||||||
if settings.MetricsPort < minServerPort || settings.MetricsPort > maxServerPort {
|
if settings.MetricsPort < minServerPort || settings.MetricsPort > maxServerPort {
|
||||||
@ -42,6 +46,10 @@ func (settings *AppSettings) validateFlags() {
|
|||||||
minServerPort, maxServerPort, settings.MetricsPort)
|
minServerPort, maxServerPort, settings.MetricsPort)
|
||||||
flagsValid = false
|
flagsValid = false
|
||||||
}
|
}
|
||||||
|
if settings.FileCollectorEnabled && settings.FileCollectorPath == "" {
|
||||||
|
fmt.Printf("file collector directory path must not be empty if collector enabled\n")
|
||||||
|
flagsValid = false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if !flagsValid {
|
if !flagsValid {
|
||||||
flag.Usage()
|
flag.Usage()
|
||||||
|
53
src/collector/f2b/collector.go
Normal file
53
src/collector/f2b/collector.go
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
package f2b
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fail2ban-prometheus-exporter/cfg"
|
||||||
|
"fail2ban-prometheus-exporter/socket"
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Collector struct {
|
||||||
|
socketPath string
|
||||||
|
exporterVersion string
|
||||||
|
lastError error
|
||||||
|
socketConnectionErrorCount int
|
||||||
|
socketRequestErrorCount int
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewExporter(appSettings *cfg.AppSettings, exporterVersion string) *Collector {
|
||||||
|
return &Collector{
|
||||||
|
socketPath: appSettings.Fail2BanSocketPath,
|
||||||
|
exporterVersion: exporterVersion,
|
||||||
|
lastError: nil,
|
||||||
|
socketConnectionErrorCount: 0,
|
||||||
|
socketRequestErrorCount: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Collector) Describe(ch chan<- *prometheus.Desc) {
|
||||||
|
ch <- metricServerUp
|
||||||
|
ch <- metricJailCount
|
||||||
|
ch <- metricJailFailedCurrent
|
||||||
|
ch <- metricJailFailedTotal
|
||||||
|
ch <- metricJailBannedCurrent
|
||||||
|
ch <- metricJailBannedTotal
|
||||||
|
ch <- metricErrorCount
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Collector) Collect(ch chan<- prometheus.Metric) {
|
||||||
|
s, err := socket.ConnectToSocket(c.socketPath)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("error opening socket: %v", err)
|
||||||
|
c.socketConnectionErrorCount++
|
||||||
|
} else {
|
||||||
|
defer s.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
c.collectServerUpMetric(ch, s)
|
||||||
|
if err == nil && s != nil {
|
||||||
|
c.collectJailMetrics(ch, s)
|
||||||
|
c.collectVersionMetric(ch, s)
|
||||||
|
}
|
||||||
|
c.collectErrorCountMetric(ch)
|
||||||
|
}
|
179
src/collector/f2b/socket.go
Normal file
179
src/collector/f2b/socket.go
Normal file
@ -0,0 +1,179 @@
|
|||||||
|
package f2b
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fail2ban-prometheus-exporter/socket"
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
namespace = "f2b"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
metricErrorCount = prometheus.NewDesc(
|
||||||
|
prometheus.BuildFQName(namespace, "", "errors"),
|
||||||
|
"Number of errors found since startup",
|
||||||
|
[]string{"type"}, nil,
|
||||||
|
)
|
||||||
|
metricServerUp = prometheus.NewDesc(
|
||||||
|
prometheus.BuildFQName(namespace, "", "up"),
|
||||||
|
"Check if the fail2ban server is up",
|
||||||
|
nil, nil,
|
||||||
|
)
|
||||||
|
metricJailCount = prometheus.NewDesc(
|
||||||
|
prometheus.BuildFQName(namespace, "", "jail_count"),
|
||||||
|
"Number of defined jails",
|
||||||
|
nil, nil,
|
||||||
|
)
|
||||||
|
metricJailFailedCurrent = prometheus.NewDesc(
|
||||||
|
prometheus.BuildFQName(namespace, "", "jail_failed_current"),
|
||||||
|
"Number of current failures on this jail's filter",
|
||||||
|
[]string{"jail"}, nil,
|
||||||
|
)
|
||||||
|
metricJailFailedTotal = prometheus.NewDesc(
|
||||||
|
prometheus.BuildFQName(namespace, "", "jail_failed_total"),
|
||||||
|
"Number of total failures on this jail's filter",
|
||||||
|
[]string{"jail"}, nil,
|
||||||
|
)
|
||||||
|
metricJailBannedCurrent = prometheus.NewDesc(
|
||||||
|
prometheus.BuildFQName(namespace, "", "jail_banned_current"),
|
||||||
|
"Number of IPs currently banned in this jail",
|
||||||
|
[]string{"jail"}, nil,
|
||||||
|
)
|
||||||
|
metricJailBannedTotal = prometheus.NewDesc(
|
||||||
|
prometheus.BuildFQName(namespace, "", "jail_banned_total"),
|
||||||
|
"Total number of IPs banned by this jail (includes expired bans)",
|
||||||
|
[]string{"jail"}, nil,
|
||||||
|
)
|
||||||
|
metricJailBanTime = prometheus.NewDesc(
|
||||||
|
prometheus.BuildFQName(namespace, "config", "jail_ban_time"),
|
||||||
|
"How long an IP is banned for in this jail (in seconds)",
|
||||||
|
[]string{"jail"}, nil,
|
||||||
|
)
|
||||||
|
metricJailFindTime = prometheus.NewDesc(
|
||||||
|
prometheus.BuildFQName(namespace, "config", "jail_find_time"),
|
||||||
|
"How far back will the filter look for failures in this jail (in seconds)",
|
||||||
|
[]string{"jail"}, nil,
|
||||||
|
)
|
||||||
|
metricJailMaxRetry = prometheus.NewDesc(
|
||||||
|
prometheus.BuildFQName(namespace, "config", "jail_max_retries"),
|
||||||
|
"The number of failures allowed until the IP is banned by this jail",
|
||||||
|
[]string{"jail"}, nil,
|
||||||
|
)
|
||||||
|
metricVersionInfo = prometheus.NewDesc(
|
||||||
|
prometheus.BuildFQName(namespace, "", "version"),
|
||||||
|
"Version of the exporter and fail2ban server",
|
||||||
|
[]string{"exporter", "fail2ban"}, nil,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func (c *Collector) collectErrorCountMetric(ch chan<- prometheus.Metric) {
|
||||||
|
ch <- prometheus.MustNewConstMetric(
|
||||||
|
metricErrorCount, prometheus.CounterValue, float64(c.socketConnectionErrorCount), "socket_conn",
|
||||||
|
)
|
||||||
|
ch <- prometheus.MustNewConstMetric(
|
||||||
|
metricErrorCount, prometheus.CounterValue, float64(c.socketRequestErrorCount), "socket_req",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Collector) collectServerUpMetric(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket) {
|
||||||
|
var serverUp float64 = 0
|
||||||
|
if s != nil {
|
||||||
|
pingSuccess, err := s.Ping()
|
||||||
|
if err != nil {
|
||||||
|
c.socketRequestErrorCount++
|
||||||
|
log.Print(err)
|
||||||
|
}
|
||||||
|
if err == nil && pingSuccess {
|
||||||
|
serverUp = 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ch <- prometheus.MustNewConstMetric(
|
||||||
|
metricServerUp, prometheus.GaugeValue, serverUp,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Collector) collectJailMetrics(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket) {
|
||||||
|
jails, err := s.GetJails()
|
||||||
|
var count float64 = 0
|
||||||
|
if err != nil {
|
||||||
|
c.socketRequestErrorCount++
|
||||||
|
log.Print(err)
|
||||||
|
}
|
||||||
|
if err == nil {
|
||||||
|
count = float64(len(jails))
|
||||||
|
}
|
||||||
|
ch <- prometheus.MustNewConstMetric(
|
||||||
|
metricJailCount, prometheus.GaugeValue, count,
|
||||||
|
)
|
||||||
|
|
||||||
|
for i := range jails {
|
||||||
|
c.collectJailStatsMetric(ch, s, jails[i])
|
||||||
|
c.collectJailConfigMetrics(ch, s, jails[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Collector) collectJailStatsMetric(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket, jail string) {
|
||||||
|
stats, err := s.GetJailStats(jail)
|
||||||
|
if err != nil {
|
||||||
|
c.socketRequestErrorCount++
|
||||||
|
log.Printf("failed to get stats for jail %s: %v", jail, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ch <- prometheus.MustNewConstMetric(
|
||||||
|
metricJailFailedCurrent, prometheus.GaugeValue, float64(stats.FailedCurrent), jail,
|
||||||
|
)
|
||||||
|
ch <- prometheus.MustNewConstMetric(
|
||||||
|
metricJailFailedTotal, prometheus.GaugeValue, float64(stats.FailedTotal), jail,
|
||||||
|
)
|
||||||
|
ch <- prometheus.MustNewConstMetric(
|
||||||
|
metricJailBannedCurrent, prometheus.GaugeValue, float64(stats.BannedCurrent), jail,
|
||||||
|
)
|
||||||
|
ch <- prometheus.MustNewConstMetric(
|
||||||
|
metricJailBannedTotal, prometheus.GaugeValue, float64(stats.BannedTotal), jail,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Collector) collectJailConfigMetrics(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket, jail string) {
|
||||||
|
banTime, err := s.GetJailBanTime(jail)
|
||||||
|
if err != nil {
|
||||||
|
c.socketRequestErrorCount++
|
||||||
|
log.Printf("failed to get ban time for jail %s: %v", jail, err)
|
||||||
|
} else {
|
||||||
|
ch <- prometheus.MustNewConstMetric(
|
||||||
|
metricJailBanTime, prometheus.GaugeValue, float64(banTime), jail,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
findTime, err := s.GetJailFindTime(jail)
|
||||||
|
if err != nil {
|
||||||
|
c.socketRequestErrorCount++
|
||||||
|
log.Printf("failed to get find time for jail %s: %v", jail, err)
|
||||||
|
} else {
|
||||||
|
ch <- prometheus.MustNewConstMetric(
|
||||||
|
metricJailFindTime, prometheus.GaugeValue, float64(findTime), jail,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
maxRetry, err := s.GetJailMaxRetries(jail)
|
||||||
|
if err != nil {
|
||||||
|
c.socketRequestErrorCount++
|
||||||
|
log.Printf("failed to get max retries for jail %s: %v", jail, err)
|
||||||
|
} else {
|
||||||
|
ch <- prometheus.MustNewConstMetric(
|
||||||
|
metricJailMaxRetry, prometheus.GaugeValue, float64(maxRetry), jail,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Collector) collectVersionMetric(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket) {
|
||||||
|
fail2banVersion, err := s.GetServerVersion()
|
||||||
|
if err != nil {
|
||||||
|
c.socketRequestErrorCount++
|
||||||
|
log.Printf("failed to get fail2ban server version: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ch <- prometheus.MustNewConstMetric(
|
||||||
|
metricVersionInfo, prometheus.GaugeValue, float64(1), c.exporterVersion, fail2banVersion,
|
||||||
|
)
|
||||||
|
}
|
48
src/collector/textfile/collector.go
Normal file
48
src/collector/textfile/collector.go
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
package textfile
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fail2ban-prometheus-exporter/cfg"
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Collector struct {
|
||||||
|
enabled bool
|
||||||
|
folderPath string
|
||||||
|
fileMap map[string]*fileData
|
||||||
|
}
|
||||||
|
|
||||||
|
type fileData struct {
|
||||||
|
readErrors int
|
||||||
|
fileName string
|
||||||
|
fileContents []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewCollector(appSettings *cfg.AppSettings) *Collector {
|
||||||
|
collector := &Collector{
|
||||||
|
enabled: appSettings.FileCollectorEnabled,
|
||||||
|
folderPath: appSettings.FileCollectorPath,
|
||||||
|
fileMap: make(map[string]*fileData),
|
||||||
|
}
|
||||||
|
if collector.enabled {
|
||||||
|
log.Printf("collector.textfile directory: %s", collector.folderPath)
|
||||||
|
}
|
||||||
|
return collector
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Collector) Describe(ch chan<- *prometheus.Desc) {
|
||||||
|
if c.enabled {
|
||||||
|
ch <- metricReadError
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Collector) Collect(ch chan<- prometheus.Metric) {
|
||||||
|
if c.enabled {
|
||||||
|
c.collectFileContents()
|
||||||
|
c.collectFileErrors(ch)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Collector) appendErrorForPath(path string) {
|
||||||
|
c.fileMap[path].readErrors++
|
||||||
|
}
|
55
src/collector/textfile/file.go
Normal file
55
src/collector/textfile/file.go
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
package textfile
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
const namespace = "textfile"
|
||||||
|
|
||||||
|
var (
|
||||||
|
metricReadError = prometheus.NewDesc(
|
||||||
|
prometheus.BuildFQName(namespace, "", "error"),
|
||||||
|
"Checks for errors while reading text files",
|
||||||
|
[]string{"path"}, nil,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func (c *Collector) collectFileContents() {
|
||||||
|
files, err := ioutil.ReadDir(c.folderPath)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("error reading directory '%s': %v", c.folderPath, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, file := range files {
|
||||||
|
fileName := file.Name()
|
||||||
|
if !strings.HasSuffix(strings.ToLower(fileName), ".prom") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
c.fileMap[fileName] = &fileData{
|
||||||
|
readErrors: 0,
|
||||||
|
fileName: fileName,
|
||||||
|
}
|
||||||
|
|
||||||
|
fullPath := filepath.Join(c.folderPath, fileName)
|
||||||
|
content, err := ioutil.ReadFile(fullPath)
|
||||||
|
if err != nil {
|
||||||
|
c.appendErrorForPath(fileName)
|
||||||
|
log.Printf("error reading contents of file '%s': %v", fileName, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
c.fileMap[fileName].fileContents = content
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Collector) collectFileErrors(ch chan<- prometheus.Metric) {
|
||||||
|
for _, f := range c.fileMap {
|
||||||
|
ch <- prometheus.MustNewConstMetric(
|
||||||
|
metricReadError, prometheus.GaugeValue, float64(f.readErrors), f.fileName,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
20
src/collector/textfile/writer.go
Normal file
20
src/collector/textfile/writer.go
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package textfile
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (c *Collector) WriteTextFileMetrics(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !c.enabled {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, f := range c.fileMap {
|
||||||
|
_, err := w.Write(f.fileContents)
|
||||||
|
if err != nil {
|
||||||
|
c.appendErrorForPath(f.fileName)
|
||||||
|
log.Printf("error writing file contents to response writer '%s': %v", f.fileName, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
84
src/db/db.go
84
src/db/db.go
@ -1,84 +0,0 @@
|
|||||||
package db
|
|
||||||
|
|
||||||
import (
|
|
||||||
"database/sql"
|
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
)
|
|
||||||
|
|
||||||
const queryBadIpsPerJail = "SELECT j.name, (SELECT COUNT(1) FROM bips b WHERE j.name = b.jail) FROM jails j"
|
|
||||||
const queryBannedIpsPerJail = "SELECT j.name, (SELECT COUNT(1) FROM bans b WHERE j.name = b.jail AND b.timeofban + b.bantime >= strftime('%s','now') + 0) FROM jails j"
|
|
||||||
const queryJailNameToEnabled = "SELECT j.name, j.enabled FROM jails j"
|
|
||||||
|
|
||||||
type Fail2BanDB struct {
|
|
||||||
DatabasePath string
|
|
||||||
sqliteDB *sql.DB
|
|
||||||
}
|
|
||||||
|
|
||||||
func MustConnectToDb(databasePath string) *Fail2BanDB {
|
|
||||||
if _, err := os.Stat(databasePath); os.IsNotExist(err) {
|
|
||||||
log.Fatalf("database path does not exist: %v", err)
|
|
||||||
}
|
|
||||||
db, err := sql.Open("sqlite3", databasePath)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
return &Fail2BanDB{
|
|
||||||
DatabasePath: databasePath,
|
|
||||||
sqliteDB: db,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *Fail2BanDB) CountBannedIpsPerJail() (map[string]int, error) {
|
|
||||||
return db.RunJailNameToCountQuery(queryBannedIpsPerJail)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *Fail2BanDB) CountBadIpsPerJail() (map[string]int, error) {
|
|
||||||
return db.RunJailNameToCountQuery(queryBadIpsPerJail)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *Fail2BanDB) JailNameToEnabledValue() (map[string]int, error) {
|
|
||||||
return db.RunJailNameToCountQuery(queryJailNameToEnabled)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *Fail2BanDB) RunJailNameToCountQuery(query string) (map[string]int, error) {
|
|
||||||
stmt, err := db.sqliteDB.Prepare(query)
|
|
||||||
defer db.mustCloseStatement(stmt)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
jailNameToCountMap := map[string]int{}
|
|
||||||
rows, err := stmt.Query()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if rows == nil {
|
|
||||||
return jailNameToCountMap, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
for rows.Next() {
|
|
||||||
if rows.Err() != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
jailName := ""
|
|
||||||
count := 0
|
|
||||||
err = rows.Scan(&jailName, &count)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
jailNameToCountMap[jailName] = count
|
|
||||||
}
|
|
||||||
return jailNameToCountMap, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *Fail2BanDB) mustCloseStatement(stmt *sql.Stmt) {
|
|
||||||
if stmt != nil {
|
|
||||||
err := stmt.Close()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
310
src/exporter.go
310
src/exporter.go
@ -2,20 +2,18 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fail2ban-prometheus-exporter/cfg"
|
"fail2ban-prometheus-exporter/cfg"
|
||||||
fail2banDb "fail2ban-prometheus-exporter/db"
|
"fail2ban-prometheus-exporter/collector/f2b"
|
||||||
"fail2ban-prometheus-exporter/socket"
|
"fail2ban-prometheus-exporter/collector/textfile"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
_ "github.com/mattn/go-sqlite3"
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
deprecatedNamespace = "fail2ban"
|
metricsPath = "/metrics"
|
||||||
namespace = "f2b"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -23,279 +21,61 @@ var (
|
|||||||
commit = "none"
|
commit = "none"
|
||||||
date = "unknown"
|
date = "unknown"
|
||||||
builtBy = "unknown"
|
builtBy = "unknown"
|
||||||
|
|
||||||
deprecatedMetricUp = prometheus.NewDesc(
|
|
||||||
prometheus.BuildFQName(deprecatedNamespace, "", "up"),
|
|
||||||
"(Deprecated) Was the last fail2ban query successful.",
|
|
||||||
nil, nil,
|
|
||||||
)
|
|
||||||
deprecatedMetricBannedIpsPerJail = prometheus.NewDesc(
|
|
||||||
prometheus.BuildFQName(deprecatedNamespace, "", "banned_ips"),
|
|
||||||
"(Deprecated) Number of banned IPs stored in the database (per jail).",
|
|
||||||
[]string{"jail"}, nil,
|
|
||||||
)
|
|
||||||
deprecatedMetricBadIpsPerJail = prometheus.NewDesc(
|
|
||||||
prometheus.BuildFQName(deprecatedNamespace, "", "bad_ips"),
|
|
||||||
"(Deprecated) Number of bad IPs stored in the database (per jail).",
|
|
||||||
[]string{"jail"}, nil,
|
|
||||||
)
|
|
||||||
deprecatedMetricEnabledJails = prometheus.NewDesc(
|
|
||||||
prometheus.BuildFQName(deprecatedNamespace, "", "enabled_jails"),
|
|
||||||
"(Deprecated) Enabled jails.",
|
|
||||||
[]string{"jail"}, nil,
|
|
||||||
)
|
|
||||||
deprecatedMetricErrorCount = prometheus.NewDesc(
|
|
||||||
prometheus.BuildFQName(deprecatedNamespace, "", "errors"),
|
|
||||||
"(Deprecated) Number of errors found since startup.",
|
|
||||||
[]string{"type"}, nil,
|
|
||||||
)
|
|
||||||
|
|
||||||
metricErrorCount = prometheus.NewDesc(
|
|
||||||
prometheus.BuildFQName(namespace, "", "errors"),
|
|
||||||
"Number of errors found since startup",
|
|
||||||
[]string{"type"}, nil,
|
|
||||||
)
|
|
||||||
metricServerUp = prometheus.NewDesc(
|
|
||||||
prometheus.BuildFQName(namespace, "", "up"),
|
|
||||||
"Check if the fail2ban server is up",
|
|
||||||
nil, nil,
|
|
||||||
)
|
|
||||||
metricJailCount = prometheus.NewDesc(
|
|
||||||
prometheus.BuildFQName(namespace, "", "jail_count"),
|
|
||||||
"Number of defined jails",
|
|
||||||
nil, nil,
|
|
||||||
)
|
|
||||||
metricJailFailedCurrent = prometheus.NewDesc(
|
|
||||||
prometheus.BuildFQName(namespace, "", "jail_failed_current"),
|
|
||||||
"Number of current failures on this jail's filter",
|
|
||||||
[]string{"jail"}, nil,
|
|
||||||
)
|
|
||||||
metricJailFailedTotal = prometheus.NewDesc(
|
|
||||||
prometheus.BuildFQName(namespace, "", "jail_failed_total"),
|
|
||||||
"Number of total failures on this jail's filter",
|
|
||||||
[]string{"jail"}, nil,
|
|
||||||
)
|
|
||||||
metricJailBannedCurrent = prometheus.NewDesc(
|
|
||||||
prometheus.BuildFQName(namespace, "", "jail_banned_current"),
|
|
||||||
"Number of IPs currently banned in this jail",
|
|
||||||
[]string{"jail"}, nil,
|
|
||||||
)
|
|
||||||
metricJailBannedTotal = prometheus.NewDesc(
|
|
||||||
prometheus.BuildFQName(namespace, "", "jail_banned_total"),
|
|
||||||
"Total number of IPs banned by this jail (includes expired bans)",
|
|
||||||
[]string{"jail"}, nil,
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Exporter struct {
|
|
||||||
db *fail2banDb.Fail2BanDB
|
|
||||||
socketPath string
|
|
||||||
lastError error
|
|
||||||
dbErrorCount int
|
|
||||||
socketConnectionErrorCount int
|
|
||||||
socketRequestErrorCount int
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
|
|
||||||
if e.db != nil {
|
|
||||||
ch <- deprecatedMetricUp
|
|
||||||
ch <- deprecatedMetricBadIpsPerJail
|
|
||||||
ch <- deprecatedMetricBannedIpsPerJail
|
|
||||||
ch <- deprecatedMetricEnabledJails
|
|
||||||
ch <- deprecatedMetricErrorCount
|
|
||||||
}
|
|
||||||
if e.socketPath != "" {
|
|
||||||
ch <- metricServerUp
|
|
||||||
ch <- metricJailCount
|
|
||||||
ch <- metricJailFailedCurrent
|
|
||||||
ch <- metricJailFailedTotal
|
|
||||||
ch <- metricJailBannedCurrent
|
|
||||||
ch <- metricJailBannedTotal
|
|
||||||
}
|
|
||||||
ch <- metricErrorCount
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
|
|
||||||
if e.db != nil {
|
|
||||||
e.collectDeprecatedBadIpsPerJailMetrics(ch)
|
|
||||||
e.collectDeprecatedBannedIpsPerJailMetrics(ch)
|
|
||||||
e.collectDeprecatedEnabledJailMetrics(ch)
|
|
||||||
e.collectDeprecatedUpMetric(ch)
|
|
||||||
e.collectDeprecatedErrorCountMetric(ch)
|
|
||||||
}
|
|
||||||
if e.socketPath != "" {
|
|
||||||
s, err := socket.ConnectToSocket(e.socketPath)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("error opening socket: %v", err)
|
|
||||||
e.socketConnectionErrorCount++
|
|
||||||
} else {
|
|
||||||
defer s.Close()
|
|
||||||
}
|
|
||||||
e.collectServerUpMetric(ch, s)
|
|
||||||
if err == nil && s != nil {
|
|
||||||
e.collectJailMetrics(ch, s)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
e.collectErrorCountMetric(ch)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *Exporter) collectDeprecatedUpMetric(ch chan<- prometheus.Metric) {
|
|
||||||
var upMetricValue float64 = 1
|
|
||||||
if e.lastError != nil {
|
|
||||||
upMetricValue = 0
|
|
||||||
}
|
|
||||||
ch <- prometheus.MustNewConstMetric(
|
|
||||||
deprecatedMetricUp, prometheus.GaugeValue, upMetricValue,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *Exporter) collectDeprecatedErrorCountMetric(ch chan<- prometheus.Metric) {
|
|
||||||
ch <- prometheus.MustNewConstMetric(
|
|
||||||
deprecatedMetricErrorCount, prometheus.CounterValue, float64(e.dbErrorCount), "db",
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *Exporter) collectDeprecatedBadIpsPerJailMetrics(ch chan<- prometheus.Metric) {
|
|
||||||
jailNameToCountMap, err := e.db.CountBadIpsPerJail()
|
|
||||||
e.lastError = err
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
e.dbErrorCount++
|
|
||||||
log.Print(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for jailName, count := range jailNameToCountMap {
|
|
||||||
ch <- prometheus.MustNewConstMetric(
|
|
||||||
deprecatedMetricBadIpsPerJail, prometheus.GaugeValue, float64(count), jailName,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *Exporter) collectDeprecatedBannedIpsPerJailMetrics(ch chan<- prometheus.Metric) {
|
|
||||||
jailNameToCountMap, err := e.db.CountBannedIpsPerJail()
|
|
||||||
e.lastError = err
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
e.dbErrorCount++
|
|
||||||
log.Print(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for jailName, count := range jailNameToCountMap {
|
|
||||||
ch <- prometheus.MustNewConstMetric(
|
|
||||||
deprecatedMetricBannedIpsPerJail, prometheus.GaugeValue, float64(count), jailName,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *Exporter) collectDeprecatedEnabledJailMetrics(ch chan<- prometheus.Metric) {
|
|
||||||
jailNameToEnabledMap, err := e.db.JailNameToEnabledValue()
|
|
||||||
e.lastError = err
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
e.dbErrorCount++
|
|
||||||
log.Print(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for jailName, count := range jailNameToEnabledMap {
|
|
||||||
ch <- prometheus.MustNewConstMetric(
|
|
||||||
deprecatedMetricEnabledJails, prometheus.GaugeValue, float64(count), jailName,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *Exporter) collectErrorCountMetric(ch chan<- prometheus.Metric) {
|
|
||||||
ch <- prometheus.MustNewConstMetric(
|
|
||||||
metricErrorCount, prometheus.CounterValue, float64(e.dbErrorCount), "db",
|
|
||||||
)
|
|
||||||
ch <- prometheus.MustNewConstMetric(
|
|
||||||
metricErrorCount, prometheus.CounterValue, float64(e.socketConnectionErrorCount), "socket_conn",
|
|
||||||
)
|
|
||||||
ch <- prometheus.MustNewConstMetric(
|
|
||||||
metricErrorCount, prometheus.CounterValue, float64(e.socketRequestErrorCount), "socket_req",
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *Exporter) collectServerUpMetric(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket) {
|
|
||||||
var serverUp float64 = 0
|
|
||||||
if s != nil {
|
|
||||||
pingSuccess, err := s.Ping()
|
|
||||||
if err != nil {
|
|
||||||
e.socketRequestErrorCount++
|
|
||||||
log.Print(err)
|
|
||||||
}
|
|
||||||
if err == nil && pingSuccess {
|
|
||||||
serverUp = 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ch <- prometheus.MustNewConstMetric(
|
|
||||||
metricServerUp, prometheus.GaugeValue, serverUp,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *Exporter) collectJailMetrics(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket) {
|
|
||||||
jails, err := s.GetJails()
|
|
||||||
var count float64 = 0
|
|
||||||
if err != nil {
|
|
||||||
e.socketRequestErrorCount++
|
|
||||||
log.Print(err)
|
|
||||||
}
|
|
||||||
if err == nil {
|
|
||||||
count = float64(len(jails))
|
|
||||||
}
|
|
||||||
ch <- prometheus.MustNewConstMetric(
|
|
||||||
metricJailCount, prometheus.GaugeValue, count,
|
|
||||||
)
|
|
||||||
|
|
||||||
for i := range jails {
|
|
||||||
e.collectJailStatsMetric(ch, s, jails[i])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *Exporter) collectJailStatsMetric(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket, jail string) {
|
|
||||||
stats, err := s.GetJailStats(jail)
|
|
||||||
if err != nil {
|
|
||||||
e.socketRequestErrorCount++
|
|
||||||
log.Printf("failed to get stats for jail %s: %v", jail, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
ch <- prometheus.MustNewConstMetric(
|
|
||||||
metricJailFailedCurrent, prometheus.GaugeValue, float64(stats.FailedCurrent), jail,
|
|
||||||
)
|
|
||||||
ch <- prometheus.MustNewConstMetric(
|
|
||||||
metricJailFailedTotal, prometheus.GaugeValue, float64(stats.FailedTotal), jail,
|
|
||||||
)
|
|
||||||
ch <- prometheus.MustNewConstMetric(
|
|
||||||
metricJailBannedCurrent, prometheus.GaugeValue, float64(stats.BannedCurrent), jail,
|
|
||||||
)
|
|
||||||
ch <- prometheus.MustNewConstMetric(
|
|
||||||
metricJailBannedTotal, prometheus.GaugeValue, float64(stats.BannedTotal), jail,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
func printAppVersion() {
|
func printAppVersion() {
|
||||||
fmt.Println(version)
|
fmt.Println(version)
|
||||||
fmt.Printf(" build date: %s\r\n commit hash: %s\r\n built by: %s\r\n", date, commit, builtBy)
|
fmt.Printf(" build date: %s\r\n commit hash: %s\r\n built by: %s\r\n", date, commit, builtBy)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func rootHtmlHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
_, err := w.Write([]byte(
|
||||||
|
`<html>
|
||||||
|
<head><title>Fail2Ban Exporter</title></head>
|
||||||
|
<body>
|
||||||
|
<h1>Fail2Ban Exporter</h1>
|
||||||
|
<p><a href="` + metricsPath + `">Metrics</a></p>
|
||||||
|
</body>
|
||||||
|
</html>`))
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("error handling root url: %v", err)
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func metricHandler(w http.ResponseWriter, r *http.Request, collector *textfile.Collector) {
|
||||||
|
promhttp.Handler().ServeHTTP(w, r)
|
||||||
|
collector.WriteTextFileMetrics(w, r)
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
appSettings := cfg.Parse()
|
appSettings := cfg.Parse()
|
||||||
if appSettings.VersionMode {
|
if appSettings.VersionMode {
|
||||||
printAppVersion()
|
printAppVersion()
|
||||||
} else {
|
} else {
|
||||||
log.Print("starting fail2ban exporter")
|
addr := fmt.Sprintf("%s:%d", appSettings.MetricsAddress, appSettings.MetricsPort)
|
||||||
|
|
||||||
exporter := &Exporter{}
|
log.Printf("starting fail2ban exporter at %s", addr)
|
||||||
if appSettings.Fail2BanDbPath != "" {
|
|
||||||
log.Print("database-based metrics have been deprecated and will be removed in a future release")
|
|
||||||
exporter.db = fail2banDb.MustConnectToDb(appSettings.Fail2BanDbPath)
|
|
||||||
}
|
|
||||||
if appSettings.Fail2BanSocketPath != "" {
|
|
||||||
exporter.socketPath = appSettings.Fail2BanSocketPath
|
|
||||||
}
|
|
||||||
prometheus.MustRegister(exporter)
|
|
||||||
|
|
||||||
http.Handle("/metrics", promhttp.Handler())
|
f2bCollector := f2b.NewExporter(appSettings, version)
|
||||||
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", appSettings.MetricsPort), nil))
|
prometheus.MustRegister(f2bCollector)
|
||||||
|
|
||||||
|
textFileCollector := textfile.NewCollector(appSettings)
|
||||||
|
prometheus.MustRegister(textFileCollector)
|
||||||
|
|
||||||
|
http.HandleFunc("/", rootHtmlHandler)
|
||||||
|
http.HandleFunc(metricsPath, func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
metricHandler(w, r, textFileCollector)
|
||||||
|
})
|
||||||
|
log.Printf("metrics available at '%s'", metricsPath)
|
||||||
|
|
||||||
|
svrErr := make(chan error)
|
||||||
|
go func() {
|
||||||
|
svrErr <- http.ListenAndServe(addr, nil)
|
||||||
|
}()
|
||||||
|
log.Print("ready")
|
||||||
|
|
||||||
|
err := <-svrErr
|
||||||
|
log.Print(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,6 @@ go 1.15
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/kisielk/og-rek v1.1.0
|
github.com/kisielk/og-rek v1.1.0
|
||||||
github.com/mattn/go-sqlite3 v1.14.6
|
|
||||||
github.com/nlpodyssey/gopickle v0.1.0
|
github.com/nlpodyssey/gopickle v0.1.0
|
||||||
github.com/prometheus/client_golang v1.9.0
|
github.com/prometheus/client_golang v1.9.0
|
||||||
)
|
)
|
||||||
|
@ -152,8 +152,6 @@ github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaO
|
|||||||
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
|
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
|
||||||
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
|
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
|
||||||
github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
|
github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
|
||||||
github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg=
|
|
||||||
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
|
|
||||||
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
|
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
|
||||||
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
|
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
|
||||||
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
|
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
|
||||||
|
@ -118,6 +118,51 @@ func (s *Fail2BanSocket) GetJailStats(jail string) (JailStats, error) {
|
|||||||
return stats, newBadFormatError(statusCommand, response)
|
return stats, newBadFormatError(statusCommand, response)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Fail2BanSocket) GetJailBanTime(jail string) (int, error) {
|
||||||
|
command := fmt.Sprintf(banTimeCommandFmt, jail)
|
||||||
|
return s.sendSimpleIntCommand(command)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Fail2BanSocket) GetJailFindTime(jail string) (int, error) {
|
||||||
|
command := fmt.Sprintf(findTimeCommandFmt, jail)
|
||||||
|
return s.sendSimpleIntCommand(command)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Fail2BanSocket) GetJailMaxRetries(jail string) (int, error) {
|
||||||
|
command := fmt.Sprintf(maxRetriesCommandFmt, jail)
|
||||||
|
return s.sendSimpleIntCommand(command)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Fail2BanSocket) GetServerVersion() (string, error) {
|
||||||
|
response, err := s.sendCommand([]string{versionCommand})
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
if lvl1, ok := response.(*types.Tuple); ok {
|
||||||
|
if versionStr, ok := lvl1.Get(1).(string); ok {
|
||||||
|
return versionStr, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "", newBadFormatError(versionCommand, response)
|
||||||
|
}
|
||||||
|
|
||||||
|
// sendSimpleIntCommand sends a command to the fail2ban socket and parses the response to extract an int.
|
||||||
|
// This command assumes that the response data is in the format of `(d, d)` where `d` is a number.
|
||||||
|
func (s *Fail2BanSocket) sendSimpleIntCommand(command string) (int, error) {
|
||||||
|
response, err := s.sendCommand(strings.Split(command, " "))
|
||||||
|
if err != nil {
|
||||||
|
return -1, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if lvl1, ok := response.(*types.Tuple); ok {
|
||||||
|
if banTime, ok := lvl1.Get(1).(int); ok {
|
||||||
|
return banTime, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1, newBadFormatError(command, response)
|
||||||
|
}
|
||||||
|
|
||||||
func newBadFormatError(command string, data interface{}) error {
|
func newBadFormatError(command string, data interface{}) error {
|
||||||
return fmt.Errorf("(%s) unexpected response format - cannot parse: %v", command, data)
|
return fmt.Errorf("(%s) unexpected response format - cannot parse: %v", command, data)
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,10 @@ const (
|
|||||||
commandTerminator = "<F2B_END_COMMAND>"
|
commandTerminator = "<F2B_END_COMMAND>"
|
||||||
pingCommand = "ping"
|
pingCommand = "ping"
|
||||||
statusCommand = "status"
|
statusCommand = "status"
|
||||||
|
versionCommand = "version"
|
||||||
|
banTimeCommandFmt = "get %s bantime"
|
||||||
|
findTimeCommandFmt = "get %s findtime"
|
||||||
|
maxRetriesCommandFmt = "get %s maxretry"
|
||||||
socketReadBufferSize = 1024
|
socketReadBufferSize = 1024
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -55,7 +59,7 @@ func (s *Fail2BanSocket) read() (interface{}, error) {
|
|||||||
unpickler := pickle.NewUnpickler(bufReader)
|
unpickler := pickle.NewUnpickler(bufReader)
|
||||||
|
|
||||||
unpickler.FindClass = func(module, name string) (interface{}, error) {
|
unpickler.FindClass = func(module, name string) (interface{}, error) {
|
||||||
if module == "builtins" && name == "str" {
|
if (module == "builtins" || module == "__builtin__") && name == "str" {
|
||||||
return &Py_builtins_str{}, nil
|
return &Py_builtins_str{}, nil
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("class not found: " + module + " : " + name)
|
return nil, fmt.Errorf("class not found: " + module + " : " + name)
|
||||||
|
Reference in New Issue
Block a user