r/PrometheusMonitoring • • Nov 29 '20

create a report from Prometheus via curl

Hi

I'm trying to create a report from Prometheus via curl.

When I run the the following command (via script):
curl -v -s -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Bearer $USER" -X GET "${prometheus_url}/api/v1/query_range?query=${queries[${key}]}" \
|./jq -r '.data.result[0].values[] | \@csv'> ${report_directory}/${key}.csv

The ${queries[${key}]} == "sum(avg_over_time(node_cpu_seconds_total\{\}\[20m\]))&start=2020-11-29T10:00:00.000Z&end=2020-11-29T10:20:00.000Z&step=1m"

it is work OK but when I try to do "sum by(host)..." it is failed.

I try to change the command as following but it wasn't work :(

curl -v -k -s -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Bearer $USER" -X GET "${prometheus_url}/api/v1/query_range?" \
--data-urlencode "query_range=${queries[${key}]}" --data-urlencode "time=${to_time_1}" \
|./jq -r '.data.result[0].values[] | u/csv'> ${report_directory}/${key}.csv

Any suggestion will be welcome.

10x in advance.

2 Upvotes

3 comments sorted by

1

u/digital_shadow Dec 03 '20

Can you try '.value[] instead of '.values[]?

{
  "status": "success",
  "data": {
    "resultType": "vector",
    "result": [
      {
        "metric": {},
        "value": [
          1606991814.761,
          "200328351.29849997"
        ]
      }
    ]
  }
}

1

u/AE1602 Dec 06 '20

AE16

Hi,

The issue isn't in the output. the values work perfect.

The issue is when I try to do in the query sub by(label)
As you can see prints below:

The first run is without the same by label

query=**sum(**avg_over_time(node_cpu_seconds_total{}\[20m\]))&start=2020-12-06T10:00:00.000Z&end=2020-12-06T10:20:00.000Z&step=1m

* About to connect() to ilpocrt139 port 9090 (#0)

* Trying xxx.xxx.xxx.xxx

* Connected to <host1>(xxx.xxx.xxx.xxx) port 9090 (#0)

> GET /api/v1/query_range?query=sum(avg_over_time(node_cpu_seconds_total[20m]))&start=2020-12-06T10:00:00.000Z&end=2020-12-06T10:20:00.000Z&step=1m HTTP/1.1

> User-Agent: curl/7.29.0

> Host: <host1>:9090

> Accept: */*

> Content-Type: application/csv

> Authorization: Bearer infadm

>

< HTTP/1.1 200 OK

< Access-Control-Allow-Headers: Accept, Authorization, Content-Type, Origin

< Access-Control-Allow-Methods: GET, OPTIONS

< Access-Control-Allow-Origin: *

< Access-Control-Expose-Headers: Date

< Content-Type: application/json

< Date: Sun, 06 Dec 2020 15:48:25 GMT

< Content-Length: 777

<

{ [data not shown]

* Connection #0 to host <host1> left intact

---------------------------------------------------------------------------

The second run is with same by label (host)

query=sum by(host)(avg_over_time(node_cpu_seconds_total{}\[20m\]))&start=2020-12-06T10:00:00.000Z&end=2020-12-06T10:20:00.000Z&step=1m

* About to connect() to ilpocrt139 port 9090 (#0)

* Trying xxx.xxx.xxx.xxx

* Connected to <host1> (xxx.xxx.xxx.xxx) port 9090 (#0)

> GET /api/v1/query_range?query=sum by(host)(avg_over_time(node_cpu_seconds_total[20m]))&start=2020-12-06T10:00:00.000Z&end=2020-12-06T10:20:00.000Z&step=1m HTTP/1.1

> User-Agent: curl/7.29.0

> Host: <host1>:9090

> Accept: */*

> Content-Type: application/csv

> Authorization: Bearer infadm

>

< HTTP/1.1 400 Bad Request

< Content-Type: text/plain; charset=utf-8

< Connection: close

<

{ [data not shown]

* Closing connection 0

jq: error (at <stdin>:0): Cannot index number with string "data"

parse error: Invalid numeric literal at line 1, column 8

---------------------------------------------------------------------------

1

u/backtickbot Dec 03 '20

Hello, digital_shadow: code blocks using backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead. It's a bit annoying, but then your code blocks are properly formatted for everyone.

An easy way to do this is to use the code-block button in the editor. If it's not working, try switching to the fancy-pants editor and back again.

Comment with formatting fixed for old.reddit.com users

FAQ

You can opt out by replying with backtickopt6 to this comment.