Evgeny Lundberg
04/17/2024, 8:58 AM100 * (1 - (avg by(host_name) (irate(system_cpu_time{state="idle"}[5m]))))
But this does not returning anything:
SELECT
host_name,
100 * (1 - avg(system_cpu_time_idle) / avg(system_cpu_time_total)) AS idle_cpu_percentage
FROM
signoz_metrics.time_serie_v2
WHERE
datetime >= now() - INTERVAL 5 MINUTE
GROUP BY
host_name
Srikanth Chekuri
04/18/2024, 3:02 AMEvgeny Lundberg
04/18/2024, 8:35 AMEvgeny Lundberg
04/18/2024, 8:51 AMsystem_cpu_time
and group by host_name ?Srikanth Chekuri
04/18/2024, 10:26 AMavg_rate
operator from the options.Evgeny Lundberg
04/18/2024, 11:16 AMavg_rate
not giving correct valuesSrikanth Chekuri
04/18/2024, 1:17 PMEvgeny Lundberg
04/19/2024, 11:13 AMtop -bn2 | grep '%Cpu' | tail -1 | grep -P '(....|...) id,'|awk '{print "CPU Usage: " 100-$8 "%"}'
on one of those host I'll get CPU Usage: 1.4%
It will not be 0.17 (17%) as shown in previous screenshot I sentEvgeny Lundberg
04/19/2024, 1:19 PM100 * (1 - (avg by(host_name) (irate(system_cpu_time{state="idle"}[5m]))))
But since it's not possible to sort results in descending order in a table - I need to achieve this with Clickhouse.
You wrote my clickhouse query was invalid - whats wrong with and how to fix it ? 🙂Evgeny Lundberg
04/30/2024, 12:46 PMSELECT
s.metric_name,
s.timestamp_ms,
s.value,
JSONExtractString(t.labels, 'host_name') AS host_name,
JSONExtractString(t.labels, 'state') AS state
FROM
signoz_metrics.samples_v2 s
INNER JOIN
signoz_metrics.time_series_v2 t
ON
s.fingerprint = t.fingerprint
WHERE
s.metric_name = 'system_cpu_time'
AND JSONExtractString(t.labels, 'host_name') = 'hostname4'
AND JSONExtractString(t.labels, 'state') = 'idle'
AND s.timestamp_ms >= (toUnixTimestamp(now() - INTERVAL 5 MINUTE) * 1000) -- Last 5 minutes
ORDER BY
s.timestamp_ms DESC LIMIT 1;
Srikanth Chekuri
04/30/2024, 3:04 PMavg_rate
from operator and use the formula expression 100 * (1-A)
to achieve result of promql query.Evgeny Lundberg
05/02/2024, 2:13 PMavg_rate
in Query builder ?Srikanth Chekuri
05/02/2024, 5:40 PMEvgeny Lundberg
05/08/2024, 8:05 AMavg_rate
operator in Query builder does not generating correct values (actual CPU usage on the machines)
However, I finally figured out how to achieve this with clickhouse:
WITH
-- Query to get the total CPU time for all hosts that exist in host_info
total_cpu_time AS (
SELECT
JSONExtractString(t.labels, 'host_name') AS host_name,
SUM(s.value) AS total_time
FROM
signoz_metrics.samples_v2 s
INNER JOIN
signoz_metrics.time_series_v2 t
ON
s.fingerprint = t.fingerprint
INNER JOIN
signoz_metrics.host_info hi ON JSONExtractString(t.labels, 'host_name') = hi.host_name
WHERE
s.metric_name = 'system_cpu_time'
AND JSONExtractString(t.labels, 'state') IN ('nice', 'user', 'steal', 'wait', 'system', 'softirq', 'idle') -- Relevant CPU states
AND s.timestamp_ms >= (toUnixTimestamp(now() - INTERVAL 5 MINUTE) * 1000) -- Last 5 minutes
GROUP BY
JSONExtractString(t.labels, 'host_name')
),
-- Query to get the idle CPU time for all hosts that exist in host_info
idle_cpu_time AS (
SELECT
JSONExtractString(t.labels, 'host_name') AS host_name,
SUM(s.value) AS idle_time
FROM
signoz_metrics.samples_v2 s
INNER JOIN
signoz_metrics.time_series_v2 t
ON
s.fingerprint = t.fingerprint
INNER JOIN
signoz_metrics.host_info hi ON JSONExtractString(t.labels, 'host_name') = hi.host_name
WHERE
s.metric_name = 'system_cpu_time'
AND JSONExtractString(t.labels, 'state') = 'idle'
AND s.timestamp_ms >= (toUnixTimestamp(now() - INTERVAL 5 MINUTE) * 1000)
GROUP BY
JSONExtractString(t.labels, 'host_name')
)
-- Calculate the CPU usage percentage for all hosts in host_info
SELECT
hi.host_name AS Host,
hi.ip_address AS IP,
ROUND(100 * (1 - (ic.idle_time / tc.total_time)), 2) AS cpu_usage
FROM
total_cpu_time tc
LEFT JOIN
idle_cpu_time ic ON tc.host_name = ic.host_name
LEFT JOIN
signoz_metrics.host_info hi ON tc.host_name = hi.host_name
WHERE
hi.host_name IS NOT NULL -- Ensure the host_name exists in host_info
ORDER BY
cpu_usage DESC
Srikanth Chekuri
05/08/2024, 8:43 AMavg_rate
gives the result. You have to use the formula on top of it then it is the same as the promql and must give the correct result. If you prefer to use ClickHouse go ahead with it.SigNoz is an open-source APM. It helps developers monitor their applications & troubleshoot problems, an open-source alternative to DataDog, NewRelic, etc.
Powered by