How to get all the metrics of an instance with prometheus api?
Asked Answered
C

4

6

I want to fetch the monitor host's metrics through the api of prometheus, and I need to initiate a request for each metric requested.

curl http://IP:9090/api/v1/query?query=node_cpu_seconds_total{instance="IP:9100"}

curl http://IP:9090/api/v1/query?query=node_memory_MemTotal_bytes{instance="IP:9100"}

Is there a way to request all the performance data from the monitoring host at once?

Capitalize answered 20/8, 2020 at 3:21 Comment(0)
G
7

You can fetch all metrics with following curl expression:

url=http://{urIPorhostname}:9090    
curl -s  $url/api/v1/label/__name__/values | jq -r ".data[]" | sort 
Guzman answered 18/6, 2021 at 16:54 Comment(0)
D
2

All the metrics for a particular instance IP:9100 can be obtained via the following PromQL query:

{instance="IP:9100"}

This query returns all the metrics, which have the given instance="IP:9100" label. See time series selector docs for more details. See also PromQL tutorial.

The {instance="IP:9100"} query can be sent to the following Prometheus querying APIs:

  • /api/v1/query - this endpoint returns matching time series values at the given timestamp specified via time query arg. For example, curl 'http://prometheus:9090/api/v1/query?query={instance="IP:9100"}' . Make sure you properly encoded query arg with percent encoding. See this demo.
  • /api/v1/series - this endpoint returns matching time series without any data, e.g. only metric names and labels are returned. For example, curl 'http://prometheus:9090/api/v1/series?match[]={instance="IP:9100"}'. Make sure you properly encoded match[] arg with percent encoding. See this demo
  • /api/v1/query_range - this endpoint returns calculated datapoints for matching time series on the selected time range [start ... end] with the given step interval between samples. See more details about calculated datapoints in these docs.

If you want returning just unique metric names without labels, then you can query /api/v1/label/__name__/values endpoint: curl http://prometheus:9090/api/v1/label/__name__/values . The __name__ is a special label name used in Prometheus for referring to metric names. Note that multiple time series may share the same metric name. For example, http_requests_total{path="/foo"} and http_requests_total{path="/bar"} time series share http_requests_total metric name, while they differ by path label values.

Defilade answered 10/4, 2022 at 14:3 Comment(2)
``` curl -s 'localhost:9090/api/v1/query?query={instance="IP:9100"}' |jq . { "status": "error", "errorType": "bad_data", "error": "invalid parameter \"query\": 1:9: parse error: unexpected \"=\"" }```Fiore
@Otheus, try using percent encoding for query argument. It is likely some chars in the query clash with special chars at your shell. See real-life exampleDefilade
V
1

You can request all current node_exporter metrics from a specific machine with the following command:

curl --request GET "http://NODE-EXPORTER-IP:9100/metrics"
Vocational answered 20/8, 2020 at 14:13 Comment(4)
Hi Marcelo, running your command doesn't seem to work as the api returns a "Method not Allowed" messageTobit
It works like a charm to me (I just tested again).Hally
@GeorgeFandango, did you used the IP from the machine which is running the Node Exporter?Hally
Hi Marcelo, I managed to test your command once again and it worked. Error was I was pointing to the wrong prometheus server. thanks a mill!Tobit
M
0

USE API

and watch out! if you config nginx for proxy to prometheus.

then

u have to do like this: curl http://localhost:9090/prometheus/api/v1/query?query=up -------------------------------------extra nginx-config in the url.

Monohydric answered 23/9, 2022 at 2:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.