Syslog日志服务器配置 For CentOS 7.8(Syslog+LogAnalyzer+LAMP)

时间:2024-04-10 15:20:28

声明:本教程仅供学习、研究、测试使用,本文作者不承担任何法律责任!

 

本次*日志服务器采用rsyslog提供日志接收服务,Mariadb作为后端数据库提供日志存储服务,

一、安装CentOS 7操作系统

1、CentOS7的操作系统安装过程本文不再赘述,本次采用CentOS-7-x86_64-DVD-1908.iso的镜像进行最小化安装系统,隶属CentOS 7.8 版本

2、实验环境IP:192.168.1.11

3、进入系统后更新系统并重启

#yum update -y

#reboot

  • CentOS7系统设置

1.设置IP地址

设置IP地址为192.168.1.11,(请根据自己的网络状况自行设置)

#vi /etc/sysconfig/network-scripts/ifcfg-ens192

请根据自己的网卡配置相应的网络IP

2.关闭防火墙

因为是在内网环境,出口有防火墙设备,为方便操作这里先把centos的防火墙关闭了,如果需要请自定放开使用到的端口

#systemctl stop firewalld

#systemctl disable firewalld

 

3.关闭SeLinux

SeLinux这里一起关闭了,留着就是莫名其妙的问题,有需要的自己研究策略吧

临时关闭可使用命令

#setenforce 0

这里直接永久关闭了

# vi /etc/sysconfig/selinux

将下面的SELINUX=enforcing改为SELINUX=disabled,然后重启服务器。

# This file controls the state of SELinux on the system.

# SELINUX= can take one of these three values:

#     enforcing - SELinux security policy is enforced.

#     permissive - SELinux prints warnings instead of enforcing.

#     disabled - No SELinux policy is loaded.

SELINUX=disabled

# SELINUXTYPE= can take one of three values:

#     targeted - Targeted processes are protected,

#     minimum - Modification of targeted policy. Only selected processes are protected.

#     mls - Multi Level Security protection.

SELINUXTYPE=targeted

 

重启后使用可确认SeLinux是否关闭

#getenforce

显示Disable则为关闭

 

三、搭建LAMP环境

应要将日志数据保存在数据库中,还要使用LogAnalyzer来做web端延时,所以这里选择LAMP环境了

1.MariaDB的安装和配置

1>安装MariaDB

Maraidb作为rsyslog和LogAnalyzer的存储数据库服务

#yum install -y mariadb mariadb-server

等待自动安装完成

2>启动mariaDB

#systemctl start mariadb

3>设置MariaDB开机启动

#systemctl enable mariadb

4>MariaDB进行简单相关配置

#mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB

      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

 

In order to log into MariaDB to secure it, we'll need the current

password for the root user.  If you've just installed MariaDB, and

you haven't set the root password yet, the password will be blank,

so you should just press enter here.

 

Enter current password for root (enter for none):(首次进入直接敲回车以设置root密码)

OK, successfully used password, moving on...

 

Setting the root password ensures that nobody can log into the MariaDB

root user without the proper authorisation.

 

Set root password? [Y/n]Y(输入Y,开始设置root密码)

New password: (输入新的root用户密码)

Re-enter new password: (在输入一次新的root用户密码)

Password updated successfully!

Reloading privilege tables..

 ... Success!

 

By default, a MariaDB installation has an anonymous user, allowing anyone

to log into MariaDB without having to have a user account created for

them.  This is intended only for testing, and to make the installation

go a bit smoother.  You should remove them before moving into a

production environment.

 

Remove anonymous users? [Y/n] (是否删除匿名用户,直接回车,表示删除掉匿名用户)

... Success!

 

Normally, root should only be allowed to connect from 'localhost'.  This

ensures that someone cannot guess at the root password from the network.

 

Disallow root login remotely? [Y/n] n(是否禁用root远程访问,直接回车,表示禁止远程访问,我这里为方便后续再电脑上查看数据库,这里使用敲n回车,表示打开远程访问)

 ... skipping.

 

By default, MariaDB comes with a database named 'test' that anyone can

access.  This is also intended only for testing, and should be removed

before moving into a production environment.

 

Remove test database and access to it? [Y/n] (是否删除测试数据库,直接回车,表示删除测试数据库)

 - Dropping test database...

 ... Success!

 - Removing privileges on test database...

 ... Success!

 

Reloading the privilege tables will ensure that all changes made so far

will take effect immediately.

 

Reload privilege tables now? [Y/n] (是否重新加载权限表,直接回车,表示重新加载)

 ... Success!

 

Cleaning up...

 

All done!  If you've completed all of the above steps, your MariaDB

installation should now be secure.

 

Thanks for using MariaDB!

 

5>配置MariaDB字符集

登录MariaDB查看数据库字符集是否为UTF-8

#mysql -u root -p

查看字符集是否为UTF-8

MariaDB [(none)]>show variables like "%character%";show variables like "%collation%";

Syslog日志服务器配置 For CentOS 7.8(Syslog+LogAnalyzer+LAMP)

可以看到默认MariaDB使用的是latin1_swedish_ci,我们需要把字符集全部改成UTF-8以避免后续出现乱码

退出MariaDB,修改MariaDB的配置文件

MariaDB [(none)]>quit;

养成的习惯,改文件先备份一下

#cp /etc/my.cnf /etc/my.cnf_bak20200430

#vi /etc/my.cnf

按i键开始编辑

在[mysqld]标签下添加

#character

init_connect='SET collation_connection = utf8_unicode_ci'

init_connect='SET NAMES utf8'

character-set-server=utf8

collation-server=utf8_unicode_ci

skip-character-set-client-handshake

编辑完成后按ESC退出编辑模式,敲:wq保存

重启MariaDB

#systemctl restart mariadb

确认字符集是否更改成功

Syslog日志服务器配置 For CentOS 7.8(Syslog+LogAnalyzer+LAMP)

2.apache和php的安装和配置

因为LogAnalyzer是PHP开发的WEB前端,这里使用apache作为web服务器,并添加PHP的相关模块

1>安装apache和php

#yum install -y httpd php php-mysql php-devel

2>启动apache

#systemctl start apache

#systemctl enable apache

浏览器中输入服务器的IP地址测试apache是否安装成功

Syslog日志服务器配置 For CentOS 7.8(Syslog+LogAnalyzer+LAMP)

 

3>验证php配置

在apache的网站根目录下创建一个test.php的测试文件

#cd /var/www/html

#vi test.php

输入以下内容

<? php

phpinfo();

?>

:wq保存

在浏览器中打开测试页http://192.168.1.11/test.php

Syslog日志服务器配置 For CentOS 7.8(Syslog+LogAnalyzer+LAMP)

出现这个界面说明php配置成功了

 

四、配置rsyslog服务

1.安装rsyslogd的mysql模块

rsyslog是CentOS 7的默认syslog服务,所以这里不需要再安装,但是我们要把日志保存到MariaDB数据库中,所以需要添加rsyslog的mysql支持

#yum install -y rsyslog-mysql

2.配置MariaDB数据库

安装完rsyslog的mysql支持模块之后,需要创建MariaDB的数据库实例

1>创建Syslog数据库

在rsyslog的doc文件夹下,会有一个新建Syslog数据库的脚本,使用脚本直接导入到MariaDB中建立数据库

#cd /usr/share/doc/rsyslog-8.24.0/(这里rsyslog的版本不同目录可能不同)

# mysql -u root -p <mysql-createDB.sql

Enter password:(输入MariaDB的root账号的密码)

登录到MariaDB中,确认数据库是否新建完成

MariaDB [(none)]> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| Syslog             |

| mysql              |

| performance_schema |

+--------------------+

4 rows in set (0.00 sec)

可以看到Syslog数据库已经添加成功

2>创建Syslog数据库的访问用户

创建并授权访问用户

MariaDB [(none)]> grant all on Syslog.* to [email protected]'localhost' identified by 'rsyslog';

MariaDB [(none)]> grant all on Syslog.* to [email protected]'127.0.0.1' identified by 'rsyslog';

MariaDB [(none)]> grant all on Syslog.* to [email protected]'主机IP地址' identified by 'rsyslog';

MariaDB [(none)]> grant all on Syslog.* to [email protected]'计算机名称' identified by 'rsyslog';

MariaDB [(none)]> grant all on Syslog.* to [email protected]'%' identified by 'rsyslog';

这里注意根据访问需要添加对应的用户和访问主机,“%”表示允许所有访问,后面的rsyslog是密码可根据需要设置

添加完成后刷新策略

MariaDB [(none)]> flush privileges;(刷新策略)

MariaDB [(none)]> exit;(退出)

 

确认账号创建是否成功

MariaDB [(none)]> select user,password,host from mysql.user where user='rsyslog';

Syslog日志服务器配置 For CentOS 7.8(Syslog+LogAnalyzer+LAMP)

3>确认数据库是否正常

MariaDB [(none)]> use Syslog;

Syslog日志服务器配置 For CentOS 7.8(Syslog+LogAnalyzer+LAMP)

查看数据库表是否正常

MariaDB [Syslog]> show tables;

Syslog日志服务器配置 For CentOS 7.8(Syslog+LogAnalyzer+LAMP)
 

查看表数据,初始是空的

MariaDB [Syslog]> select * from SystemEventsProperties;

Syslog日志服务器配置 For CentOS 7.8(Syslog+LogAnalyzer+LAMP)

MariaDB [Syslog]> select * from SystemEventsProperties;

Syslog日志服务器配置 For CentOS 7.8(Syslog+LogAnalyzer+LAMP)

3.配置rsyslog

#cp /etc/rsyslog.conf /etc/rsyslog.conf_bak20200430

#vi /etc/rsyslog.conf

这里我们开启UDP 514端口用来接收外部的syslog日志

加载rsyslog的mysql模块支持

转发所有日志到mariadb数据中

在#### MODULES #### 模块的部分下面,

Syslog日志服务器配置 For CentOS 7.8(Syslog+LogAnalyzer+LAMP)

在#### RULES #### 规则的部分下面添加转发规则

*.* :ommysql:localhost,Syslog,rsyslog,rsyslog

Syslog日志服务器配置 For CentOS 7.8(Syslog+LogAnalyzer+LAMP)

内容解释:(注意区分大小写)

“*.*”:表示所有日志

“:ommysql:”:表示使用mysql数据库模块来存储日志

“localhost”:表示Syslog的数据库主机(可以使计算机名,IP地址等,但需确认所用的计算机名称再MariaDB中有添加用户访问权限)

“Syslog”:表示MariaDB的数据库实例名称

“第一个rsyslog”:是访问Syslog数据库的用户名

“第二个rsyslog”:是访问Syslog数据库的密码

 

修改完成后:wq保存

 

修改完成后重新rsyslog服务

 

#systemctl restart rsyslog

再次查看Syslog数据库中的SystemEvents表,可以看到log数据已经可以进来了

 

五、安装LogAnalyzer

接下来LogAnalyzer用于web前端显示,本次把LogAnalyzer也安装为单独的数据库来处理

1.配置MariaDB数据库

1>创建loganalyzer的数据库

MariaDB [(none)]> create database loganalyzer;

 

2>授权loganalyzer数据库的访问用户

创建并授权用户访问

MariaDB [(none)]> grant all on loganalyzer.* to [email protected]'localhost' identified by 'loganalyzer';

MariaDB [(none)]> grant all on loganalyzer.* to [email protected]'127.0.0.1' identified by 'loganalyzer';

MariaDB [(none)]> grant all on loganalyzer.* to [email protected]'192.168.90.229' identified by 'loganalyzer';

MariaDB [(none)]> grant all on loganalyzer.* to [email protected]'yt-syslog-01' identified by 'loganalyzer';

MariaDB [(none)]> grant all on loganalyzer.* to [email protected]'%' identified by 'loganalyzer';

 

2.配置LogAnalyzer

官网下载LogAnalyzer的最新版本安装包,写文档时当前的最新版本是V4.1.10

 

解压src的内容到apache的web根目录

这里不改了,直接使用默认的目录,/var/www/html

给777权限

#chmod -R 777 /var/www/html

浏览器中打开http://192.168.1.11,首次运行会提示没有配置文件,点here就可以根据提示来初始化配置文件了

Syslog日志服务器配置 For CentOS 7.8(Syslog+LogAnalyzer+LAMP)

 

1>第一步,直接点Next

Syslog日志服务器配置 For CentOS 7.8(Syslog+LogAnalyzer+LAMP)

2>第二步,确认./config.php可写

这里需要给写入权限,前面我们已经给了777全下,所以直接Next

Syslog日志服务器配置 For CentOS 7.8(Syslog+LogAnalyzer+LAMP)

3>第三步,配置loganalyzer数据库

Syslog日志服务器配置 For CentOS 7.8(Syslog+LogAnalyzer+LAMP)

这里点YES,就可以展开数据库配置了

因为我们已经创建了loganalyzer的空数据库,这里我们填上相应的数据库信息,就可以初始化数据库了

Syslog日志服务器配置 For CentOS 7.8(Syslog+LogAnalyzer+LAMP)

4>第四步,初始化数据库

创建数据库表,直接点Next

Syslog日志服务器配置 For CentOS 7.8(Syslog+LogAnalyzer+LAMP)

 

5>第五步,检查SQL结果

可以看到SQL脚本已经执行完成了,点Next

Syslog日志服务器配置 For CentOS 7.8(Syslog+LogAnalyzer+LAMP)

6>第六步,创建loganalyzer的访问账号

这里使用root了,密码自己填,完成点Next

Syslog日志服务器配置 For CentOS 7.8(Syslog+LogAnalyzer+LAMP)

7>第七步,连接Syslog数据库

这里填上之前我们创建的Syslog的数据库信息,填写完成后点Next

Syslog日志服务器配置 For CentOS 7.8(Syslog+LogAnalyzer+LAMP)

8>第八步,完成

到这里LogAnalyzer就配置完成了,点Finish

Syslog日志服务器配置 For CentOS 7.8(Syslog+LogAnalyzer+LAMP)

3.使用LogAnalyzer

配置完成后就可以使用root和密码来登录了

Syslog日志服务器配置 For CentOS 7.8(Syslog+LogAnalyzer+LAMP)

可以看到WEB端已经可以显示日志了

Syslog日志服务器配置 For CentOS 7.8(Syslog+LogAnalyzer+LAMP)

 

4.LogAnalyzer中文语言

LogAnalyzer默认没有中文语言,需要自行添加中文语言包,可以再下面的地址下载

https://download.csdn.net/download/MosFoyer/12271598

现在之后,将zh文件夹放到 /var/www/html/lang文件夹下

然后刷新网页,右上角选择语言的地方就可以看到中文语言了

Syslog日志服务器配置 For CentOS 7.8(Syslog+LogAnalyzer+LAMP)

至此整个系统搭建完成