配置使用基于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

相关推荐

  • python基础_1

    变量/常量 常量:一旦赋值,就不能对它从新复制。python不存在常量 字面常量:一个单独出现的量,未赋值给任何变量或者常量 变量:就是一个名字,在固执符号的左边,这个名字可以指复制符号右边的内容。 变量列子: i = 3 print(i)  注:python 是强类型语言,是动态类型语言。(强类型指类型之间不能相互计算/动态类型是指变量是可以赋值…

    Linux干货 2017-10-10
  • N24 第三周 磁盘及文件系统管理

    Linux系统管理          磁盘分区及文件系统管理          RAID          LVM 动态磁盘设备管理          网络属性…

    Linux干货 2016-11-22
  • shell编程的常见实例

    1. 写一个脚本/root/bin/argsnum.sh 用户输入一个文件路径并查找文件中空白行的个数 ,如果用户没有输入路径则自动退出  实验结果如下: 如果不输入任何路径程序会自动退出:实验结果如下: 用其他方法验证是不是有两行空行: (个人总结): 1.   read 命令,如果用 -p 来做提示信息,则不支持\n换行,这…

    Linux干货 2016-08-12
  • 先给出自动化编译安装httpd的脚本,后面的mysql,php-fpm,xcache等还在测试中

    自动化编译安装httpd的脚本! FUNDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" echo "Make …

    Linux干货 2016-10-30
  • linux基础练习

    马哥教育23期网络班+第6周课堂练习 Linux 基础练习 一、linux基础练习题 1、复制/etc/rc.d/rc.sysinit 文件至/tmp 目录,将/tmp/rc.sysinit 文件中的以至少一个空白字符开头的行的行首加#; 2、复制/boot/grub/grub.conf 至/tmp目录中 删除/tmp/grub.conf 文件中的行首的空白…

    Linux干货 2016-11-01
  • N25第三周作业(用户组,和文本管理)

    列出当前系统 上所有已经登录用户的用户名,注意:同一个用户登录多次,则只显示一次即可. 列出最后登录到当前系统的用户相关信息. 命令who查看所有用户 , tail查看后几行 取出当前系统上被用户当作其默认shell的最多那个shell. 命令cut 分割 , -d 指定分隔符,-f指定字段 uniq 显示或忽略重复行信息   -c:显示并统计重复…

    Linux干货 2016-12-19