Skip to content

Commit

Permalink
added: port forwarding stats
Browse files Browse the repository at this point in the history
added: port forwarding stats
extended: output from daily.site and hourly.site stats
  • Loading branch information
malle-pietje committed Feb 10, 2016
1 parent 3fe19a4 commit 2089681
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 26 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ The Unifi API browser tool offers the following features:
- daily site stats
- health metrics
- dashboard metrics (supported on controller version 4.9.1.alpha and higher)
- port forward stats
- Hotspot
- stat vouchers
- stat payments
Expand Down
5 changes: 5 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,10 @@
$selection = 'list port forwarding rules';
$data = $unifidata->list_portforwarding();
break;
case 'list_portforward_stats':
$selection = 'list port forwarding stats';
$data = $unifidata->list_portforward_stats();
break;
case 'stat_voucher':
$selection = 'list hotspot vouchers';
$data = $unifidata->stat_voucher();
Expand Down Expand Up @@ -482,6 +486,7 @@ function to print the output
<?php if ($detected_controller_version != 'undetected' && version_compare($detected_controller_version, '4.9.1') >= 0) { ?>
<li id="list_dashboard"><a href="?action=list_dashboard">site dashboard metrics</a></li>
<?php } ?>
<li id="list_portforward_stats"><a href="?action=list_portforward_stats">port forwarding stats</a></li>
</ul>
</li>
<li id="hotspot-menu" class="dropdown">
Expand Down
75 changes: 49 additions & 26 deletions phpapi/class.unifi.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,18 +221,45 @@ public function unblock_sta($mac) {
return $return;
}

/*
daily stats method
parameter <start>
parameter <end>
NOTE: defaults to the past 30*7*24 hours
"bytes" are no longer returned with controller version 4.9.1 and later
*/
public function stat_daily_site($start = NULL, $end = NULL) {
if (!$this->is_loggedin) return false;
$return = array();
$end = is_null($end) ? ((time()-(time() % 3600))*1000) : $end;
$start = is_null($start) ? $end-18144000000 : $start;
$json = json_encode(array('attrs' => array('bytes', 'wan-tx_bytes', 'wan-rx_bytes', 'wlan_bytes', 'num_sta', 'lan-num_sta', 'wlan-num_sta', 'time'), 'start' => $start, 'end' => $end));
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/stat/report/daily.site','json='.$json));
if (isset($content_decoded->meta->rc)) {
if ($content_decoded->meta->rc == 'ok') {
if (is_array($content_decoded->data)) {
foreach ($content_decoded->data as $test) {
$return[]= $test;
}
}
}
}
return $return;
}

/*
hourly stats method for a site
parameter <start>
parameter <end>
NOTE: defaults to the past 7*24 hours
"bytes" are no longer returned with controller version 4.9.1 and later
*/
public function stat_hourly_site($start = NULL, $end = NULL) {
if (!$this->is_loggedin) return false;
$return = array();
$end = is_null($end) ? ((time())*1000) : $end;
$start = is_null($start) ? $end-604800000 : $start;
$json = json_encode(array('attrs' => array('bytes', 'num_sta', 'time'), 'start' => $start, 'end' => $end));
$json = json_encode(array('attrs' => array('bytes', 'wan-tx_bytes', 'wan-rx_bytes', 'wlan_bytes', 'num_sta', 'lan-num_sta', 'wlan-num_sta', 'time'), 'start' => $start, 'end' => $end));
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/stat/report/hourly.site','json='.$json));
if (isset($content_decoded->meta->rc)) {
if ($content_decoded->meta->rc == 'ok') {
Expand Down Expand Up @@ -322,31 +349,6 @@ public function stat_auths($start = NULL, $end = NULL) {
return $return;
}

/*
daily stats method
parameter <start>
parameter <end>
NOTE: defaults to the past 30*7*24 hours
*/
public function stat_daily_site($start = NULL, $end = NULL) {
if (!$this->is_loggedin) return false;
$return = array();
$end = is_null($end) ? ((time()-(time() % 3600))*1000) : $end;
$start = is_null($start) ? $end-18144000000 : $start;
$json = json_encode(array('attrs' => array('bytes', 'num_sta', 'time'), 'start' => $start, 'end' => $end));
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/stat/report/daily.site','json='.$json));
if (isset($content_decoded->meta->rc)) {
if ($content_decoded->meta->rc == 'ok') {
if (is_array($content_decoded->data)) {
foreach ($content_decoded->data as $test) {
$return[]= $test;
}
}
}
}
return $return;
}

/*
get details of all clients ever connected to the site
parameter <historyhours>
Expand Down Expand Up @@ -680,6 +682,27 @@ public function list_hotspotop() {
return $return;
}

/*
list port forwarding stats
returns an array of port forwarding stats
*/
public function list_portforward_stats() {
if (!$this->is_loggedin) return false;
$return = array();
$json = json_encode(array());
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/stat/portforward','json='.$json));
if (isset($content_decoded->meta->rc)) {
if ($content_decoded->meta->rc == 'ok') {
if (is_array($content_decoded->data)) {
foreach ($content_decoded->data as $portforward) {
$return[]= $portforward;
}
}
}
}
return $return;
}

/*
list port forwarding settings
returns an array of port forwarding settings
Expand Down

0 comments on commit 2089681

Please sign in to comment.