new library release. fix bugs.

This commit is contained in:
David Newhall II 2019-01-26 15:42:31 -08:00
parent 83def42f9c
commit e165a47162
5 changed files with 37 additions and 41 deletions

View File

@ -2,34 +2,24 @@
[[projects]]
branch = "master"
digest = "1:f5c630587244a2d10773eeb4ff84bb28e7a1d12833bb79136fff894193df3072"
digest = "1:e7f0acf99afe9a7b03d270164bd2976b687e1aef02ab3a0abd8db0b4de44b817"
name = "github.com/golift/unifi"
packages = ["."]
pruneopts = "UT"
revision = "24c7eb106b3c9ff4260c88b54c02f0f86301fa75"
revision = "f8fec42fbe169dceb69f15276a2323fb007a4539"
version = "v1.0.0"
[[projects]]
digest = "1:718c57979a9197414a044fff2028e57cb9fe145069e4f507059755bfe87f1bfe"
name = "github.com/influxdata/influxdb"
packages = [
"client/v2",
"models",
"pkg/escape",
]
pruneopts = "UT"
revision = "698dbc789aff13c2678357a6b93ff73dd7136571"
version = "v1.7.3"
[[projects]]
digest = "1:937258f1293bc9295b4789b0abea5b4ec030e3caecb65a4e1dc0b6999957a5ed"
name = "github.com/influxdata/platform"
branch = "master"
digest = "1:50708c8fc92aec981df5c446581cf9f90ba9e2a5692118e0ce75d4534aaa14a2"
name = "github.com/influxdata/influxdb1-client"
packages = [
"models",
"pkg/escape",
"v2",
]
pruneopts = "UT"
revision = "0f79e4ea3248354c789cba274542e0a8e55971db"
revision = "16c852ea613fa2d42fcdccc9a8b0802a8bdc6140"
[[projects]]
digest = "1:b56c589214f01a5601e0821387db484617392d0042f26234bf2da853a2f498a1"
@ -71,7 +61,7 @@
analyzer-version = 1
input-imports = [
"github.com/golift/unifi",
"github.com/influxdata/influxdb/client/v2",
"github.com/influxdata/influxdb1-client/v2",
"github.com/naoina/toml",
"github.com/ogier/pflag",
]

View File

@ -24,15 +24,6 @@
# go-tests = true
# unused-packages = true
[[constraint]]
branch = "master"
name = "github.com/golift/unifi"
[[constraint]]
name = "github.com/influxdata/influxdb"
version = "1.7.3"
[[constraint]]
name = "github.com/naoina/toml"
version = "0.1.1"

View File

@ -75,6 +75,11 @@ My access points only seem to have two radios, one interface and vAP per radio.
I'm not sure if the graphs, as-is, provide enough insight into APs with other
configurations. Help me figure that out?
- It possibly loses access to the controller at some point.
I noticed metrics stop updating after a while. I think the new code will help
isolate why this happens. We may need to issue a reconnect and get a new cookie.
# What's it look like?
Here's a picture of the Client dashboard.

View File

@ -13,9 +13,9 @@ unifi-poller(1) -- Utility to poll Unifi Metrics and drop them into InfluxDB
## OPTIONS
`unifi-poller [-c <config file>] [-h] [-v]`
`unifi-poller [-c <config-file>] [-h] [-v]`
-c, --config <file_path>
-c, --config <config-file>
Provide a configuration file (instead of the default).
-v, --version

View File

@ -25,9 +25,6 @@ func main() {
if err != nil {
flag.Usage()
log.Fatalf("Config Error '%v': %v", configFile, err)
} else if log.SetFlags(0); config.Debug {
log.SetFlags(log.Lshortfile | log.Lmicroseconds | log.Ldate)
log.Println("Debug Logging Enabled")
}
// Create an authenticated session to the Unifi Controller.
device, err := unifi.GetController(config.UnifiUser, config.UnifiPass, config.UnifiBase, config.VerifySSL)
@ -37,6 +34,11 @@ func main() {
log.Println("Authenticated to Unifi Controller @", config.UnifiBase, "as user", config.UnifiUser)
}
device.ErrorLog = log.Printf
if log.SetFlags(0); config.Debug {
device.DebugLog = log.Printf
log.SetFlags(log.Lshortfile | log.Lmicroseconds | log.Ldate)
log.Println("Debug Logging Enabled")
}
infdb, err := influx.NewHTTPClient(influx.HTTPConfig{
Addr: config.InfluxURL,
Username: config.InfluxUser,
@ -44,9 +46,10 @@ func main() {
})
if err != nil {
log.Fatalln("InfluxDB Error:", err)
} else if config.Quiet {
}
if config.Quiet {
// Doing it this way allows debug error logs (line numbers, etc)
device.DebugLog = log.Printf
device.DebugLog = nil
} else {
log.Println("Logging Unifi Metrics to InfluXDB @", config.InfluxURL, "as user", config.InfluxUser)
log.Println("Polling Unifi Controller, interval:", config.Interval.value)
@ -96,7 +99,7 @@ func GetConfig(configFile string) (Config, error) {
// PollUnifiController runs forever, polling and pushing.
func (c *Config) PollUnifiController(infdb influx.Client, uni *unifi.Unifi) {
log.Println("Everyting checks out! Beginning Poller Routine.")
log.Println("[INFO] Everyting checks out! Beginning Poller Routine.")
ticker := time.NewTicker(c.Interval.value)
for range ticker.C {
if clients, err := uni.GetClients(); err != nil {
@ -105,12 +108,12 @@ func (c *Config) PollUnifiController(infdb influx.Client, uni *unifi.Unifi) {
logErrors([]error{err}, "uni.GetDevices()")
} else if bp, err := influx.NewBatchPoints(influx.BatchPointsConfig{Database: c.InfluxDB}); err != nil {
logErrors([]error{err}, "influx.NewBatchPoints")
} else if errs := batchPoints(devices, clients, bp); errs != nil {
} else if errs := batchPoints(devices, clients, bp); errs != nil && hasErr(errs) {
logErrors(errs, "asset.Points()")
} else if err := infdb.Write(bp); err != nil {
logErrors([]error{err}, "infdb.Write(bp)")
} else if !c.Quiet {
log.Println("Logged Unifi States. Clients:", len(clients.UCLs), "- Wireless APs:",
log.Println("[INFO] Logged Unifi States. Clients:", len(clients.UCLs), "- Wireless APs:",
len(devices.UAPs), "Gateways:", len(devices.USGs), "Switches:", len(devices.USWs))
}
}
@ -141,11 +144,18 @@ func batchPoints(devices *unifi.Devices, clients *unifi.Clients, batchPoints inf
return
}
// hasErr checks a list of errors for a non-nil.
func hasErr(errs []error) bool {
for _, err := range errs {
if err != nil {
return true
}
}
return false
}
// logErrors writes a slice of errors, with a prefix, to log-out.
func logErrors(errs []error, prefix string) {
if errs == nil {
return
}
for _, err := range errs {
if err != nil {
log.Println("[ERROR]", prefix+":", err.Error())