Skip to content

Commit

Permalink
feat(ns-api): add ns.report latency-and-quality-report (#908)
Browse files Browse the repository at this point in the history
  • Loading branch information
andre8244 authored Nov 18, 2024
1 parent b68c938 commit d169124
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 0 deletions.
114 changes: 114 additions & 0 deletions packages/ns-api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7555,3 +7555,117 @@ Output example:
```json
{"hours":[["00",0],["01",0],["02",0],["03",0],["04",0],["05",0],["06",0],["07",0],["08",0],["09",0],["10",0],["11",0],["12",7877909812],["13",0],["14",0],["15",0],["16",0],["17",0],["18",0],["19",0],["20",0],["21",0],["22",0],["23",0]]}
```
### latency-and-quality-report
Report latency metrics (minimum, maximum and average) and connectivy quality data (packet delivery rate) for every host configured in Netdata fping configuration file, located at `/etc/netdata/fping.conf`.
Usage example:
```
api-cli ns.report latency-and-quality-report
```
Output example:
```json
{
"8.8.8.8": {
"latency": {
"labels": [
"time",
"minimum",
"maximum",
"average"
],
"data": [
[
1731485630,
9.2926149,
17.3652723,
11.2476048
],
[
1731485262,
9.2831232,
17.3944183,
11.5604364
],
[
1731484894,
9.294786,
18.153445,
11.376502
]
]
},
"quality": {
"labels": [
"time",
"returned"
],
"data": [
[
1731485630,
100
],
[
1731485262,
99.8152174
],
[
1731484894,
100
]
]
}
},
"1.1.1.1": {
"latency": {
"labels": [
"time",
"minimum",
"maximum",
"average"
],
"data": [
[
1731485630,
14.8992382,
25.0276303,
17.3071943
],
[
1731485262,
14.8918498,
23.8407958,
17.3052552
],
[
1731484894,
14.8776663,
24.2058399,
17.0578876
]
]
},
"quality": {
"labels": [
"time",
"returned"
],
"data": [
[
1731485630,
100
],
[
1731485262,
99.8152174
],
[
1731484894,
100
]
]
}
}
}
```
42 changes: 42 additions & 0 deletions packages/ns-api/files/ns.report
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import subprocess
from datetime import datetime
from collections import defaultdict
from nethsec import utils
import urllib.request

## Utility functions

Expand Down Expand Up @@ -315,13 +316,52 @@ def ovpnrw_bytes_by_hour_and_user(instance, day, user):
return {"hours": hours_bytes}


def get_fping_hosts():
# read fping hosts from /etc/netdata/fping.conf
try:
with open("/etc/netdata/fping.conf", 'r') as fp:
line = fp.readline()
line = line[7:-2]
hosts = line.split(" ")
return hosts
except:
return []


def get_netdata_chart_data(chart_name):
ret = {"labels": [], "data": []}
# retrieve chart data from netdata
url = f'http://127.0.0.1:19999/api/v1/data?chart={chart_name}&after=-3600&points=180&options=abs'
try:
with urllib.request.urlopen(url) as fu:
data = json.loads(fu.read())
except:
return ret
return data


def latency_and_quality_report():
hosts = get_fping_hosts()
ret = {}
for host in hosts:
host_replaced = host.replace('.', '_')
latency_chart_data = get_netdata_chart_data(f'fping.{host_replaced}_latency')
quality_chart_data = get_netdata_chart_data(f'fping.{host_replaced}_quality')
ret[host] = {
"latency": latency_chart_data,
"quality": quality_chart_data
}
return ret


cmd = sys.argv[1]

if cmd == 'list':
print(json.dumps({
'tsip-malware-report': {},
'tsip-attack-report': {},
'mwan-report': {},
'latency-and-quality-report': {},
'ovpnrw-list-days': {
'instance': 'String'
},
Expand Down Expand Up @@ -375,4 +415,6 @@ elif cmd == 'call':
if ret is None:
ret = mwan_report()
save_cached_report('mwan-report', ret)
elif action == "latency-and-quality-report":
ret = latency_and_quality_report()
print(json.dumps(ret))

0 comments on commit d169124

Please sign in to comment.