Prometheus Node Exporter CPU Monitoring with PromeSQL
PromeSQL is a SQL engine designed to query and aggregate Prometheus data. To query node-exporter CPU data, ensure that node-exporter's monitoring metrics are configured in Prometheus and added to Prometheus's monitoring target list via the prometheus.yml file. Afterwards, you can use the PromeSQL query language to retrieve node-exporter CPU data.
Here's an example query to obtain node-exporter's CPU usage:
SELECT 100 - (avg(irate(node_cpu_seconds_total{mode='idle'}[5m])) * 100) FROM node_cpu_seconds_total WHERE mode != 'idle'
This query returns a numerical representation of the CPU usage as a percentage. It utilizes Prometheus's built-in irate() function to compute the CPU usage growth rate, employing the avg() function to calculate the average over the last 5 minutes. The WHERE clause excludes idle CPU time.
To perform queries using PromeSQL within Node.js, leverage the Node.js Prometheus client library, prom-client. Below is an example code snippet demonstrating how to query node-exporter CPU data:
const promClient = require('prom-client');
const PromeSQL = require('prome-sql');
const registry = new promClient.Registry();
const promeSQL = new PromeSQL(registry);
const query = 'SELECT 100 - (avg(irate(node_cpu_seconds_total{mode='idle'}[5m])) * 100) FROM node_cpu_seconds_total WHERE mode != 'idle'';
promeSQL.query(query).then(result => {
console.log(result);
}).catch(err => {
console.error(err);
});
This code utilizes the prom-client library to create a Prometheus Registry object, which is then passed to the prome-sql object. Subsequently, it utilizes the query() method to execute the query and prints the result upon completion.
原文地址: https://www.cveoy.top/t/topic/khZY 著作权归作者所有。请勿转载和采集!