Minor Cleanup
This commit is contained in:
		
							parent
							
								
									2eba122b4a
								
							
						
					
					
						commit
						25ef711166
					
				|  | @ -5,7 +5,6 @@ import ( | ||||||
| 	"crypto/tls" | 	"crypto/tls" | ||||||
| 	"encoding/json" | 	"encoding/json" | ||||||
| 	"errors" | 	"errors" | ||||||
| 	"fmt" |  | ||||||
| 	"io/ioutil" | 	"io/ioutil" | ||||||
| 	"log" | 	"log" | ||||||
| 	"net/http" | 	"net/http" | ||||||
|  | @ -21,6 +20,7 @@ var ( | ||||||
| 	stats influx.Client | 	stats influx.Client | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
|  | // Response marshalls the payload from the controller.
 | ||||||
| type Response struct { | type Response struct { | ||||||
| 	Data []Client | 	Data []Client | ||||||
| 	Meta struct { | 	Meta struct { | ||||||
|  | @ -28,7 +28,7 @@ type Response struct { | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // DpiStat is for deep packet inspection stats
 | // DpiStat is for deep packet inspection stats.
 | ||||||
| type DpiStat struct { | type DpiStat struct { | ||||||
| 	App       int64 | 	App       int64 | ||||||
| 	Cat       int64 | 	Cat       int64 | ||||||
|  | @ -38,6 +38,7 @@ type DpiStat struct { | ||||||
| 	TxPackets int64 | 	TxPackets int64 | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | // Client defines all the data a connected-network client contains.
 | ||||||
| type Client struct { | type Client struct { | ||||||
| 	ID                  string `json:"_id"` | 	ID                  string `json:"_id"` | ||||||
| 	IsGuestByUAP        bool   `json:"_is_guest_by_uap"` | 	IsGuestByUAP        bool   `json:"_is_guest_by_uap"` | ||||||
|  | @ -58,10 +59,10 @@ type Client struct { | ||||||
| 	Essid               string | 	Essid               string | ||||||
| 	FirstSeen           int64  `json:"first_seen"` | 	FirstSeen           int64  `json:"first_seen"` | ||||||
| 	FixedIP             string `json:"fixed_ip"` | 	FixedIP             string `json:"fixed_ip"` | ||||||
| 	Hostname            string | 	Hostname            string `json:"hostname"` | ||||||
| 	GwMac               string `json:"gw_mac"` | 	GwMac               string `json:"gw_mac"` | ||||||
| 	IdleTime            int64  `json:"idle_time"` | 	IdleTime            int64  `json:"idle_time"` | ||||||
| 	Ip                  string | 	IP                  string | ||||||
| 	IsGuest             bool  `json:"is_guest"` | 	IsGuest             bool  `json:"is_guest"` | ||||||
| 	IsWired             bool  `json:"is_wired"` | 	IsWired             bool  `json:"is_wired"` | ||||||
| 	LastSeen            int64 `json:"last_seen"` | 	LastSeen            int64 `json:"last_seen"` | ||||||
|  | @ -75,6 +76,7 @@ type Client struct { | ||||||
| 	PowersaveEnabled    bool `json:"powersave_enabled"` | 	PowersaveEnabled    bool `json:"powersave_enabled"` | ||||||
| 	QosPolicyApplied    bool `json:"qos_policy_applied"` | 	QosPolicyApplied    bool `json:"qos_policy_applied"` | ||||||
| 	Radio               string | 	Radio               string | ||||||
|  | 	RadioName           string `json:"radio_name"` | ||||||
| 	RadioProto          string `json:"radio_proto"` | 	RadioProto          string `json:"radio_proto"` | ||||||
| 	RoamCount           int64  `json:"roam_count"` | 	RoamCount           int64  `json:"roam_count"` | ||||||
| 	Rssi                int64 | 	Rssi                int64 | ||||||
|  | @ -94,16 +96,19 @@ type Client struct { | ||||||
| 	Vlan                int64 | 	Vlan                int64 | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | // Point generates a datapoint for InfluxDB.
 | ||||||
| func (c Client) Point() *influx.Point { | func (c Client) Point() *influx.Point { | ||||||
| 	tags := map[string]string{ | 	tags := map[string]string{ | ||||||
| 		"mac":        c.Mac, | 		"mac":        c.Mac, | ||||||
| 		"user_id":    c.UserID, | 		"user_id":    c.UserID, | ||||||
| 		"site_id":    c.SiteID, | 		"site_id":    c.SiteID, | ||||||
| 		"ip":      c.Ip, | 		"ip":         c.IP, | ||||||
| 		"essid":      c.Essid, | 		"essid":      c.Essid, | ||||||
| 		"network":    c.Network, | 		"network":    c.Network, | ||||||
| 		"ap_mac":     c.ApMac, | 		"ap_mac":     c.ApMac, | ||||||
| 		"name":       c.Name, | 		"name":       c.Name, | ||||||
|  | 		"hostname":   c.Hostname, | ||||||
|  | 		"radio_name": c.RadioName, | ||||||
| 	} | 	} | ||||||
| 	fields := map[string]interface{}{ | 	fields := map[string]interface{}{ | ||||||
| 		"is_guest_by_uap":        c.IsGuestByUAP, | 		"is_guest_by_uap":        c.IsGuestByUAP, | ||||||
|  | @ -141,6 +146,7 @@ func (c Client) Point() *influx.Point { | ||||||
| 
 | 
 | ||||||
| 	pt, err := influx.NewPoint("client_state", tags, fields, time.Now()) | 	pt, err := influx.NewPoint("client_state", tags, fields, time.Now()) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
|  | 		log.Println("Error creating point:", err) | ||||||
| 		return nil | 		return nil | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | @ -184,8 +190,7 @@ func main() { | ||||||
| 				bp.AddPoint(device.Point()) | 				bp.AddPoint(device.Point()) | ||||||
| 			} | 			} | ||||||
| 
 | 
 | ||||||
| 			err = stats.Write(bp) | 			if err = stats.Write(bp); err != nil { | ||||||
| 			if err != nil { |  | ||||||
| 				log.Println(err) | 				log.Println(err) | ||||||
| 			} | 			} | ||||||
| 
 | 
 | ||||||
|  | @ -197,16 +202,22 @@ func main() { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func fetch() ([]Client, error) { | func fetch() ([]Client, error) { | ||||||
| 	format := "https://%s:%s/api/s/default/stat/sta" | 	url := "https://" + os.Getenv("UNIFI_ADDR") + ":" + os.Getenv("UNIFI_PORT") + "/api/s/default/stat/sta" | ||||||
| 	url := fmt.Sprintf(format, os.Getenv("UNIFI_ADDR"), os.Getenv("UNIFI_PORT")) |  | ||||||
| 	req, err := http.NewRequest("GET", url, nil) | 	req, err := http.NewRequest("GET", url, nil) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return nil, err | ||||||
|  | 	} | ||||||
| 	req.Header.Add("Accept", "application/json") | 	req.Header.Add("Accept", "application/json") | ||||||
| 	resp, err := unifi.Do(req) | 	resp, err := unifi.Do(req) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	defer resp.Body.Close() | 	defer func() { | ||||||
|  | 		if err = resp.Body.Close(); err != nil { | ||||||
|  | 			log.Println("Error closing http respond body:", err) | ||||||
|  | 		} | ||||||
|  | 	}() | ||||||
| 	body, _ := ioutil.ReadAll(resp.Body) | 	body, _ := ioutil.ReadAll(resp.Body) | ||||||
| 	response := &Response{} | 	response := &Response{} | ||||||
| 	err = json.Unmarshal(body, response) | 	err = json.Unmarshal(body, response) | ||||||
|  | @ -218,7 +229,7 @@ func fetch() ([]Client, error) { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func login() (*http.Client, error) { | func login() (*http.Client, error) { | ||||||
| 	url := fmt.Sprintf("https://%s:%s/api/login", os.Getenv("UNIFI_ADDR"), os.Getenv("UNIFI_PORT")) | 	url := "https://" + os.Getenv("UNIFI_ADDR") + ":" + os.Getenv("UNIFI_PORT") + "/api/login" | ||||||
| 	auth := map[string]string{ | 	auth := map[string]string{ | ||||||
| 		"username": os.Getenv("UNIFI_USERNAME"), | 		"username": os.Getenv("UNIFI_USERNAME"), | ||||||
| 		"password": os.Getenv("UNIFI_PASSWORD"), | 		"password": os.Getenv("UNIFI_PASSWORD"), | ||||||
|  | @ -245,7 +256,7 @@ func login() (*http.Client, error) { | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	if resp.StatusCode != http.StatusOK { | 	if resp.StatusCode != http.StatusOK { | ||||||
| 		return nil, errors.New("Not a successful request") | 		return nil, errors.New("Error Authenticating with Controller") | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	return client, nil | 	return client, nil | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue