package server import ( "net/http" "sort" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/store" ) type integritySource struct { Name string `json:"name"` State string `json:"state"` Lines uint64 `json:"lines"` Malformed uint64 `json:"malformed"` Last int64 `json:"last"` } type integrityHost struct { Name string `json:"name"` Online bool `json:"online"` LastEvent int64 `json:"last_event"` LagS int64 `json:"lag_s"` Hourly []uint32 `json:"hourly"` Gaps []store.Gap `json:"gaps"` Sources []integritySource `json:"sources"` ParseErrorRate float64 `json:"parse_error_rate"` } type integrityPeer struct { Name string `json:"name"` Online bool `json:"online"` Live bool `json:"live"` LastSync int64 `json:"last_sync"` } type integrityGeo struct { CityBuild int64 `json:"city_build"` ASNBuild int64 `json:"asn_build"` State string `json:"state"` LastUpdate int64 `json:"last_update"` Error string `json:"error,omitempty"` } type integrityOut struct { Range store.RangeInfo `json:"range"` FromHour int64 `json:"from_hour"` Hosts []integrityHost `json:"hosts"` Peers []integrityPeer `json:"peers"` EventLog store.LogStats `json:"eventlog"` Geo integrityGeo `json:"geo"` } func init() { registerUserRoute("GET /api/v1/integrity", func(s *Server) http.HandlerFunc { return s.integrity }) } // integrity answers "is the data complete?": events ingested per hour and // host, silences, source and peer states, parse errors, event log and geo base. func (s *Server) integrity(w http.ResponseWriter, r *http.Request) { rg, ok := s.rangeOf(w, r) if !ok { return } host := r.URL.Query().Get("host") if host == "" { host = "all" } if !s.knownHost(w, host) { return } now := s.opt.Now().Unix() info := rg.Info(now) parts, all := s.parts(r.Context(), rg, host) fromH, toH := info.From/3600, info.To/3600 if toH-fromH > 24*31-1 { fromH = toH - (24*31 - 1) } out := integrityOut{Range: info, FromHour: fromH, Hosts: []integrityHost{}, Peers: []integrityPeer{}} seen := map[string]bool{} for _, p := range parts { name := p.Host if name == "" && len(p.Hosts) > 0 { name = p.Hosts[0].Name } seen[name] = true hh := integrityHost{Name: name, Online: true, Sources: []integritySource{}} hh.Hourly = store.HourlyFromTimeline(p.Timeline, fromH, toH) hh.Gaps = store.FindGaps(hh.Hourly, fromH) var lines, bad uint64 for _, src := range p.Sources { if src.Name == "geo" { if name == s.opt.Store.Name() { out.Geo = integrityGeo{State: src.State, LastUpdate: src.LastEvent, Error: src.Error} if store.GeoBuilds != nil { out.Geo.CityBuild, out.Geo.ASNBuild = store.GeoBuilds() } } continue } hh.Sources = append(hh.Sources, integritySource{Name: src.Name, State: src.State, Lines: src.Lines, Malformed: src.Malformed, Last: src.LastEvent}) lines += src.Lines bad += src.Malformed if src.LastEvent > hh.LastEvent { hh.LastEvent = src.LastEvent } } if lines+bad > 0 { hh.ParseErrorRate = float64(bad) / float64(lines+bad) } if hh.LastEvent > 0 { hh.LagS = now - hh.LastEvent if hh.LagS < 0 { hh.LagS = 0 } } out.Hosts = append(out.Hosts, hh) } for _, hi := range all { if hi.Self { continue } out.Peers = append(out.Peers, integrityPeer{Name: hi.Name, Online: hi.Online, Live: hi.Live, LastSync: hi.LastSeen}) if !seen[hi.Name] && (host == "all" || host == hi.Name) { out.Hosts = append(out.Hosts, integrityHost{Name: hi.Name, Online: hi.Online, LastEvent: hi.LastSeen, Hourly: make([]uint32, toH-fromH+1), Gaps: []store.Gap{}, Sources: []integritySource{}}) } } sort.SliceStable(out.Hosts, func(i, j int) bool { return out.Hosts[i].Name < out.Hosts[j].Name }) sort.SliceStable(out.Peers, func(i, j int) bool { return out.Peers[i].Name < out.Peers[j].Name }) out.EventLog = s.opt.Store.LogStats() writeJSON(w, 200, out) }