Rsyslog配置手册

snow chuai汇总、整理、撰写---2020/2/25


1. 实现Rsyslog集中收集
1) 配置rsyslog
[root@srv1 ~]# vim /etc/rsyslog.conf
# 取消19-20行注释
$ModLoad imtcp
$InputTCPServerRun 514
# 于21行追加允许发送日志的网络段 $AllowedSender TCP, 127.0.0.1, 192.168.10.0/24, *.1000cc.net
[root@srv1 ~]# systemctl restart rsyslog
2) 配置rsyslog客户端 [root@srv2 ~]# vim /etc/rsyslog.conf # 将authpriv的信息发送至Rsyslog Server上 authpriv.* @@srv1.1000cc.net:514
[root@srv2 ~]# systemctl restart rsyslog
3) rsyslog服务器端查验 # 请在srv2上做几次账户登录的动作 [root@srv1 ~]# tail /var/log/secure | grep srv2 Feb 25 00:57:04 srv2 polkitd[687]: Unregistered Authentication Agent for unix-process:29180:1012214 (system bus name :1.72, object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale en_US.UTF-8) (disconnected from bus) Feb 25 00:57:12 srv2 su: pam_unix(su-l:session): session opened for user snow by (uid=0)
4) 分离出不同主机的日志信息(含有日期) [root@srv1 ~]# vim /etc/rsyslog.conf # 添加指定的日志格式(在"#### RULES ####"之上找一行加入) $template Secure_log,"/var/log/secure.d/%fromhost%_%$year%%$month%%$day%.secure"
# 调用指定的日志格式 authpriv.* -?Secure_log
[root@srv1 ~]# systemctl restart rsyslog
5) 确认 [root@srv1 ~]# ll /var/log/secure.d/ total 8 -rw------- 1 root root 167 Feb 25 01:03 192.168.10.12_20200225.secure -rw------- 1 root root 227 Feb 25 01:02 srv1_20200225.secure
2. 与MariaDB集成
2.1 配置MariaDB
1) 安装MariaDB
[root@srv1 ~]# yum install mariadb-server -y
[root@srv1 ~]# vim /etc/my.cnf # 于[mysqld]最尾部追加如下内容: character-set-server=utf8
[root@srv1 ~]# systemctl enable --now mariadb
2) 初始化MariaDB [root@srv1 ~]# 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): # enter 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 # 设置root密码 New password: Re-enter new password: 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] # y # 移除anonymous账户 ... 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] # y # 禁用root远程登录 ... Success!
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] # y # 移除test数据库 - 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] # y # 重新加载privilege ... 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!
3) 测试MariaDB连接 [root@srv1 ~]# mysql -u root -p Enter password: # 输入密码 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 16 Server version: 10.1.20-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> select user,host,password from mysql.user; +------+-----------+-------------------------------------------+ | user | host | password | +------+-----------+-------------------------------------------+ | root | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | | root | 127.0.0.1 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | | root | ::1 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | +------+-----------+-------------------------------------------+ 3 rows in set (0.00 sec)
MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ 3 rows in set (0.00 sec)
MariaDB [(none)]> exit Bye [root@srv1 ~]#
4) 防火墙规则设定 [root@srv1 ~]# firewall-cmd --add-service=mysql --permanent success [root@srv1 ~]# firewall-cmd --reload success
2.2 配置Rsyslog
1) 安装软件
[root@srv1 ~]# yum install rsyslog-mysql -y
2) 将数据导入至DB [root@srv1 ~]# cat /usr/share/doc/rsyslog-8.24.0/mysql-createDB.sql | mysql -u root -p Enter password:
3) 设定数据库访问权限 [root@srv1 ~]# mysql -u root -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 12 Server version: 10.1.20-MariaDB MariaDB Server
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> grant all privileges on Syslog.* to rsyslog@'localhost' identified by 'password'; Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges; Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> exit Bye
4) 配置Rsyslog [root@srv1 ~]# vim /etc/rsyslog.conf # 于22行,加入如下内容 $ModLoad ommysql
# 将authpriv信息写入至数据库 语法格式 日志设备.级别 :ommysql:主机:数据库:数据库账号:数据库密码 authpriv.* :ommysql:localhost,Syslog,rsyslog,password
[root@srv1 ~]# systemctl restart rsyslog
2.3 测试
[root@srv1 ~]# mysql -u rsyslog -p Syslog
Enter password: 
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 15 Server version: 10.1.20-MariaDB MariaDB Server
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [Syslog]> show tables; +------------------------+ | Tables_in_Syslog | +------------------------+ | SystemEvents | | SystemEventsProperties | +------------------------+ 2 rows in set (0.00 sec)
MariaDB [Syslog]> select ReceivedAt,Facility,Priority,FromHost,Message from SystemEvents; +---------------------+----------+----------+----------+------------------------------------------- | ReceivedAt | Facility | Priority | FromHost | Message +---------------------+----------+----------+----------+------------------------------------------- | 2020-02-25 01:17:55 | 10 | 5 | srv1 | Unregistered Authentication Agent for... | 2020-02-25 01:18:16 | 10 | 6 | srv2 | pam_unix(su-l:session): session closed... +---------------------+----------+----------+----------+-------------------------------------------
3. 集成LogAnalyzer
1) 安装httpd及PHP环境
[root@srv1 ~]# yum install httpd php php-mbstring php-pear php-mysql -y
[root@srv1 ~]# vim /etc/php.ini # 取消878行注释,并设定时区 date.timezone = "Asia/Shanghai" [root@srv1 ~]# systemctl enable --now httpd
2) 安装LogAnalyzer [root@srv1 ~]# wget http://download.adiscon.com/loganalyzer/loganalyzer-4.1.7.tar.gz
[root@srv1 ~]# tar xfz loganalyzer-4.1.7.tar.gz -C /var/www/html/ [root@srv1 ~]# cd /var/www/html/ [root@srv1 html]# mv loganalyzer-4.1.7 loganalyzer [root@srv1 html]# cd [root@srv1 ~]#
3) 配置LogAnalyzer [浏览器]===>http://srv1.1000cc.net/loganalyzer/src # 提示错误,需要访问index.php。点击[here]生成
# 自动检测安装需求
#发现没有config.php并且权限不是666
#创建并更改config.php及权限 [root@srv1 ~]# touch /var/www/html/loganalyzer/src/config.php [root@srv1 ~]# chmod 666 /var/www/html/loganalyzer/src/config.php
[浏览器]===>[ReCheck]


# 如果出现图8的错误,请修改config.php文件的185行,改为如下内容 [root@srv1 src]# vim +185 /var/www/html/loganalyzer/src/config.php $CFG['Sources']['Source1']['DBTableName'] = 'SystemEvents';


4) 更改LogAnalyzer网站Logo [root@srv1 ~]# cd /var/www/html/loganalyzer/src/images/main [root@srv1 ~]# ls -l total 8 -rw-rw-r-- 1 root root 7134 Dec 4 2018 Header-Logo.png # 图片文件,替换就好

 

如对您有帮助,请随缘打个赏。^-^

gold