lokiunifi

package
v0.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 17, 2023 License: MIT, MIT Imports: 14 Imported by: 0

README

lokiunifi

Loki Output Plugin for UnPoller

This plugin writes UniFi Events and IDS data to Loki. Maybe Alarms too.

Example Config:

[loki]
  # URL is the only required setting for Loki.
  url = "http://192.168.3.2:3100"

  # How often to poll UniFi and report to Loki.
  interval = "2m"

  # How long to wait for Loki responses.
  timeout = "5s"

  # Set these to use basic auth.
  #user = ""
  #pass = ""

  # Used for auth-less multi-tenant.
  #tenant_id = ""

Documentation

Index

Constants

View Source
const (
	// InputName is the name of plugin that gives us data.
	InputName = "unifi"
	// PluginName is the name of this plugin.
	PluginName = "loki"
)

Variables

This section is empty.

Functions

func CleanLabels

func CleanLabels(labels map[string]string) map[string]string

CleanLabels removes any tag that is empty.

Types

type Client

type Client struct {
	*Config
	*http.Client
}

Client holds the http client for contacting Loki.

func (*Client) Do

func (c *Client) Do(req *http.Request) (int, []byte, error)

Do makes an http request and returns the status code, body and/or an error.

func (*Client) NewRequest

func (c *Client) NewRequest(url, method, cType string, msg []byte) (*http.Request, error)

NewRequest creates the http request based on input data.

func (*Client) Post

func (c *Client) Post(logs any) error

Post marshals and posts a batch of log messages.

type Config

type Config struct {
	Disable   bool          `json:"disable" toml:"disable" xml:"disable" yaml:"disable"`
	VerifySSL bool          `json:"verify_ssl" toml:"verify_ssl" xml:"verify_ssl" yaml:"verify_ssl"`
	URL       string        `json:"url" toml:"url" xml:"url" yaml:"url"`
	Username  string        `json:"user" toml:"user" xml:"user" yaml:"user"`
	Password  string        `json:"pass" toml:"pass" xml:"pass" yaml:"pass"`
	TenantID  string        `json:"tenant_id" toml:"tenant_id" xml:"tenant_id" yaml:"tenant_id"`
	Interval  cnfg.Duration `json:"interval" toml:"interval" xml:"interval" yaml:"interval"`
	Timeout   cnfg.Duration `json:"timeout" toml:"timeout" xml:"timeout" yaml:"timeout"`
}

Config is the plugin's input data.

type LogStream

type LogStream struct {
	Labels  map[string]string `json:"stream"` // "the file name"
	Entries [][]string        `json:"values"` // "the log lines"
}

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 Logs

type Logs struct {
	Streams []LogStream `json:"streams"` // "multiple files"
}

Logs is the main logs-holding structure. This is the Loki-output format.

type Loki

type Loki struct {
	Collect poller.Collect
	*Config `json:"loki" toml:"loki" xml:"loki" yaml:"loki"`
	// contains filtered or unexported fields
}

Loki is the main library struct. This satisfies the poller.Output interface.

func (*Loki) DebugOutput

func (l *Loki) DebugOutput() (bool, error)

func (*Loki) Enabled

func (l *Loki) Enabled() bool

func (*Loki) LogDebugf

func (l *Loki) LogDebugf(msg string, v ...any)

LogDebugf logs a debug message.

func (*Loki) LogErrorf

func (l *Loki) LogErrorf(msg string, v ...any)

LogErrorf logs an error message.

func (*Loki) Logf

func (l *Loki) Logf(msg string, v ...any)

Logf logs a message.

func (*Loki) NewReport

func (l *Loki) NewReport(start time.Time) *Report

NewReport makes a new report.

func (*Loki) PollController

func (l *Loki) PollController()

PollController runs forever, polling UniFi for events and pushing them to Loki. This is started by Run().

func (*Loki) ProcessEvents

func (l *Loki) ProcessEvents(report *Report, events *poller.Events) error

ProcessEvents offloads some of the loop from PollController.

func (*Loki) Run

func (l *Loki) Run(collect poller.Collect) error

Run is fired from the poller library after the Config is unmarshalled.

func (*Loki) ValidateConfig

func (l *Loki) ValidateConfig() error

ValidateConfig sets initial "last" update time. Also creates an http client, makes sure URL is sane, and sets interval within min/max limits.

type Report

type Report struct {
	Start  time.Time
	Oldest time.Time
	poller.Logger
	Counts map[string]int
}

Report is the temporary data generated by processing events.

func (*Report) Alarm

func (r *Report) Alarm(event *unifi.Alarm, logs *Logs)

Alarm stores a structured Alarm for batch sending to Loki.

func (*Report) Anomaly

func (r *Report) Anomaly(event *unifi.Anomaly, logs *Logs)

Anomaly stores a structured Anomaly for batch sending to Loki.

func (*Report) Event

func (r *Report) Event(event *unifi.Event, logs *Logs)

Event stores a structured UniFi Event for batch sending to Loki.

func (*Report) IDS

func (r *Report) IDS(event *unifi.IDS, logs *Logs)

event stores a structured event Event for batch sending to Loki.

func (*Report) ProcessEventLogs

func (r *Report) ProcessEventLogs(events *poller.Events) *Logs

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 (*Report) String

func (r *Report) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL