Metric from API

Joined
Aug 16, 2023
Messages
2
For background: I have Grafana running on a Linux server which is used to gather system metrics of all the servers in my network(the data is collected from servers via a Python script and data is stored in my Postgres DB). I recently wanted to add my true nas core system as a machine for which I could track the metrics. I have a Python script that at the moment is able to call the true nas API and get the uptime of my true nas machine, but I am running into an issue with figuring out which endpoint to call to get the CPU utilization and memory utilization. I have tried the /system and /stat endpoint to no avail. Does anyone know where I should look and maybe an example query to get this info from the API? My script so far:

import requests
import datetime
import json
from datetime import timedelta


def getTruenas(ip,api_key):
# Get current time
now = datetime.datetime.now()

url = f"http://{ip}/api/v2.0/system/info"

payload= {}


headers = {
'Authorization': f'Bearer {api_key}'
}

response = requests.request("GET", url, headers=headers, data=payload)
data = json.loads(response.content)

uptime = data['uptime_seconds']
host_name = data['hostname']
operating_system = data['version']

print(f"Uptime: {uptime}")
print(f"Name: {host_name}")
print(f"OS: {operating_system}")
print(f"IP: {ip}")
print(f"Time: {now}")



# This code obviously won't work but I was playing around here to see what happened
url = f"http://{ip}/api/v2.0/reporting/cpu"
response = requests.request("GET",url, headers=headers, data=payload)
#data = json.loads(response.content)
print(response)

# cpu = data['CPU Usage']

# print(f"CPU Utilization: {cpu}")

api_key = "..."
ip = '...'

getTruenas(ip,api_key)
 

sretalla

Powered by Neutrality
Moderator
Joined
Jan 1, 2016
Messages
9,703
Looks to me like it won't just return data until you specify what data you want:

From the webhook example...
Code:
{
    "id": "6841f242-840a-11e6-a437-00e04d680384",
    "msg": "method",
    "method": "reporting.get_data",
    "params": [
        [{"name": "nfsstat"}],
        {"unit": "HOURLY"},
    ]
}
 

sretalla

Powered by Neutrality
Moderator
Joined
Jan 1, 2016
Messages
9,703
And looking at the Restful doc, I see it expects a POST type with data like this:

Code:
{
  "graphs": [
    {}
  ],
  "reporting_query": {
    "unit": "HOUR",
    "page": 0,
    "start": "string",
    "end": "string",
    "aggregate": true
  }
}


You may get away with leaving the graphs empty to get all data??? (EDIT: no you can't)
 
Last edited:

sretalla

Powered by Neutrality
Moderator
Joined
Jan 1, 2016
Messages
9,703
OK, there it is...

I sent this as the body of the POST to /api/v2.0/reporting/get_data

Code:
{
  "graphs": [
    { "name": "cpu" }
  ],
  "reporting_query": {
    "unit": "HOUR",
    "page": 0,
    "aggregate": true
  }
}


And I got back:

Code:
[
 {
  "name": "cpu",
  "identifier": null,
  "data": [
   [
    0.15761701676,
    8.7407988136,
    19.06658855,
    19.06658855,
    800.00000055
   ],
   [
    0.11446881927,
    6.4774365897,
    16.574489435,
    16.574489435,
    800.00000045
   ],
... some (large amount of) data removed for brevity of the post.
   [
    0.22035557213,
    5.7211495779,
    13.568013065,
    13.611302408,
    800.00000055
   ],
   [
    0.16578192625,
    7.3023212581,
    15.125401172,
    15.204126426,
    800.00000045
   ],
   [
    0.0,
    0.0,
    0.0,
    0.0,
    0.0
   ]
  ],
  "start": 1692271210,
  "end": 1692274810,
  "step": 10,
  "legend": [
   "interrupt",
   "system",
   "user",
   "nice",
   "idle"
  ],
  "aggregations": {
   "min": [
    0.0,
    0.0,
    0.0,
    0.0,
    0.0
   ],
   "mean": [
    0.12420154078755402,
    7.053033654076454,
    15.917830180578948,
    15.944780928249308,
    797.7839334681163
   ],
   "max": [
    0.42909559468,
    11.303424617,
    26.295127994,
    26.338524883,
    800.000001
   ]
  }
 }
]
 
Last edited:
Joined
Aug 16, 2023
Messages
2
This is very promising thank you @sretalla ! The only thing that I notice is all my data comes back as 0.0 , but I think that might be due to my fairly little usage of my NAS over the past hour. Something I notice is the data is returned back as an array: [0.0, 0.0, 0.0, 0.0, 0.0] do each of these represent interrupt, system, user, nice, and idle? And if so is there no way just to get average usage or is that calculated by adding all of these values?
 

sretalla

Powered by Neutrality
Moderator
Joined
Jan 1, 2016
Messages
9,703
Something I notice is the data is returned back as an array: [0.0, 0.0, 0.0, 0.0, 0.0] do each of these represent interrupt, system, user, nice, and idle?
Right, as is indicated by the legend entry near the end.

And if so is there no way just to get average usage or is that calculated by adding all of these values?
I would be using the aggregations set called "mean" for that. (second to last in that set at the bottom)
 
Top