fix: 1.4.4 read Traefik text logs, degraded source alert, mobile menu
Release / Publish tags (push) Successful in 16s
Release / Plan (reuse the published image?) (push) Successful in 5s
Release / ci (push) Successful in 1m46s
Release / Build image (amd64) (push) Successful in 21s
Release / Build image (arm64) (push) Successful in 23s
CI / Format and vet (push) Successful in 24s
CI / Tests (Linux, race detector) (push) Successful in 1m22s
CI / Dependency audit (push) Successful in 27s
CI / Docker image (build and smoke test) (push) Skipped
Release / Gitea release (push) Successful in 4s
Release / Publish tags (push) Successful in 16s
Release / Plan (reuse the published image?) (push) Successful in 5s
Release / ci (push) Successful in 1m46s
Release / Build image (amd64) (push) Successful in 21s
Release / Build image (arm64) (push) Successful in 23s
CI / Format and vet (push) Successful in 24s
CI / Tests (Linux, race detector) (push) Successful in 1m22s
CI / Dependency audit (push) Successful in 27s
CI / Docker image (build and smoke test) (push) Skipped
Release / Gitea release (push) Successful in 4s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
50cd2bd3ce
commit
0dc3268b29
13 files changed
+297
-9
No files matched your search
@@ -1,10 +1,13 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/core"
|
||||
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/geo"
|
||||
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/store"
|
||||
"io"
|
||||
@@ -46,6 +49,11 @@ func Doctor(c Config, w io.Writer, asJSON bool) bool {
|
||||
fi, _ := f.Stat()
|
||||
f.Close()
|
||||
add("source "+s.name, "ok", fmt.Sprintf("%s readable (%d bytes)", s.path, fi.Size()))
|
||||
if s.name == "traefik" {
|
||||
if bad, n := sampleMalformed(s.path); n >= winMin && bad*5 > n {
|
||||
add("source traefik format", "warn", fmt.Sprintf("degraded: %d of the last %d lines are malformed: %s", bad, n, degradedMsg))
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := os.MkdirAll(c.DataDir, 0o700); err != nil {
|
||||
add("data-dir", "fail", err.Error())
|
||||
@@ -134,3 +142,45 @@ func doctorState(c Config, add func(name, status, detail string)) map[string]any
|
||||
add("state", "ok", fmt.Sprintf("%d IPs, %d domains, %d requests in 7 days", st.IPCount(), len(tr.Domains), tr.Requests))
|
||||
return info
|
||||
}
|
||||
|
||||
// sampleMalformed parses up to the last winSize lines of a Traefik log and
|
||||
// counts the malformed ones.
|
||||
func sampleMalformed(path string) (bad, n int) {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return 0, 0
|
||||
}
|
||||
defer f.Close()
|
||||
const tailBytes = 512 << 10
|
||||
off := int64(0)
|
||||
if fi, err := f.Stat(); err == nil && fi.Size() > tailBytes {
|
||||
off = fi.Size() - tailBytes
|
||||
}
|
||||
if _, err := f.Seek(off, io.SeekStart); err != nil {
|
||||
return 0, 0
|
||||
}
|
||||
b, err := io.ReadAll(io.LimitReader(f, tailBytes))
|
||||
if err != nil {
|
||||
return 0, 0
|
||||
}
|
||||
lines := bytes.Split(b, []byte("\n"))
|
||||
if off > 0 && len(lines) > 0 {
|
||||
lines = lines[1:] // first line is cut
|
||||
}
|
||||
if len(lines) > 0 && len(lines[len(lines)-1]) == 0 {
|
||||
lines = lines[:len(lines)-1]
|
||||
}
|
||||
if len(lines) > winSize {
|
||||
lines = lines[len(lines)-winSize:]
|
||||
}
|
||||
for _, l := range lines {
|
||||
if len(bytes.TrimSpace(l)) == 0 {
|
||||
continue
|
||||
}
|
||||
n++
|
||||
if _, err := core.ParseTraefik(l); err != nil && !errors.Is(err, core.ErrIgnored) {
|
||||
bad++
|
||||
}
|
||||
}
|
||||
return bad, n
|
||||
}
|
||||
+53
-1
@@ -23,6 +23,8 @@ type source struct {
|
||||
parse func([]byte) (core.Event, error)
|
||||
|
||||
lines, malformed atomic.Uint64
|
||||
win window
|
||||
lastWarn atomic.Int64
|
||||
lastEvent atomic.Int64
|
||||
backfilling atomic.Bool
|
||||
fol atomic.Pointer[tail.Follower]
|
||||
@@ -32,6 +34,44 @@ func traefikParse(b []byte) (core.Event, error) { return core.ParseTraefik(b) }
|
||||
func authParse(b []byte) (core.Event, error) { return core.ParseAuth(string(b)) }
|
||||
func f2bParse(b []byte) (core.Event, error) { return core.ParseFail2ban(string(b)) }
|
||||
|
||||
const (
|
||||
winSize = 1000
|
||||
winMin = 50 // fewer samples than this never mark a source degraded
|
||||
degradedMsg = "formato inesperado (JSON esperado?)"
|
||||
)
|
||||
|
||||
// window remembers whether each of the last winSize lines was malformed.
|
||||
type window struct {
|
||||
mu sync.Mutex
|
||||
ring [winSize]bool
|
||||
n int // lines recorded so far, capped at winSize
|
||||
pos int
|
||||
bad int
|
||||
}
|
||||
|
||||
func (w *window) add(bad bool) {
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
if w.n == winSize && w.ring[w.pos] {
|
||||
w.bad--
|
||||
}
|
||||
w.ring[w.pos] = bad
|
||||
if bad {
|
||||
w.bad++
|
||||
}
|
||||
w.pos = (w.pos + 1) % winSize
|
||||
if w.n < winSize {
|
||||
w.n++
|
||||
}
|
||||
}
|
||||
|
||||
// degraded reports whether more than 20% of the recent lines were malformed.
|
||||
func (w *window) degraded() bool {
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
return w.n >= winMin && w.bad*5 > w.n
|
||||
}
|
||||
|
||||
func (s *source) status() store.SourceStatus {
|
||||
st := store.SourceStatus{Name: s.name, Path: s.path, Lines: s.lines.Load(), Malformed: s.malformed.Load(),
|
||||
LastEvent: s.lastEvent.Load(), State: "following"}
|
||||
@@ -55,6 +95,9 @@ func (s *source) status() store.SourceStatus {
|
||||
}
|
||||
}
|
||||
}
|
||||
if st.State == "following" && s.win.degraded() {
|
||||
st.State, st.Error = "degraded", degradedMsg
|
||||
}
|
||||
return st
|
||||
}
|
||||
|
||||
@@ -148,11 +191,20 @@ func (r *Runner) handle(s *source) func([]byte) {
|
||||
s.lines.Add(1)
|
||||
ev, err := s.parse(line)
|
||||
if err != nil {
|
||||
if !errors.Is(err, core.ErrIgnored) {
|
||||
bad := !errors.Is(err, core.ErrIgnored)
|
||||
if bad {
|
||||
s.malformed.Add(1)
|
||||
}
|
||||
s.win.add(bad)
|
||||
if bad && s.win.degraded() {
|
||||
now := time.Now().Unix()
|
||||
if last := s.lastWarn.Load(); now-last >= 3600 && s.lastWarn.CompareAndSwap(last, now) {
|
||||
r.Log.Warn("many lines of this source are malformed", "source", s.name, "detail", degradedMsg)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
s.win.add(false)
|
||||
if core.Ignored(r.Ignore, ev.IP) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -257,3 +257,25 @@ func TestEgressExtraAllow(t *testing.T) {
|
||||
t.Fatalf("%v", s)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSourceDegradedWindow(t *testing.T) {
|
||||
var w window
|
||||
for i := 0; i < 100; i++ {
|
||||
w.add(i%10 == 0)
|
||||
}
|
||||
if w.degraded() {
|
||||
t.Fatal("10% must not degrade")
|
||||
}
|
||||
for i := 0; i < 400; i++ {
|
||||
w.add(true)
|
||||
}
|
||||
if !w.degraded() {
|
||||
t.Fatal("expected degraded")
|
||||
}
|
||||
for i := 0; i < winSize; i++ {
|
||||
w.add(false)
|
||||
}
|
||||
if w.degraded() {
|
||||
t.Fatal("window must recover")
|
||||
}
|
||||
}
|
||||
@@ -2,4 +2,4 @@ package app
|
||||
|
||||
// Version is the release version (SemVer). Release builds may override it with
|
||||
// -ldflags "-X .../internal/app.Version=...".
|
||||
var Version = "1.4.3"
|
||||
var Version = "1.4.4"
|
||||
Reference in new issue
Block a user