This repository has been archived by the owner on Jun 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unifi-influxdb.php
executable file
·141 lines (114 loc) · 4.57 KB
/
unifi-influxdb.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env php
<?php
/* Check if requirements set */
if(file_exists(__DIR__ . '/vendor/autoload.php')){
require_once(__DIR__ . '/vendor/autoload.php');
}else{
exit('Composer not initialized!');
}
/* Check if configuration exsists */
if(file_exists(__DIR__ . '/config.php')){
require_once(__DIR__ . '/config.php');
}else{
exit('Configuration does not exsist.');
}
/* Check if daemon flag is set */
foreach($argv as $arg){
if($arg == '-d' or $arg == '--daemon'){
$daemon = true;
}else{
$daemon = false;
}
}
/* Connect to UniFi Controller */
$unifi = new UniFi_API\Client($cfg['UNIFI']['USER'], $cfg['UNIFI']['PASSWORD'], $cfg['UNIFI']['URL'], $cfg['UNIFI']['SITE'], $cfg['UNIFI']['VERSION'], true);
/* Connect to InfluxDB */
if($cfg['INFLUXDB']['USER'] and $cfg['INFLUXDB']['PASSWORD']){
$influxdb = new InfluxDB\Client($cfg['INFLUXDB']['HOST'], $cfg['INFLUXDB']['PORT'], $cfg['INFLUXDB']['USER'], $cfg['INFLUXDB']['PASSWORD'], $cfg['INFLUXDB']['SSL']);
}else{
$influxdb = new InfluxDB\Client($cfg['INFLUXDB']['HOST'], $cfg['INFLUXDB']['PORT'], null, null, $cfg['INFLUXDB']['SSL']);
}
/* Use InfluxDB database and check if exsists */
$db = $influxdb->selectDB($cfg['INFLUXDB']['DB']);
/* Check UniFi Controller login */
if($unifi->login()){
/* Beginning of daemon loop */
do{
/* Fetch sites from UniFi Controller */
$unifi_sites = $unifi->list_sites();
/* Prepare global counters */
$total_clients = 0;
$total_guests = 0;
$total_devices = 0;
$total_devices_connected = 0;
$total_devices_disconnected = 0;
/* Iterate through sites */
foreach($unifi_sites as $site){
/* Switch to site */
$unifi->set_site($site->name);
/* Fetch client list from UniFi Controller */
$clients = $unifi->list_clients();
/* Fetch device list from UniFi Controller */
$devices = $unifi->list_devices();
/* Prepare data arrays */
$essid = Array();
$aps = Array(
'connected' => Array(),
'disconnected' => Array()
);
/* Iterate through clients to fetch metrics */
foreach($clients as $client){
/* Check if SSID exsits and update couter variable */
if(array_key_exists($client->essid, $essid)){
$essid[$client->essid]++;
}else{
$essid[$client->essid] = 1;
}
}
foreach($devices as $device){
/* Check if device connected */
if($device->state == 1){
$total_devices_connected++;
$aps['connected'][] = $device;
}elseif($device->state == 0){
$total_devices_disconnected++;
$aps['disconnected'][] = $device;
}
}
/* Update global data */
$total_clients += count($clients);
$total_devices += count($devices);
/* Prepare data for DB */
$data_points = Array();
foreach($essid as $key => $value){
$data_points[] = new InfluxDB\Point('client', $value, ['site_id' => $site->name, 'site_name' => $site->desc, 'essid' => $key]);
}
$data_points[] = new InfluxDB\Point('client', count($clients), ['site_id' => $site->name, 'site_name' => $site->desc, 'essid' => 'total-count']);
/* Fetch device count */
if(count($devices) > 0){
$data_points[] = new InfluxDB\Point('device', count($devices), ['site_id' => $site->name, 'site_name' => $site->desc, 'state' => 'undefined']);
$data_points[] = new InfluxDB\Point('device', count($aps['connected']), ['site_id' => $site->name, 'site_name' => $site->desc, 'state' => 'connected']);
$data_points[] = new InfluxDB\Point('device', count($aps['disconnected']), ['site_id' => $site->name, 'site_name' => $site->desc, 'state' => 'disconnected']);
}
/* Write data to DB */
$db->writePoints($data_points, InfluxDB\Database::PRECISION_SECONDS);
}
/* Create measurement data */
$data_points = Array();
$data_points[] = new InfluxDB\Point('site', count($unifi_sites));
$data_points[] = new InfluxDB\Point('client', $total_clients, ['site_id' => 'all-sites', 'essid' => 'total-count']);
$data_points[] = new InfluxDB\Point('device', $total_devices, ['site_id' => 'all-sites', 'state' => 'undefined']);
$data_points[] = new InfluxDB\Point('device', $total_devices_connected, ['site_id' => 'all-sites', 'state' => 'connected']);
$data_points[] = new InfluxDB\Point('device', $total_devices_disconnected, ['site_id' => 'all-sites', 'state' => 'disconnected']);
$data_points[] = new InfluxDB\Point('guest', $total_guests, ['site_id' => 'all-sites']);
/* Write data to DB */
$db->writePoints($data_points, InfluxDB\Database::PRECISION_SECONDS);
/* Take a nap if running in daemon mode */
if($daemon){
sleep(30);
}
/* Ending of daemon loop */
}while($daemon);
}else{
exit('Connection to UniFi Controller not working properly!');
}