Ad Widget

Collapse

Zabbix 2.0: Getting graph image with cURL, graph ID changes?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • arimbun
    Junior Member
    • Jul 2013
    • 3

    #1

    Zabbix 2.0: Getting graph image with cURL, graph ID changes?

    Hi guys,

    There was a script written in PHP that I got somewhere off the net. Basically, given a graph ID, what it does is make a curl request to grab a graph image from the Zabbix server. You can then use the image for displaying on your website.

    The code looks like this:

    PHP Code:
    /**
         *
         * GraphImgByID v1.1
         *
         * It's free use it however you want.
         * ChangeLog:
         * 1/23/12 - Added width and height to GetGraph Function
         *
         * $author (c) Travis Mathis - [email protected]
         *
         * @param $graphid
         * @param $width
         * @param $height
         * @param $period
         * @return string
         */
        
    public function get_graph_image_by_id($graphid$width$height$period) {
            
    //CONFIGURATION
            
    $z_server 'http://zabbix.url/zabbix/';
            
    $z_user   'user';
            
    $z_pass   'pass'

            
    //NON CONFIGUREABLE
            
    $z_tmp_cookies "";
            
    $z_url_index   $z_server "index.php";
            
    $z_url_graph   $z_server "chart2.php";
            
    $z_login_data "name=" $z_user "&password=" $z_pass "&enter=Sign in&autologin=1&request=";

            
    // file names
            
    $filename_cookie $z_tmp_cookies "zabbix_cookie_" $graphid ".txt";

            
    //setup curl
            
    $ch curl_init();
            
    curl_setopt($chCURLOPT_URL$z_url_index);
            
    curl_setopt($chCURLOPT_HEADERfalse);
            
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
            
    curl_setopt($chCURLOPT_BINARYTRANSFERtrue);
            
    curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);
            
    curl_setopt($chCURLOPT_POSTtrue);
            
    curl_setopt($chCURLOPT_POSTFIELDS$z_login_data);
            
    curl_setopt($chCURLOPT_COOKIEJAR$filename_cookie);
            
    curl_setopt($chCURLOPT_COOKIEFILE$filename_cookie);

            
    // login
            
    curl_exec($ch);
            
    // get graph
            
    curl_setopt($chCURLOPT_URL$z_url_graph "?graphid=" $graphid "&width=" $width "&height=" $height "&period=" $period);
            
    $output curl_exec($ch);
            
    curl_close($ch);
            
    // delete cookie
            
    unlink($filename_cookie);
            return 
    base64_encode($output);
        } 
    The problem I am having is that the ID of a graph that I had setup changed recently for unknown reasons. I was able to find out when I inspected the graph image with Firebug - the <img> tag's "src" attribute contains a different graph ID to the one I had hardcoded before.

    Is there a way to fix a graph ID to the custom graph that I had setup?

    Thank you so much!
Working...