Can you write a PromQL query that will calculate the average memory usage as a percentage
- User
Can you write a PromQL query that will calculate the average memory usage as a percentage
- Assistant
Yes, here's a PromQL query that calculates the average memory usage as a percentage:
scss100 * (sum(node_memory_MemTotal - node_memory_MemFree) by (instance)) / sum(node_memory_MemTotal) by (instance)This query uses the
node_memory_MemTotalandnode_memory_MemFreemetrics, which are commonly available in Prometheus for monitoring memory usage on nodes. Thesumaggregates the values over time, and theby (instance)clause groups the calculation by instance, so the average is calculated for each node individually. Finally, the result is multiplied by 100 to express the value as a percentage. - User
This does not seem to accurately calculate the memory usage based on current values. While I am expecting a roughly 12-14% value it is giving me an over 25% value.
- Assistant
It seems like there might be an issue with the metrics you are using.
node_memory_MemTotalandnode_memory_MemFreeare common metrics, but the actual names and values may vary depending on your specific setup.It's also possible that the calculation is being performed correctly, but the result is not what you expect due to some underlying issue with your memory usage.
I suggest checking the actual values of
node_memory_MemTotalandnode_memory_MemFreeto ensure that they are correct and representative of the memory usage on your nodes. Additionally, you can try adjusting the query to use other memory metrics that may provide a more accurate representation of your memory usage. - User
It looks like you forgot to append _bytes to each metric. When I do that though the value for MemTotal bytes is 3053277184 and the value for MemFree is 2174967808
So based on your query can you calculate the percentage of usage?
- Assistant
Yes, you are correct, I apologize for that oversight. When we add the
_bytessuffix, the calculation is as follows:scss100 * (sum(node_memory_MemTotal_bytes - node_memory_MemFree_bytes) by (instance)) / sum(node_memory_MemTotal_bytes) by (instance)Using the values you provided, the calculation would be:
scss100 * (3053277184 - 2174967808) / 3053277184 = 28.76So the memory usage is approximately 28.76%.
- User
That would indicate the query is correctly using the values and giving a correct result however there must be some underlying factors like swap Usage perhaps that are not being considered?
- Assistant
Yes, that's possible. The query I provided calculates the usage of physical memory (RAM), but it does not account for swap usage.
Swap usage is memory that has been temporarily moved from RAM to disk when the system is running low on physical memory. It can have a significant impact on overall memory usage, so it's important to consider when monitoring memory usage.
You can extend the query to also consider swap usage by adding the
node_memory_SwapTotal_bytesandnode_memory_SwapFree_bytesmetrics. The new query would look like this:scss100 * (sum(node_memory_MemTotal_bytes - node_memory_MemFree_bytes - node_memory_SwapTotal_bytes + node_memory_SwapFree_bytes) by (instance)) / sum(node_memory_MemTotal_bytes + node_memory_SwapTotal_bytes) by (instance)This query calculates the sum of physical memory usage and swap usage, and then divides it by the sum of physical memory and swap capacity to get the overall memory usage as a percentage.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.