我的目标:
我想以.png格式提取与主机关联的图表。我的GOOGLE研究表明,我们没有Zabbix API来完成这项任务。很少有博客建议用户使用Chart2.php&卷曲。有人可以解释一下如何去做(详细步骤)?
注意:抱歉,从未使用过php或curl
当我尝试
时curl https://example.com/zabbix/chart2.php?graphid=1552&width=300&height=300
得到了这个,但链接不起作用
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="/zabbix/openid?graphid=1552&modauthopenid.referrer=https%3A%2F%2Fexample.com%2Fzabbix%2Fchart2.php%3Fgraphid%3D1552">here</a>.</p>
<hr>
<address>Apache/2.2.3 (Red Hat) Server at example.com Port 80</address>
</body></html>
另外,我如何将其与我的zabbix api(JAVA)电话合并?
答案 0 :(得分:11)
这适用于正常的密码身份验证,您需要将其调整为openid,我不使用它,而且大多数情况下您必须更改此选项才能使用curl。
1. wget --save-cookies=z.coo -4 --keep-session-cookies -O - -S --post-data='name=(a zabbix username)&password=(password)&enter=Enter' 'http://example.com/zabbix/index.php?login=1'
2. wget -4 --load-cookies=z.coo -O result.png 'http://example.com/zabbix/chart2.php?graphid=410&width=1778&period=102105&stime=20121129005934'
第一个发布身份验证并保存cookie。第二个加载相同的cookie文件并检索png。
您当然希望在不使用shell的情况下实现它,但是使用您喜欢的语言和zabbix的JSON-RPC API,其中已经有很多客户端库。
虽然AFAIK仍然需要像这样登录才能获得图表的图像。至少目前是这样。
编辑:https://support.zabbix.com/browse/ZBXNEXT-562是投票的人(或开始研究)
答案 1 :(得分:6)
除此之外,如果您使用的是Zabbix 2.0,则cURL POST数据会略有变化。
替换以下内容:
1. wget --save-cookies=z.coo -4 --keep-session-cookies -O - -S --post-data='name=(a zabbix username)&password=(password)&enter=Enter' 'http://example.com/zabbix/index.php?login=1'
以下内容:
1. wget --save-cookies=z.coo -4 --keep-session-cookies -O - -S --post-data='name=(a zabbix username)&password=(password)&enter=Sign in&autologin=1&request=' 'http://example.com/zabbix/index.php?login=1'
答案 2 :(得分:0)
Zabbix允许使用单个wget命令检索所需数据,该命令会在登录后导致HTTP重定向。
wget --post-data='name=(username)&password=(password)&enter=Enter&request=http%3A%2F%2Fexample.com%2Fzabbix%2Fchart2.php%3Fgraphid%3D410%26width%3D1778%26period%3D102105%26stime%3D20121129005934' -O (image file) 'http://example.com/index.php?login=1'
不要忘记对request
参数值进行网址编码。
如果您想拥有最新的时段,请将stime
设置为遥远的未来。 &#34; 202001.01亿&#34;适合我。
使用 Zabbix 1.8.11进行测试。
答案 3 :(得分:0)
在某些情况下,您不能或不想使用wget
。事实证明,您不需要登录hack来读取cookie并在下载图像时设置它们。您只需要在使用API登录时获得的Zabbix会话ID(也称为auth字符串)。现在您只需使用它来设置一个名为zbx_sessionid
的cookie,并调用URL为您提供图像。
在Java中:
private byte[] getPng(ZabbixUser user, URL pngUrl) throws IOException
{
HttpURLConnection conn = (HttpURLConnection) pngUrl.openConnection();
conn.setRequestProperty("Cookie", "zbx_sessionid=" + user.getSessionid());
try (InputStream is = conn.getInputStream()) {
return toByteArray(is);
}
}
private static byte[] toByteArray(InputStream is) throws IOException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] byteChunk = new byte[4096];
int n;
while ((n = is.read(byteChunk)) > 0) {
baos.write(byteChunk, 0, n);
}
return baos.toByteArray();
}
ZabbixUser
是我创建的一个模型,用于存储您使用"userData": true
登录时获得的登录结果。生成的对象将包含sessionid
。