rename and move a few things

This commit is contained in:
davidnewhall2 2020-06-25 04:24:57 -07:00
parent ee90410d8f
commit 6ab0da0cf6
7 changed files with 38 additions and 40 deletions

View File

@ -40,7 +40,7 @@ func (l *Loki) httpClient() *Client {
} }
// Post marshals and posts a batch of log messages. // Post marshals and posts a batch of log messages.
func (c *Client) Post(logs LogStreams) error { func (c *Client) Post(logs interface{}) error {
msg, err := json.Marshal(logs) msg, err := json.Marshal(logs)
if err != nil { if err != nil {
return err return err

View File

@ -94,25 +94,34 @@ func (l *Loki) PollController() {
ticker := time.NewTicker(interval) ticker := time.NewTicker(interval)
for start := range ticker.C { for start := range ticker.C {
if err := l.pollController(start); err != nil { events, err := l.Events(&poller.Filter{Name: InputName})
if err != nil {
l.LogErrorf("event fetch for Loki failed: %v", err)
continue
}
err = l.ProcessEvents(l.NewReport(), events, start)
if err != nil {
l.LogErrorf("%v", err) l.LogErrorf("%v", err)
} }
} }
} }
// pollController offloads the loop from PollController. // ProcessEvents offloads some of the loop from PollController.
func (l *Loki) pollController(start time.Time) error { func (l *Loki) ProcessEvents(report *Report, events *poller.Events, start time.Time) error {
events, err := l.Events(&poller.Filter{Name: InputName}) // Sometimes it gets stuck on old messages. This gets it past that.
if err != nil { if time.Since(l.last) > 4*l.Interval.Duration {
return errors.Wrap(err, "event fetch for Loki failed") l.last = time.Now().Add(-4 * l.Interval.Duration)
} }
report := &Report{ report.ProcessEventLogs(events)
Start: start,
Logger: l.Collect, if err := l.client.Post(report.Logs); err != nil {
Client: l.client, return errors.Wrap(err, "sending to Loki failed")
Last: &l.last,
} }
return report.Execute(events, 4*l.Interval.Duration) // nolint: gomnd l.last = start
report.LogOutput(l.last)
return nil
} }

View File

@ -4,7 +4,6 @@ import (
"strings" "strings"
"time" "time"
"github.com/pkg/errors"
"github.com/unifi-poller/poller" "github.com/unifi-poller/poller"
"github.com/unifi-poller/unifi" "github.com/unifi-poller/unifi"
) )
@ -16,41 +15,31 @@ type LogStream struct {
Entries [][]string `json:"values"` // "the log lines" Entries [][]string `json:"values"` // "the log lines"
} }
// LogStreams is the main logs-holding structure. // Logs is the main logs-holding structure.
type LogStreams struct { type Logs struct {
Streams []LogStream `json:"streams"` // "multiple files" Streams []LogStream `json:"streams"` // "multiple files"
} }
// Report is the temporary data generated and sent to Loki at every interval. // Report is the temporary data generated and sent to Loki at every interval.
type Report struct { type Report struct {
Logs
Counts map[string]int Counts map[string]int
Start time.Time Oldest time.Time
Last *time.Time
Client *Client
LogStreams
poller.Logger poller.Logger
} }
// Execute processes events, reports events to Loki, updates last check time, and prints a log message. // NewReport makes a new report.
func (r *Report) Execute(events *poller.Events, skipDur time.Duration) error { func (l *Loki) NewReport() *Report {
// Sometimes it gets stuck on old messages. This gets it past that. return &Report{
if time.Since(*r.Last) > skipDur { Logger: l.Collect,
*r.Last = time.Now().Add(-skipDur) Oldest: l.last,
}
} }
r.ProcessEventLogs(events) // Compile report. func (r *Report) LogOutput(start time.Time) {
// Send report to Loki.
if err := r.Client.Post(r.LogStreams); err != nil {
return errors.Wrap(err, "sending to Loki failed")
}
*r.Last = r.Start
r.Logf("Events sent to Loki. Event: %d, IDS: %d, Alarm: %d, Anomaly: %d, Dur: %v", r.Logf("Events sent to Loki. Event: %d, IDS: %d, Alarm: %d, Anomaly: %d, Dur: %v",
r.Counts[typeEvent], r.Counts[typeIDS], r.Counts[typeAlarm], r.Counts[typeAnomaly], r.Counts[typeEvent], r.Counts[typeIDS], r.Counts[typeAlarm], r.Counts[typeAnomaly],
time.Since(r.Start).Round(time.Millisecond)) time.Since(start).Round(time.Millisecond))
return nil
} }
// ProcessEventLogs loops the event Logs, matches the interface // ProcessEventLogs loops the event Logs, matches the interface

View File

@ -10,7 +10,7 @@ const typeAlarm = "alarm"
// Alarm stores a structured Alarm for batch sending to Loki. // Alarm stores a structured Alarm for batch sending to Loki.
func (r *Report) Alarm(event *unifi.Alarm) { func (r *Report) Alarm(event *unifi.Alarm) {
if event.Datetime.Before(*r.Last) { if event.Datetime.Before(r.Oldest) {
return return
} }

View File

@ -10,7 +10,7 @@ const typeAnomaly = "anomaly"
// Anomaly stores a structured Anomaly for batch sending to Loki. // Anomaly stores a structured Anomaly for batch sending to Loki.
func (r *Report) Anomaly(event *unifi.Anomaly) { func (r *Report) Anomaly(event *unifi.Anomaly) {
if event.Datetime.Before(*r.Last) { if event.Datetime.Before(r.Oldest) {
return return
} }

View File

@ -10,7 +10,7 @@ const typeEvent = "event"
// Event stores a structured UniFi Event for batch sending to Loki. // Event stores a structured UniFi Event for batch sending to Loki.
func (r *Report) Event(event *unifi.Event) { func (r *Report) Event(event *unifi.Event) {
if event.Datetime.Before(*r.Last) { if event.Datetime.Before(r.Oldest) {
return return
} }

View File

@ -10,7 +10,7 @@ const typeIDS = "ids"
// event stores a structured event Event for batch sending to Loki. // event stores a structured event Event for batch sending to Loki.
func (r *Report) IDS(event *unifi.IDS) { func (r *Report) IDS(event *unifi.IDS) {
if event.Datetime.Before(*r.Last) { if event.Datetime.Before(r.Oldest) {
return return
} }