ZABBIX的zatree插件安装和优化

一.zabbix的zatree插件安装

zabbix最近出了个比较有用的插件zatree,能够将所有的graph总体显示,而且可以根据关键字和时间进行查询以及汇总. 以下是zabbix-2.2.*版本的zatree安装,其他方式大家也可以去github项目获取安装方式. [github项目链接]: https://github.com/spide4k/zatree/tree/master/zabbix-2.2.x 嘿嘿,下面开始插件安装吧.

1.下载文件

git clone https://github.com/spide4k/zatree.git zatree

2.复制相关文件

假如zabbix web目录位置在/var/www/zabbix,定义zabbix目录

ZABBIX_PATH=/var/www/zabbix

复制相关文件和目录

cp -rf zatree/zabbix-2.2.x $ZABBIX_PATH/zatree

cd $ZABBIX_PATH/zatree/addfile

cp -f CLineGraphDraw_Zabbix.php CGraphDraw_Zabbix.php CImageTextTable_Zabbix.php $ZABBIX_PATH/include/classes/graphdraw/

cp -f zabbix.php zabbix_chart.php $ZABBIX_PATH/

cp -f CItemValue.php $ZABBIX_PATH/api/classes/

cp -f menu.inc.php $ZABBIX_PATH/include/
cp -f main.js $ZABBIX_PATH/js/

cp -f API.php $ZABBIX_PATH/include/classes/api/

3.支持web interface,修改配置文件

vi $ZABBIX_PATH/zatree/zabbix_config.php

'user'=>'xxx', //web登陆的用户名

'passowrd'=>'xxx', //web登陆的密码

二.zatree优化之左侧栏目按照主机组显示

admin用户效果图:

admin用户看到的组效果

普通用户效果图:

普通用户看到的组效果

优化法如下,这里针对zatree的zabbix-2.2.x版本做修改,方法同样适合zabbix-2.0.x版本: 进入zatree目录,修改以下两个文件: 第一步:zabbix_ajax.php 传递$_COOKIE['zbx_sessionid']给hostgroupGet(),获取当前用户具有readable权限的主机组列表。

$groups = $zabbixApi->hostgroupGet(array("output" => "extend", "monitored_hosts" => true)

修改为:

if(isset($_COOKIE['zbx_sessionid'])){
    $groups = $zabbixApi->hostgroupGet(array("output" => "extend", "monitored_hosts" => true), '', $_COOKIE['zbx_sessionid']);
        } else {
        $groups = $zabbixApi->hostgroupGet(array("output" => "extend", "monitored_hosts" => true));
    }

第二步:ZabbixApiAbstract.class.php 修改2594行hostgroupGet方法,添加$sessionid形参。当传入$sessionid参数时,设置$this->auth的值为$sessionid。

public function hostgroupGet($params=array(), $arrayKeyProperty='')
{
    // get params array for request
    $params = $this->getRequestParamsArray($params);

    // request
    return $this->request('hostgroup.get', $params, $arrayKeyProperty);
}

修改为:

public function hostgroupGet($params=array(), $arrayKeyProperty='', $sessionid='')
{
    // get params array for request
    $params = $this->getRequestParamsArray($params);

    // request
    if (isset($sessionid))
        $this->auth = $sessionid;
    return $this->request('hostgroup.get', $params, $arrayKeyProperty);
}

三.优化zatree支持HTTP Basic Authorization认证访问zabbix API

考虑到让zabbix更加安全,所以采用了https的方式进行访问,但是这是zatree标签页打开时,出现了一堆错误.如下图所示:

报错

优化过程如下:

进入zatree目录,修改以下四个文件: 步骤一、修改zabbix_config.php 添加HTTP Basic Authorization认证用户名和密码参数。如果没有设置HTTP Basic Authorization认证,http_user和http_password的值为空就行。

<?php 
global $zabbix_api_config;

$zabbix_api_config=array(
'api_url'=>'api_jsonrpc.php',
'http_user'=>'xxxxx',
'http_password'=>'xxxxx',
'user'=>'Admin',
'passowrd'=>'xxxxx',
'graph_url'=>'zabbix_chart.php',
);

?>

步骤二、修改zabbix_ajax.php第33行,添加当有HTTP认证时的$url_http。 $url_http = dirname(dirname('http://' . $_SERVER['HTTP_HOST'] . $_SERVER["REQUEST_URI"]));

修改为:

if (!empty($zabbix_api_config['http_user']) && !empty($zabbix_api_config['http_password'])) {
            $url_http = dirname(dirname('https://' . trim($zabbix_api_config['http_user']) . ':' . trim($zabbix_api_config['http_password']) . '@' . $_SERVER['HTTP_HOST'] . $_SERVER["REQUEST_URI"]));
    } else {
            $url_http = dirname(dirname('https://' . $_SERVER['HTTP_HOST'] . $_SERVER["REQUEST_URI"]));
    }

步骤三、修改graph.php第141行,添加当有HTTP认证时的$url_http。 $url_http = dirname(dirname('http://' . $_SERVER['HTTP_HOST'] . $_SERVER["REQUEST_URI"]));

修改为:

if (!empty($zabbix_api_config['http_user']) && !empty($zabbix_api_config['http_password'])) {
            $url_http = dirname(dirname('https://' . trim($zabbix_api_config['http_user']) . ':' . trim($zabbix_api_config['http_password']) . '@' . $_SERVER['HTTP_HOST'] . $_SERVER["REQUEST_URI"]));
    } else {
            $url_http = dirname(dirname('https://' . $_SERVER['HTTP_HOST'] . $_SERVER["REQUEST_URI"]));
    }

步骤四、修改big_graph.php第88行,添加当有HTTP认证时的$url_http。 $url_http = dirname(dirname('http://' . $_SERVER['HTTP_HOST'] . $_SERVER["REQUEST_URI"]));

修改为:

if (!empty($zabbix_api_config['http_user']) && !empty($zabbix_api_config['http_password'])) {
            $url_http = dirname(dirname('https://' . trim($zabbix_api_config['http_user']) . ':' . trim($zabbix_api_config['http_password']) . '@' . $_SERVER['HTTP_HOST'] . $_SERVER["REQUEST_URI"]));
    } else {
            $url_http = dirname(dirname('https://' . $_SERVER['HTTP_HOST'] . $_SERVER["REQUEST_URI"]));
    }