loganalyzer+rsyslog搭建Log Service器(解決中文亂碼、記錄ip等問題)

來源:互聯網
上載者:User

 
 
安裝指令碼如下,文章最後有打包下載,遇到的問題在指令碼中有注釋

 #!/bin/sh

 
# mysql conf
mysql_user=root #mysql root 使用者名稱
mysql_pwd=fuckmysqlintruder #mysql root 密碼

run_user=user #syslog庫使用者
run_pwd=user #syslog庫使用者密碼
 
# 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 xvf rsyslog-5.6.2.tar.gz
cd rsyslog-5.6.2
./configure --enable-mysql --prefix=/usr/local/rsyslog #支援mysql,指定安裝路徑
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
#這裡設定rsyslog.conf中的mysql串連賬戶和密碼
#rsyslog支援資料範本,我定義了一個插入db的模板,在rsyslog.conf中
#$template dbFormat,"insert into SystemEvents (Message, Facility,FromHost, Priority, DeviceReportedTime, ReceivedAt, InfoUnitID, SysLogTag) values (%msg%, %syslogfacility%, %fromhost-ip%,%syslogpriority%, %timereported:::date-mysql%, %timegenerated:::date-mysql%, %iut%, %syslogtag%)",sql
#其中紅色字型部分如果是%HOSTNAME%,那麼庫中記錄的就是hostname,不是ip;所以如果要改成顯示ip,那麼使用%fromhost-ip%

 
# stop system syslog..
service syslog stop #因為有rsyslog了,系統內建的syslog停掉
chkconfig syslog off
 
# create rsyslog startup script
cp /etc/init.d/{syslog,rsyslog} #rsyslog沒有啟動指令碼,複製syslog指令碼
 
# edit rsyslog startup script
sed -i s/syslog/rsyslog/g /etc/init.d/rsyslog #修改syslog字元為rsyslog
 
chmod 700 /etc/init.d/rsyslog
chkconfig --add rsyslog #添加rsyslog服務
chkconfig rsyslog on
 
# create rsyslog bin ln
ln -sv /usr/local/rsyslog/sbin/rsyslogd /sbin/rsyslogd #建立軟連結,rsyslog指令碼啟動的是/sbin下的rsyslogd
 
# conf mysql
service mysqld start
chkconfig mysqld on
mysqladmin -u root -p password ${mysql_pwd} #設定mysql的root密碼,指令碼執行到這裡要點下斷行符號,預設root密碼為空白
 
# create sql
cat > createDB.sql << EOF #建立Syslog資料庫和表,並授權,為什麼給all privileges,因為安裝時會有create 操作,裝完以後降權
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 #匯入sql
 
# start rsyslog
service rsyslog restart #啟動rsyslog
 
# install loganalyzer
tar xvf loganalyzer-3.0.4.tar.gz
cd loganalyzer-3.0.4
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
 
# edit src/include/functions_common.php  function GetStringWithHTMLCodes
sed -i s/htmlentities.*/htmlentities($myStr,ENT_QUOTES,"UTF-8");/g /var/www/html/loganalyzer/include/functions_common.php
#loganalyzer亂碼的原因是使用了,htmlentities對輸出進行編碼,如果有中文會出現問題,應該指定字元集,修改這個函數為
#return htmlentities($myStr,ENT_QUOTES,"UTF-8");
 
# reduce the privilege
read -n 1 -p Please go to install web, when finished, press Enter...  #到這裡先進web安裝loganalyzer,完畢以後再來降權
cat > priv.sql << EOF #發現瀏覽日誌詳細日誌的時候,會用到alter,加grant進去了.
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.0.4 rsyslog-5.6.2
 
完

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.