通过 API 将 zabbix 图转为 png
Get zabbix graph to png via API
我有一个登录脚本如下:
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
}'
哪个returns:
{"jsonrpc":"2.0","result":"1g1hd43j4d3jd4jsl4n35b4211n1d2e2","id":1}
有了这个Api令牌(result
),我可以执行所有Zabbix的方法,例如:
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
}'
现在我正在尝试获取 PNG 格式的图表。在中间屏幕中插入 user
和 pass
后,我可以以 PNG 格式查看此图:
现在我要做的是通过 API 获取此 PNG 图。我的方法如下(ps:包含的 url 与浏览器上使用的相同,它确实有效):
curl --header 'Authorization: Basic bWlndWVsLmh1cnRhZG86MTczM295ZmQxVg==' --data-raw 'auth:1g1hd43j4d3jd4jsl4n35b4211n1d2e2' http://zabbixUrl.com/chart2.php?graphid=123456&period=604800&stime=1614012539
使用此表格,我得到 401 error
。我猜它没有正确检测到令牌。
因此,我的问题是,如何通过API获取此Zabbix图表的PNG?我该怎么做才能正确检测到令牌?
当您向 API 进行身份验证时,您使用令牌,而当您向 GUI 进行身份验证时,您需要一个 cookie。使用 curl,您需要 --cookie
选项,请参阅 。
在 python requests
中,您将使用 Session
:请参阅 zabbix-gnomes 的 zgetgraph.py
以获得 python 解决方案。
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
我找到了解决方案:
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)
我有一个登录脚本如下:
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
}'
哪个returns:
{"jsonrpc":"2.0","result":"1g1hd43j4d3jd4jsl4n35b4211n1d2e2","id":1}
有了这个Api令牌(result
),我可以执行所有Zabbix的方法,例如:
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
}'
现在我正在尝试获取 PNG 格式的图表。在中间屏幕中插入 user
和 pass
后,我可以以 PNG 格式查看此图:
现在我要做的是通过 API 获取此 PNG 图。我的方法如下(ps:包含的 url 与浏览器上使用的相同,它确实有效):
curl --header 'Authorization: Basic bWlndWVsLmh1cnRhZG86MTczM295ZmQxVg==' --data-raw 'auth:1g1hd43j4d3jd4jsl4n35b4211n1d2e2' http://zabbixUrl.com/chart2.php?graphid=123456&period=604800&stime=1614012539
使用此表格,我得到 401 error
。我猜它没有正确检测到令牌。
因此,我的问题是,如何通过API获取此Zabbix图表的PNG?我该怎么做才能正确检测到令牌?
当您向 API 进行身份验证时,您使用令牌,而当您向 GUI 进行身份验证时,您需要一个 cookie。使用 curl,您需要 --cookie
选项,请参阅
在 python requests
中,您将使用 Session
:请参阅 zabbix-gnomes 的 zgetgraph.py
以获得 python 解决方案。
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
我找到了解决方案:
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)