當主機數量多起來,登入每台機器翻 log 很快就不可行。把日誌集中到一台伺服器,再用網頁介面查詢,是相當實際的做法。標準組合是:rsyslog 負責收與存(寫入 MySQL),LogAnalyzer 負責用瀏覽器呈現。
rsyslog-5.8.1.tar.gz loganalyzer-3.2.1.tar.gz
以下腳本把編譯、初始化資料庫、建立啟動腳本、佈署網頁介面等步驟串了起來:
vi rsyslog.sh
#!/bin/sh
#mysql conf
mysql_user='root'
mysql_pwd='123456'
run_user='root'
run_pwd='123456'
# Install env.
yum -y install mysql mysql-devel mysql-server php php-mysql php-pdo php-common php-gd httpd zlib-devel gcc gcc-c++
# Install rsyslog..
tar rsyslog-5.8.1.tar.gz
cd rsyslog-5.8.1
./configure --enable-mysql --prefix=/usr/local/rsyslog
make && make install
cd ..
# creat rsyslog config..
sed -e "s/ommysql:localhost,Syslog,root,/ommysql:localhost,Syslog,${run_user},${run_pwd};dbFormat/g" rsyslog.conf > /etc/rsyslog.conf
# stop system syslog..
service syslog stop
chkconfig syslog off
# create rsyslog startup script
cp /etc/init.d/{syslog,rsyslog}
# edit rsyslog startup script
sed -i 's/syslog/rsyslog/g' /etc/init.d/rsyslog
chmod 700 /etc/init.d/rsyslog
chkconfig --add rsyslog
chkconfig rsyslog on
# create rsyslog bin ln
ln -sv /usr/local/rsyslog/sbin/rsyslogd /sbin/rsyslogd
# conf mysql
service mysqld start
chkconfig mysqld on
mysqladmin -u root password "${mysql_pwd}"
# create sql
cat > createDB.sql << EOF
CREATE DATABASE Syslog default character set 'utf8';
USE Syslog;
CREATE TABLE SystemEvents
(
ID int unsigned not null auto_increment primary key,
CustomerID bigint,
ReceivedAt datetime NULL,
DeviceReportedTime datetime NULL,
Facility smallint NULL,
Priority smallint NULL,
FromHost varchar(60) NULL,
Message text,
NTSeverity int NULL,
Importance int NULL,
EventSource varchar(60),
EventUser varchar(60) NULL,
EventCategory int NULL,
EventID int NULL,
EventBinaryData text NULL,
MaxAvailable int NULL,
CurrUsage int NULL,
MinUsage int NULL,
MaxUsage int NULL,
InfoUnitID int NULL ,
SysLogTag varchar(60),
EventLogType varchar(60),
GenericFileName VarChar(60),
SystemID int NULL
) DEFAULT CHARSET='utf8';
CREATE TABLE SystemEventsProperties
(
ID int unsigned not null auto_increment primary key,
SystemEventID int NULL ,
ParamName varchar(255) NULL ,
ParamValue text NULL
) DEFAULT CHARSET='utf8';
grant all privileges on Syslog.* to '${run_user}'@localhost identified by '${run_pwd}';
EOF
# import sql
mysql -u${mysql_user} -p${mysql_pwd} < createDB.sql
# start rsyslog
service rsyslog restart
# install loganalyzer
tar xvf loganalyzer-3.2.1.tar.gz
cd loganalyzer-3.2.1
cp -r src/ /var/www/html/loganalyzer
cp -r contrib/* /var/www/html/loganalyzer
cd ..
chown -R apache.apache /var/www/html/loganalyzer
# use web install loganalyzer.
/bin/sh /var/www/html/loganalyzer/configure.sh
/bin/sh /var/www/html/loganalyzer/secure.sh
# start httpd
service httpd start
chkconfig httpd on
# 修正中文顯示:強制 htmlentities 使用 UTF-8
sed -i 's/htmlentities.*/htmlentities($myStr,ENT_QUOTES,"UTF-8");/g' /var/www/html/loganalyzer/include/functions_common.php
# reduce the privilege
read -n 1 -p 'Please go to install web, when finished, press Enter...'
cat > priv.sql << EOF
revoke all privileges on Syslog.* from '${run_user}'@localhost;
grant insert,delete,update,select,alter on Syslog.* to '${run_user}'@localhost identified by '${run_pwd}';
EOF
mysql -u${mysql_user} -p${mysql_pwd} < priv.sql
# clean
rm -rf priv.sql createDB.sql loganalyzer-3.2.1 rsyslog-5.8.1
chmod +x rsyslog.sh
./rsyslog.sh
sed 修改 functions_common.php,是為了讓 LogAnalyzer 以 UTF-8 輸出,否則中文日誌會變亂碼。/etc/rsyslog.conf,把 *.emerg * 那一行註解掉,再 service rsyslog restart 即可。收到的訊息形如:Message from syslogd@2011-06-13T19:43:04.846104+08:00 10.35.2.2 PNB0>www 1307994563。