Skip to content

Commit 271d65e

Browse files
author
Jared Hendrickson
committed
Created status gateway endpoint to pull gateway statuses and metrics, changed API query numeric lookups to use float comparison instead of integers, created unit test for status gateway endpoint, updated documentation
1 parent fb0567b commit 271d65e

7 files changed

Lines changed: 191 additions & 6 deletions

File tree

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,10 @@ There is no limit to API calls at this time but is important to note that pfSens
633633
* [Read CARP Status](#1-read-carp-status)
634634
* [Update CARP Status](#2-update-carp-status)
635635

636+
* [STATUS/GATEWAY](#statusgateway)
637+
638+
* [Read Gateway Status](#1-read-gateway-status)
639+
636640
* [STATUS/LOG](#statuslog)
637641

638642
* [Read Configuration History Status Log](#1-read-configuration-history-status-log)
@@ -3395,6 +3399,38 @@ URL: https://{{$hostname}}/api/v1/status/carp
33953399

33963400

33973401

3402+
## STATUS/GATEWAY
3403+
3404+
3405+
3406+
### 1. Read Gateway Status
3407+
3408+
3409+
Read gateway status and metrics.<br><br>
3410+
3411+
_Requires at least one of the following privileges:_ [`page-all`, `page-status-gateway`]
3412+
3413+
3414+
***Endpoint:***
3415+
3416+
```bash
3417+
Method: GET
3418+
Type: RAW
3419+
URL: https://{{$hostname}}/api/v1/status/gateway
3420+
```
3421+
3422+
3423+
3424+
***Body:***
3425+
3426+
```js
3427+
{
3428+
3429+
}
3430+
```
3431+
3432+
3433+
33983434
## STATUS/LOG
33993435

34003436

docs/documentation.json

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4557,6 +4557,58 @@
45574557
],
45584558
"protocolProfileBehavior": {},
45594559
"_postman_isSubFolder": true
4560+
},
4561+
{
4562+
"name": "GATEWAY",
4563+
"item": [
4564+
{
4565+
"name": "Read Gateway Status",
4566+
"protocolProfileBehavior": {
4567+
"disableBodyPruning": true
4568+
},
4569+
"request": {
4570+
"auth": {
4571+
"type": "bearer",
4572+
"bearer": [
4573+
{
4574+
"key": "token",
4575+
"value": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJwZlNlbnNlIiwiZXhwIjoxNTk4MDcwMTAzLCJuYmYiOjE1OTgwNjE3MDMsImRhdGEiOiJhZG1pbiJ9.hgvNu96Bvsehcq6ygTa9LPB8eymiLnxYu4V6J8_h0-o",
4576+
"type": "string"
4577+
}
4578+
]
4579+
},
4580+
"method": "GET",
4581+
"header": [],
4582+
"body": {
4583+
"mode": "raw",
4584+
"raw": "{\n \n}",
4585+
"options": {
4586+
"raw": {
4587+
"language": "json"
4588+
}
4589+
}
4590+
},
4591+
"url": {
4592+
"raw": "https://{{$hostname}}/api/v1/status/gateway",
4593+
"protocol": "https",
4594+
"host": [
4595+
"{{$hostname}}"
4596+
],
4597+
"path": [
4598+
"api",
4599+
"v1",
4600+
"status",
4601+
"gateway"
4602+
]
4603+
},
4604+
"description": "Read gateway status and metrics.<br><br>\n\n_Requires at least one of the following privileges:_ [`page-all`, `page-status-gateway`]"
4605+
},
4606+
"response": []
4607+
}
4608+
],
4609+
"description": "API endpoints that read gateway statuses.",
4610+
"protocolProfileBehavior": {},
4611+
"_postman_isSubFolder": true
45604612
}
45614613
],
45624614
"description": "API endpoints that read and update system statuses.",
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
// Copyright 2020 Jared Hendrickson
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
require_once("api/framework/APIEndpoint.inc");
17+
18+
class APIStatusGateway extends APIEndpoint {
19+
public function __construct() {
20+
$this->url = "/api/v1/status/gateway";
21+
}
22+
23+
protected function get() {
24+
return (new APIStatusGatewayRead())->call();
25+
}
26+
}

pfSense-pkg-API/files/etc/inc/api/framework/APIQuery.inc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ class APIQuery {
168168
# Checks if an integer or numeric string is less than a given value
169169
private function lt($value, $limit) {
170170
if (is_numeric($value) and is_numeric($value)) {
171-
if (intval($value) < intval($limit)) {
171+
if (floatval($value) < floatval($limit)) {
172172
return true;
173173
}
174174
}
@@ -178,7 +178,7 @@ class APIQuery {
178178
# Checks if an integer or numeric string is less than or equal to a given value
179179
private function lte($value, $limit) {
180180
if (is_numeric($value) and is_numeric($value)) {
181-
if (intval($value) <= intval($limit)) {
181+
if (floatval($value) <= floatval($limit)) {
182182
return true;
183183
}
184184
}
@@ -188,7 +188,7 @@ class APIQuery {
188188
# Checks if an integer or numeric string is greater than a given value
189189
private function gt($value, $limit) {
190190
if (is_numeric($value) and is_numeric($value)) {
191-
if (intval($value) > intval($limit)) {
191+
if (floatval($value) > floatval($limit)) {
192192
return true;
193193
}
194194
}
@@ -198,7 +198,7 @@ class APIQuery {
198198
# Checks if an integer or numeric string is greater than or equal to a given value
199199
private function gte($value, $limit) {
200200
if (is_numeric($value) and is_numeric($value)) {
201-
if (intval($value) >= intval($limit)) {
201+
if (floatval($value) >= floatval($limit)) {
202202
return true;
203203
}
204204
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
// Copyright 2020 Jared Hendrickson
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
require_once("api/framework/APIModel.inc");
17+
require_once("api/framework/APIResponse.inc");
18+
19+
20+
class APIStatusGatewayRead extends APIModel {
21+
# Create our method constructor
22+
public function __construct() {
23+
parent::__construct();
24+
$this->privileges = ["page-all", "page-status-gateways"];
25+
}
26+
27+
public function action() {
28+
return APIResponse\get(0, $this->__get_metrics());
29+
}
30+
31+
private function __get_metrics() {
32+
$gw_metrics_raw = return_gateways_status(true);
33+
$gw_metrics = [];
34+
35+
# Loop through each gateway's metrics and format time data as floats
36+
foreach ($gw_metrics_raw as $gw) {
37+
$gw["delay"] = floatval(str_replace(["%", "ms"], "", $gw["delay"]));
38+
$gw["stddev"] = floatval(str_replace(["%", "ms"], "", $gw["stddev"]));
39+
$gw["loss"] = floatval(str_replace(["%", "ms"], "", $gw["loss"]));
40+
$gw_metrics[] = $gw;
41+
}
42+
return $gw_metrics;
43+
}
44+
}

0 commit comments

Comments
 (0)