add loki config and logs to webserver

This commit is contained in:
davidnewhall2 2020-06-28 05:47:14 -07:00
parent bccf9401d1
commit 4267ada53f
6 changed files with 58 additions and 4 deletions

View File

@ -20,6 +20,7 @@ var (
errStatusCode = fmt.Errorf("unexpected HTTP status code") errStatusCode = fmt.Errorf("unexpected HTTP status code")
) )
// Client holds the http client for contacting Loki.
type Client struct { type Client struct {
*Config *Config
*http.Client *http.Client

View File

@ -4,7 +4,8 @@ go 1.14
require ( require (
github.com/pkg/errors v0.9.1 github.com/pkg/errors v0.9.1
github.com/unifi-poller/poller v0.0.8-0.20200622041812-dd79a3e3073e github.com/unifi-poller/poller v0.0.8-0.20200626082958-a9a7092a5684
github.com/unifi-poller/unifi v0.0.6-0.20200625090439-421046871a37 github.com/unifi-poller/unifi v0.0.6-0.20200625090439-421046871a37
github.com/unifi-poller/webserver v0.0.0-20200628115531-e071827d7598
golift.io/cnfg v0.0.5 golift.io/cnfg v0.0.5
) )

View File

@ -41,6 +41,8 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc=
github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
@ -90,11 +92,16 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/unifi-poller/poller v0.0.8-0.20200622041812-dd79a3e3073e h1:pSkgIalhDdb1N0/9uTAVh7emolfFxjCxo68IXIsZzII= github.com/unifi-poller/poller v0.0.8-0.20200622041812-dd79a3e3073e h1:pSkgIalhDdb1N0/9uTAVh7emolfFxjCxo68IXIsZzII=
github.com/unifi-poller/poller v0.0.8-0.20200622041812-dd79a3e3073e/go.mod h1:pJ/MeYaakLOOpbyc7s4zeZ92UzNK/rir5jkA7t5jIjo= github.com/unifi-poller/poller v0.0.8-0.20200622041812-dd79a3e3073e/go.mod h1:pJ/MeYaakLOOpbyc7s4zeZ92UzNK/rir5jkA7t5jIjo=
github.com/unifi-poller/poller v0.0.8-0.20200626082958-a9a7092a5684 h1:r1B8GoI47czgGnQ7WY89qlSKqSE1d1pQmcLfdXVW/+Y=
github.com/unifi-poller/poller v0.0.8-0.20200626082958-a9a7092a5684/go.mod h1:pJ/MeYaakLOOpbyc7s4zeZ92UzNK/rir5jkA7t5jIjo=
github.com/unifi-poller/unifi v0.0.5-0.20200623080800-1766e7bde07c h1:pW5Z6cE1DC2GAHy6CdWnkgy5t/wehBvOW19viQXxUVo= github.com/unifi-poller/unifi v0.0.5-0.20200623080800-1766e7bde07c h1:pW5Z6cE1DC2GAHy6CdWnkgy5t/wehBvOW19viQXxUVo=
github.com/unifi-poller/unifi v0.0.5-0.20200623080800-1766e7bde07c/go.mod h1:L1kMRH2buZhB31vZnRC1im7Tk/4uD3ET4biwl2faYy8= github.com/unifi-poller/unifi v0.0.5-0.20200623080800-1766e7bde07c/go.mod h1:L1kMRH2buZhB31vZnRC1im7Tk/4uD3ET4biwl2faYy8=
github.com/unifi-poller/unifi v0.0.6-0.20200625090439-421046871a37 h1:T2y8JWkjZd1vz2ZKu4vmmAk9s6PUwupuTldwhfww5xY= github.com/unifi-poller/unifi v0.0.6-0.20200625090439-421046871a37 h1:T2y8JWkjZd1vz2ZKu4vmmAk9s6PUwupuTldwhfww5xY=
github.com/unifi-poller/unifi v0.0.6-0.20200625090439-421046871a37/go.mod h1:L1kMRH2buZhB31vZnRC1im7Tk/4uD3ET4biwl2faYy8= github.com/unifi-poller/unifi v0.0.6-0.20200625090439-421046871a37/go.mod h1:L1kMRH2buZhB31vZnRC1im7Tk/4uD3ET4biwl2faYy8=
github.com/unifi-poller/webserver v0.0.0-20200628115531-e071827d7598 h1:Nmo1arOOln7V7YhjcwrT2AEFN0dl43tl5NgoNF4SgiA=
github.com/unifi-poller/webserver v0.0.0-20200628115531-e071827d7598/go.mod h1:rlM8tRx7wCxqj4+6ZkuFVt2voFoAlHhS/XTrHd7T57s=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=

View File

@ -0,0 +1,38 @@
package lokiunifi
import (
"fmt"
"time"
"github.com/unifi-poller/webserver"
)
// Logf logs a message.
func (l *Loki) Logf(msg string, v ...interface{}) {
webserver.NewInputEvent(PluginName, PluginName, &webserver.Event{
Ts: time.Now(),
Msg: fmt.Sprintf(msg, v...),
Tags: map[string]string{"type": "info"},
})
l.Collect.Logf(msg, v...)
}
// LogErrorf logs an error message.
func (l *Loki) LogErrorf(msg string, v ...interface{}) {
webserver.NewInputEvent(PluginName, PluginName, &webserver.Event{
Ts: time.Now(),
Msg: fmt.Sprintf(msg, v...),
Tags: map[string]string{"type": "error"},
})
l.Collect.LogErrorf(msg, v...)
}
// LogDebugf logs a debug message.
func (l *Loki) LogDebugf(msg string, v ...interface{}) {
webserver.NewInputEvent(PluginName, PluginName, &webserver.Event{
Ts: time.Now(),
Msg: fmt.Sprintf(msg, v...),
Tags: map[string]string{"type": "debug"},
})
l.Collect.LogDebugf(msg, v...)
}

View File

@ -2,11 +2,13 @@ package lokiunifi
import ( import (
"io/ioutil" "io/ioutil"
"strconv"
"strings" "strings"
"time" "time"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/unifi-poller/poller" "github.com/unifi-poller/poller"
"github.com/unifi-poller/webserver"
"golift.io/cnfg" "golift.io/cnfg"
) )
@ -38,7 +40,7 @@ type Config struct {
// Loki is the main library struct. This satisfies the poller.Output interface. // Loki is the main library struct. This satisfies the poller.Output interface.
type Loki struct { type Loki struct {
poller.Collect Collect poller.Collect
*Config `json:"loki" toml:"loki" xml:"loki" yaml:"loki"` *Config `json:"loki" toml:"loki" xml:"loki" yaml:"loki"`
client *Client client *Client
last time.Time last time.Time
@ -67,6 +69,11 @@ func (l *Loki) Run(collect poller.Collect) error {
} }
l.ValidateConfig() l.ValidateConfig()
fake := *l.Config
fake.Password = strconv.FormatBool(fake.Password != "")
webserver.UpdateOutput(&webserver.Output{Name: PluginName, Config: fake})
l.PollController() l.PollController()
l.LogErrorf("Loki Output Plugin Stopped!") l.LogErrorf("Loki Output Plugin Stopped!")
@ -104,7 +111,7 @@ func (l *Loki) PollController() {
ticker := time.NewTicker(interval) ticker := time.NewTicker(interval)
for start := range ticker.C { for start := range ticker.C {
events, err := l.Events(&poller.Filter{Name: InputName}) events, err := l.Collect.Events(&poller.Filter{Name: InputName})
if err != nil { if err != nil {
l.LogErrorf("event fetch for Loki failed: %v", err) l.LogErrorf("event fetch for Loki failed: %v", err)
continue continue

View File

@ -34,7 +34,7 @@ func (l *Loki) NewReport(start time.Time) *Report {
return &Report{ return &Report{
Start: start, Start: start,
Oldest: l.last, Oldest: l.last,
Logger: l.Collect, Logger: l,
Counts: make(map[string]int), Counts: make(map[string]int),
} }
} }