webserver

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

UniFi Poller: webserver plugin

Built-In Web Server Go Library for UniFi Poller.

Enabling the web server is optional. It provides a window into the running data. The web server may be secured with a simple password. SSL is also optional.

See the Web Server Wiki for more information about how it works.

Other plugins must import this library to make use of it. While this library is labeled as a plugin, it's pretty much required since everything imports it. That said, it is still disabled by default, and won't store any data unless it's enabled.

This needs a better godoc and examples.

Overview

  • Recent logs from poller are visible.
  • Uptime and Version are displayed across the top.
Controllers
  • The web server interface allows you to see the configuration for each controller.
  • Some meta data about each controller is displayed, such as sites, clients and devices.
  • Example config: up.json.example
Input Plugins
  • You may view input plugin configuration. Currently only UniFi.
  • The example config above shows input plugin data.
Output Plugins
  • You may view output plugin configuration. Currently Prometheus and InfluxDB.
  • The example config above shows output plugin data.

Documentation

Overview

Package webserver is a UniFi Poller plugin that exports running data to a web interface.

Index

Constants

View Source
const (
	// PluginName identifies this output plugin.
	PluginName = "WebServer"
	// DefaultPort is the default web http port.
	DefaultPort = 37288
	// DefaultEvents is the default number of events stored per plugin.
	DefaultEvents = 200
)

Variables

This section is empty.

Functions

func NewInputEvent

func NewInputEvent(name, id string, event *Event)

NewInputEvent adds an event for an input plugin.

func NewOutputEvent

func NewOutputEvent(name, id string, event *Event)

NewOutputEvent adds an event for an output plugin.

func UpdateInput

func UpdateInput(config *Input)

UpdateInput allows an input plugin to create an entry or update an existing entry.

func UpdateInputCounter

func UpdateInputCounter(plugin, label string, values ...int64)

UpdateInputCounter allows an input plugin to update a counter's value. Set any arbitrary counter. These are displayed on the web interface.

func UpdateOutput

func UpdateOutput(config *Output)

UpdateOutput allows an output plugin to create an entry or update an existing entry.

func UpdateOutputCounter

func UpdateOutputCounter(plugin, label string, values ...int64)

UpdateOutputCounter allows an output plugin to update a counter's value.

Types

type Client

type Client struct {
	Rx         int64     `json:"rx_bytes"`
	Tx         int64     `json:"tx_bytes"`
	Name       string    `json:"name"`
	SiteID     string    `json:"site_id"`
	Source     string    `json:"source"`
	Controller string    `json:"controller"`
	MAC        string    `json:"mac"`
	IP         string    `json:"ip"`
	Type       string    `json:"type"`
	DeviceMAC  string    `json:"device_mac"`
	Since      time.Time `json:"since"`
	Last       time.Time `json:"last"`
}

Client holds the data for a network client.

type Clients

type Clients []*Client

Clients is a list of clients with their data.

func (Clients) Filter

func (c Clients) Filter(siteid string) (clients []*Client)

type Config

type Config struct {
	Enable     bool     `json:"enable" toml:"enable" xml:"enable,attr" yaml:"enable"`
	SSLCrtPath string   `json:"ssl_cert_path" toml:"ssl_cert_path" xml:"ssl_cert_path" yaml:"ssl_cert_path"`
	SSLKeyPath string   `json:"ssl_key_path" toml:"ssl_key_path" xml:"ssl_key_path" yaml:"ssl_key_path"`
	Port       uint     `json:"port" toml:"port" xml:"port" yaml:"port"`
	Accounts   accounts `json:"accounts" toml:"accounts" xml:"accounts" yaml:"accounts"`
	HTMLPath   string   `json:"html_path" toml:"html_path" xml:"html_path" yaml:"html_path"`
	MaxEvents  uint     `json:"max_events" toml:"max_events" xml:"max_events" yaml:"max_events"`
}

Config is the webserver library input config.

type Device

type Device struct {
	Clients    int    `json:"clients"`
	Uptime     int    `json:"uptime"`
	Name       string `json:"name"`
	SiteID     string `json:"site_id"`
	Source     string `json:"source"`
	Controller string `json:"controller"`
	MAC        string `json:"mac"`
	IP         string `json:"ip"`
	Type       string `json:"type"`
	Model      string `json:"model"`
	Version    string `json:"version"`
	Config     any    `json:"config,omitempty"`
}

Device holds the data for a network device.

type Devices

type Devices []*Device

Devices is a list of network devices and their data.

func (Devices) Filter

func (c Devices) Filter(siteid string) (devices []*Device)

type Event

type Event struct {
	Ts   time.Time         `json:"ts"` // nolint: stylecheck
	Msg  string            `json:"msg"`
	Tags map[string]string `json:"tags,omitempty"`
}

Event is like a log message.

type EventGroup

type EventGroup struct {
	Latest time.Time `json:"latest"`
	Events []*Event  `json:"events"`
}

EventGroup allows each plugin to have a map of events. ie. one map per controller.

type Events

type Events map[string]*EventGroup

Events is all the events a plugin has. string = SiteID + text, or plugin name, or "whatever".

func (Events) Groups

func (e Events) Groups(prefix string) (groups []string)

type Input

type Input struct {
	Name         string
	Sites        Sites
	Events       Events
	Devices      Devices
	Clients      Clients
	Config       any
	Counter      map[string]int64
	sync.RWMutex // Locks this data structure.
}

Input is the data tracked for intput plugins. An input plugin should fill this data every time it polls this data. Partial update are OK. Set non-updated fields to nil and they're ignored.

type Output

type Output struct {
	Name         string
	Events       Events
	Config       any
	Counter      map[string]int64
	sync.RWMutex // Locks this data structure.
}

Output is the data tracked for output plugins. Output plugins should fill this data on startup, and regularly update counters for things worth counting. Setting Config will overwrite previous value.

type ResponseWriter

type ResponseWriter struct {
	Code   int
	Size   int
	Error  string
	Start  time.Time
	Writer http.ResponseWriter
}

ResponseWriter is used to override http.ResponseWriter in our http.FileServer. This allows us to catch and log the response code, size and error; maybe others.

func (*ResponseWriter) Header

func (w *ResponseWriter) Header() http.Header

Header sends a header to a client. Satisfies http.ResponseWriter interface.

func (*ResponseWriter) Write

func (w *ResponseWriter) Write(b []byte) (int, error)

Write sends bytes to the client. Satisfies http.ResponseWriter interface. This also adds the written byte count to our size total.

func (*ResponseWriter) WriteHeader

func (w *ResponseWriter) WriteHeader(code int)

WriteHeader sends an http StatusCode to a client. Satisfies http.ResponseWriter interface. This custom override method also saves the status code, and any error message (for logs).

type Server

type Server struct {
	*Config `json:"webserver" toml:"webserver" xml:"webserver" yaml:"webserver"`

	Collect poller.Collect
	// contains filtered or unexported fields
}

Server is the main library struct/data.

func (*Server) DebugOutput

func (s *Server) DebugOutput() (bool, error)

func (*Server) Enabled

func (s *Server) Enabled() bool

func (*Server) LogDebugf

func (s *Server) LogDebugf(msg string, v ...any)

LogDebugf logs a debug message.

func (*Server) LogErrorf

func (s *Server) LogErrorf(msg string, v ...any)

LogErrorf logs an error message.

func (*Server) Logf

func (s *Server) Logf(msg string, v ...any)

Logf logs a message.

func (*Server) Run

func (s *Server) Run(c poller.Collect) error

Run starts the server and gets things going.

func (*Server) Start

func (s *Server) Start() (err error)

Start gets the web server going.

type Site

type Site struct {
	ID         string `json:"id"`
	Name       string `json:"name"`
	Desc       string `json:"desc"`
	Source     string `json:"source"`
	Controller string `json:"controller"`
}

Site is a network location and its meta data.

type Sites

type Sites []*Site

Sites is a list of network locations.

Jump to

Keyboard shortcuts

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