Skip to content

Commit

Permalink
Merge pull request #139 from davidnewhall/dn2_prometheus
Browse files Browse the repository at this point in the history
Add prometheus exporter.
  • Loading branch information
davidnewhall committed Nov 30, 2019
2 parents fc1828b + 666ecb0 commit d052e69
Show file tree
Hide file tree
Showing 42 changed files with 2,019 additions and 446 deletions.
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
90 changes: 84 additions & 6 deletions Gopkg.lock

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

19 changes: 15 additions & 4 deletions examples/MANUAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ is provided so the application can be easily adapted to any environment.

interval default: 30s
How often to poll the controller for updated client and device data.
The UniFi Controller only updates traffic stats about every 30 seconds.
The UniFi Controller only updates traffic stats about every 30-60 seconds.
Only works if "mode" (below) is "influx" - other modes do not use interval.

debug default: false
This turns on time stamps and line numbers in logs, outputs a few extra
Expand All @@ -89,10 +90,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 +102,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.

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
16 changes: 13 additions & 3 deletions examples/up.conf.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
sites = ["all"]

# The UniFi Controller only updates traffic stats about every 30 seconds.
# Setting this to something lower may lead to "zeros" in your data. You've been warned.
# Setting this to something lower may lead to "zeros" in your data.
# If you're getting zeros now, set this to "1m"
interval = "30s"

# Turns on line numbers, microsecond logging, and a per-device log.
Expand All @@ -24,13 +25,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"
#
# Lambda mode makes the application exit after collecting and reporting metrics
# Mode "influxlambda" 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.
#
# Mode "prometheus" 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.
# 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
18 changes: 15 additions & 3 deletions examples/up.xml.example
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

<!--
# The UniFi Controller only updates traffic stats about every 30 seconds.
# Setting this to something lower may lead to "zeros" in your data. You've been warned.
# Setting this to something lower may lead to "zeros" in your data.
# If you're getting zeros now, set this to "1m"
-->
<interval>30s</interval>

Expand All @@ -40,14 +41,25 @@
# 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
# Mode "influxlambda" 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.
#
# Mode "prometheus" 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.
# Please help us test and provide your feedback on the github repo!
-->
<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
17 changes: 14 additions & 3 deletions examples/up.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ sites:
- all

# The UniFi Controller only updates traffic stats about every 30 seconds.
# Setting this to something lower may lead to "zeros" in your data. You've been warned.
# Setting this to something lower may lead to "zeros" in your data.
# If you're getting zeros now, set this to "1m"
# Only has effect if "mode" (below) is "influx" - other modes do not use interval.
interval: "30s"

# Turns on line numbers, microsecond logging, and a per-device log.
Expand All @@ -25,13 +27,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"
#
# Lambda mode makes the application exit after collecting and reporting metrics
# Mode "influxlambda" 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.
#
# Mode "prometheus" 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.
# 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
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
Loading

0 comments on commit d052e69

Please sign in to comment.