Zabbix to export graph (.PNG) files
Asked Answered
B

4

12

My Goal:

I would like to extract graphs associated with hosts in .png format. My GOOGLE research say's we don't have Zabbix API designed to do this task. So few blogs advised to user Chart2.php & CURL. Can someone explain me how to go about it ( detailed steps )?

Note: Sorry never worked on php nor on curl

When i tried

curl https://example.com/zabbix/chart2.php?graphid=1552&width=300&height=300

Got this, but link doesn't work

<!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>

Also how can i incorporate this with my zabbix api (JAVA ) call ?

Beatabeaten answered 30/11, 2012 at 22:3 Comment(1)
as the location sent to you by zabbix frontend suggests, you should either authenticate first or open that graph to guests. then it will work.Scrubland
S
11

This works with the normal password authentication, you need to adapt it to openid which I don't use and most certainly you will have to change options for this to work with 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'

the first one posts authentication and saves the cookie. the second loads the same cookie file and retrieves the png.

You must certainly want to implement it without using shell but in a language of your preference and zabbix's JSON-RPC API of which there are plenty of client libraries already.

Though AFAIK you still will have to login like this to get the graph's image. At least for the time being.

EDIT: https://support.zabbix.com/browse/ZBXNEXT-562 is the one to vote (or start working on it)

Scrubland answered 1/12, 2012 at 14:13 Comment(0)
C
6

Adding to the above, if you're using Zabbix 2.0, the cURL POST data has changed slightly.

Replace the following:

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'

With the following:

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'

Cass answered 16/7, 2013 at 5:15 Comment(1)
I'm using zabbix 2.2, and this was the necessary format for post-dataUnnumbered
A
0

Zabbix allows to retrieve the required data with a single wget command which causes HTTP redirection after login.

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'

Don't forget to url-encode the request parameter value.

If you want to have the latest period, set stime to some far future. "20200101000000" works well for me.

Tested with Zabbix 1.8.11.

Assignor answered 2/12, 2015 at 16:13 Comment(0)
H
0

In some cases you cannot or don't want to use wget. It turns out that you do not need the login hack to read cookies and set them when downloading the image. You only need the Zabbix session ID (also called auth string) you get when you login using the API. Now you can simply use it to set one cookie called zbx_sessionid and call the URL providing you with the image.

In 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 is a bit of model I've created to store the login result you get when you login with "userData": true. The resulting object will contain a sessionid.

Haemocyte answered 6/2, 2017 at 9:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.