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

**URL:** <https://community.dynatrace.com/t/which-api-info-of-cpu-usage-and-mem-usage/37296>\
**Category:** Dynatrace API\
**Tags:** dynatrace-api\
**Created:** [November 15, 2022, 2:46pm UTC](https://community.dynatrace.com/t/which-api-info-of-cpu-usage-and-mem-usage/37296 "2022-11-15T14:46:13Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![avrildong](https://avatars.discourse-cdn.com/v4/letter/a/9de0a6/32.png) [@avrildong](https://community.dynatrace.com/u/avrildong)\
**Post date:** [November 15, 2022, 2:46pm UTC](https://community.dynatrace.com/t/which-api-info-of-cpu-usage-and-mem-usage/37296/1 "2022-11-15T14:46:13Z")

</div>

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.&nbsp; We tried&nbsp;api/v1/oneagents and api/v1/entity/infrastructure/hosts, we are unable to find those info&nbsp;CPU% and MEM% and NetworkTraffic.

Which API we can use to get those?

---

<div class="post-metadata">

**Author:** ![paolo\_fumanelli](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/paolo_fumanelli/32/1037_2.png) [@paolo\_fumanelli](https://community.dynatrace.com/u/paolo_fumanelli)\
**Post date:** [November 15, 2022, 4:04pm UTC](https://community.dynatrace.com/t/which-api-info-of-cpu-usage-and-mem-usage/37296/2 "2022-11-15T16:04:55Z")

</div>

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)

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

```

, for example for HOST XYZ123 as shown above.

---

<div class="post-metadata">

**Author:** ![mgome](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@mgome](https://community.dynatrace.com/u/mgome)\
**Post date:** [November 15, 2022, 5:19pm UTC](https://community.dynatrace.com/t/which-api-info-of-cpu-usage-and-mem-usage/37296/3 "2022-11-15T17:19:33Z")

</div>

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. &nbsp;The data is returned with all the cpu metrics followed by all the memory metrics. &nbsp;A simple sort in Excel can put them in hostname order.

---

<div class="post-metadata">

**Author:** ![avrildong](https://avatars.discourse-cdn.com/v4/letter/a/9de0a6/32.png) [@avrildong](https://community.dynatrace.com/u/avrildong)\
**Post date:** [November 18, 2022, 9:54pm UTC](https://community.dynatrace.com/t/which-api-info-of-cpu-usage-and-mem-usage/37296/4 "2022-11-18T21:54:57Z")

</div>

[@mgome](https://community.dynatrace.com/user/8132),

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

---

<div class="post-metadata">

**Author:** ![avrildong](https://avatars.discourse-cdn.com/v4/letter/a/9de0a6/32.png) [@avrildong](https://community.dynatrace.com/u/avrildong)\
**Post date:** [November 18, 2022, 10:24pm UTC](https://community.dynatrace.com/t/which-api-info-of-cpu-usage-and-mem-usage/37296/5 "2022-11-18T22:24:35Z")

</div>

[@mgome](https://community.dynatrace.com/user/8132),

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

---

<div class="post-metadata">

**Author:** ![avrildong](https://avatars.discourse-cdn.com/v4/letter/a/9de0a6/32.png) [@avrildong](https://community.dynatrace.com/u/avrildong)\
**Post date:** [November 18, 2022, 10:29pm UTC](https://community.dynatrace.com/t/which-api-info-of-cpu-usage-and-mem-usage/37296/6 "2022-11-18T22:29:42Z")

</div>

@paolo_fumanelli, [@mgome](https://community.dynatrace.com/user/8132), thank you very much! It helps.

---

<div class="post-metadata">

**Author:** ![mgome](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@mgome](https://community.dynatrace.com/u/mgome)\
**Post date:** [November 21, 2022, 5:40pm UTC](https://community.dynatrace.com/t/which-api-info-of-cpu-usage-and-mem-usage/37296/7 "2022-11-21T17:40:47Z")

</div>

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.

> **[Metrics API - Metric selector](https://docs.dynatrace.com/docs/dynatrace-api/environment-api/metric-v2/metric-selector)**
>
> Configure the metric selector for the Metric v2 API.

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

---

<div class="post-metadata">

**Author:** ![avrildong](https://avatars.discourse-cdn.com/v4/letter/a/9de0a6/32.png) [@avrildong](https://community.dynatrace.com/u/avrildong)\
**Post date:** [November 21, 2022, 7:53pm UTC](https://community.dynatrace.com/t/which-api-info-of-cpu-usage-and-mem-usage/37296/8 "2022-11-21T19:53:40Z")

</div>

[@mgome](https://community.dynatrace.com/user/8132)

All clear, thank you very much!

---

<div class="post-metadata">

**Author:** ![mgome](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@mgome](https://community.dynatrace.com/u/mgome)\
**Post date:** [November 21, 2022, 9:35pm UTC](https://community.dynatrace.com/t/which-api-info-of-cpu-usage-and-mem-usage/37296/9 "2022-11-21T21:35:01Z")

</div>

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
