cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Which API?: Info of "CPU Usage" and "MEM Usage"

avrildong
Helper

We intend to get checklist of hosts with their CPU% and MEM% used info, which should return result to be consistent with dynatrace console > hosts page.  We tried api/v1/oneagents and api/v1/entity/infrastructure/hosts, we are unable to find those info CPU% and MEM% and NetworkTraffic.

Which API we can use to get those? 

8 REPLIES 8

Hi, you'd better use V2 Metrics API with keys

builtin:host.mem.usage

builtin:host.cpu.usage

You can filter by host name or id (also for multiple hosts)

 

api/v2/metrics/query?metricSelector=builtin%3Ahost.mem.usage&entitySelector=TYPE%28HOST%29%2CentityId%28XYZ123%29

, for example for HOST XYZ123 as shown above.

 

Additionally, you can pull both metrics in a single call and output the data in CSV format with host name:

 

/api/v2/metrics/query?metricSelector=builtin%3Ahost.%28cpu%2Cmem%29.usage%3Anames&resolution=10m&from=-5m&to=now&entitySelector=type%28%22HOST%22%29" -H "accept: text/csv; charset=utf-8" -H "Authorization: Api-Token <API-TOKEN>"

 

The above call uses curl arguments and the following metric selector with a names transformation to add the hostname:

 

builtin:host.(cpu,mem).usage:names

 

Finally, the "accept" header is set to text/csv to output the metric data in CSV format.  The data is returned with all the cpu metrics followed by all the memory metrics.  A simple sort in Excel can put them in hostname order.

@mgome,

Great thanks for the trick!

FYI:

I like this solution of csv (bundle builtin:host.(cpu,mem).usage:names) and definitely keep as future reference. This case I use json output. I am able to use your approach to get below:

Result from /api/v2/metrics/query?metricSelector=builtin%3Ahost.%28cpu%2Cmem%29.usage%3Anames&resolution=10m&from=-5m&to=now:

#1. For builtin:host.cpu.usage

//
..."result": [
{
"metricId": "builtin:host.cpu.usage:names",
"dataPointCountRatio": 0.001091,
"dimensionCountRatio": 0.02182,
"data": [
{
"dimensions": [
"HostName1",
"HostID1"
],
"dimensionMap": {
"dt.entity.host.name": "HostName1",
"dt.entity.host": "HostName1"
},
"timestamps": [
1668807600000,
1668808200000
],
"values": [
4.4364878336588545,
4.355010986328125
]
},... //
There are 2 values for this "host" (HostName1)
"values": [
4.4364878336588545,
4.355010986328125
]

#
#2. For builtin:host.mem.usage
//
{
"metricId": "builtin:host.mem.usage:names",
"dataPointCountRatio": 0.001091,
"dimensionCountRatio": 0.02182,
"data": [
{
"dimensions": [
"HostName1",
"HostID1"
],
"dimensionMap": {
"dt.entity.host.name": "HostName1",
"dt.entity.host": "HostID1"
},
"timestamps": [
1668807600000,
1668808200000
],
"values": [
23.029056294759116,
23.92061996459961
]
},... //
There are 2 values for this "host" (HostName1)
"values": [
23.029056294759116,
23.92061996459961
]

Rgds, Avril

@mgome,

3 more questions:

Q1. If feasible, any straight curl way to only pick last data-point-value? we intend to only grab latest/now data-point binding CPU, MEM%, total MEM, disk latency and network traffic value, which is same as last 30mins default timeframe shown in console > hosts page.

Q2. From "console > hosts page": there is extra "total MEM " + "Disk latency" + "Network traffic", which metrics is for "Disk latency"?

i.e. please also help to review below use case and confirm "total MEM " + "Network traffic" metrics are correct.

HostName1,

"Memory usage" = 4.46 % of 31.2 GiB (4.46% grabbed from above your providing builtin:host.mem.usage, 31.2GiB total MEM from "builtin:host.mem.total"), "Disk latency"=725 µs (? which one?), "Network traffic"="14.7 kbit/s"(from "builtin:host.net.nic.traffic")

Q3. Same use case from #2.

  • CPU%_MEM%_ByHOSTName: api/v2/metrics/query?metricSelector=builtin%3Ahost.%28cpu%2Cmem%29.usage%3Anames&resolution=10m&from=-10m&to=now
  • MEMTotal_ByHOSTName: api/v2/metrics/query?metricSelector=builtin%3Ahost.%28mem%29.total%3Anames&resolution=10m&from=-10m&to=now
  • NetworkTraffic_ByHOSTName: api/v2/metrics/query?metricSelector=builtin%3Ahost.net.nic.traffic%3Anames&resolution=10m&from=-10m&to=now

I am able to use your approach to grab those 4 metrics by hostname, via 3 separate API calls, is it feasible to make all 3 into 1 convenient API call? if feasible, what could be the syntax?

Thanks & Rgds, Avril

Q1) There is a last transformation which returns the last data point of the timeframe searched. I wouldn't suggest using it with a timeframe consisting of "now" because often times the current minute is null until the metric value is received.

https://www.dynatrace.com/support/help/dynatrace-api/environment-api/metric-v2/metric-selector#last

Q2) Disk latency is the disk read and write time metrics:
builtin:host.disk.readTime
builtin:host.disk.writeTime


Q3) You can use commas to separate multiple metrics in a single query

builtin:host.mem.usage,builtin:host.mem.total,builtin:host.net.nic.traffic

@mgome

All clear, thank you very much!

You need to apply the names transformation to each metric.

builtin:host.mem.usage:names,builtin:host.mem.total:names,builtin:host.net.nic.traffic:names

@paolo_fumanelli, @mgome, thank you very much! It helps.

Featured Posts