livecm 发表于 2010-08-26 22:30

日志服务器的搭建(logzilla+syslog-ng+lamp)-【原创】

本帖最后由 livecm 于 2010-08-26 22:35 编辑

最近计划着中央日志服务器的搭建,日志是反映一个server运行状况的重要渠道,公司在黑龙江的几十台服务器的日志的查看,一台台机器的登陆查看不是个办法,头疼了一段时间。想着用一台机器来做日志的服务器端来接收管理其他所有的服务器的日志信息,包括windows的和linux的,如果能这样那不是极大的简化了我们系统工程师的工作量了!网上看了很多资料,最终测试成功。底下是我测试实施的详细过程,拿出来和大家分享。

1.lamp的安装 (见最下面,但我用的cactiEZ省事,因为它的环境要求和cacti差不多)
##############################

2.平台初始化yum -y install libnet
cpan Text::LevenshteinXS
cpan -i Digest::SHA1
cpan -i Net::MySQL        (必须要做不然,底下mysql导入会有问题)eventlog将会生成/usr/local/lib/pkgconfig这个目录,这个目录会被syslog-ng的configure脚本使用,所以你应该添加一个环境变量PKG_CONFIG_PATH,使用如下命令添加# export PKG_CONFIG_PATH=/usr/lib/pkgconfig:$PKG_CONFIG_PATH#####################################3.安装syslog-ng组件
作者在Logzilla3.0以后做了licensed限制,所以现在使用2.99版本cd /usr/local/src
http://www.balabit.com/downloads/files/syslog-ng/open-source-edition/3.0.3/setups/rhel-5-i386/syslog-ng-3.0.3-1.rhel5.i386.rpm(32位系统)
http://www.balabit.com/downloads/files/syslog-ng/open-source-edition/3.0.5/setups/rhel-5-amd64/syslog-ng-3.0.5-1.rhel5.x86_64.rpm(64位系统)
http://www.balabit.com/downloads/files/eventlog/0.2/eventlog_0.2.9.tar.gz
http://php-syslog-ng.googlecode.com/files/logzilla_v2.9.9o.tgz保存为down.txt# wget -i down.txt
# cd /usr/local/src
tar zxvfeventlog_0.2.9.tar.gz
cd eventlog-0.2.9/
./configure && make && make install
cd ..
rpm -Uvh syslog-ng-3.0.3-1.rhel5.i386.rpmvi /opt/syslog-ng/etc/syslog-ng.conf@version: 3.0
#Default configuration file for syslog-ng.
#
# For a description of syslog-ng configuration file directives, please read
# the syslog-ng Administrator’s guide at:
#
# http://www.balabit.com/dl/html/syslog-ng-admin-guide_en.html/bk01-toc.html
#
options {
# Number of syslog lines stored in memory before being written to files
flush_lines (0);
log_fifo_size (2048);
create_dirs (yes);
perm (0640);
dir_perm (0750);
};
source s_network_1 {
udp(ip(0.0.0.0) port(514));
};
destination d_network_1 {
file("/var/log/syslog-ng/network/$YEAR.$MONTH.$DAY/$HOST/$FACILITY.log");
};
# Define the destination "d_network_1B" log directory
destination d_network_1B {
file ("/var/log/syslog-ng/network/all/network.log");
};
log {
source(s_network_1);
destination(d_network_1);
};
log {
source(s_network_1);
destination(d_network_1B);
};
destination d_logzilla {
program("/var/www/logzilla/scripts/db_insert.pl"
template("$HOST\t$FACILITY\t$PRIORITY\t$LEVEL\t$TAG\t$YEAR-$MONTH-$DAY\t$HOUR:$MIN:$SEC\t$PROGRAM\t$MSG\n"));
};
log {
source(s_network_1);
destination(d_logzilla);
};
source localhost_all {
internal();
unix-stream("/dev/log");
file("/proc/kmsg" program_override("kernel"));
};
destination localhostlog {
file ("/var/log/syslog-ng/$HOST/$YEAR-$MONTH/$DAY" create_dirs(yes));
};
log {
source(localhost_all);
destination(localhostlog);
};
destination local_logzilla {
program("/var/www/logzilla/scripts/db_insert.pl"
template("$HOST\t$FACILITY\t$PRIORITY\t$LEVEL\t$TAG\t$YEAR-$MONTH-$DAY\t$HOUR:$MIN:$SEC\t$PROGRAM\t$MSG\n"));
};
log {
source(localhost_all);
destination(local_logzilla);
};注意修改路径/var/www/logzilla/# /etc/init.d/syslog-ng restart
####################################################

4.安装logzillatar -zxvf logzilla_v2.9.9o.tgz
mv php-syslog-ng logzilla
mv logzilla /var/www/
chown -R apache:apache /var/www/logzilla/html    //确定httpd.conf中User 为apache,Group为apache
mkdir -p /var/log/logzillavi /etc/httpd/conf/httpd.conf#在最后加上Alias /logs "/var/www/logzilla/html"
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>vi /etc/php.inidisplay_errors = On         //Off改成On
magic_quotes_gpc = On   //Off改成On
memory_limit = 128M   
max_execution_time = 300   //30改成300重启apache服务 service httpd start
启动mysql service mysqld start
重启syslog /etc/init.d/syslog-ng restart
cp /var/www/logzilla/scripts/contrib/system_configs/logrotate.d /etc/logrotate.d/logzilla
/usr/bin/mysqladmin -u root password ‘zjhcsoft’
修改root的密码为zjhcsoft
#################################################

5.web安装php-syslog-ng
打开Firefox,最好不要用IE,不然在最后一步会卡死,事先不需要建立用户和表,只需要有建表和用户权限的
用户,例如root用户

http://ip/logs

安装过程注意几点
1.事先修改root密码,填写的时候只需要填写root密码,会自动创建库和用户
2. Site URL : /logs/

如果是用IE浏览器,最后一个Install CEMDB点了没反映,这个时候手工导入表结构
mysql -usysloguser -psysloguser syslog < /var/www/logzilla/html/install/sql/cemdb.sql
如果使用firefox不会出现这个问题
###################################################

6.事后处理
替换脚本路径
cd /var/www/logzilla/scripts
./fixpaths.sh
/etc/init.d/syslog-ng restart
刚开始出现这个错误:

USING TABLE: logs
There appear to be no hosts in the Database yet
You can generate fake ones using scripts/dbgen.pl修改config.php
# vim /var/www/logzilla/html/config/config.php
查找
define(‘DBHOST’, ’127.0.0.1′); #把localhost改成IP要不然不会自动插入数据
# vim /var/www/logzilla/scripts/db_insert.pl
查看85行和90行,
确保位置为/var/www/logzilla/html/config/config.php

导入测试数据 ( 出问题后看最下面的常见问题)# perl /var/www/logzilla/scripts/contrib/dbgen/dbgen.pl
Debug off, showing only inserted data...
Couldn't connect to 3306: connect: No such file or directory at /var/www/logzilla/scripts/contrib/dbgen/dbgen.pl line 205这个问题暂时没解决,但是不影响客户端写入LOG######################################################

7.客户端配置
Linux客户端配置
vim /etc/syslog.conf 添加最后一行,其中192.168.64.129是服务器端的IP

local7.*                                                /var/log/boot.log
*.emerg;*.err;*.warning (或*.*)                              @192.168.64.129
# service syslog restart
# logger -p local7.err "This is a local.err test message."在服务器上查看日志
cd /var/log/syslog-ng/network/all
tail network.log
Jul 28 09:30:14 192.168.64.128 root: This is a local.err test message.

说明可以收到信息,这个时候打开查看mysql数据库的logs表有记录,打开页面就正常了。
如果syslog数据库的logs没有记录
# cpan Text::LevenshteinXS !!!!!在这我执行了这个命令才出效果
重启syslog服务器的所有服务
# service mysqld restart
# service httpd restart
# service syslog-ng restart

点击下方的Graph后出现如下错误:
JpGraph Error Font file “/usr/share/fonts/truetype/msttcorefonts/verdana.ttf” is not readable or does not exist.
原因是,新版的php-syslog-ng考虑到旧版字库版权的问题,使用了verdana.ttf字库。而当前系统目录没有该字库
解决办法:
1)从Windows\Fonts目录中拷贝到上述的路径即可;
2)从网上下载,并放到上述目录下:执行:

# mkdir -p /usr/share/fonts/truetype/msttcorefonts/
# cd /usr/share/fonts/truetype/msttcorefonts/
# wget http://www.linuxfly.org/attachment/verdana.ttf.zip
# unzip verdana.ttf.zip
# rm -f verdana.ttf.zip建立计划任务,每天收集客户端信息
# crontab -e# LogZilla
@daily php /var/www/logzilla/scripts/logrotate.php >> /var/log/logzilla/logrotate.log
@daily find /var/www/logzilla/html/jpcache/ -atime 1 -exec rm -f '{}' ';'
0,5,10,15,20,25,30,35,40,45,50,55 * * * * php /var/www/logzilla/scripts/reloadcache.php >> /var/log/logzilla/reloadcache.log
# Demo LogZilla
# CHANGE TO MATCH YOUR DIRECTORY PATHS# service crond restartWindows下客户端的安装
因为windows没有syslog,所以需要安装一个插件

http://code.google.com/p/eventlog-to-syslog/

下载Evtsys_4.3.0_32-Bit.zip
下载后解压到c:\windows\system32\
然后安装
c:\windows\system32\evtsys.exe -i -h 192.168.0.121
然后启动服务
net start evtsys

########################################
常见问题
问题二、logs表无数据,运行/www/webroot/php-syslog-ng/scripts/contrib/dbgen/dbgen.pl
提示:Cannot determine peer address at /usr/lib/perl5/site_perl/5.8.5/Net/MySQL.pm line 277

解决方法:
1、是logzilla2.9.9安装时一个bug,新建的mysql的syslogadmin、sysloguser用户权限没有成功赋予,手工添加上就OK了,感谢【杭州】FIGO提供的故障案例。
2、如第1步不成功,尝试修改/www/webroot/php-syslog-ng/html/config/config.php文件mysql主机地址localhost为127.0.0.1。

问题三、搜索缓存图表只显示两天的数据,最近三天没有数据?
解决方法:
由于search_cache表采用的是MEMORY存储引擎,有大小的限制,修改一下/etc/my.cnf,在添加:
tmp_table_size=1G
max_heap_table_size = 1G
再重启mysql就可以了。

##########################################
LAMP的安装:
一. yum -y update
建议所有新装的机器都做此步骤,国内现在最快的源应该是mirrors.163.com
二.

yum install httpd httpd-devel httpd-manual php-pear php php-cli php-common php-devel php-gd php-imap php-mbstring php-mcrypt php-mysql php-snmp php-xml php-xmlrpc php-pear-Auth-SASL php-pear-HTTP-Request php-pear-Mail php-pear-Date php-pear-Net-SMTP php-pear-Net-Socket php-pecl-memcache net-snmp net-snmp-devel net-snmp-libs net-snmp-perl net-snmp-utils mysql mysql-devel mysql-server安装httpd(Apache),PHP,MySQL,还有SNMP相关的信息,如果有软件你是已经装过的,这个命令也不会出错而带来异样.

wget http://sourceforge.net/settings/mirror_choices?projectname=libnet&filename=libnet/0.10.11/libnet-0.10.11.tar.gz


本文来自: 漂博(http://llog.cn) 详细出处参考:http://llog.cn/?p=287

gamester88 发表于 2010-08-27 08:19

支持原创:m01:

blueswxs 发表于 2010-08-27 13:58

支持 呵呵

livecm 发表于 2010-08-31 16:38

这个版区人气可真低迷的要死:sleepy:

7717060 发表于 2010-09-02 10:18

不错学习一下,呵呵。

wdlinuxcn 发表于 2010-09-03 13:30

支持原创

sz1029 发表于 2012-04-24 16:05

支持原创。

whw5452 发表于 2014-10-27 10:22

学习一下。。不错的。东西
页: [1]
查看完整版本: 日志服务器的搭建(logzilla+syslog-ng+lamp)-【原创】