配置使用基于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 08:49
下一篇 2016-09-05 08:50

相關推薦

  • 網絡26期 第一周作業

    1、描述計算機的組成及其功能。 計算機由cpu、存儲器(內存)、輸入設備(Input)、輸出設備(Output),其中cpu中的運算器和控制器是必要的,這擁有以上五個部件就可以組成一個能正常工作的計算機,但是內存無法永久保存數據于是還需要一個硬盤來永久保存數據,硬盤也是存儲器的一種但是它卻是一個io設備,即至少是輸入或者輸出設備中的一種.所以我將其分開來說了…

    Linux干貨 2017-01-18
  • 馬哥教育網絡班21期-第六周課程練習

    請詳細總結vim編輯器的使用并完成以下練習題 1.復制/etc/rc.d/rc.sysinit文件至/tmp目錄,將/tmp/rc.sysinit文件中的以至少一個空白字符開頭的行的行首加# [root@junfeng ~]# cp /etc/rc.d/rc.sysinit /tmp [root@junfeng&nbs…

    Linux干貨 2016-08-15
  • N26-第一周作業

    1、描述計算機的組成及其功能;  計算機組成部分分為硬件及軟件。  硬件:  計算機硬件主要有五大部分組成:運算器、控制器、存儲器、輸入設備、輸出設備;  1)運算器、控制器,統稱為處理器,也就是CPU:    主要功能是對各種數據進行運算,包括加、減、乘、除的算術運算,還包括進行邏輯判斷的能力,如…

    Linux干貨 2016-12-31
  • 20160804正則表達式作業

    正則表達式表示18位身份證號 正則表達式表示手機號 正則表達式表示郵箱 ?  截取出ifconfig命令結果中所有的本機的ipv4地址   ?  查看分區中使用率最大的百分比 ?  查看UID最大的用戶名、UID以及shell類型 ?  查看/tmp權限,并且以數字方式顯示 思路:先找到如何才能查看到/tmp…

    Linux干貨 2016-08-08
  • Centos6啟動流程詳解

    一、名詞解釋 POST: Power-On-Self-Test,加電自檢,是BIOS功能的一個主要部分。負責完成對CPU、主板、內存、硬盤子系統、顯示子系統、串并行接口、鍵盤、CD-ROM光驅等硬件情況的檢測。 BIOS:Basic Input andOutput System,保存著有關計算機系統最重要的基本輸入輸出程序,系統信息設置、開機加電自檢程序和系…

    2017-03-28
  • find命令詳解

    find命令詳解 實時查找工具,通過遍歷指定起始路徑下文件系統層級結構完成文件查找; 一、工作特性: 查找速度略慢; 精確查找,只查找文件路徑的基名而非整個路徑; 實時查找; 可能只搜索用戶具備讀取和執行權限的目錄 二、 用法: find [查找起始路徑] [OPTIONS] [查找條件] [處理動作] 查找起始路徑:指定具體搜索目標起始路徑;默認為當前目錄…

    Linux干貨 2017-03-19
欧美性久久久久