mirror of
https://github.com/unpoller/unpoller.git
synced 2026-04-05 08:54:00 -04:00
Shuffle things around a bit more.
This commit is contained in:
188
main.go
188
main.go
@@ -5,104 +5,16 @@ import (
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/cookiejar"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
influx "github.com/influxdata/influxdb/client/v2"
|
||||
)
|
||||
|
||||
// Point generates a client's datapoint for InfluxDB.
|
||||
func (c Client) Point() *influx.Point {
|
||||
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 == "" {
|
||||
c.Hostname = "-no-name-"
|
||||
c.Name = "-no-name-"
|
||||
}
|
||||
tags := map[string]string{
|
||||
"id": c.ID,
|
||||
"mac": c.Mac,
|
||||
"user_id": c.UserID,
|
||||
"site_id": c.SiteID,
|
||||
"ip": c.IP,
|
||||
"fixed_ip": c.FixedIP,
|
||||
"essid": c.Essid,
|
||||
"bssid": c.Bssid,
|
||||
"network": c.Network,
|
||||
"network_id": c.NetworkID,
|
||||
"usergroup_id": c.UserGroupID,
|
||||
"ap_mac": c.ApMac,
|
||||
"gw_mac": c.GwMac,
|
||||
"sw_mac": c.SwMac,
|
||||
"sw_port": strconv.Itoa(c.SwPort),
|
||||
"oui": c.Oui,
|
||||
"name": c.Name,
|
||||
"hostname": c.Hostname,
|
||||
"radio_name": c.RadioName,
|
||||
"radio": c.Radio,
|
||||
"radio_proto": c.RadioProto,
|
||||
"authorized": strconv.FormatBool(c.Authorized),
|
||||
"is_11r": strconv.FormatBool(c.Is11R),
|
||||
"is_wired": strconv.FormatBool(c.IsWired),
|
||||
"is_guest": strconv.FormatBool(c.IsGuest),
|
||||
"is_guest_by_uap": strconv.FormatBool(c.IsGuestByUAP),
|
||||
"is_guest_by_ugw": strconv.FormatBool(c.IsGuestByUGW),
|
||||
"is_guest_by_usw": strconv.FormatBool(c.IsGuestByUSW),
|
||||
"noted": strconv.FormatBool(c.Noted),
|
||||
"powersave_enabled": strconv.FormatBool(c.PowersaveEnabled),
|
||||
"qos_policy_applied": strconv.FormatBool(c.QosPolicyApplied),
|
||||
"use_fixedip": strconv.FormatBool(c.UseFixedIP),
|
||||
"channel": strconv.Itoa(c.Channel),
|
||||
"vlan": strconv.Itoa(c.Vlan),
|
||||
}
|
||||
fields := map[string]interface{}{
|
||||
"dpi_stats_last_updated": c.DpiStatsLastUpdated,
|
||||
"last_seen_by_uap": c.LastSeenByUAP,
|
||||
"last_seen_by_ugw": c.LastSeenByUGW,
|
||||
"last_seen_by_usw": c.LastSeenByUSW,
|
||||
"uptime_by_uap": c.UptimeByUAP,
|
||||
"uptime_by_ugw": c.UptimeByUGW,
|
||||
"uptime_by_usw": c.UptimeByUSW,
|
||||
"assoc_time": c.AssocTime,
|
||||
"bytes_r": c.BytesR,
|
||||
"ccq": c.Ccq,
|
||||
"first_seen": c.FirstSeen,
|
||||
"idle_time": c.IdleTime,
|
||||
"last_seen": c.LastSeen,
|
||||
"latest_assoc_time": c.LatestAssocTime,
|
||||
"noise": c.Noise,
|
||||
"note": c.Note,
|
||||
"roam_count": c.RoamCount,
|
||||
"rssi": c.Rssi,
|
||||
"rx_bytes": c.RxBytes,
|
||||
"rx_bytes_r": c.RxBytesR,
|
||||
"rx_packets": c.RxPackets,
|
||||
"rx_rate": c.RxRate,
|
||||
"signal": c.Signal,
|
||||
"tx_bytes": c.TxBytes,
|
||||
"tx_bytes_r": c.TxBytesR,
|
||||
"tx_packets": c.TxPackets,
|
||||
"tx_power": c.TxPower,
|
||||
"tx_rate": c.TxRate,
|
||||
"uptime": c.Uptime,
|
||||
}
|
||||
|
||||
pt, err := influx.NewPoint("clients", tags, fields, time.Now())
|
||||
if err != nil {
|
||||
log.Println("Error creating point:", err)
|
||||
return nil
|
||||
}
|
||||
return pt
|
||||
}
|
||||
|
||||
func main() {
|
||||
config := GetConfig()
|
||||
if err := config.AuthController(); err != nil {
|
||||
@@ -122,6 +34,48 @@ func main() {
|
||||
config.PollUnifiController(infdb)
|
||||
}
|
||||
|
||||
// GetConfig parses and returns our configuration data.
|
||||
func GetConfig() Config {
|
||||
// TODO: A real config file.
|
||||
var err error
|
||||
config := Config{
|
||||
InfluxAddr: os.Getenv("INFLUXDB_ADDR"),
|
||||
InfluxUser: os.Getenv("INFLUXDB_USERNAME"),
|
||||
InfluxPass: os.Getenv("INFLUXDB_PASSWORD"),
|
||||
InfluxDB: os.Getenv("INFLUXDB_DATABASE"),
|
||||
UnifiUser: os.Getenv("UNIFI_USERNAME"),
|
||||
UnifiPass: os.Getenv("UNIFI_PASSWORD"),
|
||||
UnifiBase: "https://" + os.Getenv("UNIFI_ADDR") + ":" + os.Getenv("UNIFI_PORT"),
|
||||
}
|
||||
if config.Interval, err = time.ParseDuration(os.Getenv("INTERVAL")); err != nil {
|
||||
log.Println("Invalid Interval, defaulting to 15 seconds.")
|
||||
config.Interval = time.Duration(time.Second * 15)
|
||||
}
|
||||
return config
|
||||
}
|
||||
|
||||
// AuthController creates a http.Client with authenticated cookies.
|
||||
// Used to make additional, authenticated requests to the APIs.
|
||||
func (c *Config) AuthController() error {
|
||||
json := `{"username": "` + c.UnifiUser + `","password": "` + c.UnifiPass + `"}`
|
||||
jar, err := cookiejar.New(nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.uniClient = &http.Client{
|
||||
Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}},
|
||||
Jar: jar,
|
||||
}
|
||||
if req, err := c.uniRequest(LoginPath, json); err != nil {
|
||||
return err
|
||||
} else if resp, err := c.uniClient.Do(req); err != nil {
|
||||
return err
|
||||
} else if resp.StatusCode != http.StatusOK {
|
||||
return errors.New("Error Authenticating with Unifi Controller")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// PollUnifiController runs forever, polling and pushing.
|
||||
func (c *Config) PollUnifiController(infdb influx.Client) {
|
||||
ticker := time.NewTicker(c.Interval)
|
||||
@@ -150,30 +104,10 @@ func (c *Config) PollUnifiController(infdb influx.Client) {
|
||||
}
|
||||
}
|
||||
|
||||
// GetConfig parses and returns our configuration data.
|
||||
func GetConfig() Config {
|
||||
// TODO: A real config file.
|
||||
var err error
|
||||
config := Config{
|
||||
InfluxAddr: os.Getenv("INFLUXDB_ADDR"),
|
||||
InfluxUser: os.Getenv("INFLUXDB_USERNAME"),
|
||||
InfluxPass: os.Getenv("INFLUXDB_PASSWORD"),
|
||||
InfluxDB: os.Getenv("INFLUXDB_DATABASE"),
|
||||
UnifiUser: os.Getenv("UNIFI_USERNAME"),
|
||||
UnifiPass: os.Getenv("UNIFI_PASSWORD"),
|
||||
UnifiBase: "https://" + os.Getenv("UNIFI_ADDR") + ":" + os.Getenv("UNIFI_PORT"),
|
||||
}
|
||||
if config.Interval, err = time.ParseDuration(os.Getenv("INTERVAL")); err != nil {
|
||||
log.Println("Invalid Interval, defaulting to 15 seconds.")
|
||||
config.Interval = time.Duration(time.Second * 15)
|
||||
}
|
||||
return config
|
||||
}
|
||||
|
||||
// GetUnifiClients returns a response full of clients' data from the Unifi Controller.
|
||||
func (c *Config) GetUnifiClients() ([]Client, error) {
|
||||
response := &ClientResponse{}
|
||||
if req, err := uniRequest("GET", c.UnifiBase+ClientPath, nil); err != nil {
|
||||
if req, err := c.uniRequest(ClientPath, ""); err != nil {
|
||||
return nil, err
|
||||
} else if resp, err := c.uniClient.Do(req); err != nil {
|
||||
return nil, err
|
||||
@@ -187,33 +121,15 @@ func (c *Config) GetUnifiClients() ([]Client, error) {
|
||||
return response.Clients, nil
|
||||
}
|
||||
|
||||
// AuthController creates a http.Client with authenticated cookies.
|
||||
// Used to make additional, authenticated requests to the APIs.
|
||||
func (c *Config) AuthController() error {
|
||||
json := bytes.NewBufferString(`{"username": "` + c.UnifiUser + `","password": "` + c.UnifiPass + `"}`)
|
||||
jar, err := cookiejar.New(nil)
|
||||
if err != nil {
|
||||
return err
|
||||
// uniRequest is a small helper function that adds an Accept header.
|
||||
func (c *Config) uniRequest(url string, params string) (req *http.Request, err error) {
|
||||
if params != "" {
|
||||
req, err = http.NewRequest("POST", c.UnifiBase+url, bytes.NewBufferString(params))
|
||||
} else {
|
||||
req, err = http.NewRequest("GET", c.UnifiBase+url, nil)
|
||||
}
|
||||
c.uniClient = &http.Client{
|
||||
Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}},
|
||||
Jar: jar,
|
||||
if err == nil {
|
||||
req.Header.Add("Accept", "application/json")
|
||||
}
|
||||
if req, err := uniRequest("POST", c.UnifiBase+LoginPath, json); err != nil {
|
||||
return err
|
||||
} else if resp, err := c.uniClient.Do(req); err != nil {
|
||||
return err
|
||||
} else if resp.StatusCode != http.StatusOK {
|
||||
return errors.New("Error Authenticating with Unifi Controller")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func uniRequest(method string, url string, data io.Reader) (*http.Request, error) {
|
||||
req, err := http.NewRequest(method, url, data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Add("Accept", "application/json")
|
||||
return req, nil
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user