feat: export bad ip count per jail
Update exported metrics to spit the number of bad IPs per jail using metric value labels. This includes a change to the database code to use a different query that groups the count by the `jail` column.
This commit is contained in:
parent
0b40e5de82
commit
4b965017d2
34
db/db.go
34
db/db.go
@ -3,10 +3,9 @@ package db
|
|||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"log"
|
"log"
|
||||||
"strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const queryCountTotalBadIps = "SELECT COUNT(1) FROM bips"
|
const queryBadIpsPerJail = "SELECT jail, COUNT(1) FROM bips GROUP BY jail"
|
||||||
|
|
||||||
type Fail2BanDB struct {
|
type Fail2BanDB struct {
|
||||||
DatabasePath string
|
DatabasePath string
|
||||||
@ -24,22 +23,37 @@ func MustConnectToDb(databasePath string) *Fail2BanDB {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *Fail2BanDB) CountTotalBadIps() (int, error) {
|
func (db *Fail2BanDB) CountBadIpsPerJail() (map[string]int, error) {
|
||||||
stmt, err := db.sqliteDB.Prepare(queryCountTotalBadIps)
|
stmt, err := db.sqliteDB.Prepare(queryBadIpsPerJail)
|
||||||
defer db.mustCloseStatement(stmt)
|
defer db.mustCloseStatement(stmt)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return -1, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
result := ""
|
jailNameToCountMap := map[string]int{}
|
||||||
err = stmt.QueryRow().Scan(&result)
|
rows, err := stmt.Query()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return -1, err
|
return nil, err
|
||||||
|
}
|
||||||
|
if rows == nil {
|
||||||
|
return jailNameToCountMap, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return strconv.Atoi(result)
|
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) {
|
func (db *Fail2BanDB) mustCloseStatement(stmt *sql.Stmt) {
|
||||||
|
29
exporter.go
29
exporter.go
@ -18,10 +18,10 @@ var (
|
|||||||
"Was the last fail2ban query successful.",
|
"Was the last fail2ban query successful.",
|
||||||
nil, nil,
|
nil, nil,
|
||||||
)
|
)
|
||||||
metricBadIpTotal = prometheus.NewDesc(
|
metricBadIpsPerJail = prometheus.NewDesc(
|
||||||
prometheus.BuildFQName(namespace, "", "badip_total"),
|
prometheus.BuildFQName(namespace, "", "bad_ips"),
|
||||||
"Total number of bad IPs stored in the database.",
|
"Number of bad IPs stored in the database (per jail).",
|
||||||
nil, nil,
|
[]string{"jail"}, nil,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -30,22 +30,27 @@ type Exporter struct {
|
|||||||
|
|
||||||
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
|
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
|
||||||
ch <- metricUp
|
ch <- metricUp
|
||||||
ch <- metricBadIpTotal
|
ch <- metricBadIpsPerJail
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
|
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
|
||||||
ch <- prometheus.MustNewConstMetric(
|
ch <- prometheus.MustNewConstMetric(
|
||||||
metricUp, prometheus.GaugeValue, 1,
|
metricUp, prometheus.GaugeValue, 1,
|
||||||
)
|
)
|
||||||
ch <- *collectTotalBadIpMetric()
|
collectBadIpsPerJailMetrics(ch)
|
||||||
}
|
}
|
||||||
|
|
||||||
func collectTotalBadIpMetric() *prometheus.Metric {
|
func collectBadIpsPerJailMetrics(ch chan<- prometheus.Metric) {
|
||||||
count, _ := db.CountTotalBadIps()
|
jailNameToCountMap, err := db.CountBadIpsPerJail()
|
||||||
metric := prometheus.MustNewConstMetric(
|
if err != nil {
|
||||||
metricBadIpTotal, prometheus.GaugeValue, float64(count),
|
log.Print(err)
|
||||||
)
|
}
|
||||||
return &metric
|
|
||||||
|
for jailName, count := range jailNameToCountMap {
|
||||||
|
ch <- prometheus.MustNewConstMetric(
|
||||||
|
metricBadIpsPerJail, prometheus.GaugeValue, float64(count), jailName,
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
Loading…
Reference in New Issue
Block a user