CPU usage polling via API

tsumaru720

Well-Known Member
May 1, 2016
66
2
48
44
Hi Guys

I'm looking at making some scripts that give me stats of my containers but I'm having difficulty getting CPU usage for it via the API.

When looking at a container's status via the API, I suspect the "cpu" value should represent the percentage of CPU its using against its allocation?

This container is at 20% usage (looking at its summary in the web UI), but the value is 0.

Code:
pve:/> get nodes/pmx01/lxc/100/status/current
200 OK
{
   "cpu" : 0,
   "cpus" : "1",
   "disk" : 24936472576,
   "diskread" : "911796413440",
   "diskwrite" : "2439126016",
   "ha" : {
      "managed" : 0
   },
   "lock" : "",
   "maxdisk" : 52710469632,
   "maxmem" : 2147483648,
   "maxswap" : 2147483648,
   "mem" : 1572667392,
   "name" : "syslog.example.com",
   "netin" : 3827656894,
   "netout" : 2730394259,
   "pid" : "2980",
   "status" : "running",
   "swap" : 626429952,
   "template" : "",
   "type" : "lxc",
   "uptime" : 257654
}

The same seems to apply to QEMU/KVM too - I'd loaded this VM at 100% but "cpu" still 0

Code:
pve:/> get nodes/pmx01/qemu/104/status/current
200 OK
{
   "balloon" : 536870912,
   "ballooninfo" : {
      "actual" : 536870912,
      "max_mem" : 536870912
   },
   "blockstat" : {
      "ide2" : {
         "account_failed" : false,
         "account_invalid" : false,
         "failed_flush_operations" : 0,
         "failed_rd_operations" : 0,
         "failed_wr_operations" : 0,
         "flush_operations" : 0,
         "flush_total_time_ns" : 0,
         "idle_time_ns" : 2319738740,
         "invalid_flush_operations" : 0,
         "invalid_rd_operations" : 0,
         "invalid_wr_operations" : 0,
         "rd_bytes" : 14274774,
         "rd_merged" : 0,
         "rd_operations" : 793042,
         "rd_total_time_ns" : 24046696658,
         "timed_stats" : [],
         "wr_bytes" : 0,
         "wr_highest_offset" : 0,
         "wr_merged" : 0,
         "wr_operations" : 0,
         "wr_total_time_ns" : 0
      },
      "virtio0" : {
         "account_failed" : true,
         "account_invalid" : true,
         "failed_flush_operations" : 0,
         "failed_rd_operations" : 0,
         "failed_wr_operations" : 0,
         "flush_operations" : 0,
         "flush_total_time_ns" : 0,
         "idle_time_ns" : 185813278619,
         "invalid_flush_operations" : 0,
         "invalid_rd_operations" : 0,
         "invalid_wr_operations" : 0,
         "rd_bytes" : 196230144,
         "rd_merged" : 4,
         "rd_operations" : 19872,
         "rd_total_time_ns" : 65452421340,
         "timed_stats" : [],
         "wr_bytes" : 1512230912,
         "wr_highest_offset" : 1860009472,
         "wr_merged" : 436,
         "wr_operations" : 76615,
         "wr_total_time_ns" : 1770928176815
      }
   },
   "cpu" : 0,
   "cpus" : 1,
   "disk" : 0,
   "diskread" : 210504918,
   "diskwrite" : 1512230912,
   "ha" : {
      "managed" : 0
   },
   "maxdisk" : 2147483648,
   "maxmem" : 536870912,
   "mem" : 266082277,
   "name" : "vpn1.example.com",
   "netin" : 2130369187,
   "netout" : 1473923150,
   "nics" : {
      "tap104i0" : {
         "netin" : "1148498060",
         "netout" : "31456760"
      },
      "tap104i1" : {
         "netin" : "981871127",
         "netout" : "1442466390"
      }
   },
   "pid" : "8787",
   "qmpstatus" : "running",
   "status" : "running",
   "template" : "",
   "uptime" : 2370124
}

Am I looking in the right place? :)
 
Hi @wolfgang

Thanks, but I was hoping more for somthing I could poll to get usage at the time of polling. That command seems to give me a lot of un-nessecery output and seems to be delayed by 1 minute. Should the command I listed above not output CPU usage at the time of polling?

It seems like it should, but isnt - So is it a bug?
 
Hi @wolfgang

Thanks, but I was hoping more for somthing I could poll to get usage at the time of polling. That command seems to give me a lot of un-nessecery output and seems to be delayed by 1 minute. Should the command I listed above not output CPU usage at the time of polling?

It seems like it should, but isnt - So is it a bug?

this is a limitation of how we collect the information.

the linux kernel measures the cpu time since the start, so a momentary information is not really possible, only when you have at least 2 points in time you can
calculate the difference (which makes sense, because at any point in time only one process is active at one core, so you have to calculate over a time period to get cpu usage in %)

since you call pvesh it will only have one point in time -> no sensible cpu usage information

as an alternative you can query via curl and the api -> the pvedaemon collects the information on a regular basis, so it has more datapoints
 
Is there any updates for this? i was looking some something similar.

i think not much has changed with regards to this.

you can still use an external software for monitoring purposes though (zabbix or similar)
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!