LoginSignup
2
1

More than 3 years have passed since last update.

log4phpを使ってZabbix Webインタフェースのログを出力する - systemd & Zabbix4.0

Last updated at Posted at 2019-12-17

以前書いた以下の記事ですが、pearでlog4phpがインストールできなくなったり、systemdのプラベートディレクトリでログの出力先が変わったりと色々あるので、再度投稿しておきます。

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

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

1.composer-setup.php の取得

]$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
]# ls -l
合計 17064
-rw-r--r--  1 root root   273773 12月 15 10:49 composer-setup.php

2.composer-setup.phpの実行

]# php composer-setup.php
All settings correct for using Composer
Downloading...

Composer (version 1.9.1) successfully installed to: /usr/local/src/composer.phar
Use it: php composer.phar
]# ls -l
合計 18688
-rw-r--r--  1 root root   273773 12月 15 10:49 composer-setup.php
-rwxr-xr-x  1 root root  1933813 12月 15 10:49 composer.phar

3.composer-setup.phpの削除

]$ php -r "unlink('composer-setup.php');"
  1. composer.pharの移動
]# mv composer.phar /usr/local/bin/composer

5.ルートでcomposerを動かす為の環境変数変更

export COMPOSER_ALLOW_SUPERUSER=1
export COMPOSER_NO_INTERACTION=1

6.実行

]# composer require apache/log4php
Using version ^2.3 for apache/log4php
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing apache/log4php (2.3.0): Downloading (100%)         
Writing lock file
Generating autoload files

7.確認

]# find / -name 'log4php'
/root/.cache/composer/files/apache/log4php
/usr/local/src/vendor/apache/log4php

8.PHPのPATHの通ったディレクトリに配置

cd /usr/local/src/vendor/apache/log4php
cp -R src/main/php/* /usr/share/php/log4php/

ここからlog4phpの設定…

まずは、設定ファイルの作成。
以前と同じ様にzlog4と言うロガーを作ります。
ログの出力先はsystemdのプライベートディレクトリ配下の/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>

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

/usr/share/zabbix/include/classes/core/Z.php
<?php
/*
** Zabbix
** Copyright (C) 2001-2019 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 {

}
global $zbxlog4php;

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

/usr/share/zabbix/api_jsonrpc.php


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

これで準備OK

テストはWebインタフェースでホストの一覧を実行させる際に呼ばれるhost.getでログを取ってみます。
開始直後とSQL実行と終了直線でログを出力します。

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

        public function get($options = []) {
global $zbxlog4php;
$zbxlog4php->info("host.get start");
                $result = [];

                $sqlParts = $this->applyQueryOutputOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts);
                $sqlParts = $this->applyQuerySortOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts);
$zbxlog4php->info($this->createSelectQueryFromParts($sqlParts));
                $res = DBselect($this->createSelectQueryFromParts($sqlParts), $sqlParts['limit']);
                while ($host = DBfetch($res)) {
                        if ($options['countOutput']) {
                                if ($options['groupCount']) {
                                        $result[] = $host;
                                }
                                else {
                                        $result = $host['rowscount'];
                                }
                        }
                        else {
                                $result[$host['hostid']] = $host;
                        }
                }
$zbxlog4php->info("host.get end");

※:2019/12/17 SQL文で取得できるように修正

Webインタフェースでメニュー「設定」「ホスト」を選択すると以下の様にログが出力されます。

]# ls /tmp/systemd-private-29c0de66840e4d7c9fa623923b9ab4c0-httpd.service-AtrK1l/tmp/
ZabbixAPI.log

下記はログの内容

/tmp/systemd-private-29c0de66840e4d7c9fa623923b9ab4c0-httpd.service-AtrK1l/tmp/ZabbixAPI.log
2019-12-17 18:46:58,867 [zlog4] host.get start
2019-12-17 18:46:58,868 [zlog4] SELECT h.hostid,h.name FROM hosts h WHERE h.flags IN (0,4) AND h.status IN (0,1) ORDER BY h.name
2019-12-17 18:46:58,870 [zlog4] host.get end
2019-12-17 18:46:58,873 [zlog4] host.get start
2019-12-17 18:46:58,873 [zlog4] SELECT h.* FROM hosts h WHERE h.flags IN (0,4) AND h.hostid=10084 AND h.status IN (0,1)
2019-12-17 18:46:58,875 [zlog4] host.get end
2
1
0

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
2
1