prometheus-fail2ban-exporter/db/db.go
Hector 4b965017d2 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.
2021-02-06 12:14:39 +00:00

65 lines
1.1 KiB
Go

package db
import (
"database/sql"
"log"
)
const queryBadIpsPerJail = "SELECT jail, COUNT(1) FROM bips GROUP BY jail"
type Fail2BanDB struct {
DatabasePath string
sqliteDB *sql.DB
}
func MustConnectToDb(databasePath string) *Fail2BanDB {
db, err := sql.Open("sqlite3", databasePath)
if err != nil {
log.Fatal(err)
}
return &Fail2BanDB{
DatabasePath: databasePath,
sqliteDB: db,
}
}
func (db *Fail2BanDB) CountBadIpsPerJail() (map[string]int, error) {
stmt, err := db.sqliteDB.Prepare(queryBadIpsPerJail)
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) {
err := stmt.Close()
if err != nil {
log.Fatal(err)
}
}