a little more cleanup

This commit is contained in:
David Newhall II 2019-04-17 02:06:06 -07:00
parent d0a1460226
commit ac156bcd12
5 changed files with 25 additions and 35 deletions

View File

@ -10,15 +10,15 @@ import (
// Points generates Unifi Client datapoints for InfluxDB.
// These points can be passed directly to influx.
func (c UCL) Points() ([]*influx.Point, error) {
var points []*influx.Point
// Fix name and hostname fields. Sometimes one or the other is blank.
if c.Name == "" && c.Hostname != "" {
c.Name = c.Hostname
} else if c.Hostname == "" && c.Name != "" {
c.Hostname = c.Name
} else if c.Hostname == "" && c.Name == "" {
switch {
case c.Hostname == "" && c.Name == "":
c.Hostname = "-no-name-"
c.Name = "-no-name-"
case c.Hostname == "" && c.Name != "":
c.Hostname = c.Name
case c.Name == "" && c.Hostname != "":
c.Name = c.Hostname
}
tags := map[string]string{
"id": c.ID,
@ -100,7 +100,7 @@ func (c UCL) Points() ([]*influx.Point, error) {
}
pt, err := influx.NewPoint("clients", tags, fields, time.Now())
if err == nil {
points = append(points, pt)
return nil, err
}
return points, err
return []*influx.Point{pt}, nil
}

View File

@ -7,47 +7,40 @@ func (u *Unifi) parseDevices(data []json.RawMessage) *Devices {
devices := new(Devices)
for _, r := range data {
// Loop each item in the raw JSON message, detect its type and unmarshal it.
var obj map[string]interface{}
var uap UAP
var usg USG
var usw USW
if u.unmarshalDevice("interface{}", &obj, r) != nil {
assetType := "<type key missing>"
if o := make(map[string]interface{}); u.unmarshalDevice("map", r, &o) != nil {
continue
}
assetType := "<missing>"
if t, ok := obj["type"].(string); ok {
} else if t, ok := o["type"].(string); ok {
assetType = t
}
u.dLogf("Unmarshalling Device Type: %v", assetType)
// Choose which type to unmarshal into based on the "type" json key.
switch assetType { // Unmarshal again into the correct type..
case "uap":
if u.unmarshalDevice(assetType, &uap, r) == nil {
if uap := (UAP{}); u.unmarshalDevice(assetType, r, &uap) == nil {
devices.UAPs = append(devices.UAPs, uap)
}
case "ugw", "usg": // in case they ever fix the name in the api.
if u.unmarshalDevice(assetType, &usg, r) == nil {
if usg := (USG{}); u.unmarshalDevice(assetType, r, &usg) == nil {
devices.USGs = append(devices.USGs, usg)
}
case "usw":
if u.unmarshalDevice(assetType, &usw, r) == nil {
if usw := (USW{}); u.unmarshalDevice(assetType, r, &usw) == nil {
devices.USWs = append(devices.USWs, usw)
}
default:
u.eLogf("unknown asset type - %v - skipping", assetType)
continue
}
}
return devices
}
// unmarshalDevice handles logging for the unmarshal operations in parseDevices().
func (u *Unifi) unmarshalDevice(device string, ptr interface{}, payload json.RawMessage) error {
err := json.Unmarshal(payload, ptr)
if err != nil {
u.eLogf("json.Unmarshal(%v): %v", device, err)
func (u *Unifi) unmarshalDevice(dev string, data json.RawMessage, v interface{}) (err error) {
if err = json.Unmarshal(data, v); err != nil {
u.eLogf("json.Unmarshal(%v): %v", dev, err)
u.eLogf("Enable Debug Logging to output the failed payload.")
json, err := payload.MarshalJSON()
json, err := data.MarshalJSON()
u.dLogf("Failed Payload: %s (marshal err: %v)", json, err)
u.dLogf("The above payload can prove useful during torubleshooting when you open an Issue:")
u.dLogf("==- https://github.com/golift/unifi/issues/new -==")

View File

@ -13,7 +13,6 @@ func (u UAP) Points() ([]*influx.Point, error) {
/* I generally suck at InfluxDB, so if I got the tags/fields wrong,
please send me a PR or open an Issue to address my faults. Thanks!
*/
var points []*influx.Point
tags := map[string]string{
"id": u.ID,
"mac": u.Mac,
@ -174,7 +173,7 @@ func (u UAP) Points() ([]*influx.Point, error) {
if err != nil {
return nil, err
}
points = append(points, pt)
points := []*influx.Point{pt}
for _, p := range u.RadioTable {
tags := map[string]string{
"device_name": u.Name,

View File

@ -10,7 +10,6 @@ import (
// Points generates Unifi Gateway datapoints for InfluxDB.
// These points can be passed directly to influx.
func (u USG) Points() ([]*influx.Point, error) {
var points []*influx.Point
tags := map[string]string{
"id": u.ID,
"mac": u.Mac,
@ -130,7 +129,7 @@ func (u USG) Points() ([]*influx.Point, error) {
if err != nil {
return nil, err
}
points = append(points, pt)
points := []*influx.Point{pt}
for _, p := range u.NetworkTable {
tags := map[string]string{
"device_name": u.Name,
@ -183,5 +182,5 @@ func (u USG) Points() ([]*influx.Point, error) {
}
points = append(points, pt)
}
return points, err
return points, nil
}

View File

@ -10,7 +10,6 @@ import (
// Points generates Unifi Switch datapoints for InfluxDB.
// These points can be passed directly to influx.
func (u USW) Points() ([]*influx.Point, error) {
var points []*influx.Point
tags := map[string]string{
"id": u.ID,
"mac": u.Mac,
@ -112,8 +111,8 @@ func (u USW) Points() ([]*influx.Point, error) {
// Add the port stats too.
}
pt, err := influx.NewPoint("usw", tags, fields, time.Now())
if err == nil {
points = append(points, pt)
if err != nil {
return nil, err
}
return points, err
return []*influx.Point{pt}, nil
}