Files
Lucas RayanandClaude Opus 5.5 8546d14cef
CI / Format and vet (pull_request) Failing after 53s
CI / Dependency audit (pull_request) Successful in 33s
CI / Tests (Linux, race detector) (pull_request) Successful in 2m1s
CI / Docker images (build and smoke test) (pull_request) Failing after 47s
feat!: Urutau 2.0.0, ultralight probes and a single analysis hub
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>
2026-11-15 22:14:50 -03:00

177 lines
3.9 KiB
Go

package probeagent
import (
"encoding/json"
"errors"
"os"
"path/filepath"
"sync"
"time"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/wire"
)
// State is the probe's only durable bookkeeping besides the spool.
type State struct {
Seq uint64 `json:"seq"`
LastCmd uint64 `json:"last_cmd"`
Positions map[string]wire.Pos `json:"positions"`
Dropped uint64 `json:"dropped"`
Alive int64 `json:"alive"` // unix seconds, refreshed by the process; read by Check
}
type stateStore struct {
mu sync.Mutex
path string
s State
}
func loadState(dir string) (*stateStore, error) {
st := &stateStore{path: filepath.Join(dir, "state.json")}
st.s.Positions = map[string]wire.Pos{}
b, err := os.ReadFile(st.path)
if errors.Is(err, os.ErrNotExist) {
return st, nil
}
if err != nil {
return nil, err
}
if err := json.Unmarshal(b, &st.s); err != nil {
// a corrupt state must not keep the probe down: start clean, the hub's
// offsets in the hello cover the gap.
st.s = State{}
}
if st.s.Positions == nil {
st.s.Positions = map[string]wire.Pos{}
}
return st, nil
}
// saveLocked writes state.json atomically. Caller holds mu.
func (st *stateStore) saveLocked() error {
st.s.Alive = time.Now().Unix()
b, err := json.Marshal(&st.s)
if err != nil {
return err
}
tmp := st.path + ".tmp"
f, err := os.OpenFile(tmp, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o600)
if err != nil {
return err
}
if _, err := f.Write(b); err != nil {
f.Close()
return err
}
if err := f.Sync(); err != nil {
f.Close()
return err
}
if err := f.Close(); err != nil {
return err
}
return os.Rename(tmp, st.path)
}
func (st *stateStore) touch() error {
st.mu.Lock()
defer st.mu.Unlock()
return st.saveLocked()
}
func (st *stateStore) snapshot() State {
st.mu.Lock()
defer st.mu.Unlock()
c := st.s
c.Positions = make(map[string]wire.Pos, len(st.s.Positions))
for k, v := range st.s.Positions {
c.Positions[k] = v
}
return c
}
func (st *stateStore) pos(src string) (wire.Pos, bool) {
st.mu.Lock()
defer st.mu.Unlock()
p, ok := st.s.Positions[src]
return p, ok
}
func (st *stateStore) lastCmd() uint64 {
st.mu.Lock()
defer st.mu.Unlock()
return st.s.LastCmd
}
func (st *stateStore) setLastCmd(n uint64) error {
st.mu.Lock()
defer st.mu.Unlock()
st.s.LastCmd = n
return st.saveLocked()
}
func (st *stateStore) dropped() uint64 {
st.mu.Lock()
defer st.mu.Unlock()
return st.s.Dropped
}
func (st *stateStore) addDropped(n uint64) {
if n == 0 {
return
}
st.mu.Lock()
defer st.mu.Unlock()
st.s.Dropped += n
_ = st.saveLocked()
}
// raiseSeq makes sure new batches are numbered above what the hub already
// applied (state lost on the probe, or hub ahead).
func (st *stateStore) raiseSeq(n uint64) {
st.mu.Lock()
defer st.mu.Unlock()
if n > st.s.Seq {
st.s.Seq = n
_ = st.saveLocked()
}
}
func (st *stateStore) lastSeq() uint64 {
st.mu.Lock()
defer st.mu.Unlock()
return st.s.Seq
}
// commit allocates the next batch seq. persist is called with it before the
// positions move, so a spooled batch always has its position recorded.
func (st *stateStore) commit(build func(seq uint64) (dropped uint64), pos map[string]wire.Pos) {
st.mu.Lock()
defer st.mu.Unlock()
st.s.Seq++
st.s.Dropped += build(st.s.Seq)
for k, v := range pos {
st.s.Positions[k] = v
}
_ = st.saveLocked()
}
// Check is the container healthcheck: the process refreshes state.json every
// few seconds, so a stale or missing file means it is not running.
func Check(stateDir string) error {
b, err := os.ReadFile(filepath.Join(stateDir, "state.json"))
if err != nil {
return errors.New("probe state unreadable")
}
var s State
if err := json.Unmarshal(b, &s); err != nil {
return errors.New("probe state corrupt")
}
if age := time.Since(time.Unix(s.Alive, 0)); age > aliveMax {
return errors.New("probe not alive")
}
return nil
}
const aliveMax = 90 * time.Second