mirror of
https://github.com/unpoller/unpoller.git
synced 2026-03-31 06:33:57 -04:00
- Add 'tag' label to all device metric descriptors - Update exportWithTags helper to create separate metric series per tag - Update all device export functions (UAP, USW, UDM, USG, UXG, PDU, UBB, UCI) to include tags - Update all label arrays (VAP, Radio, Port, etc.) to include tag label - Devices with multiple tags create multiple metric series (one per tag) - Devices without tags export with tag="" Requires unpoller/unifi#92
98 lines
2.5 KiB
Go
98 lines
2.5 KiB
Go
package lokiunifi
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/unpoller/unifi/v5"
|
|
"github.com/unpoller/unpoller/pkg/poller"
|
|
)
|
|
|
|
// LogStream contains a stream of logs (like a log file).
|
|
// This app uses one stream per log entry because each log may have different labels.
|
|
type LogStream struct {
|
|
Labels map[string]string `json:"stream"` // "the file name"
|
|
Entries [][]string `json:"values"` // "the log lines"
|
|
}
|
|
|
|
// Logs is the main logs-holding structure. This is the Loki-output format.
|
|
type Logs struct {
|
|
Streams []LogStream `json:"streams"` // "multiple files"
|
|
}
|
|
|
|
// Report is the temporary data generated by processing events.
|
|
type Report struct {
|
|
Start time.Time
|
|
Oldest time.Time
|
|
Collect poller.Collect
|
|
poller.Logger
|
|
Counts map[string]int
|
|
}
|
|
|
|
// NewReport makes a new report.
|
|
func (l *Loki) NewReport(start time.Time) *Report {
|
|
return &Report{
|
|
Start: start,
|
|
Oldest: l.last,
|
|
Collect: l.Collect,
|
|
Logger: l,
|
|
Counts: make(map[string]int),
|
|
}
|
|
}
|
|
|
|
// ProcessEventLogs loops the event Logs, matches the interface type, calls the
|
|
// appropriate method for the data, and compiles the Logs into a Loki format.
|
|
// This runs once per interval, if there was no collection error.
|
|
func (r *Report) ProcessEventLogs(events *poller.Events) *Logs {
|
|
logs := &Logs{}
|
|
|
|
for _, e := range events.Logs {
|
|
switch event := e.(type) {
|
|
case *unifi.IDS:
|
|
r.IDs(event, logs)
|
|
case *unifi.Event:
|
|
r.Event(event, logs)
|
|
case *unifi.Alarm:
|
|
r.Alarm(event, logs)
|
|
case *unifi.Anomaly:
|
|
r.Anomaly(event, logs)
|
|
case *unifi.SystemLogEntry:
|
|
r.SystemLogEvent(event, logs)
|
|
case *unifi.ProtectLogEntry:
|
|
r.ProtectLogEvent(event, logs)
|
|
default: // unlikely.
|
|
if r.Collect != nil && r.Collect.Poller().LogUnknownTypes {
|
|
r.LogDebugf("unknown event type: %T", e)
|
|
}
|
|
}
|
|
}
|
|
|
|
return logs
|
|
}
|
|
|
|
func (r *Report) String() string {
|
|
s := fmt.Sprintf("%s: %d, %s: %d, %s: %d, %s: %d, %s: %d, %s: %d",
|
|
typeEvent, r.Counts[typeEvent], typeIDs, r.Counts[typeIDs],
|
|
typeAlarm, r.Counts[typeAlarm], typeAnomaly, r.Counts[typeAnomaly],
|
|
typeSystemLog, r.Counts[typeSystemLog], typeProtectLog, r.Counts[typeProtectLog])
|
|
|
|
if r.Counts[typeProtectThumbnail] > 0 {
|
|
s += fmt.Sprintf(" (thumbs: %d)", r.Counts[typeProtectThumbnail])
|
|
}
|
|
|
|
s += fmt.Sprintf(", Dur: %v", time.Since(r.Start).Round(time.Millisecond))
|
|
return s
|
|
}
|
|
|
|
// CleanLabels removes any tag that is empty.
|
|
func CleanLabels(labels map[string]string) map[string]string {
|
|
for i := range labels {
|
|
if strings.TrimSpace(labels[i]) == "" {
|
|
delete(labels, i)
|
|
}
|
|
}
|
|
|
|
return labels
|
|
}
|