update
This commit is contained in:
@@ -75,6 +75,35 @@ CREATE TABLE IF NOT EXISTS agent_log_entries (
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS agent_log_entries_time_idx ON agent_log_entries(event_at DESC);
|
||||
CREATE INDEX IF NOT EXISTS agent_log_entries_server_time_idx ON agent_log_entries(server_id, event_at DESC);
|
||||
CREATE TABLE IF NOT EXISTS connection_samples (
|
||||
server_id text NOT NULL REFERENCES servers(id) ON DELETE CASCADE,
|
||||
sampled_at timestamptz NOT NULL DEFAULT now(),
|
||||
protocol text NOT NULL,
|
||||
local_address text NOT NULL,
|
||||
local_port integer NOT NULL CHECK (local_port BETWEEN 0 AND 65535),
|
||||
remote_address text NOT NULL,
|
||||
remote_port integer NOT NULL CHECK (remote_port BETWEEN 0 AND 65535),
|
||||
state text NOT NULL,
|
||||
process text NOT NULL DEFAULT '',
|
||||
connection_count integer NOT NULL CHECK (connection_count > 0)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS connection_samples_server_time_idx ON connection_samples(server_id, sampled_at DESC);
|
||||
CREATE TABLE IF NOT EXISTS fail2ban_events (
|
||||
id bigserial PRIMARY KEY,
|
||||
server_id text NOT NULL REFERENCES servers(id) ON DELETE CASCADE,
|
||||
event_at timestamptz NOT NULL DEFAULT now(),
|
||||
jail text NOT NULL,
|
||||
ip text NOT NULL,
|
||||
action text NOT NULL CHECK (action IN ('ban','unban')),
|
||||
source text NOT NULL
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS fail2ban_events_server_time_idx ON fail2ban_events(server_id, event_at DESC);
|
||||
CREATE TABLE IF NOT EXISTS fail2ban_ban_state (
|
||||
server_id text NOT NULL REFERENCES servers(id) ON DELETE CASCADE,
|
||||
jail text NOT NULL,
|
||||
ip text NOT NULL,
|
||||
PRIMARY KEY(server_id,jail,ip)
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS audit_events (
|
||||
id bigserial PRIMARY KEY,
|
||||
user_id text REFERENCES users(id) ON DELETE SET NULL,
|
||||
@@ -139,6 +168,37 @@ type AgentLogEntry struct {
|
||||
PID *int `json:"pid,omitempty"`
|
||||
}
|
||||
|
||||
type ConnectionHistoryRow struct {
|
||||
Protocol string `json:"protocol"`
|
||||
LocalAddress string `json:"local_address"`
|
||||
LocalPort int `json:"local_port"`
|
||||
RemoteAddress string `json:"remote_address"`
|
||||
Process string `json:"process"`
|
||||
Samples int64 `json:"samples"`
|
||||
Peak int `json:"peak"`
|
||||
FirstSeen time.Time `json:"first_seen"`
|
||||
LastSeen time.Time `json:"last_seen"`
|
||||
}
|
||||
|
||||
type ConnectionTimelinePoint struct {
|
||||
SampledAt time.Time `json:"sampled_at"`
|
||||
Count int64 `json:"count"`
|
||||
}
|
||||
|
||||
type ConnectionHistory struct {
|
||||
Rows []ConnectionHistoryRow `json:"rows"`
|
||||
Timeline []ConnectionTimelinePoint `json:"timeline"`
|
||||
}
|
||||
|
||||
type Fail2BanEvent struct {
|
||||
ID int64 `json:"id"`
|
||||
EventAt time.Time `json:"event_at"`
|
||||
Jail string `json:"jail"`
|
||||
IP string `json:"ip"`
|
||||
Action string `json:"action"`
|
||||
Source string `json:"source"`
|
||||
}
|
||||
|
||||
type MetricPoint struct {
|
||||
ServerID string
|
||||
ServerName string
|
||||
@@ -372,6 +432,84 @@ func (store *Store) Logs(ctx context.Context, serverID string, limit int) ([]Age
|
||||
return entries, rows.Err()
|
||||
}
|
||||
|
||||
func (store *Store) ConnectionHistory(ctx context.Context, serverID string, since time.Time) (ConnectionHistory, error) {
|
||||
result := ConnectionHistory{Rows: []ConnectionHistoryRow{}, Timeline: []ConnectionTimelinePoint{}}
|
||||
rows, err := store.pool.Query(ctx, `SELECT protocol,local_address,local_port,remote_address,process,
|
||||
SUM(connection_count),MAX(connection_count),MIN(sampled_at),MAX(sampled_at)
|
||||
FROM connection_samples WHERE server_id=$1 AND sampled_at >= $2
|
||||
GROUP BY protocol,local_address,local_port,remote_address,process
|
||||
ORDER BY SUM(connection_count) DESC LIMIT 500`, serverID, since)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var row ConnectionHistoryRow
|
||||
if err = rows.Scan(&row.Protocol, &row.LocalAddress, &row.LocalPort, &row.RemoteAddress, &row.Process, &row.Samples, &row.Peak, &row.FirstSeen, &row.LastSeen); err != nil {
|
||||
return result, err
|
||||
}
|
||||
result.Rows = append(result.Rows, row)
|
||||
}
|
||||
if err = rows.Err(); err != nil {
|
||||
return result, err
|
||||
}
|
||||
timeline, err := store.pool.Query(ctx, `SELECT date_bin(interval '5 minutes',sampled_at,timestamptz 'epoch'),SUM(connection_count)
|
||||
FROM connection_samples WHERE server_id=$1 AND sampled_at >= $2
|
||||
GROUP BY 1 ORDER BY 1 LIMIT 9000`, serverID, since)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
defer timeline.Close()
|
||||
for timeline.Next() {
|
||||
var point ConnectionTimelinePoint
|
||||
if err = timeline.Scan(&point.SampledAt, &point.Count); err != nil {
|
||||
return result, err
|
||||
}
|
||||
result.Timeline = append(result.Timeline, point)
|
||||
}
|
||||
return result, timeline.Err()
|
||||
}
|
||||
|
||||
func (store *Store) Fail2BanEvents(ctx context.Context, serverID string, limit int) ([]Fail2BanEvent, error) {
|
||||
rows, err := store.pool.Query(ctx, `SELECT id,event_at,jail,ip,action,source FROM fail2ban_events
|
||||
WHERE server_id=$1 ORDER BY event_at DESC LIMIT $2`, serverID, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
events := []Fail2BanEvent{}
|
||||
for rows.Next() {
|
||||
var event Fail2BanEvent
|
||||
if err = rows.Scan(&event.ID, &event.EventAt, &event.Jail, &event.IP, &event.Action, &event.Source); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
events = append(events, event)
|
||||
}
|
||||
return events, rows.Err()
|
||||
}
|
||||
|
||||
func (store *Store) AddFail2BanEvent(ctx context.Context, serverID, jail, ip, action, source string) {
|
||||
tx, err := store.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
log.Printf("write Fail2Ban event: %v", err)
|
||||
return
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
if _, err = tx.Exec(ctx, `INSERT INTO fail2ban_events(server_id,jail,ip,action,source) VALUES($1,$2,$3,$4,$5)`, serverID, jail, ip, action, source); err == nil {
|
||||
if action == "ban" {
|
||||
_, err = tx.Exec(ctx, `INSERT INTO fail2ban_ban_state(server_id,jail,ip) VALUES($1,$2,$3) ON CONFLICT DO NOTHING`, serverID, jail, ip)
|
||||
} else {
|
||||
_, err = tx.Exec(ctx, `DELETE FROM fail2ban_ban_state WHERE server_id=$1 AND jail=$2 AND ip=$3`, serverID, jail, ip)
|
||||
}
|
||||
}
|
||||
if err == nil {
|
||||
err = tx.Commit(ctx)
|
||||
}
|
||||
if err != nil {
|
||||
log.Printf("write Fail2Ban event: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (store *Store) MetricSeries(ctx context.Context) ([]map[string]string, error) {
|
||||
rows, err := store.pool.Query(ctx, "SELECT id,name FROM servers ORDER BY id LIMIT 1000")
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user