IDS code cleanup

This commit is contained in:
davidnewhall2 2019-07-11 01:09:58 -07:00
parent b9a2a014fc
commit 085ce28943
2 changed files with 53 additions and 36 deletions

View File

@ -90,10 +90,22 @@ type IDS struct {
func (u *Unifi) GetIDS(sites []Site, from, to time.Time) ([]IDS, error) { func (u *Unifi) GetIDS(sites []Site, from, to time.Time) ([]IDS, error) {
data := []IDS{} data := []IDS{}
for _, site := range sites { for _, site := range sites {
u.DebugLog("Polling Controller for IDS/IPS Data, site %s (%s) ", site.Name, site.Desc)
ids, err := u.GetSiteIDS(site, from, to)
if err != nil {
return data, err
}
data = append(data, ids...)
}
return data, nil
}
// GetSiteIDS is a helper to offload the for-loop work.
// This method retreives the Intrusion Detection System Data for 1 Site.
func (u *Unifi) GetSiteIDS(site Site, from, to time.Time) ([]IDS, error) {
var response struct { var response struct {
Data []IDS `json:"data"` Data []IDS `json:"data"`
} }
u.DebugLog("Polling Controller, retreiving Unifi IDS/IPS Data, site %s (%s) ", site.Name, site.Desc)
URIpath := fmt.Sprintf(IPSEvents, site.Name) URIpath := fmt.Sprintf(IPSEvents, site.Name)
params := fmt.Sprintf(`{"start":"%v000","end":"%v000","_limit":50000}`, from.Unix(), to.Unix()) params := fmt.Sprintf(`{"start":"%v000","end":"%v000","_limit":50000}`, from.Unix(), to.Unix())
req, err := u.UniReq(URIpath, params) req, err := u.UniReq(URIpath, params)
@ -120,10 +132,15 @@ func (u *Unifi) GetIDS(sites []Site, from, to time.Time) ([]IDS, error) {
for i := range response.Data { for i := range response.Data {
response.Data[i].SiteName = site.SiteName response.Data[i].SiteName = site.SiteName
} }
data = append(data, response.Data...) return response.Data, nil
u.DebugLog("Found %d IDS entries. %s", len(data), params)
} }
return data, nil
// PointsAt has no usefulness. It is provided to satisfy external interfaces.
// These events have a timestamp, so that is used instead of any passed-in value.
// This method generates intrusion detection datapoints for InfluxDB.
// These points can be passed directly to influx.
func (i IDS) PointsAt(now time.Time) ([]*influx.Point, error) {
return i.Points()
} }
// Points generates intrusion detection datapoints for InfluxDB. // Points generates intrusion detection datapoints for InfluxDB.

View File

@ -1,4 +1,4 @@
// Package unifi provides a set of types to unload (unmarshal) Unifi Ubiquiti // Package unifi provides a set of types to unload (unmarshal) Ubiquiti UniFi
// controller data. Also provided are methods to easily get data for devices - // controller data. Also provided are methods to easily get data for devices -
// things like access points and switches, and for clients - the things // things like access points and switches, and for clients - the things
// connected to those access points and switches. As a bonus, each device and // connected to those access points and switches. As a bonus, each device and
@ -70,14 +70,14 @@ func (u *Unifi) getServer() error {
return u.GetData(StatusPath, &response) return u.GetData(StatusPath, &response)
} }
// GetClients returns a response full of clients' data from the Unifi Controller. // GetClients returns a response full of clients' data from the UniFi Controller.
func (u *Unifi) GetClients(sites []Site) (Clients, error) { func (u *Unifi) GetClients(sites []Site) (Clients, error) {
data := make([]Client, 0) data := make([]Client, 0)
for _, site := range sites { for _, site := range sites {
var response struct { var response struct {
Data []Client `json:"data"` Data []Client `json:"data"`
} }
u.DebugLog("Polling Controller, retreiving Unifi Clients, site %s (%s) ", site.Name, site.Desc) u.DebugLog("Polling Controller, retreiving UniFi Clients, site %s (%s) ", site.Name, site.Desc)
clientPath := fmt.Sprintf(ClientPath, site.Name) clientPath := fmt.Sprintf(ClientPath, site.Name)
if err := u.GetData(clientPath, &response); err != nil { if err := u.GetData(clientPath, &response); err != nil {
return nil, err return nil, err
@ -94,7 +94,7 @@ func (u *Unifi) GetClients(sites []Site) (Clients, error) {
return data, nil return data, nil
} }
// GetDevices returns a response full of devices' data from the Unifi Controller. // GetDevices returns a response full of devices' data from the UniFi Controller.
func (u *Unifi) GetDevices(sites []Site) (*Devices, error) { func (u *Unifi) GetDevices(sites []Site) (*Devices, error) {
devices := new(Devices) devices := new(Devices)
for _, site := range sites { for _, site := range sites {
@ -113,7 +113,7 @@ func (u *Unifi) GetDevices(sites []Site) (*Devices, error) {
return devices, nil return devices, nil
} }
// GetSites returns a list of configured sites on the Unifi controller. // GetSites returns a list of configured sites on the UniFi controller.
func (u *Unifi) GetSites() (Sites, error) { func (u *Unifi) GetSites() (Sites, error) {
var response struct { var response struct {
Data []Site `json:"data"` Data []Site `json:"data"`
@ -144,7 +144,7 @@ func (u *Unifi) GetData(methodPath string, v interface{}) error {
} }
// UniReq is a small helper function that adds an Accept header. // UniReq is a small helper function that adds an Accept header.
// Use this if you're unmarshalling Unifi data into custom types. // Use this if you're unmarshalling UniFi data into custom types.
// And if you're doing that... sumbut a pull request with your new struct. :) // And if you're doing that... sumbut a pull request with your new struct. :)
// This is a helper method that is exposed for convenience. // This is a helper method that is exposed for convenience.
func (u *Unifi) UniReq(apiPath string, params string) (req *http.Request, err error) { func (u *Unifi) UniReq(apiPath string, params string) (req *http.Request, err error) {