使用Prometheus和Cadvisor在Grafana中获取CPU使用率(按主机和名称排序)

huangapple go评论80阅读模式
英文:

Obtaining CPU Usage (sorted by host and name) with Prometheus and Cadvisor in Grafana

问题

感谢@markalex对他关于我的先前问题的直观且信息丰富的答案,因为我认为它可能解决我的问题——实际上是以最初陈述的方式——但为了简化,我没有提到我有两个不同的主机,并且尝试在同一个查询中也按它们的核心分离。我想在原始问题中添加这些附加信息,但后来被告知这样做是不明智的,不仅对寻找明确答案的人不利,而且对回答该问题的人也不利。

至于问题,如果我想使相同的查询(来自上面的链接)不仅按名称分隔CPU使用率,还按主机分隔(我有两个主机),这是否可能?这是我尝试的内容,但它不起作用:

sum(rate(container_cpu_usage_seconds_total{host=~".+",name=~".+"}[$__rate_interval])) by (host,name) / on() group_left() sum(machine_cpu_cores{host=~".+"})

坦率地说,我认为这个查询不会起作用,因为它最终会除以我两个主机的核心总数,但我曾希望也许有一种直观的方法可以在执行该除法时将它们分开并匹配左边,同时只使用1个查询。如果我只能使用两个查询并明确命名主机,我会接受,但我希望有什么东西我错过了。毕竟,我并不完全知道自己在做什么。

编辑:

我可能已经找到了使用以下查询执行此功能的方法:

sum(rate(container_cpu_usage_seconds_total{host=~".+",name=~".+"}[$__rate_interval])) by (host,name) / on(host) group_left() sum(machine_cpu_cores{host=~".+"}) by (host)

请告诉我这是否可行,这次,我在有人尝试回答之前添加了编辑,所以希望这次可以接受?

英文:

I'd like to thank @markalex for his very intuitive and informative answer regarding my previous question as I thought it might solve my issue - and it does the way the question was initially stated - but for simplicity, I left out the fact that I have two different hosts and was trying to also separate by their cores in the same query. I wanted to add this additional info in the original question, but was then made aware that it's a bad idea to do it this way, and not only does a disservice to anyone looking for a clear answer to the question, but also to the person that answered that question.

Getting to the question, if I wanted to make it so that same query (from the above link) would be separating cpu usage not only by name, but also by host (I have two hosts), is that even possible? Here's what I tried, but it doesn't work:

sum(rate(container_cpu_usage_seconds_total{host=~".+",name=~".+"}[$__rate_interval])) by (host,name) / on() group_left() sum(machine_cpu_cores{host=~".+"})

Admittedly, I didn't think this query was going to work as it just ends up dividing by the combined amount of cores of both my hosts, but I had hoped that maybe there was an intuitive way to separate them and match by the left side when performing that division while using only 1 query. If all I can do is instead use two queries and specifically name the hosts, I'll accept that, but I'm hoping that there's something I missed. After all, it's not like I know exactly what I'm doing yet.

Edit:

I may have found a way to perform this function with the below query:

sum(rate(container_cpu_usage_seconds_total{host=~".+",name=~".+"}[$__rate_interval])) by (host,name) / on(host) group_left() sum(machine_cpu_cores{host=~".+"}) by (host)

Please let me know if this is feasible, and this time, I added the edit before someone attempted to answer, so hopefully this is ok?

答案1

得分: 0

以下是翻译好的部分:

我的上面的编辑对我有效,所以我将其发布为答案:

sum(rate(container_cpu_usage_seconds_total{host=~".+",name=~".+"}[$__rate_interval])) by (host,name) / on(host) group_left() sum(machine_cpu_cores{host=~".+"}) by (host)
英文:

My above edit works for me, so I'll post this as the answer:

sum(rate(container_cpu_usage_seconds_total{host=~".+",name=~".+"}[$__rate_interval])) by (host,name) / on(host) group_left() sum(machine_cpu_cores{host=~".+"}) by (host)

huangapple
  • 本文由 发表于 2023年6月12日 10:32:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76453333.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定