經過 loganalyzer 展現數據庫中的日誌
環境準備
三臺主機
一臺日誌服務器,利用上一個案例實現,IP:192.168.39.7,
一臺數據庫服務器,利用上一個案例實現,IP:192.168.39.27,
一臺當httpd+php 服務器,並安裝loganalyzer展現web圖形,IP:192.168.39.77
日誌服務器工具 loganalyzer-4.1.8.tar.gz
準備服務器:
# 日誌服務器
[root@centos7 ~]$hostname rsyslog
[root@centos7 ~]$exit
[root@rsyslog ~]$
# 數據庫服務器
[root@centos7 ~]$hostname mysql
[root@centos7 ~]$exit
[root@mysql ~]$
# websrv服務器
[root@centos ~]# hostname websrv
[root@centos ~]# exit
[root@websrv ~]$
日誌服務器:
- 在rsyslog服務器上安裝鏈接mysql模塊相關的程序包
[root@rsyslog ~]$yum install rsyslog-mysql -y
- 找到sql腳本發送到數據庫
# 下載輔助軟件查找
[root@rsyslog ~]$yum install mlocate
[root@rsyslog ~]$updatedb # 更新數據庫信息
[root@rsyslog ~]$locate mysql-createDB.sql # 使用locatedb查找腳本文件存放路徑
/usr/share/doc/rsyslog-8.24.0/mysql-createDB.sql
[root@rsyslog ~]$cat /usr/share/doc/rsyslog-8.24.0/mysql-createDB.sql # 腳本文件內容
CREATE DATABASE Syslog;
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
);
CREATE TABLE SystemEventsProperties
(
ID int unsigned not null auto_increment primary key,
SystemEventID int NULL ,
ParamName varchar(255) NULL ,
ParamValue text NULL
);
[root@rsyslog ~]$scp /usr/share/doc/rsyslog-8.24.0/mysql-createDB.sql 192.168.39.27:/root # 發送到數據庫服務器
The authenticity of host '192.168.39.27 (192.168.39.27)' can't be established.
ECDSA key fingerprint is SHA256:XVNFzEbN3eaCzTwYrlQg2SzHZXHbd0dS0YKLuIOXVr0.
ECDSA key fingerprint is MD5:df:4d:86:ba:0c:e6:c1:a2:6c:45:71:e9:ac:ea:1d:a5.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.39.27' (ECDSA) to the list of known hosts.
root@192.168.39.27's password:
mysql-createDB.sql 100% 1046 588.1KB/s 00:00
- 修改配置文件啓動服務模塊而且寫如數據庫信息(用於給數據庫服務器發送日誌信息)
# The imjournal module bellow is now used as a message source instead of imuxsock.
$ModLoad ommysql # 添加這一行
# Don't log private authentication messages!
*.info;mail.none;authpriv.none;cron.none /var/log/messages
*.info;mail.none;authpriv.none;cron.none :ommysql:192.168.39.27,Syslog,syslog,taotaobao
#配置rsyslog將日誌保存到mysql中
[root@centos8 ~]#vim /etc/rsyslog.conf
####MODULES####
#在 MODULES 語言下面,若是是 CentOS 8 加下面行
module(load="ommysql")
#在 MODULES 語言下面,若是是 CentOS 7,6 加下面行
$ModLoad ommysql
數據庫服務器:
- 安裝數據庫
[root@mysql ~]$yum install mariadb-server -y
[root@mysql ~]$systemctl start mariadb.service
- 執行考過來的腳本
[root@mysql ~]$mysql <
anaconda-ks.cfg .cshrc .tcshrc
.bash_history ifcfg-eth0 .viminfo
.bash_logout init_env_191113.sh .vimrc
.bash_profile mysql-createDB.sql
.bashrc .pki/
[root@mysql ~]$mysql < mysql-createDB.sql # 直接導入數據庫
- 建立Syslog庫使用的帳號
[root@mysql ~]$mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> grant all on Syslog.* to syslog@'192.168.39.%' identified by 'taotaobao'; # 注意這裏的數據庫名要對應執行的sql腳本里的數據庫名。
Query OK, 0 rows affected (0.00 sec)
測試日誌服務器和數據庫是否鏈接:
MariaDB [Syslog]> show tables;
+------------------------+
| Tables_in_Syslog |
+------------------------+
| SystemEvents |
| SystemEventsProperties |
+------------------------+
2 rows in set (0.00 sec)
MariaDB [Syslog]>
MariaDB [Syslog]> SELECT COUNT(*) FROM SystemEvents;
+----------+
| COUNT(*) |
+----------+
| 0 |
+----------+
1 row in set (0.00 sec)
[root@rsyslog ~]$systemctl restart rsyslog.service
[root@rsyslog ~]$logger "this is a test log" # 日誌服務器觸發日誌(logger觸發日誌命令)
MariaDB [Syslog]> SELECT COUNT(*) FROM SystemEvents;
+----------+
| COUNT(*) |
+----------+
| 8 |
+----------+
1 row in set (0.00 sec)
websrv服務器端:
- 解壓縮工具
[root@websrv ~]# ll
-rw-r--r-- 1 root root 2943754 Oct 10 13:04 loganalyzer-4.1.8.tar.gz # web界面日誌服務工具包
[root@websrv ~]# tar xvf loganalyzer-4.1.8.tar.gz
- 安裝須要的服務實現LAMP架構
# 注意這裏的php版本必定要是在5.6版本相同或以上要不這個軟件不支持
[root@websrv ~]# yum install httpd php56-php-fpm.x86_64 php56-php-mysqlnd.x86_64 -y
- 把loganalyzer須要的目錄切到網站目錄下
[root@websrv loganalyzer-4.1.8]# ll
total 100
drwxrwxr-x 13 root root 4096 Sep 26 23:41 src
-rw-rw-r-- 1 root root 48571 Sep 26 23:41 ChangeLog
drwxrwxr-x 2 root root 43 Sep 26 23:41 contrib
-rw-rw-r-- 1 root root 35497 Sep 26 23:41 COPYING
drwxrwxr-x 3 root root 258 Sep 26 23:41 doc
-rw-rw-r-- 1 root root 8449 Sep 26 23:41 INSTALL
[root@websrv loganalyzer-4.1.8]# mv src/ /var/www/html/log # 切到網站目錄下
[root@websrv loganalyzer-4.1.8]# ll /var/www/html/
total 4
drwxrwxr-x 13 root root 4096 Sep 26 23:41 log
# 修改全部者爲apache
[root@websrv loganalyzer-4.1.8]# cd /var/www/html/
[root@websrv html]# chown -R apache.apache log/
- 修改httpd配置文件支持php-fpm
# 添加index.php
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
# 找到這個模塊位置,添加中間三行內容。
<IfModule mime_module>
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
ProxyRequests Off
</IfModule>
- 建立調用模塊文件(或者理解爲反向代理)
[root@websrv html]# vim /etc/httpd/conf.d/fcgi.conf
Directoryindex index.php
Proxyrequests off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/var/www/html/$1
第四、5這兩項的配置在centos7上要加。centos8是默認配置好的不用修改。
- web界面測試
- 進行檢測
- 這裏若是錯了可能會顯示沒有用於寫的文件
- 解決方法(是缺乏這個文件致使的)
[root@websrv html]# cd log/
[root@websrv log]# pwd
/var/www/html/log
[root@websrv log]# touch config.php # 建立這個文件
[root@websrv log]# chmod 666 config.php # 給這個文件全部人可讀和可寫權限(根據工做需求更改)
- 這些信息是最後顯示的日誌格式是什麼樣的
- 填寫數據庫的信息
- 查看添加的表名
[root@centos7 ~]$mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> use Syslog;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [Syslog]> show tables;
+------------------------+
| Tables_in_Syslog |
+------------------------+
| SystemEvents | # 這個表是做爲存儲日誌信息使用的
| SystemEventsProperties |
+------------------------+
2 rows in set (0.00 sec)
[root@websrv html]# yum install php56-php-gd.x86_64 -y # 支持畫餅狀圖的包
[root@websrv html]# systemctl restart httpd.service php56-php-fpm.service # 重啓服務
- 刷新web界面(這樣就會產生比較清晰的餅狀圖)