Add support for alluser

This commit is contained in:
spsobole 2021-08-12 12:46:14 -06:00
parent 551cf71062
commit fb3f2e5578
3 changed files with 80 additions and 0 deletions

View File

@ -28,6 +28,8 @@ const (
APIClientDPI string = "/api/s/%s/stat/stadpi"
// APIClientPath is Unifi Clients API Path.
APIClientPath string = "/api/s/%s/stat/sta"
// APIAllUserPath is Unifi Insight all previous Clients API Path.
APIAllUserPath string = "/api/s/%s/stat/alluser"
// APINetworkPath is where we get data about Unifi networks.
APINetworkPath string = "/api/s/%s/rest/networkconf"
// APIDevicePath is where we get data about Unifi devices.
@ -36,6 +38,8 @@ const (
APILoginPath string = "/api/login"
// APILoginPathNew is how we log into UDM 5.12.55+.
APILoginPathNew string = "/api/auth/login"
// APILogoutPath is the how we logout from UDM
APILogoutPath string = "/api/auth/logout"
// APIEventPathIDS returns Intrusion Detection/Prevention Systems Events.
APIEventPathIDS string = "/api/s/%s/stat/ips/event"
// APIEventPathAlarms contains the site alarms.

View File

@ -142,6 +142,13 @@ func (u *Unifi) Login() error {
return nil
}
// Logout closes the current session
func (u *Unifi) Logout() error {
var response struct {
}
return u.GetData(APILogoutPath, &response)
}
// with the release of controller version 5.12.55 on UDM in Jan 2020 the api paths
// changed and broke this library. This function runs when `NewUnifi()` is called to
// check if this is a newer controller or not. If it is, we set new to true.

69
core/unifi/users.go Normal file
View File

@ -0,0 +1,69 @@
package unifi
import (
"fmt"
"strings"
)
// GetUsers returns a response full of clients that connected to the UDM within the provided amount of time
// it uses the insight historical connections data set
func (u *Unifi) GetUsers(sites []*Site, hours int) ([]*User, error) {
data := make([]*User, 0)
for _, site := range sites {
var response struct {
Data []*User `json:"data"`
}
params := fmt.Sprintf(`{ "type": "all:", "conn": "all", "within":%d }`, hours)
u.DebugLog("Polling Controller, retrieving UniFi Users, site %s ", site.SiteName)
clientPath := fmt.Sprintf(APIAllUserPath, site.Name)
if err := u.GetData(clientPath, &response, params); err != nil {
return nil, err
}
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.
response.Data[i].SiteName = site.SiteName
// Fix name and hostname fields. Sometimes one or the other is blank.
response.Data[i].Hostname = strings.TrimSpace(pick(d.Hostname, d.Name, d.Mac))
response.Data[i].Name = strings.TrimSpace(pick(d.Name, d.Hostname))
}
data = append(data, response.Data...)
}
return data, nil
}
// User defines the metadata available for previously connected clients
type User struct {
SourceName string `json:"-"`
SiteName string `json:"-"`
ID string `json:"_id"`
Mac string `json:"mac"`
SiteID string `json:"site_id"`
Oui string `json:"oui,omitempty"`
IsGuest bool `json:"is_guest"`
FirstSeen int64 `json:"first_seen,omitempty"`
LastSeen int64 `json:"last_seen,omitempty"`
IsWired bool `json:"is_wired,omitempty"`
Hostname string `json:"hostname,omitempty"`
Duration int64 `json:"duration,omitempty"`
TxBytes int64 `json:"tx_bytes,omitempty"`
TxPackets int64 `json:"tx_packets,omitempty"`
RxBytes int64 `json:"rx_bytes,omitempty"`
RxPackets int64 `json:"rx_packets,omitempty"`
WifiTxAttempts int64 `json:"wifi_tx_attempts,omitempty"`
TxRetries int64 `json:"tx_retries,omitempty"`
UsergroupID string `json:"usergroup_id,omitempty"`
Name string `json:"name,omitempty"`
Note string `json:"note,omitempty"`
Noted bool `json:"noted,omitempty"`
Blocked bool `json:"blocked,omitempty"`
DevIDOverride int64 `json:"dev_id_override,omitempty"`
FingerprintOverride bool `json:"fingerprint_override,omitempty"`
}