You've already forked prometheus-fail2ban-exporter
Compare commits
19 Commits
Author | SHA1 | Date | |
---|---|---|---|
61a8a58754 | |||
4f2d8d9079 | |||
f2cd6ebb7b | |||
695447a4c2 | |||
5363cad4ce | |||
1a660ab046 | |||
4c122609b8 | |||
b268f8654c | |||
025347b7ca | |||
56730c8774 | |||
bb5c15de1b | |||
60e6365e1f | |||
7cdaf1ebd1 | |||
9ccad42342 | |||
3591582b61 | |||
0b6a941b38 | |||
d8ce799223 | |||
5a107cc547 | |||
351d3344f7 |
@ -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
|
||||||
|
12
CHANGELOG.md
12
CHANGELOG.md
@ -7,6 +7,17 @@ The format is based on [Keep a Changelog], and this project adheres to [Semantic
|
|||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
*Nothing yet*
|
*Nothing yet*
|
||||||
|
|
||||||
|
## [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
|
## [0.3.0] - 2021-09-27
|
||||||
*Export new version metrics ([#12](https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/issues/12))*
|
*Export new version metrics ([#12](https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/issues/12))*
|
||||||
|
|
||||||
@ -58,3 +69,4 @@ The format is based on [Keep a Changelog], and this project adheres to [Semantic
|
|||||||
[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.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
|
||||||
|
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) .
|
||||||
|
103
README.md
103
README.md
@ -15,21 +15,10 @@ 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
|
|
||||||
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 default path to the fail2ban database is: `/var/lib/fail2ban/fail2ban.sqlite3`
|
|
||||||
|
|
||||||
## 2. Running the Exporter
|
## 2. Running the Exporter
|
||||||
|
|
||||||
@ -44,14 +33,16 @@ See the [releases page](https://gitlab.com/hectorjsmith/fail2ban-prometheus-expo
|
|||||||
```
|
```
|
||||||
$ fail2ban-prometheus-exporter -h
|
$ fail2ban-prometheus-exporter -h
|
||||||
|
|
||||||
-db string
|
|
||||||
path to the fail2ban sqlite database (deprecated)
|
|
||||||
-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 +51,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 +62,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 +74,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 +90,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 +105,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"
|
||||||
@ -153,6 +141,10 @@ 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**
|
||||||
|
|
||||||
@ -181,9 +173,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 +205,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"
|
||||||
|
@ -12,18 +12,23 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type AppSettings struct {
|
type AppSettings struct {
|
||||||
VersionMode bool
|
VersionMode bool
|
||||||
MetricsPort int
|
MetricsPort int
|
||||||
Fail2BanDbPath string
|
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.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")
|
||||||
|
|
||||||
|
// deprecated: to be removed in next version
|
||||||
|
_ = flag.String("db", "", "path to the fail2ban sqlite database (removed)")
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
appSettings.validateFlags()
|
appSettings.validateFlags()
|
||||||
@ -33,8 +38,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 +47,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()
|
||||||
|
55
src/collector/f2b/collector.go
Normal file
55
src/collector/f2b/collector.go
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
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
|
||||||
|
dbErrorCount int
|
||||||
|
socketConnectionErrorCount int
|
||||||
|
socketRequestErrorCount int
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewExporter(appSettings *cfg.AppSettings, exporterVersion string) *Collector {
|
||||||
|
return &Collector{
|
||||||
|
socketPath: appSettings.Fail2BanSocketPath,
|
||||||
|
exporterVersion: exporterVersion,
|
||||||
|
lastError: nil,
|
||||||
|
dbErrorCount: 0,
|
||||||
|
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)
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package export
|
package f2b
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fail2ban-prometheus-exporter/socket"
|
"fail2ban-prometheus-exporter/socket"
|
||||||
@ -46,6 +46,21 @@ var (
|
|||||||
"Total number of IPs banned by this jail (includes expired bans)",
|
"Total number of IPs banned by this jail (includes expired bans)",
|
||||||
[]string{"jail"}, nil,
|
[]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(
|
metricVersionInfo = prometheus.NewDesc(
|
||||||
prometheus.BuildFQName(namespace, "", "version"),
|
prometheus.BuildFQName(namespace, "", "version"),
|
||||||
"Version of the exporter and fail2ban server",
|
"Version of the exporter and fail2ban server",
|
||||||
@ -53,24 +68,24 @@ var (
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
func (e *Exporter) collectErrorCountMetric(ch chan<- prometheus.Metric) {
|
func (c *Collector) collectErrorCountMetric(ch chan<- prometheus.Metric) {
|
||||||
ch <- prometheus.MustNewConstMetric(
|
ch <- prometheus.MustNewConstMetric(
|
||||||
metricErrorCount, prometheus.CounterValue, float64(e.dbErrorCount), "db",
|
metricErrorCount, prometheus.CounterValue, float64(c.dbErrorCount), "db",
|
||||||
)
|
)
|
||||||
ch <- prometheus.MustNewConstMetric(
|
ch <- prometheus.MustNewConstMetric(
|
||||||
metricErrorCount, prometheus.CounterValue, float64(e.socketConnectionErrorCount), "socket_conn",
|
metricErrorCount, prometheus.CounterValue, float64(c.socketConnectionErrorCount), "socket_conn",
|
||||||
)
|
)
|
||||||
ch <- prometheus.MustNewConstMetric(
|
ch <- prometheus.MustNewConstMetric(
|
||||||
metricErrorCount, prometheus.CounterValue, float64(e.socketRequestErrorCount), "socket_req",
|
metricErrorCount, prometheus.CounterValue, float64(c.socketRequestErrorCount), "socket_req",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Exporter) collectServerUpMetric(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket) {
|
func (c *Collector) collectServerUpMetric(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket) {
|
||||||
var serverUp float64 = 0
|
var serverUp float64 = 0
|
||||||
if s != nil {
|
if s != nil {
|
||||||
pingSuccess, err := s.Ping()
|
pingSuccess, err := s.Ping()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
e.socketRequestErrorCount++
|
c.socketRequestErrorCount++
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
}
|
}
|
||||||
if err == nil && pingSuccess {
|
if err == nil && pingSuccess {
|
||||||
@ -82,11 +97,11 @@ func (e *Exporter) collectServerUpMetric(ch chan<- prometheus.Metric, s *socket.
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Exporter) collectJailMetrics(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket) {
|
func (c *Collector) collectJailMetrics(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket) {
|
||||||
jails, err := s.GetJails()
|
jails, err := s.GetJails()
|
||||||
var count float64 = 0
|
var count float64 = 0
|
||||||
if err != nil {
|
if err != nil {
|
||||||
e.socketRequestErrorCount++
|
c.socketRequestErrorCount++
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
}
|
}
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@ -97,14 +112,15 @@ func (e *Exporter) collectJailMetrics(ch chan<- prometheus.Metric, s *socket.Fai
|
|||||||
)
|
)
|
||||||
|
|
||||||
for i := range jails {
|
for i := range jails {
|
||||||
e.collectJailStatsMetric(ch, s, jails[i])
|
c.collectJailStatsMetric(ch, s, jails[i])
|
||||||
|
c.collectJailConfigMetrics(ch, s, jails[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Exporter) collectJailStatsMetric(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket, jail string) {
|
func (c *Collector) collectJailStatsMetric(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket, jail string) {
|
||||||
stats, err := s.GetJailStats(jail)
|
stats, err := s.GetJailStats(jail)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
e.socketRequestErrorCount++
|
c.socketRequestErrorCount++
|
||||||
log.Printf("failed to get stats for jail %s: %v", jail, err)
|
log.Printf("failed to get stats for jail %s: %v", jail, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -123,18 +139,44 @@ func (e *Exporter) collectJailStatsMetric(ch chan<- prometheus.Metric, s *socket
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Exporter) collectVersionMetric(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket) {
|
func (c *Collector) collectJailConfigMetrics(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket, jail string) {
|
||||||
var err error
|
banTime, err := s.GetJailBanTime(jail)
|
||||||
var fail2banVersion = ""
|
if err != nil {
|
||||||
if s != nil {
|
c.socketRequestErrorCount++
|
||||||
fail2banVersion, err = s.GetServerVersion()
|
log.Printf("failed to get ban time for jail %s: %v", jail, err)
|
||||||
if err != nil {
|
} else {
|
||||||
e.socketRequestErrorCount++
|
ch <- prometheus.MustNewConstMetric(
|
||||||
log.Printf("failed to get fail2ban server version: %v", err)
|
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(
|
ch <- prometheus.MustNewConstMetric(
|
||||||
metricVersionInfo, prometheus.GaugeValue, float64(1), e.exporterVersion, fail2banVersion,
|
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,25 +0,0 @@
|
|||||||
package export
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fail2ban-prometheus-exporter/cfg"
|
|
||||||
fail2banDb "fail2ban-prometheus-exporter/db"
|
|
||||||
"log"
|
|
||||||
)
|
|
||||||
|
|
||||||
func NewExporter(appSettings *cfg.AppSettings, exporterVersion string) *Exporter {
|
|
||||||
exporter := &Exporter{
|
|
||||||
exporterVersion: exporterVersion,
|
|
||||||
lastError: nil,
|
|
||||||
dbErrorCount: 0,
|
|
||||||
socketConnectionErrorCount: 0,
|
|
||||||
socketRequestErrorCount: 0,
|
|
||||||
}
|
|
||||||
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
|
|
||||||
}
|
|
||||||
return exporter
|
|
||||||
}
|
|
@ -1,102 +0,0 @@
|
|||||||
package export
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
|
||||||
"log"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
deprecatedNamespace = "fail2ban"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
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,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
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,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,64 +0,0 @@
|
|||||||
package export
|
|
||||||
|
|
||||||
import (
|
|
||||||
fail2banDb "fail2ban-prometheus-exporter/db"
|
|
||||||
"fail2ban-prometheus-exporter/socket"
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
|
||||||
"log"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Exporter struct {
|
|
||||||
db *fail2banDb.Fail2BanDB
|
|
||||||
socketPath string
|
|
||||||
exporterVersion 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.collectVersionMetric(ch, s)
|
|
||||||
} else {
|
|
||||||
e.collectVersionMetric(ch, nil)
|
|
||||||
}
|
|
||||||
e.collectErrorCountMetric(ch)
|
|
||||||
}
|
|
@ -2,12 +2,12 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fail2ban-prometheus-exporter/cfg"
|
"fail2ban-prometheus-exporter/cfg"
|
||||||
"fail2ban-prometheus-exporter/export"
|
"fail2ban-prometheus-exporter/collector/f2b"
|
||||||
|
"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"
|
||||||
)
|
)
|
||||||
@ -43,6 +43,11 @@ func rootHtmlHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
@ -52,11 +57,16 @@ func main() {
|
|||||||
|
|
||||||
log.Printf("starting fail2ban exporter at %s", addr)
|
log.Printf("starting fail2ban exporter at %s", addr)
|
||||||
|
|
||||||
exporter := export.NewExporter(appSettings, version)
|
f2bCollector := f2b.NewExporter(appSettings, version)
|
||||||
prometheus.MustRegister(exporter)
|
prometheus.MustRegister(f2bCollector)
|
||||||
|
|
||||||
|
textFileCollector := textfile.NewCollector(appSettings)
|
||||||
|
prometheus.MustRegister(textFileCollector)
|
||||||
|
|
||||||
http.HandleFunc("/", rootHtmlHandler)
|
http.HandleFunc("/", rootHtmlHandler)
|
||||||
http.Handle(metricsPath, promhttp.Handler())
|
http.HandleFunc(metricsPath, func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
metricHandler(w, r, textFileCollector)
|
||||||
|
})
|
||||||
log.Printf("metrics available at '%s'", metricsPath)
|
log.Printf("metrics available at '%s'", metricsPath)
|
||||||
|
|
||||||
svrErr := make(chan error)
|
svrErr := make(chan error)
|
||||||
|
@ -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,21 @@ 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) {
|
func (s *Fail2BanSocket) GetServerVersion() (string, error) {
|
||||||
response, err := s.sendCommand([]string{versionCommand})
|
response, err := s.sendCommand([]string{versionCommand})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -132,6 +147,22 @@ func (s *Fail2BanSocket) GetServerVersion() (string, error) {
|
|||||||
return "", newBadFormatError(versionCommand, response)
|
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)
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,9 @@ const (
|
|||||||
pingCommand = "ping"
|
pingCommand = "ping"
|
||||||
statusCommand = "status"
|
statusCommand = "status"
|
||||||
versionCommand = "version"
|
versionCommand = "version"
|
||||||
|
banTimeCommandFmt = "get %s bantime"
|
||||||
|
findTimeCommandFmt = "get %s findtime"
|
||||||
|
maxRetriesCommandFmt = "get %s maxretry"
|
||||||
socketReadBufferSize = 1024
|
socketReadBufferSize = 1024
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user