Skip to content
This repository has been archived by the owner on Aug 6, 2018. It is now read-only.

Commit

Permalink
- Updated vendored unifi package and associated tests.
Browse files Browse the repository at this point in the history
- Added back default settings from before config file update change.
  • Loading branch information
someone1 committed Jun 4, 2018
1 parent a479bf7 commit 347ee1f
Show file tree
Hide file tree
Showing 3 changed files with 229 additions and 194 deletions.
25 changes: 17 additions & 8 deletions cmd/unifi_exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,21 @@ func main() {
username := config.Unifi["username"]
password := config.Unifi["password"]
site := config.Unifi["site"]
ins := config.Unifi["insecure"]
insecure, err := strconv.ParseBool(ins)
if err != nil {
log.Fatalf("failed to parse bool %s: %v", ins, err)

insecure := false
if ins, ok := config.Unifi["insecure"]; ok {
insecure, err = strconv.ParseBool(ins)
if err != nil {
log.Fatalf("failed to parse bool %s: %v", ins, err)
}
}
to := config.Unifi["timeout"]
timeout, err := time.ParseDuration(to)
if err != nil {
log.Fatalf("failed to parse duration %q: %v", to, err)

timeout := 5*time.Second
if to, ok := config.Unifi["timeout"]; ok {
timeout, err = time.ParseDuration(to)
if err != nil {
log.Fatalf("failed to parse duration %q: %v", to, err)
}
}

if unifiAddr == "" {
Expand All @@ -72,6 +78,9 @@ func main() {
// Set default port to 9130 if left blank in config.yml
listenAddr = ":9130"
}
if metricsPath == "" {
metricsPath = "/metrics"
}

clientFn := newClient(
unifiAddr,
Expand Down
274 changes: 147 additions & 127 deletions devicecollector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,17 @@ func TestDeviceCollector(t *testing.T) {
"ethernet_table": [{
"mac": "de:ad:be:ef:de:ad"
}],
"ng-num_sta": 3,
"ng-user-num_sta": 2,
"ng-guest-num_sta": 1,
"na-num_sta": 6,
"na-user-num_sta": 4,
"na-guest-num_sta": 2,
"radio_table_stats": [{
"guest-num_sta": 1,
"name": "wifi0",
"num_sta": 3,
"user-num_sta": 2
}, {
"guest-num_sta": 2,
"name": "wifi1",
"num_sta": 6,
"user-num_sta": 4
}],
"radio_table": [
{
"name": "wifi0",
Expand Down Expand Up @@ -98,91 +103,101 @@ func TestDeviceCollector(t *testing.T) {
{
desc: "two devices, one site",
input: strings.TrimSpace(`
{
"data": [
{
"_id": "abc",
"adopted": true,
"inform_ip": "192.168.1.1",
"name": "ABC",
"ethernet_table": [{
"mac": "de:ad:be:ef:de:ad"
}],
"ng-num_sta": 3,
"ng-user-num_sta": 2,
"ng-guest-num_sta": 1,
"na-num_sta": 6,
"na-user-num_sta": 4,
"na-guest-num_sta": 2,
"radio_table": [
{
"name": "wifi0",
"radio": "ng"
},
"data": [
{
"name": "wifi1",
"radio": "na"
}
],
"stat": {
"bytes": 100,
"rx_bytes": 80,
"tx_bytes": 20,
"rx_packets": 4,
"tx_packets": 1,
"tx_dropped": 1
},
"uplink": {
"rx_bytes": 20,
"tx_bytes": 10,
"rx_packets": 2,
"tx_packets": 1
},
"uptime": 10
},
{
"_id": "def",
"adopted": false,
"inform_ip": "192.168.1.1",
"name": "DEF",
"ethernet_table": [{
"mac": "ab:ad:1d:ea:ab:ad"
}],
"ng-num_sta": 3,
"ng-user-num_sta": 2,
"ng-guest-num_sta": 1,
"na-num_sta": 6,
"na-user-num_sta": 4,
"na-guest-num_sta": 2,
"radio_table": [
{
"name": "wifi0",
"radio": "ng"
"_id": "abc",
"adopted": true,
"inform_ip": "192.168.1.1",
"name": "ABC",
"ethernet_table": [{
"mac": "de:ad:be:ef:de:ad"
}],
"radio_table_stats": [{
"guest-num_sta": 1,
"name": "wifi0",
"num_sta": 3,
"user-num_sta": 2
}, {
"guest-num_sta": 2,
"name": "wifi1",
"num_sta": 6,
"user-num_sta": 4
}],
"radio_table": [
{
"name": "wifi0",
"radio": "ng"
},
{
"name": "wifi1",
"radio": "na"
}
],
"stat": {
"bytes": 100,
"rx_bytes": 80,
"tx_bytes": 20,
"rx_packets": 4,
"tx_packets": 1,
"tx_dropped": 1
},
"uplink": {
"rx_bytes": 20,
"tx_bytes": 10,
"rx_packets": 2,
"tx_packets": 1
},
"uptime": 10
},
{
"name": "wifi1",
"radio": "na"
"_id": "def",
"adopted": false,
"inform_ip": "192.168.1.1",
"name": "DEF",
"ethernet_table": [{
"mac": "ab:ad:1d:ea:ab:ad"
}],
"radio_table_stats": [{
"guest-num_sta": 1,
"name": "wifi0",
"num_sta": 3,
"user-num_sta": 2
}, {
"guest-num_sta": 2,
"name": "wifi1",
"num_sta": 6,
"user-num_sta": 4
}],
"radio_table": [
{
"name": "wifi0",
"radio": "ng"
},
{
"name": "wifi1",
"radio": "na"
}
],
"stat": {
"bytes": 200,
"rx_bytes": 10,
"tx_bytes": 190,
"rx_packets": 1,
"tx_packets": 19,
"tx_dropped": 1
},
"uplink": {
"rx_bytes": 40,
"tx_bytes": 20,
"rx_packets": 4,
"tx_packets": 2
},
"uptime": 20
}
],
"stat": {
"bytes": 200,
"rx_bytes": 10,
"tx_bytes": 190,
"rx_packets": 1,
"tx_packets": 19,
"tx_dropped": 1
},
"uplink": {
"rx_bytes": 40,
"tx_bytes": 20,
"rx_packets": 4,
"tx_packets": 2
},
"uptime": 20
]
}
]
}
`),
`),
matches: []*regexp.Regexp{
regexp.MustCompile(`unifi_devices{site="Default"} 2`),
regexp.MustCompile(`unifi_devices_adopted{site="Default"} 1`),
Expand Down Expand Up @@ -240,51 +255,56 @@ func TestDeviceCollector(t *testing.T) {
{
desc: "two devices, two sites (same device, but this is okay for tests)",
input: strings.TrimSpace(`
{
"data": [
{
"_id": "123",
"adopted": true,
"inform_ip": "192.168.1.1",
"name": "OneTwoThree",
"ethernet_table": [{
"mac": "ab:ad:1d:ea:ab:ad"
}],
"ng-num_sta": 3,
"ng-user-num_sta": 2,
"ng-guest-num_sta": 1,
"na-num_sta": 6,
"na-user-num_sta": 4,
"na-guest-num_sta": 2,
"radio_table": [
"data": [
{
"name": "wifi0",
"radio": "ng"
},
{
"name": "wifi1",
"radio": "na"
"_id": "123",
"adopted": true,
"inform_ip": "192.168.1.1",
"name": "OneTwoThree",
"ethernet_table": [{
"mac": "ab:ad:1d:ea:ab:ad"
}],
"radio_table_stats": [{
"guest-num_sta": 1,
"name": "wifi0",
"num_sta": 3,
"user-num_sta": 2
}, {
"guest-num_sta": 2,
"name": "wifi1",
"num_sta": 6,
"user-num_sta": 4
}],
"radio_table": [
{
"name": "wifi0",
"radio": "ng"
},
{
"name": "wifi1",
"radio": "na"
}
],
"stat": {
"bytes": 100,
"rx_bytes": 80,
"tx_bytes": 20,
"rx_packets": 4,
"tx_packets": 1,
"tx_dropped": 1
},
"uplink": {
"rx_bytes": 20,
"tx_bytes": 10,
"rx_packets": 2,
"tx_packets": 1
},
"uptime": 10
}
],
"stat": {
"bytes": 100,
"rx_bytes": 80,
"tx_bytes": 20,
"rx_packets": 4,
"tx_packets": 1,
"tx_dropped": 1
},
"uplink": {
"rx_bytes": 20,
"tx_bytes": 10,
"rx_packets": 2,
"tx_packets": 1
},
"uptime": 10
]
}
]
}
`),
`),
matches: []*regexp.Regexp{
regexp.MustCompile(`unifi_devices{site="Default"} 1`),
regexp.MustCompile(`unifi_devices_adopted{site="Default"} 1`),
Expand Down Expand Up @@ -360,7 +380,7 @@ func TestDeviceCollector(t *testing.T) {
t.Logf("\t[%02d:%02d] match: %s", i, j, m.String())

if !m.Match(out) {
t.Fatal("\toutput failed to match regex")
t.Fatal("\toutput failed to match regex.")
}
}
}
Expand Down
Loading

0 comments on commit 347ee1f

Please sign in to comment.