You've already forked prometheus-fail2ban-exporter
Compare commits
69 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 | |||
e4aa5edaa0 | |||
e4cf21fdf1 | |||
062abe561c | |||
920cf08619 | |||
742019a025 | |||
92fcae5cda | |||
5b62670e9d | |||
737a86b6fd | |||
4da46f3c4a | |||
828b67cdd9 | |||
acb40a94bd | |||
aef73df3fa | |||
2ab1f7dc52 | |||
82a7bbe1e0 | |||
1964dde273 | |||
617d711ecf | |||
e5714b7485 | |||
e083b48461 | |||
39133d0a76 | |||
9d6b35c59a | |||
526b1c7272 | |||
a5e1ae4495 | |||
8726afcd6b | |||
a406e019e2 | |||
bd841c3a35 | |||
a9e41188f6 | |||
1282d635eb | |||
5f9085aa5a |
@ -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
|
||||||
|
65
CHANGELOG.md
65
CHANGELOG.md
@ -4,7 +4,61 @@ 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]## [0.1.0] - 2021-03-28
|
## [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
|
||||||
|
*Collect metrics through fail2ban socket - based on [#11](https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/issues/11)*
|
||||||
|
|
||||||
|
### Added
|
||||||
|
- (39133d0) feat: collect new up metric from fail2ban socket
|
||||||
|
- (4da46f3) feat: export metrics with socket errors
|
||||||
|
- (bd841c3) feat: set up metric to 0 if errors found
|
||||||
|
- (1964dde) feat: export metrics for failed/banned counts
|
||||||
|
- (2ab1f7d) feat: support reading fail2ban socket in docker
|
||||||
|
- (1282d63) feat: new metric for enabled jails ([#1](https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/issues/1))
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
- (526b1c7) fix: update banned metrics to exclude expired bans ([#11](https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/issues/11))
|
||||||
|
|
||||||
|
### Deprecated
|
||||||
|
- Use of the fail2ban database has been deprecated. The exporter now collects metrics through the fail2ban socket file. See [#11](https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/-/issues/11) for more details.
|
||||||
|
|
||||||
|
## [0.1.0] - 2021-03-28
|
||||||
|
*Initial release*
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
- (6355c9e) feat: fail on startup if database file does not exist ([#8](https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/issues/8))
|
- (6355c9e) feat: fail on startup if database file does not exist ([#8](https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/issues/8))
|
||||||
@ -18,13 +72,16 @@ The format is based on [Keep a Changelog], and this project adheres to [Semantic
|
|||||||
### Fixed
|
### Fixed
|
||||||
- (0842419) fix: compile tool without cgo_enabled flag
|
- (0842419) fix: compile tool without cgo_enabled flag
|
||||||
|
|
||||||
|
## 0.0.0 - 2021-02-05
|
||||||
|
*Repository creation*
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
*This changelog is automatically generated by [git-chglog]*
|
|
||||||
|
|
||||||
[Keep a Changelog]: https://keepachangelog.com/en/1.0.0/
|
[Keep a Changelog]: https://keepachangelog.com/en/1.0.0/
|
||||||
[Semantic Versioning]: https://semver.org/spec/v2.0.0.html
|
[Semantic Versioning]: https://semver.org/spec/v2.0.0.html
|
||||||
[git-chglog]: https://github.com/git-chglog/git-chglog
|
|
||||||
[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.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
|
||||||
|
28
Makefile
28
Makefile
@ -1,21 +1,23 @@
|
|||||||
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:
|
||||||
./tools/git-chglog_linux_amd64 --config tools/chglog/config.yml 0.0.0.. > CHANGELOG.md
|
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
|
||||||
|
|
||||||
build/snapshot:
|
build/snapshot:
|
||||||
./tools/goreleaser_linux_amd64 --snapshot --rm-dist --skip-publish
|
./tools/goreleaser_linux_amd64 --snapshot --rm-dist --skip-publish
|
||||||
@ -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) .
|
||||||
|
234
README.md
234
README.md
@ -3,36 +3,72 @@
|
|||||||
Go tool to collect and export metrics on Fail2Ban
|
Go tool to collect and export metrics on Fail2Ban
|
||||||
|
|
||||||
## Table of Contents
|
## Table of Contents
|
||||||
1. How to use
|
1. Introduction
|
||||||
2. Docker
|
2. Running the Exporter
|
||||||
1. Volumes
|
3. Running in Docker
|
||||||
2. Docker run
|
|
||||||
3. Docker compose
|
|
||||||
3. CLI usage
|
|
||||||
4. Metrics
|
4. Metrics
|
||||||
|
|
||||||
## 1. How to use
|
## 1. Introduction
|
||||||
|
This exporter collects metrics from a running fail2ban instance.
|
||||||
|
|
||||||
Run the exporter by providing it with a fail2ban database to read data from.
|
|
||||||
Read access to the database is required.
|
|
||||||
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 this can be modified with the `-port` flag.
|
(The default port is `9191` but can be modified with the `-port` flag)
|
||||||
|
|
||||||
**Note:** By default fail2ban stores the database file at: `/var/lib/fail2ban/fail2ban.sqlite3`
|
The exporter communicates with the fail2ban server over its socket.
|
||||||
|
This allows the data collected by the exporter to always align with the output of the `fail2ban-client`.
|
||||||
|
|
||||||
**Fail2Ban Jails**
|
The default location of the socket is: `/var/run/fail2ban/fail2ban.sock`
|
||||||
|
|
||||||
fail2ban can be configured to process different log files and use different rules for each one.
|
## 1.1. Grafana
|
||||||
These separate configurations are referred to as *jails*.
|
|
||||||
|
|
||||||
For example, fail2ban can be configured to watch the system logs for failed SSH connections and Nextcloud logs for failed logins.
|
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.
|
||||||
In this configuration, there will be two jails - one for IPs banned from the SSH logs, and one for IPs banned from the Nextcloud logs.
|
|
||||||
|
|
||||||
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.
|
*(Sample dashboard is compatible with Grafana `8.2.1` and above)*
|
||||||
This can be useful to track what services are seeing more failed logins.
|
|
||||||
|
|
||||||
## 2. Docker
|
## 2. Running the Exporter
|
||||||
|
|
||||||
|
The exporter is compiled and released as a single binary.
|
||||||
|
This makes it very easy to run in any environment.
|
||||||
|
No additional runtime dependencies are required.
|
||||||
|
|
||||||
|
Compiled binaries for various platforms are provided in each release.
|
||||||
|
See the [releases page](https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/-/releases) for more information.
|
||||||
|
|
||||||
|
**Usage**
|
||||||
|
```
|
||||||
|
$ fail2ban-prometheus-exporter -h
|
||||||
|
|
||||||
|
-web.listen-address string
|
||||||
|
address to use for metrics server (default 0.0.0.0)
|
||||||
|
-port int
|
||||||
|
port to use for the metrics server (default 9191)
|
||||||
|
-socket string
|
||||||
|
path to the fail2ban server socket
|
||||||
|
-version
|
||||||
|
show version info and exit
|
||||||
|
-collector.textfile
|
||||||
|
enable the textfile collector
|
||||||
|
-collector.textfile.directory string
|
||||||
|
directory to read text files with metrics from
|
||||||
|
```
|
||||||
|
|
||||||
|
**Example**
|
||||||
|
|
||||||
|
```
|
||||||
|
fail2ban-prometheus-exporter -socket /var/run/fail2ban/fail2ban.sock -port 9191
|
||||||
|
```
|
||||||
|
|
||||||
|
Note that the exporter will need read access to the fail2ban socket.
|
||||||
|
|
||||||
|
### 2.1. Compile from Source
|
||||||
|
|
||||||
|
The code can be compiled from source by running `go build` inside the `src/` folder.
|
||||||
|
Go version `1.15` or greater is required.
|
||||||
|
|
||||||
|
Run `go mod download` to download all necessary dependencies before running the build.
|
||||||
|
|
||||||
|
## 3. Running in Docker
|
||||||
|
|
||||||
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:
|
||||||
@ -44,26 +80,30 @@ registry.gitlab.com/hectorjsmith/fail2ban-prometheus-exporter:latest
|
|||||||
Use the `:latest` tag to get the most up to date code (less stable) or use one of the version tagged images to use a specific release.
|
Use the `:latest` tag to get the most up to date code (less stable) or use one of the version tagged images to use a specific release.
|
||||||
See the [registry page](https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/container_registry) for all available tags.
|
See the [registry page](https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/container_registry) for all available tags.
|
||||||
|
|
||||||
### 2.1. Volumes
|
### 3.1. Volumes
|
||||||
|
|
||||||
The docker image is designed to run by mounting the fail2ban sqlite3 database.
|
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 database can be mounted with read-only permissions.
|
The folder can be mounted with read-only (`ro`) permissions.
|
||||||
|
|
||||||
### 2.2. Docker run
|
**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.
|
||||||
|
See [this reply](https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/-/issues/11#note_665003499) for more details.
|
||||||
|
|
||||||
Use the following command to run the forwarder as a docker container.
|
### 3.2. Docker run
|
||||||
|
|
||||||
|
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 \
|
||||||
-p "9191:9191"
|
-p "9191:9191" \
|
||||||
registry.gitlab.com/hectorjsmith/fail2ban-prometheus-exporter:latest
|
registry.gitlab.com/hectorjsmith/fail2ban-prometheus-exporter:latest
|
||||||
```
|
```
|
||||||
|
|
||||||
### 2.3. Docker compose
|
### 3.3. Docker compose
|
||||||
|
|
||||||
The following is a simple docker-compose file to run the exporter.
|
The following is a simple docker-compose file to run the exporter.
|
||||||
|
|
||||||
@ -73,51 +113,121 @@ 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
|
||||||
ports:
|
ports:
|
||||||
- "9191:9191"
|
- "9191:9191"
|
||||||
```
|
```
|
||||||
|
|
||||||
## 3. CLI usage
|
|
||||||
|
|
||||||
```
|
|
||||||
$ fail2ban-prometheus-exporter -h
|
|
||||||
|
|
||||||
-db string
|
|
||||||
path to the fail2ban sqlite database
|
|
||||||
-port int
|
|
||||||
port to use for the metrics server (default 9191)
|
|
||||||
-version
|
|
||||||
show version info and exit
|
|
||||||
```
|
|
||||||
|
|
||||||
## 4. Metrics
|
## 4. Metrics
|
||||||
|
|
||||||
Access exported metrics at `/metrics` (on the provided port).
|
Access exported metrics at the `/metrics` path on the configured port.
|
||||||
|
|
||||||
**Note:** All metric names include the `fail2ban_` prefix to make sure they are unique and easier to find.
|
**Note on Fail2Ban Jails**
|
||||||
|
|
||||||
|
fail2ban can be configured to process different log files and use different rules for each one.
|
||||||
|
These separate configurations are referred to as *jails*.
|
||||||
|
|
||||||
|
For example, fail2ban can be configured to watch the system logs for failed SSH connections and Nextcloud logs for failed logins.
|
||||||
|
In this configuration, there will be two jails - one for IPs banned from the SSH logs, and one for IPs banned from the Nextcloud logs.
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
### 4.1. Fail2Ban Metrics
|
||||||
|
|
||||||
|
These are the metrics exported by reading data from the fail2ban server socket.
|
||||||
|
All metrics are prefixed with `f2b_`.
|
||||||
|
|
||||||
Exposed metrics:
|
Exposed metrics:
|
||||||
* `up` - Returns 1 if the service is up
|
* `up` - Returns 1 if the fail2ban server is up and connection succeeds
|
||||||
* `bad_ips` (per jail)
|
* `errors` - Number of errors since startup
|
||||||
* A *bad IP* is defined as an IP that has been banned at least once in the past
|
* `socket_conn` - Errors connecting to the fail2ban socket (e.g. connection refused)
|
||||||
* Bad IPs are counted per jail
|
* `socket_req` - Errors sending requests to the fail2ban server (e.g. invalid responses)
|
||||||
* `banned_ips` (per jail)
|
* `jail_count` - Number of jails configured in fail2ban
|
||||||
* A *banned IP* is defined as an IP that is currently banned on the firewall
|
* `jail_banned_current` (per jail) - Number of IPs currently banned
|
||||||
* Banned IPs are counted per jail
|
* `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_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 fail2ban_bad_ips Number of bad IPs stored in the database (per jail).
|
# HELP f2b_errors Number of errors found since startup
|
||||||
# TYPE fail2ban_bad_ips gauge
|
# TYPE f2b_errors counter
|
||||||
fail2ban_bad_ips{jail="jail1"} 6
|
f2b_errors{type="socket_conn"} 0
|
||||||
fail2ban_bad_ips{jail="jail2"} 8
|
f2b_errors{type="socket_req"} 0
|
||||||
# HELP fail2ban_banned_ips Number of banned IPs stored in the database (per jail).
|
# HELP f2b_jail_banned_current Number of IPs currently banned in this jail
|
||||||
# TYPE fail2ban_banned_ips gauge
|
# TYPE f2b_jail_banned_current gauge
|
||||||
fail2ban_banned_ips{jail="jail1"} 3
|
f2b_jail_banned_current{jail="recidive"} 5
|
||||||
fail2ban_banned_ips{jail="jail2"} 2
|
f2b_jail_banned_current{jail="sshd"} 15
|
||||||
# HELP fail2ban_up Was the last fail2ban query successful.
|
# HELP f2b_jail_banned_total Total number of IPs banned by this jail (includes expired bans)
|
||||||
# TYPE fail2ban_up gauge
|
# TYPE f2b_jail_banned_total gauge
|
||||||
fail2ban_up 1
|
f2b_jail_banned_total{jail="recidive"} 6
|
||||||
|
f2b_jail_banned_total{jail="sshd"} 31
|
||||||
|
# HELP f2b_jail_count Number of defined jails
|
||||||
|
# TYPE f2b_jail_count gauge
|
||||||
|
f2b_jail_count 2
|
||||||
|
# HELP f2b_jail_failed_current Number of current failures on this jail's filter
|
||||||
|
# TYPE f2b_jail_failed_current gauge
|
||||||
|
f2b_jail_failed_current{jail="recidive"} 5
|
||||||
|
f2b_jail_failed_current{jail="sshd"} 6
|
||||||
|
# HELP f2b_jail_failed_total Number of total failures on this jail's filter
|
||||||
|
# TYPE f2b_jail_failed_total gauge
|
||||||
|
f2b_jail_failed_total{jail="recidive"} 7
|
||||||
|
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
|
||||||
|
# TYPE f2b_up gauge
|
||||||
|
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:
|
||||||
|
```
|
||||||
|
Status for the jail: sshd|- Filter
|
||||||
|
| |- Currently failed: 6
|
||||||
|
| |- Total failed: 125
|
||||||
|
| `- File list: /var/log/auth.log
|
||||||
|
`- Actions
|
||||||
|
|- Currently banned: 15
|
||||||
|
|- Total banned: 31
|
||||||
|
`- Banned IP list: ...
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4.2. Textfile Metrics
|
||||||
|
|
||||||
|
For more flexibility the exporter also allows exporting metrics collected from a text file.
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
By exporting textfile metrics an extra metric is also exported with an error count for each file:
|
||||||
|
|
||||||
|
```
|
||||||
|
# HELP textfile_error Checks for errors while reading text files
|
||||||
|
# TYPE textfile_error gauge
|
||||||
|
textfile_error{path="file.prom"} 0
|
||||||
|
```
|
||||||
|
|
||||||
|
**NOTE:** Any file not ending with `.prom` will be ignored.
|
||||||
|
@ -3,7 +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
|
||||||
|
|
||||||
|
socket_path=/var/run/fail2ban/fail2ban.sock
|
||||||
|
textfile_dir=/app/textfile/
|
||||||
|
textfile_enabled=false
|
||||||
|
|
||||||
|
# Enable textfile metrics if the folder exists (i.e. was mounted by docker)
|
||||||
|
if [ -d $textfile_dir ]; then
|
||||||
|
textfile_enabled=true
|
||||||
|
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 /app/fail2ban.sqlite3
|
-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,16 +12,22 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type AppSettings struct {
|
type AppSettings struct {
|
||||||
VersionMode bool
|
VersionMode bool
|
||||||
MetricsPort int
|
MetricsAddress string
|
||||||
Fail2BanDbPath string
|
MetricsPort int
|
||||||
|
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")
|
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()
|
||||||
@ -31,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 == "" {
|
if settings.Fail2BanSocketPath == "" {
|
||||||
fmt.Println("missing flag 'db'")
|
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 {
|
||||||
@ -40,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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
79
src/db/db.go
79
src/db/db.go
@ -1,79 +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) 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) 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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
122
src/exporter.go
122
src/exporter.go
@ -2,102 +2,80 @@ 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/collector/textfile"
|
||||||
"fmt"
|
"fmt"
|
||||||
_ "github.com/mattn/go-sqlite3"
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
|
||||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||||
)
|
)
|
||||||
|
|
||||||
const namespace = "fail2ban"
|
const (
|
||||||
|
metricsPath = "/metrics"
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
version = "dev"
|
version = "dev"
|
||||||
commit = "none"
|
commit = "none"
|
||||||
date = "unknown"
|
date = "unknown"
|
||||||
builtBy = "unknown"
|
builtBy = "unknown"
|
||||||
|
|
||||||
metricUp = prometheus.NewDesc(
|
|
||||||
prometheus.BuildFQName(namespace, "", "up"),
|
|
||||||
"Was the last fail2ban query successful.",
|
|
||||||
nil, nil,
|
|
||||||
)
|
|
||||||
metricBannedIpsPerJail = prometheus.NewDesc(
|
|
||||||
prometheus.BuildFQName(namespace, "", "banned_ips"),
|
|
||||||
"Number of banned IPs stored in the database (per jail).",
|
|
||||||
[]string{"jail"}, nil,
|
|
||||||
)
|
|
||||||
metricBadIpsPerJail = prometheus.NewDesc(
|
|
||||||
prometheus.BuildFQName(namespace, "", "bad_ips"),
|
|
||||||
"Number of bad IPs stored in the database (per jail).",
|
|
||||||
[]string{"jail"}, nil,
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Exporter struct {
|
|
||||||
db *fail2banDb.Fail2BanDB
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
|
|
||||||
ch <- metricUp
|
|
||||||
ch <- metricBadIpsPerJail
|
|
||||||
ch <- metricBannedIpsPerJail
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
|
|
||||||
ch <- prometheus.MustNewConstMetric(
|
|
||||||
metricUp, prometheus.GaugeValue, 1,
|
|
||||||
)
|
|
||||||
e.collectBadIpsPerJailMetrics(ch)
|
|
||||||
e.collectBannedIpsPerJailMetrics(ch)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *Exporter) collectBadIpsPerJailMetrics(ch chan<- prometheus.Metric) {
|
|
||||||
jailNameToCountMap, err := e.db.CountBadIpsPerJail()
|
|
||||||
if err != nil {
|
|
||||||
log.Print(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for jailName, count := range jailNameToCountMap {
|
|
||||||
ch <- prometheus.MustNewConstMetric(
|
|
||||||
metricBadIpsPerJail, prometheus.GaugeValue, float64(count), jailName,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *Exporter) collectBannedIpsPerJailMetrics(ch chan<- prometheus.Metric) {
|
|
||||||
jailNameToCountMap, err := e.db.CountBannedIpsPerJail()
|
|
||||||
if err != nil {
|
|
||||||
log.Print(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for jailName, count := range jailNameToCountMap {
|
|
||||||
ch <- prometheus.MustNewConstMetric(
|
|
||||||
metricBannedIpsPerJail, prometheus.GaugeValue, float64(count), jailName,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
||||||
db: fail2banDb.MustConnectToDb(appSettings.Fail2BanDbPath),
|
|
||||||
}
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ module fail2ban-prometheus-exporter
|
|||||||
go 1.15
|
go 1.15
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/mattn/go-sqlite3 v1.14.6
|
github.com/kisielk/og-rek v1.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
|
||||||
)
|
)
|
||||||
|
@ -137,6 +137,8 @@ github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7V
|
|||||||
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
|
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
|
||||||
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
|
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
|
||||||
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
|
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
|
||||||
|
github.com/kisielk/og-rek v1.1.0 h1:u10TvQbPtrlY/6H4+BiFsBywwSVTGFsx0YOVtpx3IbI=
|
||||||
|
github.com/kisielk/og-rek v1.1.0/go.mod h1:6ihsOSzSAxR/65S3Bn9zNihoEqRquhDQZ2c6I2+MG3c=
|
||||||
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||||
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||||
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
|
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
|
||||||
@ -150,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=
|
||||||
@ -175,6 +175,8 @@ github.com/nats-io/nats.go v1.9.1/go.mod h1:ZjDU1L/7fJ09jvUSRVBR2e7+RnLiiIQyqyzE
|
|||||||
github.com/nats-io/nkeys v0.1.0/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxziKVo7w=
|
github.com/nats-io/nkeys v0.1.0/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxziKVo7w=
|
||||||
github.com/nats-io/nkeys v0.1.3/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxziKVo7w=
|
github.com/nats-io/nkeys v0.1.3/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxziKVo7w=
|
||||||
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
|
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
|
||||||
|
github.com/nlpodyssey/gopickle v0.1.0 h1:9wjwRqXsOSYWZl4c4ko472b6RW+VB1I441ZcfFg1r5g=
|
||||||
|
github.com/nlpodyssey/gopickle v0.1.0/go.mod h1:YIUwjJ2O7+vnBsxUN+MHAAI3N+adqEGiw+nDpwW95bY=
|
||||||
github.com/oklog/oklog v0.3.2/go.mod h1:FCV+B7mhrz4o+ueLpx+KqkyXRGMWOYEvfiXtdGtbWGs=
|
github.com/oklog/oklog v0.3.2/go.mod h1:FCV+B7mhrz4o+ueLpx+KqkyXRGMWOYEvfiXtdGtbWGs=
|
||||||
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
|
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
|
||||||
github.com/olekukonko/tablewriter v0.0.0-20170122224234-a0225b3f23b5/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
|
github.com/olekukonko/tablewriter v0.0.0-20170122224234-a0225b3f23b5/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
|
||||||
|
8
src/socket/decoder.go
Normal file
8
src/socket/decoder.go
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package socket
|
||||||
|
|
||||||
|
// Py_builtins_str is used by the pickle decoder to parse the server response into a format Go can understand
|
||||||
|
type Py_builtins_str struct{}
|
||||||
|
|
||||||
|
func (c Py_builtins_str) Call(args ...interface{}) (interface{}, error) {
|
||||||
|
return args[0], nil
|
||||||
|
}
|
179
src/socket/fail2banSocket.go
Normal file
179
src/socket/fail2banSocket.go
Normal file
@ -0,0 +1,179 @@
|
|||||||
|
package socket
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/kisielk/og-rek"
|
||||||
|
"github.com/nlpodyssey/gopickle/types"
|
||||||
|
"net"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Fail2BanSocket struct {
|
||||||
|
socket net.Conn
|
||||||
|
encoder *ogĂłrek.Encoder
|
||||||
|
}
|
||||||
|
|
||||||
|
type JailStats struct {
|
||||||
|
FailedCurrent int
|
||||||
|
FailedTotal int
|
||||||
|
BannedCurrent int
|
||||||
|
BannedTotal int
|
||||||
|
}
|
||||||
|
|
||||||
|
func ConnectToSocket(path string) (*Fail2BanSocket, error) {
|
||||||
|
c, err := net.Dial("unix", path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &Fail2BanSocket{
|
||||||
|
socket: c,
|
||||||
|
encoder: ogĂłrek.NewEncoder(c),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Fail2BanSocket) Close() error {
|
||||||
|
return s.socket.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Fail2BanSocket) Ping() (bool, error) {
|
||||||
|
response, err := s.sendCommand([]string{pingCommand, "100"})
|
||||||
|
if err != nil {
|
||||||
|
return false, newConnectionError(pingCommand, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if t, ok := response.(*types.Tuple); ok {
|
||||||
|
if (*t)[1] == "pong" {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
return false, fmt.Errorf("unexpected response data (expecting 'pong'): %s", (*t)[1])
|
||||||
|
}
|
||||||
|
return false, newBadFormatError(pingCommand, response)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Fail2BanSocket) GetJails() ([]string, error) {
|
||||||
|
response, err := s.sendCommand([]string{statusCommand})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if lvl1, ok := response.(*types.Tuple); ok {
|
||||||
|
if lvl2, ok := lvl1.Get(1).(*types.List); ok {
|
||||||
|
if lvl3, ok := lvl2.Get(1).(*types.Tuple); ok {
|
||||||
|
if lvl4, ok := lvl3.Get(1).(string); ok {
|
||||||
|
splitJails := strings.Split(lvl4, ",")
|
||||||
|
return trimSpaceForAll(splitJails), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, newBadFormatError(statusCommand, response)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Fail2BanSocket) GetJailStats(jail string) (JailStats, error) {
|
||||||
|
response, err := s.sendCommand([]string{statusCommand, jail})
|
||||||
|
if err != nil {
|
||||||
|
return JailStats{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
stats := JailStats{
|
||||||
|
FailedCurrent: -1,
|
||||||
|
FailedTotal: -1,
|
||||||
|
BannedCurrent: -1,
|
||||||
|
BannedTotal: -1,
|
||||||
|
}
|
||||||
|
|
||||||
|
if lvl1, ok := response.(*types.Tuple); ok {
|
||||||
|
if lvl2, ok := lvl1.Get(1).(*types.List); ok {
|
||||||
|
if filter, ok := lvl2.Get(0).(*types.Tuple); ok {
|
||||||
|
if filterLvl1, ok := filter.Get(1).(*types.List); ok {
|
||||||
|
if filterCurrentTuple, ok := filterLvl1.Get(0).(*types.Tuple); ok {
|
||||||
|
if filterCurrent, ok := filterCurrentTuple.Get(1).(int); ok {
|
||||||
|
stats.FailedCurrent = filterCurrent
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if filterTotalTuple, ok := filterLvl1.Get(1).(*types.Tuple); ok {
|
||||||
|
if filterTotal, ok := filterTotalTuple.Get(1).(int); ok {
|
||||||
|
stats.FailedTotal = filterTotal
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if actions, ok := lvl2.Get(1).(*types.Tuple); ok {
|
||||||
|
if actionsLvl1, ok := actions.Get(1).(*types.List); ok {
|
||||||
|
if actionsCurrentTuple, ok := actionsLvl1.Get(0).(*types.Tuple); ok {
|
||||||
|
if actionsCurrent, ok := actionsCurrentTuple.Get(1).(int); ok {
|
||||||
|
stats.BannedCurrent = actionsCurrent
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if actionsTotalTuple, ok := actionsLvl1.Get(1).(*types.Tuple); ok {
|
||||||
|
if actionsTotal, ok := actionsTotalTuple.Get(1).(int); ok {
|
||||||
|
stats.BannedTotal = actionsTotal
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return stats, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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 {
|
||||||
|
return fmt.Errorf("(%s) unexpected response format - cannot parse: %v", command, data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func newConnectionError(command string, err error) error {
|
||||||
|
return fmt.Errorf("(%s) failed to send command through socket: %v", command, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func trimSpaceForAll(slice []string) []string {
|
||||||
|
for i := range slice {
|
||||||
|
slice[i] = strings.TrimSpace(slice[i])
|
||||||
|
}
|
||||||
|
return slice
|
||||||
|
}
|
69
src/socket/protocol.go
Normal file
69
src/socket/protocol.go
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
package socket
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"github.com/nlpodyssey/gopickle/pickle"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
commandTerminator = "<F2B_END_COMMAND>"
|
||||||
|
pingCommand = "ping"
|
||||||
|
statusCommand = "status"
|
||||||
|
versionCommand = "version"
|
||||||
|
banTimeCommandFmt = "get %s bantime"
|
||||||
|
findTimeCommandFmt = "get %s findtime"
|
||||||
|
maxRetriesCommandFmt = "get %s maxretry"
|
||||||
|
socketReadBufferSize = 1024
|
||||||
|
)
|
||||||
|
|
||||||
|
func (s *Fail2BanSocket) sendCommand(command []string) (interface{}, error) {
|
||||||
|
err := s.write(command)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return s.read()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Fail2BanSocket) write(command []string) error {
|
||||||
|
err := s.encoder.Encode(command)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = s.socket.Write([]byte(commandTerminator))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Fail2BanSocket) read() (interface{}, error) {
|
||||||
|
reader := bufio.NewReader(s.socket)
|
||||||
|
|
||||||
|
data := []byte{}
|
||||||
|
for {
|
||||||
|
buf := make([]byte, socketReadBufferSize)
|
||||||
|
_, err := reader.Read(buf)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
data = append(data, buf...)
|
||||||
|
containsTerminator := bytes.Contains(data, []byte(commandTerminator))
|
||||||
|
if containsTerminator {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bufReader := bytes.NewReader(data)
|
||||||
|
unpickler := pickle.NewUnpickler(bufReader)
|
||||||
|
|
||||||
|
unpickler.FindClass = func(module, name string) (interface{}, error) {
|
||||||
|
if (module == "builtins" || module == "__builtin__") && name == "str" {
|
||||||
|
return &Py_builtins_str{}, nil
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("class not found: " + module + " : " + name)
|
||||||
|
}
|
||||||
|
|
||||||
|
return unpickler.Load()
|
||||||
|
}
|
Reference in New Issue
Block a user