0

I have a login script as follows:

curl --location --request POST 'https://zabbixUrl.com/api_jsonrpc.php' \
--header 'Authorization: Basic bWlndWVsLmh1cnRhZG86MTczM295ZmQxVg==' \
--header 'Content-Type: application/json' \
--data-raw '{
    "jsonrpc": "2.0",
    "method": "user.login",
    "params": {
        "user": "user",
        "password": "pass"
    },
    "id": 1
}'

Which returns:

{"jsonrpc":"2.0","result":"1g1hd43j4d3jd4jsl4n35b4211n1d2e2","id":1}

With this Api token (result), I can execute all the Zabbix's methods, for instance:

curl --location --request POST 'https://zabbixUrl.com/api_jsonrpc.php' \
--header 'Authorization: Basic bWlndWVsLmh1cnRhZG86MTczM295ZmQxVg==' \
--header 'Content-Type: application/json' \
--data-raw '
{
    "jsonrpc": "2.0",
    "method": "screenitem.get",
    "params": {
        "output": "extend",
        "screenids": "258"
    },
    "auth": "1g1hd43j4d3jd4jsl4n35b4211n1d2e2",
    "id": 1
}'

Now I am trying to obtain a graph in PNG format. I can view this graph in PNG format after inserting the user and pass in the intermediate screen:

Access via web

Now what I am trying to do is to get this PNG graph via API. My approach is the following (ps: the url included is the same one as the one used on the browser, where it does work):

curl --header 'Authorization: Basic bWlndWVsLmh1cnRhZG86MTczM295ZmQxVg==' --data-raw 'auth:1g1hd43j4d3jd4jsl4n35b4211n1d2e2' http://zabbixUrl.com/chart2.php?graphid=123456&period=604800&stime=1614012539

Using this form, I get a 401 error. I guess that it is not correctly detecting the token.

Therefore, my question is, how can I obtain the PNG of this Zabbix's graph via API? How can I do it so it correctly detects the token?

qwerty
  • 486
  • 1
  • 11
  • 37

2 Answers2

1

When you authenticate to the API you use a token, when you authenticate to the GUI you need a cookie. Using curl, you need the --cookie option, see Save cookies between two curl requests.

In python requests, you would use a Session: see zabbix-gnomes's zgetgraph.py for a python solution.

A working example in Zabbix 5.0:

auth=$(curl -s --data '{ "jsonrpc": "2.0", "method": "user.login", "params": { "user": "...", "password": "..." }, "id": 1 }' --header 'Content-Type: application/json' https://.../zabbix/api_jsonrpc.php | jq -r .result)

curl --cookie "zbx_sessionid=$auth" "https://.../zabbix/chart2.php?graphid=517&from=now-2d&to=now&height=201&width=1717&profileIdx=web.charts.filter&_=up0bkgs0" -o file.png
Iron Bishop
  • 1,749
  • 1
  • 7
  • 16
0

I found the solution:

import requests
import json
import shutil

​

##### GET CREDENTIALS #####
​
USER = "user"
PASSWORD = "pass"
URL_AUTH = "https://url.com/api_jsonrpc.php"

HEADER_AUTH = {
    'Authorization': 'Basic bWlndWVsLmh1cnRhZG86MTczM295ZmQxVg==',
    'Content-Type': 'application/json',
}

DATA_AUTH = """
{
    "jsonrpc": "2.0",
    "method": "user.login",
    "params": {
        "user": "%s",
        "password": "%s",
        "userData": true
    },
    "id": 1
}
""" % (USER, PASSWORD)

respAuth = requests.post(URL_AUTH, headers=HEADER_AUTH, data=DATA_AUTH)

zabbixSessionId = json.loads(respAuth.content.decode('utf8').replace("'", '"'))["result"]["sessionid"]

##### GET PNG #####

URL_CHART = "https://url.com/chart2.php?graphid=12345&period=7200"

HEADER_CHART = {
  'Authorization': 'Basic bWlndWVsLmh1cnRhZG86MTczM295ZmQxVg==',
  'Content-Type': 'image/png',
  'Cookie': 'zbx_sessionid=%s' % (zabbixSessionId)
}

PNG_PATH = "./image.png"

respChart = requests.request("POST", URL_CHART, headers=HEADER_CHART, stream=True)

with open(PNG_PATH, 'wb') as out_file:
    shutil.copyfileobj(respChart.raw, out_file)
qwerty
  • 486
  • 1
  • 11
  • 37