From 085ce28943a101ddb44dbaa8c57bbc0b195fef12 Mon Sep 17 00:00:00 2001 From: davidnewhall2 Date: Thu, 11 Jul 2019 01:09:58 -0700 Subject: [PATCH] IDS code cleanup --- core/unifi/ids.go | 77 +++++++++++++++++++++++++++------------------ core/unifi/unifi.go | 12 +++---- 2 files changed, 53 insertions(+), 36 deletions(-) diff --git a/core/unifi/ids.go b/core/unifi/ids.go index 4e0421fc..47da5aa2 100644 --- a/core/unifi/ids.go +++ b/core/unifi/ids.go @@ -90,42 +90,59 @@ type IDS struct { func (u *Unifi) GetIDS(sites []Site, from, to time.Time) ([]IDS, error) { data := []IDS{} for _, site := range sites { - var response struct { - 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) - params := fmt.Sprintf(`{"start":"%v000","end":"%v000","_limit":50000}`, from.Unix(), to.Unix()) - req, err := u.UniReq(URIpath, params) + 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 nil, err + return data, err } - resp, err := u.Do(req) - if err != nil { - return nil, err - } - defer func() { - _ = resp.Body.Close() - }() - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return nil, err - } - if resp.StatusCode != http.StatusOK { - return nil, errors.Errorf("invalid status code from server %s", resp.Status) - } - if err := json.Unmarshal(body, &response); err != nil { - return nil, err - } - for i := range response.Data { - response.Data[i].SiteName = site.SiteName - } - data = append(data, response.Data...) - u.DebugLog("Found %d IDS entries. %s", len(data), params) + 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 { + Data []IDS `json:"data"` + } + URIpath := fmt.Sprintf(IPSEvents, site.Name) + params := fmt.Sprintf(`{"start":"%v000","end":"%v000","_limit":50000}`, from.Unix(), to.Unix()) + req, err := u.UniReq(URIpath, params) + if err != nil { + return nil, err + } + resp, err := u.Do(req) + if err != nil { + return nil, err + } + defer func() { + _ = resp.Body.Close() + }() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + if resp.StatusCode != http.StatusOK { + return nil, errors.Errorf("invalid status code from server %s", resp.Status) + } + if err := json.Unmarshal(body, &response); err != nil { + return nil, err + } + for i := range response.Data { + response.Data[i].SiteName = site.SiteName + } + return response.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. // These points can be passed directly to influx. func (i IDS) Points() ([]*influx.Point, error) { diff --git a/core/unifi/unifi.go b/core/unifi/unifi.go index 25e08683..8d985f21 100644 --- a/core/unifi/unifi.go +++ b/core/unifi/unifi.go @@ -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 - // things like access points and switches, and for clients - the things // 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) } -// 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) { data := make([]Client, 0) for _, site := range sites { var response struct { 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) if err := u.GetData(clientPath, &response); err != nil { return nil, err @@ -94,7 +94,7 @@ func (u *Unifi) GetClients(sites []Site) (Clients, error) { 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) { devices := new(Devices) for _, site := range sites { @@ -113,7 +113,7 @@ func (u *Unifi) GetDevices(sites []Site) (*Devices, error) { 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) { var response struct { 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. -// 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. :) // This is a helper method that is exposed for convenience. func (u *Unifi) UniReq(apiPath string, params string) (req *http.Request, err error) {