update
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"regexp"
|
||||
@@ -41,6 +42,8 @@ func (poller *Poller) Run(ctx context.Context) {
|
||||
if _, err := poller.store.pool.Exec(ctx, "DELETE FROM agent_log_entries WHERE event_at < $1", time.Now().Add(-poller.logRetention)); err != nil {
|
||||
log.Printf("log retention cleanup: %v", err)
|
||||
}
|
||||
_, _ = poller.store.pool.Exec(ctx, "DELETE FROM connection_samples WHERE sampled_at < $1", time.Now().Add(-poller.metricRetention))
|
||||
_, _ = poller.store.pool.Exec(ctx, "DELETE FROM fail2ban_events WHERE event_at < $1", time.Now().Add(-poller.logRetention))
|
||||
_, _ = poller.store.pool.Exec(ctx, "DELETE FROM sessions WHERE expires_at < now()")
|
||||
}
|
||||
}
|
||||
@@ -119,13 +122,26 @@ func (poller *Poller) pollServer(ctx context.Context, server Server) {
|
||||
}
|
||||
}
|
||||
}
|
||||
type socket struct {
|
||||
Protocol string `json:"protocol"`
|
||||
LocalAddress string `json:"local_address"`
|
||||
LocalPort int `json:"local_port"`
|
||||
RemoteAddress string `json:"remote_address"`
|
||||
RemotePort int `json:"remote_port"`
|
||||
State string `json:"state"`
|
||||
Process *string `json:"process"`
|
||||
}
|
||||
connections := 0
|
||||
connectionRows := []socket{}
|
||||
if code, payload, callErr := poller.agent.Do(ctx, server, http.MethodGet, "/v1/network/connections", nil); callErr == nil && code == 200 {
|
||||
var items []json.RawMessage
|
||||
if json.Unmarshal(payload, &items) == nil {
|
||||
connections = len(items)
|
||||
if json.Unmarshal(payload, &connectionRows) == nil {
|
||||
if len(connectionRows) > 10000 {
|
||||
connectionRows = connectionRows[:10000]
|
||||
}
|
||||
connections = len(connectionRows)
|
||||
}
|
||||
}
|
||||
bans := poller.pollFail2Ban(ctx, server)
|
||||
txDB, err := poller.store.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
@@ -134,7 +150,41 @@ func (poller *Poller) pollServer(ctx context.Context, server Server) {
|
||||
_, err = txDB.Exec(ctx, "UPDATE servers SET status='online',hostname=$2,last_seen=now(),updated_at=now() WHERE id=$1", server.ID, status.Hostname)
|
||||
if err == nil {
|
||||
_, err = txDB.Exec(ctx, `INSERT INTO metric_samples(server_id,cpu,memory,disk,rx,tx,connections,bans)
|
||||
VALUES($1,$2,$3,$4,$5,$6,$7,0)`, server.ID, status.CPU, memory, disk, rx, tx, connections)
|
||||
VALUES($1,$2,$3,$4,$5,$6,$7,$8)`, server.ID, status.CPU, memory, disk, rx, tx, connections, bans)
|
||||
}
|
||||
if err == nil {
|
||||
type aggregate struct {
|
||||
row socket
|
||||
count int
|
||||
}
|
||||
grouped := map[string]aggregate{}
|
||||
for _, row := range connectionRows {
|
||||
if net.ParseIP(row.LocalAddress) == nil || net.ParseIP(row.RemoteAddress) == nil || row.LocalPort < 0 || row.LocalPort > 65535 || row.RemotePort < 0 || row.RemotePort > 65535 || len(row.Protocol) > 8 || len(row.State) > 32 {
|
||||
continue
|
||||
}
|
||||
process := ""
|
||||
if row.Process != nil {
|
||||
process = truncateUTF8(*row.Process, 128)
|
||||
}
|
||||
key := fmt.Sprintf("%s\x00%s\x00%d\x00%s\x00%d\x00%s\x00%s", row.Protocol, row.LocalAddress, row.LocalPort, row.RemoteAddress, row.RemotePort, row.State, process)
|
||||
entry := grouped[key]
|
||||
entry.row = row
|
||||
entry.row.Process = &process
|
||||
entry.count++
|
||||
grouped[key] = entry
|
||||
}
|
||||
inserted := 0
|
||||
for _, entry := range grouped {
|
||||
if inserted >= 2000 {
|
||||
break
|
||||
}
|
||||
_, err = txDB.Exec(ctx, `INSERT INTO connection_samples(server_id,protocol,local_address,local_port,remote_address,remote_port,state,process,connection_count)
|
||||
VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9)`, server.ID, entry.row.Protocol, entry.row.LocalAddress, entry.row.LocalPort, entry.row.RemoteAddress, entry.row.RemotePort, entry.row.State, *entry.row.Process, entry.count)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
inserted++
|
||||
}
|
||||
}
|
||||
if err == nil {
|
||||
err = txDB.Commit(ctx)
|
||||
@@ -145,6 +195,78 @@ func (poller *Poller) pollServer(ctx context.Context, server Server) {
|
||||
poller.pollLogs(ctx, server)
|
||||
}
|
||||
|
||||
func (poller *Poller) pollFail2Ban(ctx context.Context, server Server) int {
|
||||
status, payload, err := poller.agent.Do(ctx, server, http.MethodGet, "/v1/fail2ban/status", nil)
|
||||
if err != nil || status != http.StatusOK {
|
||||
return 0
|
||||
}
|
||||
var response struct {
|
||||
JailStatuses []struct {
|
||||
Jail string `json:"jail"`
|
||||
Banned []string `json:"banned_ips"`
|
||||
} `json:"jail_statuses"`
|
||||
}
|
||||
if json.Unmarshal(payload, &response) != nil || len(response.JailStatuses) > 64 {
|
||||
return 0
|
||||
}
|
||||
current := map[string][2]string{}
|
||||
total := 0
|
||||
for _, jail := range response.JailStatuses {
|
||||
if !validSimpleName(jail.Jail) || len(jail.Banned) > 10000 {
|
||||
continue
|
||||
}
|
||||
for _, ip := range jail.Banned {
|
||||
if net.ParseIP(ip) != nil {
|
||||
current[jail.Jail+"\x00"+ip] = [2]string{jail.Jail, ip}
|
||||
total++
|
||||
}
|
||||
}
|
||||
}
|
||||
rows, err := poller.store.pool.Query(ctx, "SELECT jail,ip FROM fail2ban_ban_state WHERE server_id=$1", server.ID)
|
||||
if err != nil {
|
||||
return total
|
||||
}
|
||||
previous := map[string][2]string{}
|
||||
for rows.Next() {
|
||||
var jail, ip string
|
||||
if rows.Scan(&jail, &ip) == nil {
|
||||
previous[jail+"\x00"+ip] = [2]string{jail, ip}
|
||||
}
|
||||
}
|
||||
rows.Close()
|
||||
tx, err := poller.store.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return total
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
for key, value := range current {
|
||||
if _, found := previous[key]; !found {
|
||||
_, err = tx.Exec(ctx, "INSERT INTO fail2ban_events(server_id,jail,ip,action,source) VALUES($1,$2,$3,'ban','agent')", server.ID, value[0], value[1])
|
||||
if err != nil {
|
||||
return total
|
||||
}
|
||||
}
|
||||
}
|
||||
for key, value := range previous {
|
||||
if _, found := current[key]; !found {
|
||||
_, err = tx.Exec(ctx, "INSERT INTO fail2ban_events(server_id,jail,ip,action,source) VALUES($1,$2,$3,'unban','agent')", server.ID, value[0], value[1])
|
||||
if err != nil {
|
||||
return total
|
||||
}
|
||||
}
|
||||
}
|
||||
if _, err = tx.Exec(ctx, "DELETE FROM fail2ban_ban_state WHERE server_id=$1", server.ID); err != nil {
|
||||
return total
|
||||
}
|
||||
for _, value := range current {
|
||||
if _, err = tx.Exec(ctx, "INSERT INTO fail2ban_ban_state(server_id,jail,ip) VALUES($1,$2,$3)", server.ID, value[0], value[1]); err != nil {
|
||||
return total
|
||||
}
|
||||
}
|
||||
_ = tx.Commit(ctx)
|
||||
return total
|
||||
}
|
||||
|
||||
func (poller *Poller) pollLogs(ctx context.Context, server Server) {
|
||||
var cursor string
|
||||
var since time.Time
|
||||
|
||||
Reference in New Issue
Block a user