a little more cleanup
This commit is contained in:
parent
d0a1460226
commit
ac156bcd12
|
|
@ -10,15 +10,15 @@ import (
|
||||||
// Points generates Unifi Client datapoints for InfluxDB.
|
// Points generates Unifi Client datapoints for InfluxDB.
|
||||||
// These points can be passed directly to influx.
|
// These points can be passed directly to influx.
|
||||||
func (c UCL) Points() ([]*influx.Point, error) {
|
func (c UCL) Points() ([]*influx.Point, error) {
|
||||||
var points []*influx.Point
|
|
||||||
// Fix name and hostname fields. Sometimes one or the other is blank.
|
// Fix name and hostname fields. Sometimes one or the other is blank.
|
||||||
if c.Name == "" && c.Hostname != "" {
|
switch {
|
||||||
c.Name = c.Hostname
|
case c.Hostname == "" && c.Name == "":
|
||||||
} else if c.Hostname == "" && c.Name != "" {
|
|
||||||
c.Hostname = c.Name
|
|
||||||
} else if c.Hostname == "" && c.Name == "" {
|
|
||||||
c.Hostname = "-no-name-"
|
c.Hostname = "-no-name-"
|
||||||
c.Name = "-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{
|
tags := map[string]string{
|
||||||
"id": c.ID,
|
"id": c.ID,
|
||||||
|
|
@ -100,7 +100,7 @@ func (c UCL) Points() ([]*influx.Point, error) {
|
||||||
}
|
}
|
||||||
pt, err := influx.NewPoint("clients", tags, fields, time.Now())
|
pt, err := influx.NewPoint("clients", tags, fields, time.Now())
|
||||||
if err == nil {
|
if err == nil {
|
||||||
points = append(points, pt)
|
return nil, err
|
||||||
}
|
}
|
||||||
return points, err
|
return []*influx.Point{pt}, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,47 +7,40 @@ func (u *Unifi) parseDevices(data []json.RawMessage) *Devices {
|
||||||
devices := new(Devices)
|
devices := new(Devices)
|
||||||
for _, r := range data {
|
for _, r := range data {
|
||||||
// Loop each item in the raw JSON message, detect its type and unmarshal it.
|
// Loop each item in the raw JSON message, detect its type and unmarshal it.
|
||||||
var obj map[string]interface{}
|
assetType := "<type key missing>"
|
||||||
var uap UAP
|
if o := make(map[string]interface{}); u.unmarshalDevice("map", r, &o) != nil {
|
||||||
var usg USG
|
|
||||||
var usw USW
|
|
||||||
|
|
||||||
if u.unmarshalDevice("interface{}", &obj, r) != nil {
|
|
||||||
continue
|
continue
|
||||||
}
|
} else if t, ok := o["type"].(string); ok {
|
||||||
assetType := "<missing>"
|
|
||||||
if t, ok := obj["type"].(string); ok {
|
|
||||||
assetType = t
|
assetType = t
|
||||||
}
|
}
|
||||||
u.dLogf("Unmarshalling Device Type: %v", assetType)
|
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..
|
switch assetType { // Unmarshal again into the correct type..
|
||||||
case "uap":
|
case "uap":
|
||||||
if u.unmarshalDevice(assetType, &uap, r) == nil {
|
if uap := (UAP{}); u.unmarshalDevice(assetType, r, &uap) == nil {
|
||||||
devices.UAPs = append(devices.UAPs, uap)
|
devices.UAPs = append(devices.UAPs, uap)
|
||||||
}
|
}
|
||||||
case "ugw", "usg": // in case they ever fix the name in the api.
|
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)
|
devices.USGs = append(devices.USGs, usg)
|
||||||
}
|
}
|
||||||
case "usw":
|
case "usw":
|
||||||
if u.unmarshalDevice(assetType, &usw, r) == nil {
|
if usw := (USW{}); u.unmarshalDevice(assetType, r, &usw) == nil {
|
||||||
devices.USWs = append(devices.USWs, usw)
|
devices.USWs = append(devices.USWs, usw)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
u.eLogf("unknown asset type - %v - skipping", assetType)
|
u.eLogf("unknown asset type - %v - skipping", assetType)
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return devices
|
return devices
|
||||||
}
|
}
|
||||||
|
|
||||||
// unmarshalDevice handles logging for the unmarshal operations in parseDevices().
|
// unmarshalDevice handles logging for the unmarshal operations in parseDevices().
|
||||||
func (u *Unifi) unmarshalDevice(device string, ptr interface{}, payload json.RawMessage) error {
|
func (u *Unifi) unmarshalDevice(dev string, data json.RawMessage, v interface{}) (err error) {
|
||||||
err := json.Unmarshal(payload, ptr)
|
if err = json.Unmarshal(data, v); err != nil {
|
||||||
if err != nil {
|
u.eLogf("json.Unmarshal(%v): %v", dev, err)
|
||||||
u.eLogf("json.Unmarshal(%v): %v", device, err)
|
|
||||||
u.eLogf("Enable Debug Logging to output the failed payload.")
|
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("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("The above payload can prove useful during torubleshooting when you open an Issue:")
|
||||||
u.dLogf("==- https://github.com/golift/unifi/issues/new -==")
|
u.dLogf("==- https://github.com/golift/unifi/issues/new -==")
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,6 @@ func (u UAP) Points() ([]*influx.Point, error) {
|
||||||
/* I generally suck at InfluxDB, so if I got the tags/fields wrong,
|
/* 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!
|
please send me a PR or open an Issue to address my faults. Thanks!
|
||||||
*/
|
*/
|
||||||
var points []*influx.Point
|
|
||||||
tags := map[string]string{
|
tags := map[string]string{
|
||||||
"id": u.ID,
|
"id": u.ID,
|
||||||
"mac": u.Mac,
|
"mac": u.Mac,
|
||||||
|
|
@ -174,7 +173,7 @@ func (u UAP) Points() ([]*influx.Point, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
points = append(points, pt)
|
points := []*influx.Point{pt}
|
||||||
for _, p := range u.RadioTable {
|
for _, p := range u.RadioTable {
|
||||||
tags := map[string]string{
|
tags := map[string]string{
|
||||||
"device_name": u.Name,
|
"device_name": u.Name,
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,6 @@ import (
|
||||||
// Points generates Unifi Gateway datapoints for InfluxDB.
|
// Points generates Unifi Gateway datapoints for InfluxDB.
|
||||||
// These points can be passed directly to influx.
|
// These points can be passed directly to influx.
|
||||||
func (u USG) Points() ([]*influx.Point, error) {
|
func (u USG) Points() ([]*influx.Point, error) {
|
||||||
var points []*influx.Point
|
|
||||||
tags := map[string]string{
|
tags := map[string]string{
|
||||||
"id": u.ID,
|
"id": u.ID,
|
||||||
"mac": u.Mac,
|
"mac": u.Mac,
|
||||||
|
|
@ -130,7 +129,7 @@ func (u USG) Points() ([]*influx.Point, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
points = append(points, pt)
|
points := []*influx.Point{pt}
|
||||||
for _, p := range u.NetworkTable {
|
for _, p := range u.NetworkTable {
|
||||||
tags := map[string]string{
|
tags := map[string]string{
|
||||||
"device_name": u.Name,
|
"device_name": u.Name,
|
||||||
|
|
@ -183,5 +182,5 @@ func (u USG) Points() ([]*influx.Point, error) {
|
||||||
}
|
}
|
||||||
points = append(points, pt)
|
points = append(points, pt)
|
||||||
}
|
}
|
||||||
return points, err
|
return points, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,6 @@ import (
|
||||||
// Points generates Unifi Switch datapoints for InfluxDB.
|
// Points generates Unifi Switch datapoints for InfluxDB.
|
||||||
// These points can be passed directly to influx.
|
// These points can be passed directly to influx.
|
||||||
func (u USW) Points() ([]*influx.Point, error) {
|
func (u USW) Points() ([]*influx.Point, error) {
|
||||||
var points []*influx.Point
|
|
||||||
tags := map[string]string{
|
tags := map[string]string{
|
||||||
"id": u.ID,
|
"id": u.ID,
|
||||||
"mac": u.Mac,
|
"mac": u.Mac,
|
||||||
|
|
@ -112,8 +111,8 @@ func (u USW) Points() ([]*influx.Point, error) {
|
||||||
// Add the port stats too.
|
// Add the port stats too.
|
||||||
}
|
}
|
||||||
pt, err := influx.NewPoint("usw", tags, fields, time.Now())
|
pt, err := influx.NewPoint("usw", tags, fields, time.Now())
|
||||||
if err == nil {
|
if err != nil {
|
||||||
points = append(points, pt)
|
return nil, err
|
||||||
}
|
}
|
||||||
return points, err
|
return []*influx.Point{pt}, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue