package main

import (
	"bytes"
	"crypto/sha256"
	"encoding/hex"
	"encoding/json"
	"log/slog"
	"os"
	"path/filepath"
	"runtime"
	"sort"
	"strconv"
	"strings"
	"sync"
	"time"
)

const (
	hogPercent  = 90.0             // one process at this share of a core ...
	hogDuration = 15 * time.Minute // ... for this long, without a break
	fillWindow  = 6 * 3600         // disk fill projection looks at the last 6 hours
	clockStep   = 30 * time.Second // a wall clock change this much larger than the elapsed time is a jump
)

// ---------------------------------------------------------------------------
// Data shared by the messages (messages.go). Field names are short: they go over the network.
// ---------------------------------------------------------------------------

type Minute struct {
	T      int64   `json:"t"`       // start of the minute, unix seconds (UTC)
	CPU    float64 `json:"cpu"`     // % of all cores, average
	CPUMax float64 `json:"cpu_max"` // highest sample in the minute
	Load1  float64 `json:"load1"`
	Mem    float64 `json:"mem"` // % used (MemAvailable based)
	MemMax float64 `json:"mem_max"`
	Swap   float64 `json:"swap"`    // % of swap used
	SwapIO float64 `json:"swap_io"` // pages swapped in + out per second
	RX     float64 `json:"rx"`      // bytes per second
	TX     float64 `json:"tx"`
	Disk   float64 `json:"disk"`    // fullest mount, % used
	TCPEst float64 `json:"tcp_est"` // established TCP connections, average
	IO     float64 `json:"io"`      // busiest block device, % of time busy (highest sample)
	OOM    uint64  `json:"oom"`     // OOM kills in the minute
	N      int     `json:"n"`       // samples in the minute
}

type DiskOut struct {
	Mount     string   `json:"mount"`
	FS        string   `json:"fs"`
	Total     uint64   `json:"total"`
	Used      uint64   `json:"used"`
	Pct       float64  `json:"pct"`
	InodesPct float64  `json:"inodes_pct"`
	RateBPH   *float64 `json:"rate_bph,omitempty"`  // bytes per hour over the last 6 h (least squares)
	FullInH   *float64 `json:"full_in_h,omitempty"` // hours until full at that rate
}

type PortOut struct {
	Proto     string `json:"proto"`
	Addr      string `json:"addr"`
	Port      int    `json:"port"`
	Local     bool   `json:"local"` // bound to loopback only
	PID       int    `json:"pid,omitempty"`
	Name      string `json:"name,omitempty"`
	User      string `json:"user,omitempty"`
	Exe       string `json:"exe,omitempty"`       // 0.3.0: the owner's executable
	Unit      string `json:"unit,omitempty"`      // 0.3.0: its systemd unit
	Container string `json:"container,omitempty"` // 0.3.0: Docker container name
	CID       string `json:"cid,omitempty"`       // 0.3.0: container id, 12 hex digits
}

func (p PortOut) Key() string {
	k := p.Proto + "/" + strconv.Itoa(p.Port)
	if p.Local {
		k += "/local"
	}
	return k
}

type MemOut struct {
	Total     uint64  `json:"total"`
	Available uint64  `json:"available"`
	UsedPct   float64 `json:"used_pct"`
	SwapTotal uint64  `json:"swap_total"`
	SwapUsed  uint64  `json:"swap_used"`
	SwapPct   float64 `json:"swap_pct"`
}

// TableOut is a kernel table and how full it is (open files, conntrack entries).
type TableOut struct {
	Used uint64  `json:"used"`
	Max  uint64  `json:"max"`
	Pct  float64 `json:"pct"`
}

func newTable(used, max uint64) *TableOut {
	t := &TableOut{Used: used, Max: max}
	if max > 0 {
		t.Pct = round2(clamp(100*float64(used)/float64(max), 0, 100))
	}
	return t
}

type Current struct {
	At        int64      `json:"at"`
	CPU       float64    `json:"cpu"`
	Load      [3]float64 `json:"load"`
	Mem       MemOut     `json:"mem"`
	Disks     []DiskOut  `json:"disks"`
	RX        float64    `json:"rx"`
	TX        float64    `json:"tx"`
	Procs     int        `json:"procs"`
	TopCPU    []Proc     `json:"top_cpu"`
	TopMem    []Proc     `json:"top_mem"`
	Ports     []PortOut  `json:"ports"`
	TCP       *TCPStates `json:"tcp,omitempty"`
	FD        *TableOut  `json:"fd,omitempty"`
	Conntrack *TableOut  `json:"conntrack,omitempty"` // only when the conntrack module is loaded
	IO        []IOOut    `json:"io,omitempty"`
}

type Signal struct {
	Kind   string         `json:"kind"` // miner | tmp_exe | deleted_exe | cpu_hog
	Key    string         `json:"key"`  // stable across samples and restarts of the process
	At     int64          `json:"at"`   // first seen in this report window
	Detail map[string]any `json:"detail"`
}

type WebRootOut struct {
	Roots    []string    `json:"roots"`
	Files    int         `json:"files"`
	LastScan int64       `json:"last_scan,omitempty"`
	Baseline bool        `json:"baseline,omitempty"` // first scan: nothing to compare with yet
	Changes  *WebChanges `json:"changes,omitempty"`
}

type AgentInfo struct {
	Version   string   `json:"version"`
	Root      bool     `json:"root"`
	UID       int      `json:"uid"`
	Skipped   []string `json:"skipped"`
	Disabled  []string `json:"disabled"` // checks switched off in the config
	SampleS   int      `json:"sample_s"`
	ReportS   int      `json:"report_s"`
	StartedAt int64    `json:"started_at"`
	Panics    int      `json:"panics,omitempty"` // internal errors recovered since start
	// 0.3.0
	Mode    string   `json:"mode,omitempty"` // unprivileged | detailed | root
	Caps    []string `json:"caps,omitempty"` // effective capabilities that matter (dac_read_search, sys_ptrace)
	CPUPct  float64  `json:"cpu_pct"`        // the agent's own CPU, % of one core, average over the window
	CPUPeak float64  `json:"cpu_peak"`       // highest one-sample-interval average in the window
	RSS     int64    `json:"rss"`            // bytes, average of the samples in the window
	RSSMax  int64    `json:"rss_max"`
	WindowS int64    `json:"window_s"`
}

type HostInfo struct {
	Hostname string `json:"hostname"`
	OS       string `json:"os"`
	Kernel   string `json:"kernel"`
	Arch     string `json:"arch"`
	Cores    int    `json:"cores"`
	BootTime int64  `json:"boot_time"`
	UptimeS  int64  `json:"uptime_s"`
	Virt     string `json:"virt,omitempty"`    // 0.3.0: kvm, vmware, docker, lxc, none… ("" unknown)
	Vendor   string `json:"vendor,omitempty"`  // 0.3.0: DMI system vendor
	Product  string `json:"product,omitempty"` // 0.3.0: DMI product name
}

// HealthOut goes in every rollup: what is not a chart but tells whether the machine is well.
type HealthOut struct {
	Units    *UnitsOut   `json:"units,omitempty"` // nil: systemd's unit list could not be read
	OOMKills uint64      `json:"oom_kills"`       // since the previous rollup
	OOMTotal uint64      `json:"oom_total"`       // since boot
	SSH      *SSHAuthOut `json:"ssh,omitempty"`   // nil: no readable auth log
	Time     TimeOut     `json:"time"`
}

type UnitsOut struct {
	Failed []string     `json:"failed"`
	At     int64        `json:"at"`
	Detail []UnitDetail `json:"detail,omitempty"` // 0.3.0: what systemd says about each failed unit
}

// ---------------------------------------------------------------------------
// Collector
// ---------------------------------------------------------------------------

type minuteAgg struct {
	n                                                          int
	cpu, cpuMax, load, mem, memMax, swap, swapIO, rx, tx, disk float64
	tcpEst, io                                                 float64
	oom                                                        uint64
}

type unitsState struct {
	failed []string
	byName map[string]UnitInfo
	at     int64
}

type Collector struct {
	cfg     Config
	version string
	proc    string // /proc
	sys     string // /sys
	host    string // / (root of the other files read)
	users   *UserCache
	procs   *ProcSampler
	started time.Time
	statfs  func(string) (DiskUsage, error)
	busPath string
	units   func(socket string, timeout time.Duration) ([]UnitInfo, error)

	prevT    time.Time
	prevCPU  CPUTimes
	prevNet  NetCounters
	prevVM   VMStat
	havePrev bool

	fill       map[string]*FillTracker
	minutes    map[int64]*minuteAgg
	sampleSig  map[string]Signal  // process signals seen in the latest sample
	hog        map[pkey]time.Time // pid:start -> first sample over hogPercent
	rules      *Rules
	pending    []Event // tripped since the last event message
	io         *IOTracker
	prevTopMem map[pkey]Proc   // pid:start -> the largest processes of the previous sample
	procNames  map[string]bool // process names of the latest sample (time and firewall daemons)
	oomWindow  uint64
	oomTotal   uint64
	clockJumps int

	portInodes map[uint64]bool
	portOwners map[uint64]Owner // filled by the worker (FindOwners), read under mu
	ownersWant bool             // the socket set changed: the worker should look for owners
	docker     *DockerIndex
	load       HostLoad

	// cached between samples: what changes rarely
	mountRaw    []byte
	mounts      []Mount
	sysBlk      map[string]bool
	sysBlkAt    time.Time
	portLo      int
	portHi      int
	portRangeAt time.Time
	hostAt      time.Time
	hostStatic  HostInfo
	netBuf      []byte
	lastProcs   []Proc
	namesGen    int
	namesAt     int
	ident       Identity

	// the agent's own footprint, per rollup window
	self selfStats
	x    extra

	current  Current
	hostInfo HostInfo
	samples  int
	errors   int

	mu       sync.Mutex // guards what the slow-checks goroutine fills in, below
	setuid   []string   // nil until the first scan
	setuidAt int64
	web      *WebRootOut
	webPrev  map[string]string
	webPend  *WebChanges
	unitSt   *unitsState // nil: never read
	unitsErr string
	auth     AuthLog
	authWin  AuthCounts
	authFrom time.Time
	authErr  string // why the auth log is not read ("" when it is)
	posture  *PostureOut
	sshdNoRd bool
	sudoNoRd bool

	cronUnreadable []string
	sshUnreadable  []string
}

func NewCollector(cfg Config, version string) *Collector {
	users := NewUserCache("/etc/passwd")
	sendCmdline = cfg.On("cmdline")
	c := &Collector{
		cfg: cfg, version: version, proc: "/proc", sys: "/sys", host: "/", users: users,
		procs: NewProcSampler("/proc", users), started: time.Now(), statfs: statDisk, busPath: SystemBusPath(), units: ListUnits,
		fill: map[string]*FillTracker{}, minutes: map[int64]*minuteAgg{}, sampleSig: map[string]Signal{},
		hog: map[pkey]time.Time{}, portInodes: map[uint64]bool{}, portOwners: map[uint64]Owner{}, rules: NewRules(),
		io: NewIOTracker(), prevTopMem: map[pkey]Proc{}, procNames: map[string]bool{},
	}
	c.auth.Path = FindAuthLog(c.host, cfg.AuthLog)
	c.authFrom = c.started
	c.docker = NewDockerIndex(c.host)
	c.ident = DetectIdentity(c.proc)
	c.initExtra()
	c.procs.Chunk, c.procs.Pause, c.procs.Sleep = 100, 250*time.Millisecond, time.Sleep
	return c
}

// withRoot points the collector at another tree (tests use testdata/root).
func (c *Collector) withRoot(root string) *Collector {
	c.proc, c.sys, c.host = filepath.Join(root, "proc"), filepath.Join(root, "sys"), root
	c.users = NewUserCache(filepath.Join(root, "etc/passwd"))
	c.procs = NewProcSampler(c.proc, c.users)
	c.auth.Path = FindAuthLog(root, c.cfg.AuthLog)
	c.docker = NewDockerIndex(root)
	c.initExtra()
	return c
}

func (c *Collector) read(name string) ([]byte, error) {
	return readSmall(filepath.Join(c.proc, name), 1<<20)
}

// Sample takes one reading of everything that is sampled every interval.
func (c *Collector) Sample(now time.Time) {
	c.samples++
	ok := true
	var cur Current
	cur.At = now.Unix()

	b, err := c.read("stat")
	ps, perr := ParseProcStat(b)
	if err != nil || perr != nil {
		ok = false
	}
	b, _ = c.read("meminfo")
	mem, merr := ParseMemInfo(b)
	if merr != nil {
		ok = false
	}
	b, _ = c.read("vmstat")
	vm := ParseVMStat(b)
	b, _ = c.read("net/dev")
	netc := ParseNetDev(b)
	b, _ = c.read("loadavg")
	cur.Load, _ = ParseLoadAvg(b)
	b, _ = c.read("uptime")
	up, _ := ParseUptime(b)

	// Intervals use the monotonic clock; a step of the wall clock (NTP correction, manual change,
	// a VM resumed) is only counted and logged.
	elapsed := now.Sub(c.prevT).Seconds()
	if c.havePrev {
		if d := now.Round(0).Sub(c.prevT.Round(0)) - now.Sub(c.prevT); d > clockStep || d < -clockStep {
			c.clockJumps++
			logLimited("clock-jump", time.Hour, slog.LevelWarn, "the wall clock jumped", "by", d.Round(time.Second).String())
		}
	}
	var swapIO float64
	var oomDelta uint64
	if c.havePrev && elapsed > 0 {
		cur.CPU = round1(CPUPercent(c.prevCPU, ps.CPU))
		if dt := float64(ps.CPU.Total()) - float64(c.prevCPU.Total()); dt > 0 && ps.CPU.IOWait >= c.prevCPU.IOWait {
			c.load.Set(cur.Load[0], ps.Cores, 100*float64(ps.CPU.IOWait-c.prevCPU.IOWait)/dt)
		}
		if netc.RX >= c.prevNet.RX {
			cur.RX = float64(netc.RX-c.prevNet.RX) / elapsed
		}
		if netc.TX >= c.prevNet.TX {
			cur.TX = float64(netc.TX-c.prevNet.TX) / elapsed
		}
		if vm.PswpIn >= c.prevVM.PswpIn && vm.PswpOut >= c.prevVM.PswpOut {
			swapIO = float64(vm.PswpIn-c.prevVM.PswpIn+vm.PswpOut-c.prevVM.PswpOut) / elapsed
		}
		if vm.HasOOM && c.prevVM.HasOOM && vm.OOMKill >= c.prevVM.OOMKill {
			oomDelta = vm.OOMKill - c.prevVM.OOMKill
		}
	}
	cur.RX, cur.TX = float64(int64(cur.RX)), float64(int64(cur.TX))
	if vm.HasOOM || !c.havePrev {
		c.prevVM = vm
	} else {
		c.prevVM = VMStat{PswpIn: vm.PswpIn, PswpOut: vm.PswpOut}
	}
	c.prevCPU, c.prevNet, c.prevT, c.havePrev = ps.CPU, netc, now, true
	c.oomTotal = vm.OOMKill

	cur.Mem = MemOut{Total: mem.Total, Available: mem.Available, UsedPct: round1(mem.UsedPct()), SwapTotal: mem.SwapTotal,
		SwapUsed: mem.SwapUsed(), SwapPct: round1(mem.SwapPct())}

	// Kernel tables
	if b, err := c.read("sys/fs/file-nr"); err == nil {
		if used, max, err := ParseFileNR(b); err == nil {
			cur.FD = newTable(used, max)
		}
	}
	if b, err := c.read("sys/net/netfilter/nf_conntrack_count"); err == nil {
		if n, err := ParseUintFile(b); err == nil {
			if b, err := c.read("sys/net/netfilter/nf_conntrack_max"); err == nil {
				if max, err := ParseUintFile(b); err == nil && max > 0 {
					cur.Conntrack = newTable(n, max)
				}
			}
		}
	}

	// Disks
	b, _ = c.read("self/mountinfo")
	if !bytes.Equal(b, c.mountRaw) { // parsed again only when the mounts changed
		c.mountRaw, c.mounts = b, ParseMountInfo(b)
	}
	maxDisk := 0.0
	seenMounts := make(map[string]bool, len(c.mounts))
	mono := now.Sub(c.started).Seconds() // the fill trend is not fooled by a clock step
	for _, m := range c.mounts {
		d, err := c.statfs(m.Point)
		if err != nil || d.Total == 0 {
			continue
		}
		seenMounts[m.Point] = true
		ft := c.fill[m.Point]
		if ft == nil {
			ft = NewFillTracker(fillWindow)
			c.fill[m.Point] = ft
		}
		ft.Add(int64(mono), d.Used)
		o := DiskOut{Mount: m.Point, FS: m.FSType, Total: d.Total, Used: d.Used, Pct: round1(d.UsedPct), InodesPct: round1(d.InodesPct)}
		if r, ok := ft.Rate(); ok {
			r = float64(int64(r))
			o.RateBPH = &r
			if h, ok := ft.FullInHours(d.Avail); ok {
				h = round1(h)
				o.FullInH = &h
			}
		}
		if o.Pct > maxDisk {
			maxDisk = o.Pct
		}
		cur.Disks = append(cur.Disks, o)
		if len(cur.Disks) >= 40 {
			break
		}
	}
	for k := range c.fill {
		if !seenMounts[k] {
			delete(c.fill, k)
		}
	}
	b, _ = c.read("diskstats")
	if c.sysBlk == nil || now.Sub(c.sysBlkAt) > 10*time.Minute || now.Before(c.sysBlkAt) {
		c.sysBlk, c.sysBlkAt = c.sysBlock(), now
	}
	cur.IO = c.io.Update(now, ParseDiskStats(b, wholeDisk(c.sysBlk)))

	// Processes
	c.procs.bootTime = ps.BootTime
	procs := c.procs.Sample(now)
	cur.Procs = len(procs)
	cur.TopCPU = c.withUnits(TopBy(procs, 10, func(p Proc) float64 { return p.CPU }))
	cur.TopMem = c.withUnits(TopBy(procs, 10, func(p Proc) float64 { return float64(p.RSS) }))
	var victims []Proc
	if oomDelta > 0 {
		victims = c.vanished(procs)
	}
	c.rememberTopMem(procs)
	c.processSignals(now, procs)
	c.lastProcs = procs
	c.namesGen++
	c.self.sample(c.proc, now)

	// Sockets: listening ports and TCP connections by state
	var tcp TCPStates
	cur.Ports = c.ports(procs, &tcp)
	cur.TCP = &tcp

	c.current = cur
	c.hostInfo = c.readHost(now, ps, up)

	// Local rules: what trips now goes out in the next event message (and only then).
	c.mu.Lock()
	var failed []string
	unitsKnown := c.unitSt != nil && c.unitsErr == ""
	if unitsKnown {
		failed = c.unitSt.failed
	}
	c.mu.Unlock()
	evs, _ := c.rules.Update(now, Reading{Mem: cur.Mem.UsedPct, SwapIO: swapIO, SwapPct: cur.Mem.SwapPct, Load1: cur.Load[0],
		Cores: ps.Cores, Disks: cur.Disks, TopCPU: cur.TopCPU, TopMem: cur.TopMem, Signals: c.sampleSig,
		OOMKills: oomDelta, OOMTotal: vm.OOMKill, OOMVictims: victims, FD: cur.FD, Conntrack: cur.Conntrack, IO: cur.IO,
		FailedUnits: failed, UnitsKnown: unitsKnown})
	c.pending = append(c.pending, evs...)
	if len(c.pending) > 100 {
		c.pending = c.pending[len(c.pending)-100:]
	}
	c.oomWindow += oomDelta

	if !ok {
		c.errors++
		return
	}
	t := now.Unix() - now.Unix()%60
	a := c.minutes[t]
	if a == nil {
		if len(c.minutes) >= 24*60 { // rollups stopped somehow: keep the memory bounded
			c.minutes = map[int64]*minuteAgg{}
		}
		a = &minuteAgg{}
		c.minutes[t] = a
	}
	worstIO := 0.0
	for _, d := range cur.IO {
		worstIO = maxF(worstIO, d.Util)
	}
	a.n++
	a.cpu += cur.CPU
	a.cpuMax = maxF(a.cpuMax, cur.CPU)
	a.load += cur.Load[0]
	a.mem += cur.Mem.UsedPct
	a.memMax = maxF(a.memMax, cur.Mem.UsedPct)
	a.swap += cur.Mem.SwapPct
	a.swapIO += swapIO
	a.rx += cur.RX
	a.tx += cur.TX
	a.disk = maxF(a.disk, maxDisk)
	a.tcpEst += float64(tcp.Established)
	a.io = maxF(a.io, worstIO)
	a.oom += oomDelta
}

// sysBlock lists /sys/block (whole devices); empty when it cannot be read.
func (c *Collector) sysBlock() map[string]bool {
	ents, err := os.ReadDir(filepath.Join(c.sys, "block"))
	if err != nil {
		return nil
	}
	m := make(map[string]bool, len(ents))
	for _, e := range ents {
		m[e.Name()] = true
	}
	return m
}

// pkey identifies a process across samples (a pid is reused; pid + start time is not).
type pkey struct {
	pid   int
	start uint64
}

func procID(p Proc) pkey { return pkey{p.PID, p.startTime} }

func (c *Collector) rememberTopMem(procs []Proc) {
	top := topRaw(procs, 10, func(p Proc) float64 { return float64(p.RSS) })
	m := make(map[pkey]Proc, len(top))
	for _, p := range top {
		m[procID(p)] = p.Reported()
	}
	c.prevTopMem = m
}

// vanished: the largest processes of the previous sample that are gone now (after an OOM kill,
// probably its victims).
func (c *Collector) vanished(procs []Proc) []Proc {
	live := make(map[pkey]bool, len(procs))
	for _, p := range procs {
		live[procID(p)] = true
	}
	var out []Proc
	for id, p := range c.prevTopMem {
		if !live[id] {
			out = append(out, p)
		}
	}
	sort.Slice(out, func(i, j int) bool { return out[i].RSS > out[j].RSS })
	if len(out) > 3 {
		out = out[:3]
	}
	return out
}

func (c *Collector) readHost(now time.Time, ps ProcStat, up float64) HostInfo {
	if c.hostStatic.Kernel == "" || now.Sub(c.hostAt) > 10*time.Minute || now.Before(c.hostAt) {
		c.hostStatic, c.hostAt = c.readHostStatic(), now
	}
	h := c.hostStatic
	h.Arch, h.Cores, h.BootTime, h.UptimeS = runtime.GOARCH, ps.Cores, ps.BootTime, int64(up)
	return h
}

func (c *Collector) readHostStatic() HostInfo {
	var h HostInfo
	v := DetectVirt(c.host, c.proc, c.sys)
	h.Virt, h.Vendor, h.Product = v.Virt, v.Vendor, v.Product
	if b, err := c.read("sys/kernel/hostname"); err == nil {
		h.Hostname = truncate(strings.TrimSpace(string(b)), 100)
	}
	if b, err := c.read("sys/kernel/osrelease"); err == nil {
		h.Kernel = truncate(strings.TrimSpace(string(b)), 100)
	}
	if b, err := readSmall(filepath.Join(c.host, "etc/os-release"), 64*1024); err == nil {
		h.OS = ParseOSRelease(b)
	} else if b, err := readSmall(filepath.Join(c.host, "usr/lib/os-release"), 64*1024); err == nil {
		h.OS = ParseOSRelease(b)
	}
	return h
}

func (c *Collector) processSignals(now time.Time, procs []Proc) {
	c.sampleSig = map[string]Signal{}
	live := make(map[pkey]bool, len(procs))
	for _, p := range procs {
		if p.kernel || p.PID == os.Getpid() {
			continue
		}
		// Crypto miner by name or arguments
		why := p.minerWhy
		if !p.sigDone {
			why = sigs.MinerMatch(p.Name, p.argv0, p.cmdRaw)
		}
		if why != "" {
			c.addSignal(now, "miner", "miner:"+strings.ToLower(p.Name)+":"+exeOrArgv0(p), p, map[string]any{"match": why})
		}
		// Executable in a temporary directory, or deleted after start
		exe := strings.TrimSuffix(p.Exe, " (deleted)")
		switch {
		case p.exeKnown && InTmp(exe):
			c.addSignal(now, "tmp_exe", "tmp_exe:"+exe, p, map[string]any{"source": "exe"})
		case !p.exeKnown && strings.HasPrefix(p.argv0, "/") && InTmp(p.argv0):
			c.addSignal(now, "tmp_exe", "tmp_exe:"+p.argv0, p, map[string]any{"source": "argv0"})
		}
		if p.exeKnown && SuspiciousDeleted(p.Exe) {
			c.addSignal(now, "deleted_exe", "deleted_exe:"+exe, p, map[string]any{"source": "exe"})
		}
		// Sustained CPU by one process
		id := procID(p)
		live[id] = true
		if p.CPU >= hogPercent {
			first, seen := c.hog[id]
			if !seen {
				c.hog[id] = now
				first = now
			}
			if now.Sub(first) >= hogDuration && !sigs.Busy[strings.ToLower(p.Name)] {
				c.addSignal(now, "cpu_hog", "cpu_hog:"+strings.ToLower(p.Name), p,
					map[string]any{"minutes": int(now.Sub(first).Minutes()), "since": first.Unix()})
			}
		} else {
			delete(c.hog, id)
		}
	}
	for id := range c.hog {
		if !live[id] {
			delete(c.hog, id)
		}
	}
}

func exeOrArgv0(p Proc) string {
	if p.exeKnown {
		return strings.TrimSuffix(p.Exe, " (deleted)")
	}
	return p.argv0
}

func (c *Collector) addSignal(now time.Time, kind, key string, p Proc, detail map[string]any) {
	if len(c.sampleSig) >= 50 {
		return
	}
	key = truncate(key, 300)
	detail["process"] = p.Reported()
	c.sampleSig[key] = Signal{Kind: kind, Key: key, At: now.Unix(), Detail: detail}
}

// localPortRange: UDP sockets bound to a port in this range are clients (resolvers,
// time sync), not services.
func (c *Collector) localPortRange() (int, int) {
	if c.portLo > 0 && time.Since(c.portRangeAt) < 10*time.Minute {
		return c.portLo, c.portHi
	}
	c.portLo, c.portHi = c.readPortRange()
	c.portRangeAt = time.Now()
	return c.portLo, c.portHi
}

func (c *Collector) readPortRange() (int, int) {
	b, err := c.read("sys/net/ipv4/ip_local_port_range")
	if err == nil {
		f := strings.Fields(string(b))
		if len(f) == 2 {
			lo, e1 := strconv.Atoi(f[0])
			hi, e2 := strconv.Atoi(f[1])
			if e1 == nil && e2 == nil {
				return lo, hi
			}
		}
	}
	return 32768, 60999
}

func (c *Collector) ports(procs []Proc, tcp *TCPStates) []PortOut {
	var socks []Socket
	if c.netBuf == nil {
		c.netBuf = make([]byte, 64*1024)
	}
	for _, f := range [][2]string{{"net/tcp", "tcp"}, {"net/tcp6", "tcp"}, {"net/udp", "udp"}, {"net/udp6", "udp"}} {
		fh, err := os.Open(filepath.Join(c.proc, f[0]))
		if err != nil {
			continue
		}
		var st *TCPStates
		if f[1] == "tcp" {
			st = tcp
		}
		socks = scanNetFast(fh, f[1], maxNetLines, st, c.netBuf, socks)
		fh.Close()
	}
	lo, hi := c.localPortRange()
	inodes := map[uint64]bool{}
	var kept []Socket
	for _, s := range socks {
		if s.Proto == "udp" && s.Port >= lo && s.Port <= hi {
			continue
		}
		kept = append(kept, s)
		if s.Inode != 0 {
			inodes[s.Inode] = true
		}
	}
	if !sameSet(inodes, c.portInodes) {
		c.portInodes = inodes
		c.ownersWant = true // the worker looks for the owners (FindOwners)
	}
	c.mu.Lock()
	owners := c.portOwners
	c.mu.Unlock()
	seen := map[string]bool{}
	var out []PortOut
	for _, s := range kept {
		po := PortOut{Proto: s.Proto, Addr: s.Addr, Port: s.Port, Local: s.Local()}
		if o, ok := owners[s.Inode]; ok && o.PID > 0 {
			po.PID, po.Name, po.User, po.Exe, po.Unit, po.Container, po.CID = o.PID, o.Name, o.User, o.Exe, o.Unit, o.Container, o.CID
			if po.User == "" {
				po.User = c.users.Name(s.UID)
			}
		} else {
			po.User = c.users.Name(s.UID)
		}
		k := po.Key() + "|" + po.Addr
		if seen[k] {
			continue
		}
		seen[k] = true
		out = append(out, po)
	}
	sort.Slice(out, func(i, j int) bool {
		if out[i].Proto != out[j].Proto {
			return out[i].Proto < out[j].Proto
		}
		if out[i].Port != out[j].Port {
			return out[i].Port < out[j].Port
		}
		return out[i].Addr < out[j].Addr
	})
	if len(out) > 200 {
		out = out[:200]
	}
	return out
}

func sameSet(a, b map[uint64]bool) bool {
	if len(a) != len(b) {
		return false
	}
	for k := range a {
		if !b[k] {
			return false
		}
	}
	return true
}

// ---------------------------------------------------------------------------
// Slow work, on its own goroutine: setuid scan (6 h), web roots (15 min), systemd units and the
// auth log (every heartbeat interval), the posture check (hourly).
// ---------------------------------------------------------------------------

func (c *Collector) ScanSetuidNow(g *Gentle) {
	if !c.cfg.On("setuid") {
		return
	}
	items, _ := ScanSetuid(setuidRoots, 200000, g)
	if len(items) > 500 {
		items = items[:500]
	}
	if items == nil {
		items = []string{}
	}
	c.mu.Lock()
	c.setuid, c.setuidAt = items, time.Now().Unix()
	c.mu.Unlock()
}

// CheckUnits reads systemd's unit list over D-Bus.
func (c *Collector) CheckUnits(now time.Time) {
	if !c.cfg.On("systemd") {
		return
	}
	list, err := c.units(c.busPath, 3*time.Second)
	c.mu.Lock()
	defer c.mu.Unlock()
	if err != nil {
		if c.unitsErr == "" {
			slog.Info("systemd unit list not available; failed units are not checked", "err", err)
		}
		c.unitsErr = truncate(err.Error(), 200)
		return
	}
	st := &unitsState{byName: make(map[string]UnitInfo, len(list)), at: now.Unix(), failed: []string{}}
	for _, u := range list {
		st.byName[u.Name] = u
		if u.ActiveState == "failed" && len(st.failed) < maxUnitRules {
			st.failed = append(st.failed, truncate(u.Name, 120))
		}
	}
	sort.Strings(st.failed)
	c.unitSt, c.unitsErr = st, ""
	failed := st.failed
	c.mu.Unlock()
	c.unitDetails(now, list, failed)
	c.mu.Lock()
}

// ReadAuthLog adds what was appended to the auth log since the previous look.
func (c *Collector) ReadAuthLog() {
	if !c.cfg.On("auth_log") {
		return
	}
	c.mu.Lock()
	defer c.mu.Unlock()
	if c.auth.Path == "" {
		c.auth.Path = FindAuthLog(c.host, c.cfg.AuthLog)
		if c.auth.Path == "" {
			c.authErr = "missing"
			return
		}
	}
	got, err := c.auth.Read()
	switch {
	case err == nil:
		c.authErr = ""
		c.authWin.add(got)
		c.saveLoginState()
	case os.IsPermission(err):
		c.authErr = "unreadable"
	case os.IsNotExist(err):
		c.authErr = "missing"
		c.auth = AuthLog{} // look for it again next time
	default:
		c.authErr = "error"
		logLimited("auth-log", time.Hour, slog.LevelWarn, "cannot read the auth log", "path", c.auth.Path, "err", err)
	}
}

// PostureInput is what the posture check needs from the sampling loop, copied there so that the
// slow goroutine never reads the sampler's state.
type PostureInput struct {
	Ports     []PortOut
	ProcNames map[string]bool
	Kernel    string
	BootTime  int64
	Procs     []procRef
}

// PostureSnapshot is taken on the sampling goroutine (each sample replaces these, never mutates them).
func (c *Collector) PostureSnapshot() PostureInput {
	return PostureInput{Ports: c.current.Ports, ProcNames: c.procNameSet(), Kernel: c.hostInfo.Kernel, BootTime: c.hostInfo.BootTime,
		Procs: c.procRefs()}
}

// CheckPosture runs the security settings check.
func (c *Collector) CheckPosture(now time.Time, in PostureInput) {
	if !c.cfg.On("posture") {
		return
	}
	c.mu.Lock()
	var units map[string]UnitInfo
	if c.unitSt != nil && c.unitsErr == "" {
		units = c.unitSt.byName
	}
	c.mu.Unlock()
	p := &PostureOut{At: now.Unix()}
	p.SSH = ReadSSHD(c.host)
	if p.SSH != nil {
		p.SSH.Running = in.ProcNames["sshd"]
	}
	p.Firewall = DetectFirewall(c.host, units, in.ProcNames)
	p.AutoUpd = DetectAutoUpdates(c.host, units)
	p.UID0 = ExtraUID0(c.users.All())
	p.Sudo = ReadSudoers(c.host)
	p.PublicDB = PublicDBPorts(in.Ports)
	p.Reboot = CheckReboot(c.host, in.Kernel, in.BootTime)
	p.Updates = ReadUpdates(c.host)
	// 0.3.0
	users := c.users.All()
	var keys map[string]KeyCount
	keysOK := false
	if c.cfg.On("ssh_keys") {
		keys, keysOK = CountSSHKeys(c.host, users)
		if p.SSH != nil {
			p.SSH.Keys = keyList(keys)
		}
	}
	p.Fail2ban = ReadFail2ban(c.host, in.ProcNames)
	c.mu.Lock()
	last := c.x.last
	c.mu.Unlock()
	p.Users = BuildUsers(c.host, users, p.Sudo, last, keys, keysOK)
	p.FW = ReadFirewallDetail(c.host, units, in.ProcNames, p.Firewall)
	p.Pkg = ReadPkg(c.host)
	c.mu.Lock()
	c.posture = p
	c.sshdNoRd = p.SSH != nil && !p.SSH.Readable
	c.sudoNoRd = !p.Sudo.Readable
	c.mu.Unlock()
}

// Posture returns the latest security settings check (nil before the first one).
func (c *Collector) Posture() *PostureOut {
	c.mu.Lock()
	defer c.mu.Unlock()
	return c.posture
}

// Health closes the health window: units, OOM kills and SSH failures since the previous rollup.
func (c *Collector) Health(now time.Time, reset bool) HealthOut {
	c.mu.Lock()
	defer c.mu.Unlock()
	h := HealthOut{OOMKills: c.oomWindow, OOMTotal: c.oomTotal}
	if c.unitSt != nil && c.unitsErr == "" {
		h.Units = &UnitsOut{Failed: c.unitSt.failed, At: c.unitSt.at, Detail: c.unitDetailList(c.unitSt.failed, reset)}
	}
	if c.cfg.On("auth_log") && c.authErr == "" && c.auth.started {
		h.SSH = c.authWin.Out(filepath.Base(c.auth.Path), int64(now.Sub(c.authFrom).Seconds()))
	}
	var units map[string]UnitInfo
	if c.unitSt != nil && c.unitsErr == "" {
		units = c.unitSt.byName
	}
	h.Time = TimeSync(c.host, units, c.procNameSet())
	h.Time.Jumps = c.clockJumps
	if reset {
		c.oomWindow = 0
		c.authWin = AuthCounts{}
		c.authFrom = now
	}
	return h
}

func (c *Collector) webStatePath() string { return filepath.Join(c.cfg.StateDir, "webroot.json") }

// LoadWebState reads the previous web root scan (kept across restarts).
func (c *Collector) LoadWebState() {
	b, err := readSmall(c.webStatePath(), 32<<20)
	if err != nil {
		return
	}
	var m map[string]string
	if json.Unmarshal(b, &m) == nil {
		c.mu.Lock()
		c.webPrev = m
		c.mu.Unlock()
	}
}

// ScanWebNow compares the web roots with the previous scan. persist=false (the `check`
// command, --dry-run) leaves the saved state alone.
func (c *Collector) ScanWebNow(persist bool, g *Gentle) {
	if len(c.cfg.WebRoots) == 0 {
		return
	}
	files, truncated := ScanWebRoots(c.cfg.WebRoots, c.cfg.WebRootExclude, c.cfg.WebRootMaxFiles, g)
	c.mu.Lock()
	defer c.mu.Unlock()
	out := &WebRootOut{Roots: c.cfg.WebRoots, Files: len(files), LastScan: time.Now().Unix()}
	if c.webPrev == nil {
		out.Baseline = true
	} else {
		ch := DiffWebRoots(c.webPrev, files, 50)
		ch.Truncated = truncated
		if !ch.Empty() {
			c.webPend = mergeWeb(c.webPend, &ch)
		}
	}
	c.web = out
	c.webPrev = files
	if persist {
		if b, err := json.Marshal(files); err == nil {
			if err := writeFileAtomic(c.webStatePath(), b, 0o600); err != nil {
				logLimited("web-state", time.Hour, slog.LevelWarn, "cannot save the web root state", "err", err)
			}
		}
	}
}

func mergeWeb(a, b *WebChanges) *WebChanges {
	if a == nil {
		return b
	}
	m := *a
	m.Added = capList(dedupSorted(sortedCopy(append(m.Added, b.Added...))), 50)
	m.Changed = capList(dedupSorted(sortedCopy(append(m.Changed, b.Changed...))), 50)
	m.Removed = capList(dedupSorted(sortedCopy(append(m.Removed, b.Removed...))), 50)
	m.NAdded += b.NAdded
	m.NChanged += b.NChanged
	m.NRemoved += b.NRemoved
	m.Files = b.Files
	m.Truncated = m.Truncated || b.Truncated
	return &m
}

func capList(xs []string, n int) []string {
	if len(xs) > n {
		return xs[:n]
	}
	return xs
}

// ---------------------------------------------------------------------------
// Building the report
// ---------------------------------------------------------------------------

// Skipped names the checks this agent cannot do here, with its permissions or on this system.
func (c *Collector) Skipped() []string {
	var s []string
	if !c.ident.SeesOthers() {
		s = append(s, "exe_paths_other_users", "port_owners_other_users")
	}
	if len(c.cronUnreadable) > 0 {
		s = append(s, "crontabs_unreadable")
	}
	if len(c.sshUnreadable) > 0 {
		s = append(s, "ssh_keys_unreadable")
	}
	if len(c.cfg.WebRoots) == 0 {
		s = append(s, "web_roots_not_configured")
	}
	c.mu.Lock()
	defer c.mu.Unlock()
	if c.cfg.On("systemd") && c.unitsErr != "" {
		s = append(s, "systemd_units_unavailable")
	}
	if c.cfg.On("auth_log") {
		switch c.authErr {
		case "unreadable":
			s = append(s, "auth_log_unreadable")
		case "missing":
			s = append(s, "auth_log_missing")
		}
	}
	if c.cfg.On("posture") && c.sshdNoRd {
		s = append(s, "sshd_config_unreadable")
	}
	if c.cfg.On("posture") && c.sudoNoRd {
		s = append(s, "sudoers_unreadable")
	}
	if c.cfg.On("logins") {
		switch c.x.wtmpErr {
		case "unreadable":
			s = append(s, "wtmp_unreadable")
		case "missing":
			s = append(s, "wtmp_missing")
		}
	}
	if c.cfg.On("unit_logs") && c.x.syslogErr == "unreadable" {
		s = append(s, "syslog_unreadable")
	}
	return s
}

// hashJSON: a short hash of a value's JSON (to send something only when it changed).
func hashJSON(v any) string {
	b, _ := json.Marshal(v)
	sum := sha256.Sum256(b)
	return hex.EncodeToString(sum[:8])
}

func (c *Collector) Disabled() []string {
	out := []string{}
	for k := range c.cfg.Disabled {
		out = append(out, k)
	}
	sort.Strings(out)
	return out
}

func maxF(a, b float64) float64 {
	if a > b {
		return a
	}
	return b
}

func round2(v float64) float64 { return float64(int64(v*100+0.5)) / 100 }

func writeFileAtomic(path string, b []byte, mode os.FileMode) error {
	if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
		return err
	}
	tmp := path + ".tmp"
	f, err := os.OpenFile(tmp, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, mode)
	if err != nil {
		return err
	}
	if _, err := f.Write(b); err != nil {
		f.Close()
		os.Remove(tmp)
		return err
	}
	if err := f.Close(); err != nil {
		os.Remove(tmp)
		return err
	}
	return os.Rename(tmp, path)
}
