Each server runs an ultralight probe (scratch image, ~7 MiB RSS) that tails the logs, ships raw lines to the hub over HTTPS with an on-disk spool, and applies enforce decisions locally. The hub keeps one node per probe (primary node = the 1.x data dir, unchanged), does all analysis and serves the UI. Peer federation is removed. 'urutau import' brings 1.x data dirs in with hash and count verification. UI: probes page, host qualified incidents, IPs never wrap, egress heatmap labels, live and replay globes no longer auto-rotate, replay repeat, live traffic metrics. BREAKING CHANGE: URUTAU_PEERS, URUTAU_PEER_* and /api/v1/peer/* are gone; log path variables move from the hub to the probe. See docs/migration-2.0.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
177 lines
4.0 KiB
Go
177 lines
4.0 KiB
Go
package probeagent
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
|
|
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/wire"
|
|
)
|
|
|
|
// Spool keeps unacknowledged batches on disk, one file per batch, in order.
|
|
type Spool struct {
|
|
mu sync.Mutex
|
|
dir string
|
|
max int64
|
|
items []spoolItem
|
|
bytes int64
|
|
wbuf bytes.Buffer // reused encoder buffer
|
|
}
|
|
|
|
type spoolItem struct {
|
|
seq uint64
|
|
size int64
|
|
}
|
|
|
|
func spoolName(seq uint64) string { return fmt.Sprintf("%012d.json", seq) }
|
|
|
|
// OpenSpool scans dir (created if missing) and returns the spool.
|
|
func OpenSpool(dir string, max int64) (*Spool, error) {
|
|
if err := os.MkdirAll(dir, 0o700); err != nil {
|
|
return nil, err
|
|
}
|
|
sp := &Spool{dir: dir, max: max}
|
|
ents, err := os.ReadDir(dir)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, e := range ents {
|
|
n := e.Name()
|
|
if strings.HasSuffix(n, ".tmp") {
|
|
os.Remove(filepath.Join(dir, n))
|
|
continue
|
|
}
|
|
if !strings.HasSuffix(n, ".json") {
|
|
continue
|
|
}
|
|
seq, err := strconv.ParseUint(strings.TrimSuffix(n, ".json"), 10, 64)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
fi, err := e.Info()
|
|
if err != nil {
|
|
continue
|
|
}
|
|
sp.items = append(sp.items, spoolItem{seq, fi.Size()})
|
|
sp.bytes += fi.Size()
|
|
}
|
|
sort.Slice(sp.items, func(i, j int) bool { return sp.items[i].seq < sp.items[j].seq })
|
|
return sp, nil
|
|
}
|
|
|
|
// MaxSeq is the highest batch number on disk (0 when empty).
|
|
func (sp *Spool) MaxSeq() uint64 {
|
|
sp.mu.Lock()
|
|
defer sp.mu.Unlock()
|
|
if len(sp.items) == 0 {
|
|
return 0
|
|
}
|
|
return sp.items[len(sp.items)-1].seq
|
|
}
|
|
|
|
// Add stores b (b.Seq must be above everything already stored). When the cap
|
|
// is exceeded the oldest files are deleted, but never the one just added; the
|
|
// number of log lines lost that way is returned.
|
|
func (sp *Spool) Add(b wire.Batch) (dropped uint64, err error) {
|
|
sp.mu.Lock()
|
|
defer sp.mu.Unlock()
|
|
sp.wbuf.Reset()
|
|
if err := json.NewEncoder(&sp.wbuf).Encode(&b); err != nil {
|
|
return 0, err
|
|
}
|
|
data := sp.wbuf.Bytes()
|
|
if sp.wbuf.Cap() > 4<<20 {
|
|
defer func() { sp.wbuf = bytes.Buffer{} }()
|
|
}
|
|
p := filepath.Join(sp.dir, spoolName(b.Seq))
|
|
tmp := p + ".tmp"
|
|
f, err := os.OpenFile(tmp, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o600)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if _, err = f.Write(data); err == nil {
|
|
err = f.Sync()
|
|
}
|
|
if cerr := f.Close(); err == nil {
|
|
err = cerr
|
|
}
|
|
if err == nil {
|
|
err = os.Rename(tmp, p)
|
|
}
|
|
if err != nil {
|
|
os.Remove(tmp)
|
|
return 0, err
|
|
}
|
|
sp.items = append(sp.items, spoolItem{b.Seq, int64(len(data))})
|
|
sp.bytes += int64(len(data))
|
|
for sp.bytes > sp.max && len(sp.items) > 1 {
|
|
it := sp.items[0]
|
|
dropped += sp.countLines(it.seq)
|
|
sp.removeLocked(0)
|
|
}
|
|
return dropped, nil
|
|
}
|
|
|
|
func (sp *Spool) countLines(seq uint64) uint64 {
|
|
data, err := os.ReadFile(filepath.Join(sp.dir, spoolName(seq)))
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
var b wire.Batch
|
|
if json.Unmarshal(data, &b) != nil {
|
|
return 0
|
|
}
|
|
return uint64(len(b.Lines))
|
|
}
|
|
|
|
func (sp *Spool) removeLocked(i int) {
|
|
it := sp.items[i]
|
|
os.Remove(filepath.Join(sp.dir, spoolName(it.seq)))
|
|
sp.bytes -= it.size
|
|
sp.items = append(sp.items[:i], sp.items[i+1:]...)
|
|
}
|
|
|
|
// Head returns the oldest batch.
|
|
func (sp *Spool) Head() (wire.Batch, bool, error) {
|
|
sp.mu.Lock()
|
|
if len(sp.items) == 0 {
|
|
sp.mu.Unlock()
|
|
return wire.Batch{}, false, nil
|
|
}
|
|
seq := sp.items[0].seq
|
|
sp.mu.Unlock()
|
|
data, err := os.ReadFile(filepath.Join(sp.dir, spoolName(seq)))
|
|
var b wire.Batch
|
|
if err == nil {
|
|
err = json.Unmarshal(data, &b)
|
|
}
|
|
if err != nil {
|
|
sp.Ack(seq) // unreadable: discard so the queue moves on
|
|
return wire.Batch{}, false, fmt.Errorf("spool batch %d unreadable: %w", seq, err)
|
|
}
|
|
b.Seq = seq
|
|
return b, true, nil
|
|
}
|
|
|
|
// Ack removes every batch with seq <= n.
|
|
func (sp *Spool) Ack(n uint64) {
|
|
sp.mu.Lock()
|
|
defer sp.mu.Unlock()
|
|
for len(sp.items) > 0 && sp.items[0].seq <= n {
|
|
sp.removeLocked(0)
|
|
}
|
|
}
|
|
|
|
// Stats returns the bytes and file count held.
|
|
func (sp *Spool) Stats() (bytes int64, files int) {
|
|
sp.mu.Lock()
|
|
defer sp.mu.Unlock()
|
|
return sp.bytes, len(sp.items)
|
|
}
|