php-syslog-ng (syslog管理用Webインターフェース)

更新履歴

  • 2006/06/06 初版

概要

  • syslogを管理するためのWebインターフェース
    • syslog-ng を利用して、syslogをデータベースに保存する
    • syslogデータベースにPHPで接続して管理する
    • データベースのテーブルローテション機能がある

バージョン

  • php-syslog-ng 2.8

インストール環境

  • CentOS 4.3
    • Apache 2.2.2
    • MySQL 5.0.21
    • PHP 5.1.4
    • syslog-ng 1.6.11

前提条件

  • Apache,MySQL,PHP,syslog-ng がインストールされ、正常に動作していること
    • 各サーバのインストール手順は こちら

参考URL

インストール手順

syslog保管用データベースの作成

  • php-syslog-ngに付属しているデータベース作成スクリプト(dbsetup.sql)を使用して、syslog保存用データベースを作成する。
# cd /usr/local/src/
# wget http://www.phpwizardry.com/php-syslog-ng/phpsyslogng-2.8.tar.gz
# tar xvzf phpsyslogng-2.8.tar.gz
# cd phpsyslogng-2.8/scripts/
# vi dbsetup.sql

dbsetup.sql

71,74,77行目のパスワードを変更


70 # create users
71 INSERT INTO user (Host, User, Password) VALUES ('localhost','sysloguser', password('sysloguser'));
72 INSERT INTO db (Host, Db, User) VALUES ('localhost','syslog','sysloguser');
73 
74 INSERT INTO user (Host, User, Password) VALUES ('localhost','syslogfeeder', password('syslogfeeder'));
75 INSERT INTO db (Host, Db, User) VALUES ('localhost','syslog','syslogfeeder');
76 
77 INSERT INTO user (Host, User, Password) VALUES ('localhost','syslogadmin',password('syslogadmin'));
78 INSERT INTO db (Host, Db, User) VALUES ('localhost','syslog','syslogadmin');
79 COMMIT;
80 FLUSH PRIVILEGES;
# mysql -u root -p < dbsetup.sql
Enter password: 管理者パスワードを入力
# 
# mysql -u root -p syslog
Enter password: 管理者パスワードを入力
Welcome to the MySQL monitor.  Commands END with ; OR \g.
Your MySQL connection id IS 2239 to server version: 5.0.21-LOG
 
Type 'help;' OR '\h' for HELP. Type '\c' to clear the buffer.
 
mysql> SHOW tables;
+------------------+
| Tables_in_syslog |
+------------------+
| actions          | 
| logs             | 
| search_cache     | 
| user_access      | 
| users            | 
+------------------+
5 rows IN SET (0.01 sec)
 
mysql> exit
Bye

syslog-ng の設定

  • syslog-ngの設定を変更して、syslog保存用データベースにログを出力するようにする。
  • 本手順の syslog-ng.conf は以下の特殊な設定を行っているため、環境に応じて適宜変更すること。
    • 自分自身のsyslogもネットワーク経由で送信(ローカルホストのログのIPを127.0.0.1にしないため)
    • すべてのログを一つのファイルに集約
# mkfifo /tmp/mysql.pipe
# 
# vi /etc/syslog-ng/syslog-ng.conf

/etc/syslog-ng/syslog-ng.conf

####################
# options
####################

options {
        sync (0);
        time_reopen (10);
        stats(86400);
        log_fifo_size (2048);
        log_msg_size(8192);
        chain_hostnames(no);
        long_hostnames (off);
        keep_hostname (no);
        use_dns (no);
        use_fqdn (no);
        create_dirs(yes);
        dir_perm(0700);
        perm(0600);
        group(root);
        owner(root);
        };

####################
# sources
####################

source local { file ("/proc/kmsg" log_prefix("kernel: ")); unix-stream ("/dev/log"); internal(); };
source net { udp( ip(0.0.0.0) port(514) ); internal(); };

####################
# destination
####################

destination netudp      { udp("<自分自身のIPアドレス>" port(514) ); };
destination d_summary   { file("/var/log/summary.log"); };
destination d_mysql_p    {
            pipe("/tmp/mysql.pipe"
                template("INSERT INTO logs
                (host, facility, priority, level, tag, datetime, program, msg)
                VALUES ( '$HOST', '$FACILITY', '$PRIORITY', '$LEVEL', '$TAG', '$YEAR-$MONTH-$DAY $HOUR:$MIN:$SEC',
                '$PROGRAM', '$MSG' );\n") template-escape(yes));
};

####################
# filter
####################

filter f_summary        { level(debug..emerg); };

####################
# log
####################

log { source(local); filter(f_summary); destination(netudp); };
log { source(net); filter(f_summary); destination(d_summary); };
log { source(net); filter(f_summary); destination(d_mysql_p); };
  • syslog-ng 起動時に MySQLにログを出力するように起動スクリプトを修正する。

/etc/init.d/syslog-ng

################################################################################
#
# Program: syslog-ng init script for Red Hat
#
################################################################################
# the following information is for use by chkconfig
# if you are want to manage this through chkconfig (as you should), you must
# first must add syslog-ng to chkconfig's list of startup scripts it
# manages by typing:  
#
#               chkconfig --add syslog-ng
#
# DO NOT CHANGE THESE LINES (unless you know what you are doing) 
# chkconfig: 2345 12 88
# description: syslog-ng is the next generation of the syslog daemon. \
# syslog-ng gives you the flexibility of logging not only by facility and \
# severity, but also by host, message content, date, etc. it can also replace \
# klogd's function of logging kernel messages
#
# This following block of lines is correct, do not change! (for more info, see 
# http://www.linuxbase.org/spec/refspecs/LSB_1.1.0/gLSB/facilname.html)
### BEGIN INIT INFO 
# Provides: $syslog
### END INIT INFO
################################################################################
#
#  This is an init script for syslog-ng on the Linux platform.
#
#  It totally relies on the Redhat function library and works the same
#  way as other typical Redhat init scripts.
#
#
# Platforms (tested): Linux (Redhat 7.3)
#
#
# Author: Gregor Binder <gbinder@sysfive.com>
# Changed: October 10, 2000
#
# Last Changed: September 27, 2002
# Updated by: Diane Davidowicz
#       changes: Brought the start script up to snuff as far as compliance
#                with managing the startup script through chkconfig; 
#                added PATH variable ability to hook in path to syslog-ng (if 
#                its necessary); converted init script format to the 
#                standard init script format in Red Hat (7.3 to be exact)
#                including using the /etc/sysconfig/syslog-ng file to
#                managed the arguments to syslog-ng without changing this
#                script, and disabled klogd but noted where and under what
#                conditions it should be enabled. HAPPY LOGGING.
#
#     Copyright (c) 2000 by sysfive.com GmbH, All rights reserved.
#
#
################################################################################
#
# configuration
#

INIT_PROG=syslog-ng

#
# Source Redhat function library.
#
. /etc/rc.d/init.d/functions

# Tack on path to syslog-ng if not already in PATH
SYSLOGNG_PATH=":/usr/local/sbin"
MYSQL="/usr/local/mysql/bin/mysql"

PATH=$PATH$SYSLOGNG_PATH
export PATH

# for MySQL
test -e /tmp/mysql.pipe || /usr/bin/mkfifo /tmp/mysql.pipe

# /etc/sysconfig/ is the standard way to pull in options for a daemon to use.
# Source config
if [ -f /etc/sysconfig/syslog-ng ] ; then
        . /etc/sysconfig/syslog-ng
else
        SYSLOGNG_OPTIONS=
fi

RETVAL=0

umask 077
ulimit -c 0

# See how we were called.
start() {
        echo -n "Starting $INIT_PROG: "
        daemon $INIT_PROG $SYSLOGNG_OPTIONS
        RETVAL=$?
        echo
        $MYSQL -u syslogfeeder --password=syslogfeeder syslog < /tmp/mysql.pipe &
        # syslog-ng can handle kernel messages. If you do this, don't
        # run klogd. Consult the following FAQ question to find out why.
        #
        # http://www.campin.net/syslog-ng/faq.html#klogd
        #
        # If you still prefer to run klogd without syslog-ng handling
        # kernel messages, uncomment the following block of lines

        #echo -n $"Starting kernel logger: "
        #daemon klogd $KLOGD_OPTIONS
        #echo

        [ $RETVAL -eq 0 ] && touch "/var/lock/subsys/${INIT_PROG}"
        return $RETVAL
}

stop() {
        # Same here concerning klogd. Uncomment the following block of 
        # code if you are needing to run it

        #echo -n $"Shutting down kernel logger: "
        #killproc klogd
        #echo

        echo -n "Stopping $INIT_PROG: "
        killproc $INIT_PROG
        RETVAL=$?
        echo

        [ $RETVAL -eq 0 ] && rm -f "/var/lock/subsys/${INIT_PROG}"
        return $RETVAL

}

rhstatus() {
        status $INIT_PROG
}

restart() {
        stop
        start
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        rhstatus
        ;;
  restart|reload)
        restart
        ;;
  condrestart)
        [ -f /var/lock/subsys/syslog-ng ] && restart || :
        ;;
  *)
        echo $"Usage: $0 {start|stop|status|restart|reload}"
        exit 1
esac

exit $?
  • syslog-ng を再起動する。
# /etc/init.d/syslog-ng stop
Stopping syslog-ng:                                        [  OK  ]
# /etc/init.d/syslog-ng start
Starting syslog-ng:                                        [  OK  ]

php-syslog-ng インストール

  • php-syslog-ngをインストールする。
    • 本手順では、Apacheの公開ディレクトリを「/var/www/html」としている。
# cd /usr/local/src/
# cp -r phpsyslogng-2.8 /var/www/html/phpsyslogng
# chown -R apache:apache /var/www/html/phpsyslogng/
# 
# cd /var/www/html/phpsyslogng/
# vi config/config.php

config/config.php

45,51行目のパスワードを変更

38 //========================================================================
39 // BEGIN: DATABASE CONNECTION INFO
40 //========================================================================
41 // DBUSER is the name of the basic user.
42 define('DBUSER', 'sysloguser');
43 
44 // DBUSERPW is DBUSER's database password.
45 define('DBUSERPW', 'sysloguser');
46 
47 // DBADMIN is the name of the admin user.
48 define('DBADMIN', 'syslogadmin');
49 
50 // DBADMINPW is DBADMIN's database password.
51 define('DBADMINPW', 'syslogadmin');
52 
53 // DBNAME is the name of the database you are using.
54 define('DBNAME', 'syslog');
55 
56 // DBHOST is the host where the MySQL server is running.
57 define('DBHOST', 'localhost');
58 
59 // DBPORT is the port where the MySQL server is listening.
60 // The default port is 3306.
61 define('DBPORT', '3306');
62 //========================================================================
63 // END: DATABASE CONNECTION INFO

syslogデータベースのローテション設定

  • syslogをデータベースに大量に保存していると、検索に非常に時間が掛かる。
  • よって、毎日23:55 にログを保存しているテーブルをローテションする。
# cd /var/www/html/phpsyslogng/scripts/
# vi logrotate.php

logrotate.php

1行目 php のパスを変更
6行目 php-syslog-ng のパスを変更

1 #!/usr/local/bin/php
2 <?php
3 // Copyright (C) 2005 Claus Lund, clauslund@gmail.com
4 echo "\nStarting logrotate\n";
5 echo date("Y-m-d H:i:s");
6 $APP_ROOT = '/var/www/html/phpsyslogng';
# crontab -e

以下を追加

55 23 * * * /usr/local/bin/php /var/www/html/phpsyslogng/scripts/logrotate.php > /dev/null 2>&1

php-syslog-ng 動作確認

  • 以下URLにアクセスする
    • http://サーバIP/phpsyslogng/
      • デフォルトのユーザ・パスワードは admin/admin
      • ログイン後、デフォルトパスワードを必ず変更すること!!

  • 以下サンプル画面
    • 検索画面

検索画面

  • 検索結果画面

検索結果画面

 
manage/phpsyslogng.txt · 最終更新: 2007/10/16 12:02
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki NINJA TOOLS