Add source parameter

This commit is contained in:
davidnewhall2 2019-12-15 13:41:09 -08:00
parent 7026de1398
commit f287cf6d0c
8 changed files with 19 additions and 8 deletions

View File

@ -19,6 +19,8 @@ func (u *Unifi) GetClients(sites Sites) (Clients, error) {
} }
for i, d := range response.Data { for i, d := range response.Data {
// Add special SourceName value.
response.Data[i].SourceName = u.URL
// Add the special "Site Name" to each client. This becomes a Grafana filter somewhere. // Add the special "Site Name" to each client. This becomes a Grafana filter somewhere.
response.Data[i].SiteName = site.Desc + " (" + site.Name + ")" response.Data[i].SiteName = site.Desc + " (" + site.Name + ")"
// Fix name and hostname fields. Sometimes one or the other is blank. // Fix name and hostname fields. Sometimes one or the other is blank.
@ -37,6 +39,7 @@ type Clients []*Client
// Client defines all the data a connected-network client contains. // Client defines all the data a connected-network client contains.
type Client struct { type Client struct {
SourceName string `json:"-"`
Anomalies int64 `json:"anomalies,omitempty"` Anomalies int64 `json:"anomalies,omitempty"`
ApMac string `json:"ap_mac"` ApMac string `json:"ap_mac"`
ApName string `json:"-"` ApName string `json:"-"`

View File

@ -48,25 +48,25 @@ func (u *Unifi) parseDevices(data []json.RawMessage, siteName string) *Devices {
switch assetType { // Unmarshal again into the correct type.. switch assetType { // Unmarshal again into the correct type..
case "uap": case "uap":
dev := &UAP{SiteName: siteName} dev := &UAP{SiteName: siteName, SourceName: u.URL}
if u.unmarshalDevice(assetType, r, dev) == nil { if u.unmarshalDevice(assetType, r, dev) == nil {
dev.Name = pick(dev.Name, dev.Mac) dev.Name = pick(dev.Name, dev.Mac)
devices.UAPs = append(devices.UAPs, dev) devices.UAPs = append(devices.UAPs, dev)
} }
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.
dev := &USG{SiteName: siteName} dev := &USG{SiteName: siteName, SourceName: u.URL}
if u.unmarshalDevice(assetType, r, dev) == nil { if u.unmarshalDevice(assetType, r, dev) == nil {
dev.Name = pick(dev.Name, dev.Mac) dev.Name = pick(dev.Name, dev.Mac)
devices.USGs = append(devices.USGs, dev) devices.USGs = append(devices.USGs, dev)
} }
case "usw": case "usw":
dev := &USW{SiteName: siteName} dev := &USW{SiteName: siteName, SourceName: u.URL}
if u.unmarshalDevice(assetType, r, dev) == nil { if u.unmarshalDevice(assetType, r, dev) == nil {
dev.Name = pick(dev.Name, dev.Mac) dev.Name = pick(dev.Name, dev.Mac)
devices.USWs = append(devices.USWs, dev) devices.USWs = append(devices.USWs, dev)
} }
case "udm": case "udm":
dev := &UDM{SiteName: siteName} dev := &UDM{SiteName: siteName, SourceName: u.URL}
if u.unmarshalDevice(assetType, r, dev) == nil { if u.unmarshalDevice(assetType, r, dev) == nil {
dev.Name = pick(dev.Name, dev.Mac) dev.Name = pick(dev.Name, dev.Mac)
devices.UDMs = append(devices.UDMs, dev) devices.UDMs = append(devices.UDMs, dev)

View File

@ -13,6 +13,7 @@ type IDSList []*IDS
// IDS holds an Intrusion Prevention System Event. // IDS holds an Intrusion Prevention System Event.
type IDS struct { type IDS struct {
SourceName string `json:"-"`
ID string `json:"_id"` ID string `json:"_id"`
Archived FlexBool `json:"archived"` Archived FlexBool `json:"archived"`
Timestamp int64 `json:"timestamp"` Timestamp int64 `json:"timestamp"`
@ -94,7 +95,9 @@ func (u *Unifi) GetIDS(sites Sites, from, to time.Time) ([]*IDS, error) {
if err != nil { if err != nil {
return data, err return data, err
} }
for i := range ids {
ids[i].SourceName = u.URL
}
data = append(data, ids...) data = append(data, ids...)
} }

View File

@ -15,6 +15,8 @@ func (u *Unifi) GetSites() (Sites, error) {
sites := []string{} // used for debug log only sites := []string{} // used for debug log only
for i, d := range response.Data { for i, d := range response.Data {
// Add special SourceName value.
response.Data[i].SourceName = u.URL
// If the human name is missing (description), set it to the cryptic name. // If the human name is missing (description), set it to the cryptic name.
response.Data[i].Desc = pick(d.Desc, d.Name) response.Data[i].Desc = pick(d.Desc, d.Name)
// Add the custom site name to each site. used as a Grafana filter somewhere. // Add the custom site name to each site. used as a Grafana filter somewhere.
@ -32,6 +34,7 @@ type Sites []*Site
// Site represents a site's data. // Site represents a site's data.
type Site struct { type Site struct {
SourceName string `json:"-"`
ID string `json:"_id"` ID string `json:"_id"`
Name string `json:"name"` Name string `json:"name"`
Desc string `json:"desc"` Desc string `json:"desc"`

View File

@ -6,10 +6,9 @@ import (
) )
// UAP represents all the data from the Ubiquiti Controller for a Unifi Access Point. // UAP represents all the data from the Ubiquiti Controller for a Unifi Access Point.
// This was auto generated then edited by hand to get all the data types right.
type UAP struct { type UAP struct {
/* This was auto generated and then slowly edited by hand SourceName string `json:"-"`
to get all the data types right and graphable.
*/
ID string `json:"_id"` ID string `json:"_id"`
Adopted FlexBool `json:"adopted"` Adopted FlexBool `json:"adopted"`
AntennaTable []struct { AntennaTable []struct {

View File

@ -3,6 +3,7 @@ package unifi
// UDM represents all the data from the Ubiquiti Controller for a Unifi Dream Machine. // UDM represents all the data from the Ubiquiti Controller for a Unifi Dream Machine.
// The UDM shares several structs/type-data with USW and USG. // The UDM shares several structs/type-data with USW and USG.
type UDM struct { type UDM struct {
SourceName string `json:"-"`
SiteID string `json:"site_id"` SiteID string `json:"site_id"`
SiteName string `json:"-"` SiteName string `json:"-"`
Mac string `json:"mac"` Mac string `json:"mac"`

View File

@ -7,6 +7,7 @@ import (
// USG represents all the data from the Ubiquiti Controller for a Unifi Security Gateway. // USG represents all the data from the Ubiquiti Controller for a Unifi Security Gateway.
type USG struct { type USG struct {
SourceName string `json:"-"`
ID string `json:"_id"` ID string `json:"_id"`
Adopted FlexBool `json:"adopted"` Adopted FlexBool `json:"adopted"`
Cfgversion string `json:"cfgversion"` Cfgversion string `json:"cfgversion"`

View File

@ -7,6 +7,7 @@ import (
// USW represents all the data from the Ubiquiti Controller for a Unifi Switch. // USW represents all the data from the Ubiquiti Controller for a Unifi Switch.
type USW struct { type USW struct {
SourceName string `json:"-"`
SiteName string `json:"-"` SiteName string `json:"-"`
ID string `json:"_id"` ID string `json:"_id"`
Adopted FlexBool `json:"adopted"` Adopted FlexBool `json:"adopted"`