Merge pull request #22 from davidnewhall/dn2_vendor_fixes
Vendor fixes.
This commit is contained in:
commit
a1c509cf16
|
|
@ -1,6 +1,6 @@
|
|||
/up.conf
|
||||
/unifi-poller
|
||||
/*.1.gz
|
||||
/*.gz
|
||||
/*.1
|
||||
/vendor
|
||||
.DS_Store
|
||||
|
|
|
|||
|
|
@ -40,14 +40,6 @@
|
|||
revision = "e6f5723bf2a66af014955e0888881314cf294129"
|
||||
version = "v0.1.1"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:8fd3a15613c7e70cceff3aa03dd57560dba87c4868864e397d5eb2f14addd3f5"
|
||||
name = "github.com/ogier/pflag"
|
||||
packages = ["."]
|
||||
pruneopts = "UT"
|
||||
revision = "32a05c62658bd1d7c7e75cbc8195de5d585fde0f"
|
||||
version = "v0.0.1"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:cf31692c14422fa27c83a05292eb5cbe0fb2775972e8f1f8446a71549bd8980b"
|
||||
name = "github.com/pkg/errors"
|
||||
|
|
@ -56,6 +48,14 @@
|
|||
revision = "ba968bfe8b2f7e042a574c888954fccecfa385b4"
|
||||
version = "v0.8.1"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:c1b1102241e7f645bc8e0c22ae352e8f0dc6484b6cb4d132fa9f24174e0119e2"
|
||||
name = "github.com/spf13/pflag"
|
||||
packages = ["."]
|
||||
pruneopts = "UT"
|
||||
revision = "298182f68c66c05229eb03ac171abe6e309ee79a"
|
||||
version = "v1.0.3"
|
||||
|
||||
[solve-meta]
|
||||
analyzer-name = "dep"
|
||||
analyzer-version = 1
|
||||
|
|
@ -63,7 +63,7 @@
|
|||
"github.com/golift/unifi",
|
||||
"github.com/influxdata/influxdb1-client/v2",
|
||||
"github.com/naoina/toml",
|
||||
"github.com/ogier/pflag",
|
||||
"github.com/spf13/pflag",
|
||||
]
|
||||
solver-name = "gps-cdcl"
|
||||
solver-version = 1
|
||||
|
|
|
|||
|
|
@ -28,10 +28,6 @@
|
|||
name = "github.com/naoina/toml"
|
||||
version = "0.1.1"
|
||||
|
||||
[[constraint]]
|
||||
name = "github.com/ogier/pflag"
|
||||
version = "0.0.1"
|
||||
|
||||
[prune]
|
||||
go-tests = true
|
||||
unused-packages = true
|
||||
|
|
|
|||
|
|
@ -11,7 +11,8 @@ import (
|
|||
"github.com/golift/unifi"
|
||||
influx "github.com/influxdata/influxdb1-client/v2"
|
||||
"github.com/naoina/toml"
|
||||
flag "github.com/ogier/pflag"
|
||||
"github.com/pkg/errors"
|
||||
flag "github.com/spf13/pflag"
|
||||
)
|
||||
|
||||
// Asset is used to give all devices and clients a common interface.
|
||||
|
|
@ -25,37 +26,47 @@ func main() {
|
|||
config, err := GetConfig(configFile)
|
||||
if err != nil {
|
||||
flag.Usage()
|
||||
log.Fatalf("Config Error '%v': %v", configFile, err)
|
||||
log.Fatalf("[ERROR] config file '%v': %v", configFile, err)
|
||||
}
|
||||
if err := config.Run(); err != nil {
|
||||
log.Fatalln("[ERROR]", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Run invokes all the application logic and routines.
|
||||
func (c *Config) Run() error {
|
||||
// Create an authenticated session to the Unifi Controller.
|
||||
controller, err := unifi.NewUnifi(config.UnifiUser, config.UnifiPass, config.UnifiBase, config.VerifySSL)
|
||||
controller, err := unifi.NewUnifi(c.UnifiUser, c.UnifiPass, c.UnifiBase, c.VerifySSL)
|
||||
if err != nil {
|
||||
log.Fatalln("Unifi Controller Error:", err)
|
||||
} else if !config.Quiet {
|
||||
log.Println("Authenticated to Unifi Controller @", config.UnifiBase, "as user", config.UnifiUser)
|
||||
return errors.Wrap(err, "unifi controller")
|
||||
}
|
||||
controller.ErrorLog = log.Printf
|
||||
if log.SetFlags(0); config.Debug {
|
||||
controller.DebugLog = log.Printf
|
||||
log.SetFlags(log.Lshortfile | log.Lmicroseconds | log.Ldate)
|
||||
if !c.Quiet {
|
||||
log.Println("Authenticated to Unifi Controller @", c.UnifiBase, "as user", c.UnifiUser)
|
||||
}
|
||||
|
||||
controller.ErrorLog = log.Printf // Log all errors.
|
||||
if log.SetFlags(0); c.Debug {
|
||||
log.Println("Debug Logging Enabled")
|
||||
log.SetFlags(log.Lshortfile | log.Lmicroseconds | log.Ldate)
|
||||
controller.DebugLog = log.Printf // Log debug messages.
|
||||
}
|
||||
infdb, err := influx.NewHTTPClient(influx.HTTPConfig{
|
||||
Addr: config.InfluxURL,
|
||||
Username: config.InfluxUser,
|
||||
Password: config.InfluxPass,
|
||||
Addr: c.InfluxURL,
|
||||
Username: c.InfluxUser,
|
||||
Password: c.InfluxPass,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalln("InfluxDB Error:", err)
|
||||
return errors.Wrap(err, "influxdb")
|
||||
}
|
||||
if config.Quiet {
|
||||
if c.Quiet {
|
||||
// Doing it this way allows debug error logs (line numbers, etc)
|
||||
controller.DebugLog = nil
|
||||
} else {
|
||||
log.Println("Logging Unifi Metrics to InfluXDB @", config.InfluxURL, "as user", config.InfluxUser)
|
||||
log.Printf("Polling Unifi Controller (sites %v), interval: %v", config.Sites, config.Interval.value)
|
||||
log.Println("Logging Unifi Metrics to InfluXDB @", c.InfluxURL, "as user", c.InfluxUser)
|
||||
log.Printf("Polling Unifi Controller (sites %v), interval: %v", c.Sites, c.Interval.value)
|
||||
}
|
||||
config.PollUnifiController(controller, infdb)
|
||||
c.PollUnifiController(controller, infdb)
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseFlags() string {
|
||||
|
|
@ -100,20 +111,35 @@ func GetConfig(configFile string) (Config, error) {
|
|||
func (c *Config) PollUnifiController(controller *unifi.Unifi, infdb influx.Client) {
|
||||
log.Println("[INFO] Everyting checks out! Beginning Poller Routine.")
|
||||
ticker := time.NewTicker(c.Interval.value)
|
||||
|
||||
for range ticker.C {
|
||||
if sites, err := filterSites(controller, c.Sites); err != nil {
|
||||
sites, err := filterSites(controller, c.Sites)
|
||||
if err != nil {
|
||||
logErrors([]error{err}, "uni.GetSites()")
|
||||
} else if clients, err := controller.GetClients(sites); err != nil {
|
||||
}
|
||||
// Get all the points.
|
||||
clients, err := controller.GetClients(sites)
|
||||
if err != nil {
|
||||
logErrors([]error{err}, "uni.GetClients()")
|
||||
} else if devices, err := controller.GetDevices(sites); err != nil {
|
||||
}
|
||||
devices, err := controller.GetDevices(sites)
|
||||
if err != nil {
|
||||
logErrors([]error{err}, "uni.GetDevices()")
|
||||
} else if bp, err := influx.NewBatchPoints(influx.BatchPointsConfig{Database: c.InfluxDB}); err != nil {
|
||||
}
|
||||
bp, err := influx.NewBatchPoints(influx.BatchPointsConfig{Database: c.InfluxDB})
|
||||
if err != nil {
|
||||
logErrors([]error{err}, "influx.NewBatchPoints")
|
||||
} else if errs := batchPoints(devices, clients, bp); errs != nil && hasErr(errs) {
|
||||
continue
|
||||
}
|
||||
|
||||
// Batch all the points.
|
||||
if errs := batchPoints(devices, clients, bp); errs != nil && hasErr(errs) {
|
||||
logErrors(errs, "asset.Points()")
|
||||
} else if err := infdb.Write(bp); err != nil {
|
||||
}
|
||||
if err := infdb.Write(bp); err != nil {
|
||||
logErrors([]error{err}, "infdb.Write(bp)")
|
||||
} else if !c.Quiet {
|
||||
}
|
||||
if !c.Quiet {
|
||||
log.Printf("[INFO] Logged Unifi States. Sites: %d Clients: %d, Wireless APs: %d, Gateways: %d, Switches: %d",
|
||||
len(sites), len(clients.UCLs), len(devices.UAPs), len(devices.USGs), len(devices.USWs))
|
||||
}
|
||||
|
|
@ -143,6 +169,9 @@ func filterSites(controller *unifi.Unifi, filter []string) ([]unifi.Site, error)
|
|||
// batchPoints combines all device and client data into influxdb data points.
|
||||
func batchPoints(devices *unifi.Devices, clients *unifi.Clients, batchPoints influx.BatchPoints) (errs []error) {
|
||||
process := func(asset Asset) error {
|
||||
if asset == nil {
|
||||
return nil
|
||||
}
|
||||
influxPoints, err := asset.Points()
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -150,17 +179,21 @@ func batchPoints(devices *unifi.Devices, clients *unifi.Clients, batchPoints inf
|
|||
batchPoints.AddPoints(influxPoints)
|
||||
return nil
|
||||
}
|
||||
for _, asset := range devices.UAPs {
|
||||
errs = append(errs, process(asset))
|
||||
if devices != nil {
|
||||
for _, asset := range devices.UAPs {
|
||||
errs = append(errs, process(asset))
|
||||
}
|
||||
for _, asset := range devices.USGs {
|
||||
errs = append(errs, process(asset))
|
||||
}
|
||||
for _, asset := range devices.USWs {
|
||||
errs = append(errs, process(asset))
|
||||
}
|
||||
}
|
||||
for _, asset := range devices.USGs {
|
||||
errs = append(errs, process(asset))
|
||||
}
|
||||
for _, asset := range devices.USWs {
|
||||
errs = append(errs, process(asset))
|
||||
}
|
||||
for _, asset := range clients.UCLs {
|
||||
errs = append(errs, process(asset))
|
||||
if clients != nil {
|
||||
for _, asset := range clients.UCLs {
|
||||
errs = append(errs, process(asset))
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue