1.首先在rsyslog服务的配置文件定义一个符合自己日志类别
[root@centos7 ~]# vim /etc/rsyslog.conf #rsyslog的主配置文件
# Save boot messages also to boot.log
local7.* /var/log/boot.log
local0.* /var/log/sshd.log # 添加一行这个local0-7没有用的数字,local0.这个里如果写*则表示任何信息的记录,当然也可以选择记录那个级别的
[root@centos7 ~]# vim /etc/ssh/sshd_config
# Logging
#SyslogFacility AUTH
SyslogFacility local0 # 这里修改成刚刚在rsyslog配置文件中定义的名字
#LogLevel INFO # 这个是日志级别默认是没有启用的
systemctl restart sshd
systemctl restart rsyslog
[root@centos7 ~]# cat /var/log/sshd.log
Feb 3 15:07:18 centos7 sshd[6206]: Server listening on 0.0.0.0 port 22.
Feb 3 15:07:18 centos7 sshd[6206]: Server listening on :: port 22.
Feb 3 15:07:38 centos7 sshd[6221]: Accepted password for root from 192.168.27.1 port 49380 ssh2
[root@centos7 ~]# logger -p local0.info "this is a test log" # 用-p选项
[root@centos7 ~]# cat /var/log/sshd.log
Feb 3 15:07:18 centos7 sshd[6206]: Server listening on 0.0.0.0 port 22.
Feb 3 15:07:18 centos7 sshd[6206]: Server listening on :: port 22.
Feb 3 15:07:38 centos7 sshd[6221]: Accepted password for root from 192.168.27.1 port 49380 ssh2
Feb 3 15:28:02 centos7 root: this is a test log
# Save boot messages also to boot.log
local7.* /var/log/boot.log
local0.* /var/log/sshd.log
local1.* guo,root #可以通知多个用户,如果是通知多个用户就写*
systemctl restart rsyslog
[root@centos7 ~]# logger -p local1.info 'this is test'
[root@centos7 ~]# root:this is test # 这个可以的
# Provides UDP syslog reception
$ModLoad imudp # 加载模块
$UDPServerRun 514 # 监听udp的514端口,因为udp不是安全的传输,所以我们使用tcp
# Provides TCP syslog reception
$ModLoad imtcp # 加载模块
$InputTCPServerRun 514 # 监听tcp的514端口
[root@centos6 ~]# service rsyslog restart
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
[root@centos6 ~]# ss -ntlu |grep 514
udp UNCONN 0 0 *:514 *:*
udp UNCONN 0 0 :::514 :::*
tcp LISTEN 0 25 :::514 :::*
tcp LISTEN 0 25 *:514 *:*
[root@centos7 ~]# vim /etc/rsyslog.conf
# Log anything (except mail) of level info or higher.
# Don't log private authentication messages!
#*.info;mail.none;authpriv.none;cron.none /var/log/messages # 当开启将日志转发给日志服务器时,本地默认是不会存日志的,所有也可以把这一行注释去掉,这样本地也会存放日志
*.info;mail.none;authpriv.none;cron.none @@192.168.27.128 # 如果只是用udp的话只用一个@符号就可以
systemctl restart rsyslog
[root@centos7 ~]# logger "this is test 2th"
[root@centos7 ~]# logger "this is test 2th"
[root@centos6 ~]# tailf /var/log/messages
Feb 3 16:34:37 centos7 root: this is test 2=th
Feb 3 16:34:38 centos7 root: this is test 2=th
[root@centos7 ~]# yum install mariadb-server.x86_64 1:5.5.56-2.el7
[root@centos7 ~]# systemctl enable mariadb
[root@centos7 ~]# systemctl start mariadb
[root@centos6 ~]# rpm -ql rsyslog-mysql.x86_64 0:5.8.10-10.el6_6
/lib64/rsyslog/ommysql.so
/usr/share/doc/rsyslog-mysql-5.8.10
/usr/share/doc/rsyslog-mysql-5.8.10/createDB.sql # 这是一个mariadb脚本,这里会创建表等内容。
package 0:5.8.10-10.el6_6 is not installed
[root@centos6 ~]# scp /usr/share/doc/rsyslog-mysql-5.8.10/createDB.sql 192.168.27.129:
MariaDB [(none)]> source createDB.sql
Query OK, 1 row affected (0.00 sec)
Database changed
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.00 sec)
MariaDB [Syslog]>
# 查看是否生成数据库和表
ariaDB [Syslog]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| Syslog | # 这个就是生成的数据库
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [Syslog]>
MariaDB [Syslog]> flush privileges; # 因为刚刚对数据库做了增的操作,所有要刷新一下
MariaDB [Syslog]> grant all on Syslog.* to syslog@'192.168.27.128' identified by 'guo123456';
Query OK, 0 rows affected (0.00 sec)
[root@centos6 ~]# mysql -usyslog -pguo123456 -h192.168.27.129
Welcome to the MySQL monitor. Commands end with ; or \g.
#### MODULES ####
$ModLoad imuxsock # provides support for local system logging (e.g. via logger command)
$ModLoad imklog # provides kernel logging support (previously done by rklogd)
#$ModLoad immark # provides --MARK-- message capability
$ModLoad ommysql # 加载这个模块,就是刚刚用yum安装的
#### RULES ####
*.info;mail.none;authpriv.none;cron.none :ommysql:192.168.27.129,Syslog,syslog,guo123456 #:模块:数据库主机IP,数据库文件,用户名,密码
[root@centos6 ~]# service rsyslog restart
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
MariaDB [Syslog]> select * from SystemEvents\G ;
*************************** 183. row ***************************
ID: 183
CustomerID: NULL
ReceivedAt: 2017-11-11 04:13:15
DeviceReportedTime: 2017-11-11 04:13:15
Facility: 1
Priority: 5
FromHost: centos6
Message: test
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: root:
EventLogType: NULL
GenericFileName: NULL
SystemID: NULL
183 rows in set (0.00 sec)
yum install -y httpd php-gd php php-mysql
MariaDB [(none)]> grant all on Syslog.* to log@'192.168.27.%' identified by 'guo123456';
Query OK, 0 rows affected (0.00 sec)
[root@localhost html]# vim /var/www/html/info.php
$mysqli=new mysqli("192.168.27.129","log","guo123456");
if(mysqli_connect_errno()){
echo "Failure";
$mysqli=null;
exit;
}
echo "OK";
$mysqli->close();
?>
# 启动服务
[root@localhost html]# systemctl start httpd
tar xvf loganalyzer-4.1.6.tar.gz
cd loganalyzer-4.1.6
cp -r src /var/www/html/log # 因为解压出来很多文件,我们只需要src的目录的文件
touch /var/www/html/log/config.php # 创建这个文件这是向导是要写的配置
chmod 666 /var/www/html/log/config.php # 改权限
chmod 644 /var/www/html/log/config.php