【问题标题】:Zabbix to export graph (.PNG) filesZabbix 导出图形 (.PNG) 文件
【发布时间】:2012-11-19 04:24:18
【问题描述】:

我的目标:

我想以 .png 格式提取与主机关联的图表。我的 GOOGLE 研究表明我们没有设计用于执行此任务的 Zabbix API。很少有博客建议用户使用 Chart2.php 和 CURL。有人可以解释一下如何去做(详细步骤)吗?

注意:对不起,从来没有在 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&amp;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) 调用结合起来?

【问题讨论】:

  • 正如 zabbix 前端发送给您的位置所建议的那样,您应该首先进行身份验证或将该图表打开给客人。然后它会工作。

标签: php linux curl zabbix


【解决方案1】:

这适用于普通密码身份验证,您需要将其调整为我不使用的 openid,而且您肯定必须更改选项以使其与 curl 一起使用。

1. wget --save-cookies=z.coo -4 --keep-session-cookies -O - -S --post-data='name=(a zabbix username)&amp;password=(password)&amp;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&amp;width=1778&amp;period=102105&amp;stime=20121129005934'

第一个发布身份验证并保存 cookie。第二个加载相同的 cookie 文件并检索 png。

你肯定想在不使用 shell 的情况下使用你喜欢的语言和 zabbix 的 JSON-RPC API 来实现它,其中已经有很多客户端库。

尽管 AFAIK,您仍然必须像这样登录才能获取图表的图像。至少目前是这样。

编辑:https://support.zabbix.com/browse/ZBXNEXT-562 是投票(或开始工作)的人

【讨论】:

    【解决方案2】:

    除此之外,如果您使用的是 Zabbix 2.0,cURL POST 数据略有变化。

    替换以下内容:

    1. wget --save-cookies=z.coo -4 --keep-session-cookies -O - -S --post-data='name=(a zabbix username)&amp;password=(password)&amp;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)&amp;password=(password)&amp;enter=Sign in&amp;autologin=1&amp;request=' 'http://example.com/zabbix/index.php?login=1'

    【讨论】:

    • 我用的是zabbix 2.2,这是post-data的必要格式
    【解决方案3】:

    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 参数值进行 url 编码。

    如果您想获得最新的周期,请将stime 设置为某个遥远的未来。 “20200101000000”对我来说效果很好。

    使用 Zabbix 1.8.11 测试。

    【讨论】:

      【解决方案4】:

      在某些情况下,您不能或不想使用wget。事实证明,您不需要登录 hack 来读取 cookie 并在下载图像时设置它们。您只需要使用 API 登录时获得的 Zabbix 会话 ID(也称为身份验证字符串)。现在您可以简单地使用它来设置一个名为 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

      【讨论】:

        猜你喜欢
        • 2021-11-10
        • 1970-01-01
        • 2019-03-25
        • 2011-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-19
        • 2012-09-27
        相关资源
        最近更新 更多