配置使用基于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.178linux.com/42866

(0)
马哥Net19_小斌斌马哥Net19_小斌斌
上一篇 2016-09-05 08:50
下一篇 2016-09-05 08:50

相关推荐

  • vim的总结、练习和周期性任务

    vim:VI的增强版,是一款可视化功能非常强大的文本编辑器,属于全屏编辑器。 vim:使用方法: 命令格式:    ~]#vim [options] [file ..]      选项:        +#:打开文件后,直接让光标处于第#行首;   …

    Linux干货 2017-08-14
  • IP地址的三种表示格式及在Socket编程中的应用

       使用TCP/IP协议进行网络应用开发的朋友首先要面对的就是对IP地址信息的处理。IP地址其实有三种不同的表示格式:        1)Ascii(网络点分字符串)-        2) 网络地址(32位无符号整形,网络字节序,大头) &nbsp…

    Linux干货 2015-04-10
  • iptables使用示例

    iptables规则

    2017-10-14
  • 马哥教育网络班19期+第九周课程练习

    1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin),分别这两类用户的个数,通过字符串比较来实现。 #!/bin/bash for user_shell in `awk -F: '{print $7}'&nbsp…

    Linux干货 2016-07-01
  • 终端类型

        终端是什么?终端不仅仅是显示器,还包括与之配套的键盘。在linux中表现为一个字符设备。Linux与用户交互时,直接向终端设备发送数据,数据就会被发送到屏幕上,用户通过键盘写的数据,就是向这个字符设备写数据,数据会同步显示到显示器上,回车后数据才会被linux执行命令。 终端类型 1串行口终端  &n…

    Linux干货 2016-10-17
  • DNS基础及使用BIND搭建域名服务器

        本文主线是DNS服务相关概念和服务器搭建,在此之前了解一下域名这个东东很有其必要性,因为DNS服务就是应域名而生的。那么域名是什么哪?     域名     域名是用一串用点分开的字符串来表示的internet上的某一台或者一组计算机的名称,用于在数据传输时标识计算机的电子方位。举…

    Linux干货 2015-06-11