Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prometheus exporter. #139

Merged
merged 85 commits into from
Nov 30, 2019
Merged
Show file tree
Hide file tree
Changes from 74 commits
Commits
Show all changes
85 commits
Select commit Hold shift + click to select a range
7094c2c
this is just a start, nothing works yet
davidnewhall Nov 12, 2019
e2af4d9
rename internal package
davidnewhall Nov 12, 2019
641095f
split influx code into new package
davidnewhall Nov 12, 2019
f6c73e7
Rename all the packages
davidnewhall Nov 12, 2019
dd27f90
a few more steps
davidnewhall Nov 12, 2019
9bd4737
Base layout .. maybe.
davidnewhall Nov 17, 2019
f62115f
fix a bug and send a few more pieces into the mix
davidnewhall Nov 17, 2019
681df83
rename package
davidnewhall Nov 17, 2019
e7292ed
a few bugs
davidnewhall Nov 17, 2019
6f32f7d
new files
davidnewhall Nov 17, 2019
bee5a59
add site code
davidnewhall Nov 17, 2019
403961e
dont need that anymore
davidnewhall Nov 17, 2019
a270474
dep ensure
davidnewhall Nov 17, 2019
5a28645
code shrinking
davidnewhall Nov 17, 2019
c9e137e
Add USW and conuter
davidnewhall Nov 17, 2019
a7ebbdb
some cleanup
davidnewhall Nov 17, 2019
9d0d9e9
fix comment
davidnewhall Nov 17, 2019
ed94bed
better logging
davidnewhall Nov 18, 2019
cc66677
remove IDS, fix metric names to standards, remove timestamp
davidnewhall Nov 18, 2019
647618e
missed one
davidnewhall Nov 18, 2019
39c421d
a few more fixes
davidnewhall Nov 18, 2019
1775f6e
and some more desciprion fixes
davidnewhall Nov 18, 2019
4a73669
add switch sys stats
davidnewhall Nov 18, 2019
39c6fdc
add usg exports
davidnewhall Nov 18, 2019
6016c74
less appendage
davidnewhall Nov 18, 2019
ae2877c
Add speed test stats to usg
davidnewhall Nov 18, 2019
b7918e0
Add more usw data, start uap
davidnewhall Nov 18, 2019
ba42bb3
spread things out
davidnewhall Nov 18, 2019
8a01655
found a bug in site code
davidnewhall Nov 18, 2019
1798f31
this makes more sense
davidnewhall Nov 18, 2019
166c4a2
rename some things
davidnewhall Nov 18, 2019
7e35c87
move tx/rx labels around
davidnewhall Nov 18, 2019
6cb1fb8
missed a couple
davidnewhall Nov 18, 2019
1ffbccc
fix bugs
davidnewhall Nov 18, 2019
1b75963
Updates
davidnewhall Nov 25, 2019
640075d
fix more port numbers
davidnewhall Nov 25, 2019
e88f889
rename client struct
davidnewhall Nov 25, 2019
184a20c
shave off some nanoseconds
davidnewhall Nov 25, 2019
6622050
shave a few more nanoseconds
davidnewhall Nov 25, 2019
58e49c8
fix lint errors
davidnewhall Nov 25, 2019
72052f9
fix version setting
davidnewhall Nov 25, 2019
1f1f1bc
add more uap stats
davidnewhall Nov 26, 2019
00b3326
add radio table stats
davidnewhall Nov 26, 2019
b199a31
rearrange wait group
davidnewhall Nov 26, 2019
e492ca1
update deps
davidnewhall Nov 26, 2019
6bcdfcd
Update comment
davidnewhall Nov 27, 2019
dce7501
Update promunifi/clients.go
davidnewhall Nov 27, 2019
7956f8e
fix the names
davidnewhall Nov 28, 2019
a1b47e7
fix linter
davidnewhall Nov 28, 2019
ad29c59
clean up site and clients
davidnewhall Nov 28, 2019
0cb2350
trim a few more zeros
davidnewhall Nov 28, 2019
faae635
track zeros
davidnewhall Nov 28, 2019
8d2244b
more fixes
davidnewhall Nov 28, 2019
204a93f
more fixes!
davidnewhall Nov 28, 2019
3db371f
Update promunifi/usw.go
davidnewhall Nov 28, 2019
6388d32
Update promunifi/clients.go
davidnewhall Nov 28, 2019
13b188b
convert speeds to bps
davidnewhall Nov 28, 2019
b99253d
Update promunifi/clients.go
davidnewhall Nov 28, 2019
63bf858
fixes
davidnewhall Nov 28, 2019
8fd947d
Apply suggestions from code review
davidnewhall Nov 28, 2019
898b145
fix a few more unit locations
davidnewhall Nov 28, 2019
3444db7
small qol update
davidnewhall Nov 28, 2019
632936d
re-arrange a file pieces.
davidnewhall Nov 28, 2019
0a15850
cosolidate code
davidnewhall Nov 28, 2019
6419422
add UDM support
davidnewhall Nov 29, 2019
61b43f2
unify variable names
davidnewhall Nov 29, 2019
70aae28
cleanup
davidnewhall Nov 29, 2019
f91ecec
add an interface for testing
davidnewhall Nov 29, 2019
2a479dd
bug fix
davidnewhall Nov 29, 2019
c6c26e8
fix up client labels
davidnewhall Nov 29, 2019
ea66248
fix labels on UAP
davidnewhall Nov 29, 2019
93d6aac
use the same labels on all client metrics
davidnewhall Nov 29, 2019
01926aa
use the same labels on all client metrics - derp
davidnewhall Nov 29, 2019
ad3e085
add radio label to vaps
davidnewhall Nov 29, 2019
f176db5
remove all
davidnewhall Nov 29, 2019
9743a83
fix comments
davidnewhall Nov 29, 2019
23f4c9b
use fewer tags
davidnewhall Nov 29, 2019
a9a7862
Add more to the interface
davidnewhall Nov 30, 2019
a58c121
more cleanup
davidnewhall Nov 30, 2019
1d93777
tighter for loops
davidnewhall Nov 30, 2019
29c59e7
still cleaning
davidnewhall Nov 30, 2019
883a5a7
still cleaning
davidnewhall Nov 30, 2019
adb1363
update deps
davidnewhall Nov 30, 2019
616365f
add two more metrics to clients
davidnewhall Nov 30, 2019
666ecb0
one more..
davidnewhall Nov 30, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .metadata.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ URL="${SOURCE_URL}"
# This parameter is passed in as -X to go build. Used to override the Version variable in a package.
# This makes a path like github.com/user/hello-world/helloworld.Version=1.3.3
# Name the Version-containing library the same as the github repo, without dashes.
VERSION_PATH="${IMPORT_PATH}/$(echo ${BINARY} | tr -d -- -).Version"
VERSION_PATH="${IMPORT_PATH}/poller.Version"

# Dynamic. Recommend not changing.
VVERSION=$(git describe --abbrev=0 --tags $(git rev-list --tags --max-count=1))
Expand Down
84 changes: 81 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 13 additions & 3 deletions examples/MANUAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ is provided so the application can be easily adapted to any environment.
mode default: "influx"
* Value: influx
This default mode runs this application as a daemon. It will poll
the controller at the configured interval. Providing an invalid value
will run in this default mode.
the controller at the configured interval and report measurements to
InfluxDB. Providing an invalid value will run in this default mode.

* Value: influxlambda - (the only other available option right now)
* Value: influxlambda
Setting this value will invoke a run-once mode where the application
immediately polls the controller and reports the metrics to InfluxDB.
Then it exits. This mode is useful in an AWS Lambda or a crontab where
Expand All @@ -101,6 +101,16 @@ is provided so the application can be easily adapted to any environment.
This mode can also be combined with a "test database" in InfluxDB to
give yourself a "test config file" you may run ad-hoc to test changes.

* Value: prometheus
In this mode the application opens an http interface and exports the
measurements at /metrics for collection by prometheus. Enabling this
mode disables InfluxDB usage entirely. This is BETA.

http_listen default: 0.0.0.0:9130
This option controls the IP and port the http listener uses when the
mode is set to prometheus. This setting has no effect when other modes
are in use. Metrics become available at the /metrics URI.

influx_url default: http://127.0.0.1:8086
This is the URL where the Influx web server is available.

Expand Down
11 changes: 10 additions & 1 deletion examples/up.conf.example
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,22 @@ quiet = false
# an invalid mode will also result in "influx". In this default mode the application
# runs as a daemon and polls the controller at the configured interval.
#
# There is only one other option at this time: "influxlambda"
# There are two other options at this time: "influxlambda" and "prometheus"
SuperQ marked this conversation as resolved.
Show resolved Hide resolved
#
# Lambda mode makes the application exit after collecting and reporting metrics
# to InfluxDB one time. This mode requires an external process like an AWS Lambda
# or a simple crontab to keep the timings accurate on UniFi Poller run intervals.
#
# Prometheus mode opens an HTTP server on port 9130 and exports the metrics at
# /metrics for polling collection by a prometheus server. This disables influxdb.
# IMPORTANT: The prometheus mode is still beta and isn't tested very well.
# Please help us test and provide your feedback on the github repo!
mode = "influx"

# This controls on which ip and port /metrics is exported when mode is "prometheus".
# This has no effect in other modes. Must contain a colon and port.
http_listen = "0.0.0.0:9130"

# InfluxDB does not require auth by default, so the user/password are probably unimportant.
influx_url = "http://127.0.0.1:8086"
influx_user = "unifi"
Expand Down
1 change: 1 addition & 0 deletions examples/up.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"debug": false,
"quiet": false,
"mode": "influx",
"http_listen": "0.0.0.0:9130",
"influx_url": "http://127.0.0.1:8086",
"influx_user": "unifi",
"influx_pass": "unifi",
Expand Down
12 changes: 11 additions & 1 deletion examples/up.xml.example
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,24 @@
# an invalid mode will also result in "influx". In this default mode the application
# runs as a daemon and polls the controller at the configured interval.
#
# There is only one other option at this time: "influxlambda"
# There are two other options at this time: "influxlambda" and "prometheus"
#
# Lambda mode makes the application exit after collecting and reporting metrics
# to InfluxDB one time. This mode requires an external process like an AWS Lambda
# or a simple crontab to keep the timings accurate on UniFi Poller run intervals.
#
# Prometheus mode opens an HTTP server on port 9130 and exports the metrics at
# /metrics for polling collection by a prometheus server. This disables influxdb.
# IMPORTANT: The prometheus mode is still beta and isn't tested very well.
-->
<mode>influx</mode>

<!--
# This controls on which ip and port /metrics is exported when mode is "prometheus".
# This has no effect in other modes. Must contain a colon and port.
-->
<http_listen>0.0.0.0:9130</http_listen>

<!--
# InfluxDB does not require auth by default, so the user/password are probably unimportant.
-->
Expand Down
10 changes: 9 additions & 1 deletion examples/up.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,21 @@ quiet: false
# an invalid mode will also result in "influx". In this default mode the application
# runs as a daemon and polls the controller at the configured interval.
#
# There is only one other option at this time: "influxlambda"
# There are two other options at this time: "influxlambda" and "prometheus"
#
# Lambda mode makes the application exit after collecting and reporting metrics
# to InfluxDB one time. This mode requires an external process like an AWS Lambda
# or a simple crontab to keep the timings accurate on UniFi Poller run intervals.
#
# Prometheus mode opens an HTTP server on port 9130 and exports the metrics at
# /metrics for polling collection by a prometheus server. This disables influxdb.
# IMPORTANT: The prometheus mode is still beta and isn't tested very well.
mode: "influx"

# This controls on which ip and port /metrics is exported when mode is "prometheus".
# This has no effect in other modes. Must contain a colon and port.
http_listen: "0.0.0.0:9130"

# InfluxDB does not require auth by default, so the user/password are probably unimportant.
influx_url: "http://127.0.0.1:8086"
influx_user: "unifi"
Expand Down
4 changes: 4 additions & 0 deletions influxunifi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# influx

This package provides the methods to turn UniFi measurements into influx
data-points with appropriate tags and fields.
2 changes: 1 addition & 1 deletion unifipoller/influx_clients.go → influxunifi/clients.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package unifipoller
package influxunifi

import (
"time"
Expand Down
2 changes: 1 addition & 1 deletion unifipoller/influx_ids.go → influxunifi/ids.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package unifipoller
package influxunifi

import (
influx "github.com/influxdata/influxdb1-client/v2"
Expand Down
69 changes: 69 additions & 0 deletions influxunifi/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Package influx provides the methods to turn UniFi measurements into influx
// data-points with appropriate tags and fields.
package influxunifi

import (
"github.com/davidnewhall/unifi-poller/metrics"
client "github.com/influxdata/influxdb1-client/v2"
)

// Metrics contains all the data from the controller and an influx endpoint to send it to.
type Metrics struct {
*metrics.Metrics
client.BatchPoints
}

// ProcessPoints batches all device and client data into influxdb data points.
// Call this after you've collected all the data you care about.
// This function is sorta weird and returns a slice of errors. The reasoning is
// that some points may process while others fail, so we attempt to process them
// all. This is (usually) run in a loop, so we can't really exit on error,
// we just log the errors and tally them on a counter. In reality, this never
// returns any errors because we control the data going in; cool right? But we
// still check&log it in case the data going is skewed up and causes errors!
func (m *Metrics) ProcessPoints() []error {
errs := []error{}
processPoints := func(m *Metrics, p []*client.Point, err error) {
switch {
case err != nil:
errs = append(errs, err)
case p == nil:
default:
m.AddPoints(p)
}
}

for _, asset := range m.Sites {
pts, err := SitePoints(asset, m.TS)
processPoints(m, pts, err)
}
for _, asset := range m.Clients {
pts, err := ClientPoints(asset, m.TS)
processPoints(m, pts, err)
}
for _, asset := range m.IDSList {
pts, err := IDSPoints(asset) // no m.TS.
processPoints(m, pts, err)
}

if m.Devices == nil {
return errs
}
for _, asset := range m.Devices.UAPs {
pts, err := UAPPoints(asset, m.TS)
processPoints(m, pts, err)
}
for _, asset := range m.Devices.USGs {
pts, err := USGPoints(asset, m.TS)
processPoints(m, pts, err)
}
for _, asset := range m.Devices.USWs {
pts, err := USWPoints(asset, m.TS)
processPoints(m, pts, err)
}
for _, asset := range m.Devices.UDMs {
pts, err := UDMPoints(asset, m.TS)
processPoints(m, pts, err)
}
return errs
}
2 changes: 1 addition & 1 deletion unifipoller/influx_site.go → influxunifi/site.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package unifipoller
package influxunifi

import (
"strings"
Expand Down
2 changes: 1 addition & 1 deletion unifipoller/influx_uap.go → influxunifi/uap.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package unifipoller
package influxunifi

import (
"time"
Expand Down
2 changes: 1 addition & 1 deletion unifipoller/influx_udm.go → influxunifi/udm.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package unifipoller
package influxunifi

import (
"time"
Expand Down
2 changes: 1 addition & 1 deletion unifipoller/influx_usg.go → influxunifi/usg.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package unifipoller
package influxunifi

import (
"strings"
Expand Down
2 changes: 1 addition & 1 deletion unifipoller/influx_usw.go → influxunifi/usw.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package unifipoller
package influxunifi

import (
"time"
Expand Down
2 changes: 1 addition & 1 deletion init/docker/docker-compose.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ UP_BRANCH=stable
UP_UNIFI_USER=influx
UP_UNIFI_PASS=
UP_UNIFI_URL=https://127.0.0.1:8443
UP_DEBUG_MODE=false
UP_DEBUG=false
2 changes: 1 addition & 1 deletion init/docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ services:
- UP_UNIFI_USER=${UP_UNIFI_USER}
- UP_UNIFI_PASS=${UP_UNIFI_PASS}
- UP_UNIFI_URL=${UP_UNIFI_URL}
- UP_DEBUG_MODE=${UP_DEBUG_MODE}
- UP_DEBUG=${UP_DEBUG}
volumes:
influxdb-storage:
chronograf-storage:
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package main
import (
"log"

"github.com/davidnewhall/unifi-poller/unifipoller"
"github.com/davidnewhall/unifi-poller/poller"
)

// Keep it simple.
func main() {
if err := unifipoller.Start(); err != nil {
if err := poller.New().Start(); err != nil {
log.Fatalln("[ERROR]", err)
}
}