5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

log4phpを使ってZabbixAPIのログを出力する

Last updated at Posted at 2016-07-03

ZabbixAPIですが通常ではログが出せなくデバックが辛いのでlog4phpを使ってなんとかならないかと試してみました。

取り敢えず、インストール

yum -y install php-pear
pear channel-discover pear.apache.org/log4php
pear install log4php/Apache_log4php

で、設定ファイルの作成。
zlog4と言うロガーを作りました。
ログは/tmp/ZabbixAPI.logに出力します。

/usr/share/zabbix/log4php.xml
<configuration xmlns="http://logging.apache.org/log4php/">

    <appender name="myConsoleAppender" class="LoggerAppenderConsole" />

    <appender name="myFileAppender" class="LoggerAppenderFile">
        <layout class="LoggerLayoutPattern">
            <param name="conversionPattern" value="%date{Y-m-d H:i:s,u} [%logger] %message%newline" />
        </layout>
        <param name="file" value="/tmp/ZabbixAPI.log" />
    </appender>

    <logger name="zlog4">
        <appender_ref ref="myFileAppender" />
    </logger>

    <root>
        <level value="DEBUG" />
        <appender_ref ref="myConsoleAppender" />
    </root>
</configuration>

2016年7月4日api_jsonrpc.phpのみにlog4phpの設定を入れるとWebインタフェースの画面が表示されなくなる事象が発生。
内部でapi_jsonrpc.php通らずにhost.getが呼ばれているようです。
と言う訳で、下記の様に変更しました。

Z.phpでロガーを設定し…

/usr/share/zabbix/include/classes/core/Z.php
<?php
/*
** Zabbix
** Copyright (C) 2001-2015 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**/


require_once dirname(__FILE__).'/ZBase.php';

require_once('log4php/Logger.php');                   <-- log4phpの設定
Logger::configure('/usr/share/zabbix/log4php.xml');   <-- log4phpの設定
$zbxlog4php = Logger::getLogger('zlog4');             <-- log4phpの設定
global $zbxlog4php;                                   <-- log4phpの設定


/**
 * A wrapper for the ZBase class.
 *
 * Feel free to modify and extend it to change the functionality of ZBase.
 */
class Z extends ZBase {

}

api_jsonrpc.phpの最後の一行にshutdownを設定

/usr/share/zabbix/api_jsonrpc.php

        echo CJs::encodeJson($response);
}
$zbxlog4php->shutdown();                              <-- log4phpの設定

これで準備OK

テストはhost.getで行いました。
開始直後とSQL実行と終了直線でログを出力します。

/usr/share/zabbix/api/classes/CHost.php

        public function get($options = array()) {
                $result = array();
                $userType = self::$userData['type'];
                $userid = self::$userData['userid'];

global $zbxlog4php;
$zbxlog4php->info("host.get start");

                $sqlParts = $this->applyQueryOutputOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts);
                $sqlParts = $this->applyQuerySortOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts);
                $sqlParts = $this->applyQueryNodeOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts);
$zbxlog4php->info($sqlParts);
                $res = DBselect($this->createSelectQueryFromParts($sqlParts), $sqlParts['limit']);
                while ($host = DBfetch($res)) {

                // removing keys (hash -> array)
                if (is_null($options['preservekeys'])) {
                        $result = zbx_cleanHashes($result);
                }

$zbxlog4php->info("host.get end");
                return $result;
        }

        /**
         * Get Host ID by Host name
         *
         * @param array $host_data
         * @param string $host_data['host']
         *
         * @return int|boolean
         */
        public function getObjects($hostData) {


下記が実行結果

]# cat /tmp/ZabbixAPI.log
2016-07-03 09:34:37,850 [zlog4] host.get start
2016-07-03 09:34:37,853 [zlog4] Array
(
    [select] => Array
        (
            [0] => h.*
        )

    [from] => Array
        (
            [hosts] => hosts h
        )

    [where] => Array
        (
            [flags] => h.flags IN (0,4)
            [status] => h.status IN (0,1)
            [filter] => h.host='Zabbix server'
        )

    [group] => Array
        (
        )

    [order] => Array
        (
        )

    [limit] =>
)

2016-07-03 09:34:37,853 [zlog4] host.get end

これで画面も表示できてデバック情報出すことで着ました。
因みに画面からhost.get呼ばれる場合もこれでログが出ます。

5
4
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?