配置使用基于mysql存儲rsyslog日志信息

配置使用基于mysql存儲rsyslog日志信息

 

日志對于我們來說,肯定不會陌生。每個系統應用,只要有人訪問,每時每刻都會產生大量的日志,用來記錄服務器應用的運行信息。以便于我們在服務有異常時進行查看,或是從日志記錄中提取出應用系統的運行信息。某些電商Web網站甚至會利用日志記錄去對用戶的購買行為作分析,以便更好的服務于客戶。     

相信大家最經常聽到的日志系統就是ELK(Elasticsearch Logstash Kibana)了,ELK功能強大,但也有點復雜,所以,一般只是中大型的互聯網公司所采用。對于中小型公司的我們,每天服務的訪問量不是很大,產生的日志也不多,使用ELK就有點殺雞用牛刀的意思。那,什么系統適合我們呢?答案就是rsyslog+mysql組合。采用mysql作為日志存儲組件,將前方服務器所采集來的信息集中存放,統一管理,是個不錯的解決方案。

本文將會重點介紹使用基于mysql存儲rsyslog日志信息系統的搭建,內容如有錯漏,請各位斧正。

實驗環境:

前端服務器:

角色 日志收集服務器、Web服務器,且安裝loganalyzer及php, IP地址:  172.16.10.1

后端服務器:

角色 日志存儲服務器、MariaDB服務器,IP地址: 172.16.10.2

兩臺服務器均為CentOS7.2的系統,內核版本 3.10.0-327。出于演示方便,

所有軟件均采用yum的方式進行安裝,且初始安裝假設CentOS已安裝配置完畢。

以下為安裝配置步驟:

 

[root@web ~]# rpm -q rsyslog

rsyslog-7.4.7-12.el7.x86_64

[root@web ~]#

 

系統默認已安裝好rsyslog軟件,我們只需在前端安裝配套的rsyslog-mysql組件,及后端的mariadb即可。

 

先安裝mariadb:

以下在后端服務器mariadb172.16.10.2中操作:

##安裝mariadb

[root@mairadb /]# yum install -y mariadb mairadb-server

Installed:

  mariadb.x86_64 1:5.5.44-2.el7.centos                                                                 

 

Complete!

[root@mairadb /]#

##更改數據庫默認密碼

[root@mairadb /]# systemctl start mariadb

[root@mairadb /]# ss -ntlp | grep 3306

LISTEN     0      50           *:3306                     *:*                   users:(("mysqld",pid=50513,fd=14))

[root@mairadb /]#

[root@mairadb /]# mysql

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 2

Server version: 5.5.44-MariaDB MariaDB Server

 

Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.

 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 

MariaDB [(none)]> use mysql

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 [mysql]> select user,host,password from user;

+——+———–+———-+

| user | host      | password |

+——+———–+———-+

| root | localhost |          |

| root | mairadb   |          |

| root | 127.0.0.1 |          |

| root | ::1       |          |

|      | localhost |          |

|      | mairadb   |          |

+——+———–+———-+

6 rows in set (0.00 sec)

 

MariaDB [mysql]> update user set password = password('redhat');

Query OK, 6 rows affected (0.00 sec)

Rows matched: 6  Changed: 6  Warnings: 0

 

MariaDB [mysql]> select user,host,password from user;

+——+———–+——————————————-+

| user | host      | password                                  |

+——+———–+——————————————-+

| root | localhost | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |

| root | mairadb   | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |

| root | 127.0.0.1 | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |

| root | ::1       | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |

|      | localhost | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |

|      | mairadb   | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |

+——+———–+——————————————-+

6 rows in set (0.00 sec)

 

MariaDB [mysql]> flush privileges;

Query OK, 0 rows affected (0.00 sec)

 

MariaDB [mysql]>

 

##轉到web服務器

##安裝rsyslog-mysql,以便使用ommysql模塊

[root@web yum.repos.d]# yum install -y rsyslog-mysql

 

##創建數據庫連接用戶

[root@localhost /]# mysql -uroot -h172.16.10.2 -p

Enter password:

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 5

Server version: 5.5.44-MariaDB MariaDB Server

 

Copyright (c) 2000, 2015, 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'@'172.16.%.%' identified by 'redhat';

Query OK, 0 rows affected (0.01 sec)

 

MariaDB [(none)]> flush privileges;

Query OK, 0 rows affected (0.00 sec)

 

##創建Syslog數據庫

[root@localhost /]# mysql -usyslog -h172.16.10.2 -predhat < /usr/share/doc/rsyslog-7.4.7/mysql-createDB.sql

 

 

 

##配置rsyslog使用ommysql模塊

##在modules區塊段加入

#### MODULES ####

 $ModLoad ommysql

 

 

##在rules區塊段加入

#### RULES ###

##*.info;mail.none;authpriv.none;cron.none                /var/log/messages

*.info;mail.none;authpriv.none;cron.none                :ommysql:172.16.100.2,Syslog,syslog,redhat

##其中,:ommysql:172.16.10.2,Syslog,syslog,redhat代表的意思是,前面冒號中間代表的是啟用mysql作為syslog的存儲數據庫,172.16.10.2是mysql的IP地址,Syslog是數據庫名稱,syslog是mysql的登錄用戶名,redhat是登錄密碼

 

##重啟rsyslog服務

[root@localhost /]# systemctl restart rsyslog

[root@localhost /]#

 

##連到數據庫上檢查當前沒有有記錄

[root@localhost /]# mysql -uroot -h172.16.10.2 -predhat

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 8

Server version: 5.5.44-MariaDB MariaDB Server

 

Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.

 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 

MariaDB [(none)]> show databases;

+——————–+

| Database           |

+——————–+

| information_schema |

| Syslog             |

| mysql              |

| performance_schema |

| test               |

+——————–+

5 rows in set (0.00 sec)

 

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.01 sec)

 

MariaDB [Syslog]> select * from SystemEvents;

Empty set (0.00 sec)

 

MariaDB [Syslog]>

 

##轉到mariadb的主機,先啟用rsyslog的遠程監聽

# Provides UDP syslog reception

#$ModLoad imudp

#$UDPServerRun 514

$ModLoad imudp

$UDPServerRun 514

 

# Provides TCP syslog reception

#$ModLoad imtcp

#$InputTCPServerRun 514

$ModLoad imtcp

$InputTCPServerRun 514

##重啟服務

[root@localhost ~]# systemctl restart rsyslog

 

##轉回www的主機

##隨便做些操作,“制造”一下日志

 

 

##再查詢數據庫,可看到已有記錄(記錄太多,節選)

*************************** 49. row ***************************

                ID: 49

        CustomerID: NULL

        ReceivedAt: 2016-08-31 19:13:17

DeviceReportedTime: 2016-08-31 19:13:17

          Facility: 3

          Priority: 4

          FromHost: localhost

           Message: Configuration file /usr/lib/systemd/system/wpa_supplicant.service is marked executable. Please remove executable permission bits. Proceeding anyway.

        NTSeverity: NULL

        Importance: NULL

       EventSource: NULL

         EventUser: NULL

     EventCategory: NULL

           EventID: NULL

   EventBinaryData: NULL

      MaxAvailable: NULL

         CurrUsage: NULL

          MinUsage: NULL

          MaxUsage: NULL

        InfoUnitID: 1

         SysLogTag: systemd:

      EventLogType: NULL

   GenericFileName: NULL

          SystemID: NULL

*************************** 50. row ***************************

                ID: 50

        CustomerID: NULL

        ReceivedAt: 2016-08-31 19:13:17

DeviceReportedTime: 2016-08-31 19:13:17

          Facility: 1

          Priority: 6

          FromHost: localhost

           Message: Installed: vsftpd-3.0.2-10.el7.x86_64

        NTSeverity: NULL

        Importance: NULL

       EventSource: NULL

         EventUser: NULL

     EventCategory: NULL

           EventID: NULL

   EventBinaryData: NULL

      MaxAvailable: NULL

         CurrUsage: NULL

          MinUsage: NULL

          MaxUsage: NULL

        InfoUnitID: 1

         SysLogTag: yum[9255]:

      EventLogType: NULL

   GenericFileName: NULL

          SystemID: NULL

50 rows in set (0.01 sec)

 

MariaDB [Syslog]>

 

##數據庫存儲日志成功

##下一步,再安裝基于php的日志展示工具loganalyzer

##先安裝好php環境

[root@www setup]# yum install httpd php php-mysql php-gd

[root@www setup]# systemctl start httpd

 

##以下一系列操作為解壓loganalyzer到/var/www/html目錄,執行configure.sh及secure.sh腳本,創建config.php文件

 

[root@www setup]# tar xzvf loganalyzer-3.6.6.tar.gz -C /var/www/html

[root@www html]# mv loganalyzer-3.6.6/ loganalyzer

[root@www html]# cd loganalyzer/

[root@www src]# mv * ../../

[root@www src]# cd ../contrib/

[root@www contrib]# ls

configure.sh  secure.sh

[root@www contrib]# mv * ../../

[root@www html]# rm loganalyzer/ -rf

[root@www html]# chmod +x *.sh

[root@www html]# chmod +x *.sh

[root@www html]# ./configure.sh

 

[root@www html]# ./secure.sh

[root@www html]# chmod 666 config.php

 

##至此,準備工作都已做好,打開瀏覽器訪問www,進行后續配置

##第一步,點擊Next

step1.png

##第二步,再Next

step2.png

  

##第三步,按需選擇即可

step3.png

##然后,錄入數據庫連接用戶及密碼,表名稱等信息

step4.png

##下一步

step5.png

##最后一步,點Finsh

step6.png

 

OK,大功告成!現在可以在web界面查看各種log信息了。

 

以上是基于mysql存儲rsyslog日志信息的配置!作為初學者,我對linux的認識還是很膚淺,上述可能有不正確的地方,如有錯漏,希望各位能及時指正,共同進步。

 

我的QQ153975050   小斌斌

在此感謝馬哥及馬哥團隊,在linux的道路上引領我一直前進!

                                                                                        2016-09-2

 

原創文章,作者:馬哥Net19_小斌斌,如若轉載,請注明出處:http://www.www58058.com/42866

(0)
馬哥Net19_小斌斌馬哥Net19_小斌斌
上一篇 2016-09-05
下一篇 2016-09-05

相關推薦

  • MySQL 字符集亂碼及解決方案

    1、簡介         字符集是一套符號和編碼的規則,不論是在oracle數據庫還是在mysql數據庫,都存在字符集的選擇問題,而且如果在數據庫創建階段沒有正確選擇字符集,那么可能在后期需要更換字符集,而字符集的更換是代價比較高的操作,也存在一定的風險,所以,我們推薦在應用開始階段,就按…

    Linux干貨 2015-09-27
  • 進程管理的總結

    進程管理的總結 進程相關概念: Process: 運行中的程序的一個副本,是被載入內存的一個指令集合。進程是程序的基本執????????????????????? 行實體;程序是指令、數據及其組織形式的描述,進程是程序的實體 Process ID:進程的標記號碼 task struct:Linux內核存儲進程信息的數據結構格式 task list:多個任務的…

    2017-12-23
  • 磁盤配額、Raid及邏輯卷LVS

    磁盤配額、Raid及邏輯卷LVS 磁盤配額 Linux是多用戶的操作系統,同一時間可能有多個用戶登錄到系統上進行操作,某些用戶可能會寫入很多數據,占用大量磁盤空間,導致其他用戶沒有磁盤空間可用。如何限定某個用戶對磁盤的使用空間大小,以及可使用的inode多少,也就是限定可創建多少個文件,這就是磁盤配額管理的作用。 磁盤配額特點 在內核中執行; 以文件系統為單…

    Linux干貨 2016-09-19
  • LA LN MP

    L A/N MP 安裝 添加epel源 yum install epel* 安裝所需環境 yum install gcc gcc-c++ gcc-g77 flex bison autoconf automake bzip2-devel zl…

    Linux干貨 2016-05-28
  • hello 北京&Linux啟程

    北京初感&Linux啟程

    Linux干貨 2018-03-26
欧美性久久久久