日志服务管理

## 1.日志的记录方式

**日志的分类:**

- daemon 后台进程相关 
- kern 内核产生的信息
- lpr 打印系统产生的
- authpriv 安全认证
- cron 定时相关
- mail 邮件相关
- syslog 日志服务本身的
- news 新闻系统
- local0~7 自定义的日志设备
- local0-local7 8 个系统保留的类, 供其它的程序使用或者是用户自定义

**日志的级别:   轻 --> 重**

```http
编码   优先级        严重性
 7     debug    信息对开发人员调试应用程序有用,在操作过程中无用
 6     info     正常的操作信息,可以收集报告,测量吞吐量等
 5     notice   注意,正常但重要的事件,
 4     warning  警告,提示如果不采取行动。将会发生错误。比如文件系统使用 90%
 3     err      错误,阻止某个模块或程序的功能不能正常使用
 2     crit     关键的错误,已经影响了整个系统或软件不能正常工作的信息
 1     alert    警报,需要立刻修改的信息
 0     emerg    紧急,内核崩溃等严重信息
```
## 2.实战案例
### 2.1自定义ssh服务的日志类型和存储位置

```
[root@centos7 ~]#vim /etc/rsyslog.conf
# 把自定义 local0 类别的日志,保存到 /var/log/sshd.log 路径
#以 73 行下,插入以下内容
local0.*                                                /var/log/sshd.log

#定义 ssh 服务的日志类别为 local0,编辑 sshd 服务的主配置文件
[root@centos7 ~]#vim /etc/ssh/sshd_config
#改:SyslogFacility AUTHPRIV
#为:SyslogFacility local0
SyslogFacility local0


#把 sshd 服务日志 默认的安全认证类别 改成我们的 自定义 local0 类别
#先重启 rsyslog 服务(生效配置)
[root@centos7 ~]#systemctl restart rsyslog

#再重启 sshd 服务.生成日志
[root@centos7 ~]#systemctl restart sshd

#验证是否生成日志并查看其中的内容
[root@centos7 ~]#cat /var/log/sshd.log      #说明修改成功
Sep  8 23:55:29 centos7 sshd[1250]: Server listening on 0.0.0.0 port 22.
Sep  8 23:55:29 centos7 sshd[1250]: Server listening on :: port 22.

#上面对就的信息:时间 主机 服务 进程 ID 相关的信息

#如何防止日志删除?
[root@centos7 ~]#chattr +a /var/log/sshd.log
[root@centos7 ~]#lsattr /var/log/sshd.log 
-----a---------- /var/log/sshd.log
[root@centos7 ~]#systemctl restart sshd
[root@centos7 ~]#cat /var/log/sshd.log    #重启服务,查看日志有所增加
Sep  8 23:55:29 centos7 sshd[1250]: Server listening on 0.0.0.0 port 22.
Sep  8 23:55:29 centos7 sshd[1250]: Server listening on :: port 22.
Sep  8 23:57:31 centos7 sshd[1250]: Received signal 15; terminating.
Sep  8 23:57:31 centos7 sshd[1261]: Server listening on 0.0.0.0 port 22.
Sep  8 23:57:31 centos7 sshd[1261]: Server listening on :: port 22.

#注:这个功能看着很强大,其实不实用,因这样会让系统日志切割时报错,最主的是,黑客可以取消这个属性。
[root@centos7 ~]#chattr -a /var/log/sshd.log  #取消,这里一定要取消,不然后面做日志切割报错
```

### 2.2使用logrotate进行ssh日志分割
**定义了 ssh 日志存储在/var/log/sshd 的基础上执行**

```
[root@centos7 ~]#vim /etc/logrotate.d/sshd  #创建一个 sshd 配置文件,插入内容
/var/log/sshd.log {
    missingok
    weekly
    create 0600 root root
    minsize 10M
    rotate 3
}

#如果说我不想每周,或每月,我想一分钟分割一次日志,日志当然没有这样分割的,但你可以用计划任务调用这个脚本就行了
[root@centos7 ~]#crontab -e
*/1 * * * * logrotate -vf /etc/logrotate.d/sshd

[root@centos7 ~]#systemctl restart rsyslog

#预演,不实际轮询(切割)
[root@centos7 ~]#logrotate -d /etc/logrotate.d/sshd
reading config file /etc/logrotate.d/sshd
Allocating hash table for state file, size 15360 B

Handling 1 logs

rotating pattern: /var/log/sshd.log  weekly (3 rotations)
empty log files are rotated, only log files >= 1048576 bytes are rotated, old logs are removed
considering log /var/log/sshd.log
  log does not need rotating (log has been already rotated)您在 /var/spool/mail/root 中有邮件

#强制轮询(切割),也就是说即使轮循条件没有满足,也可以通过加-f 强制让 logrotate 轮循日志文件
-v 显示指令执行过程
-f 强制执行
[root@centos7 ~]# logrotate -vf /etc/logrotate.d/sshd

[root@centos7 ~]#ls /var/log/sshd*
/var/log/sshd.log  /var/log/sshd.log.1  /var/log/sshd.log.2  /var/log/sshd.log.3

#再次查看日志文件大小,已经为 0
[root@centos7 ~]#ll /var/log/sshd* -h
-rw-------. 1 root root 0 9月   9 00:18 /var/log/sshd.log
```
### 2.3配置远程日志服务器-实现日志集中的管理

**Server 端配置:**

```
#关闭 selinux 和防火墙
[root@centos7 ~]#setenforce 0
[root@centos7 ~]#systemctl stop firewalld

[root@centos7 ~]#vim /etc/rsyslog.conf     #使用 TCP 协议方式,收集日志
改:19 #$ModLoad imtcp
   20 #$InputTCPServerRun 514
为:
19 $ModLoad imtcp
20 $InputTCPServerRun 514

Rocky8 把下面 2 行的注释去掉
24 #module(load="imtcp") # needs to be done just once
25 #input(type="imtcp" port="514")

注:使用 UDP 协议 速度快 不保证数据的完整,使用 TCP 协议 可靠.完整

[root@centos7 ~]#systemctl restart rsyslog    #重新启动 rsyslog

#查看服务监听的状态:
[root@centos7 ~]#netstat -anlpt| grep 514
tcp        0      0 0.0.0.0:514             0.0.0.0:*               LISTEN      1538/rsyslogd       
tcp6       0      0 :::514                  :::*                    LISTEN      1538/rsyslogd
```

**Client 端配置:**

```
[root@centos7 ~]#vim /etc/rsyslog.conf   #在 90 行之后,插入
*.* @@192.168.2.71:514      #写入服务端的 ip 地址

#注: *.* 所有类别和级别的日志; @@192.168.2.71:514 服务端 tcp 协议的日志服务端的 IP 和端口

#重启 rsyslog 服务
[root@centos7 ~]#systemctl restart rsyslog.service
```

**服务端查看日志:**

```
#动态查看日志
[root@centos7 ~]#tail -f /var/log/messages | grep '2.72' --color

#在客户端 xuegod64 进行测试
#语法: logger 要模拟发送的日志
[root@centos7 ~]# logger "aaaaa"

#服务器端到查看消息
[root@centos7 ~]#tail -f /var/log/messages
Sep  9 00:33:43 centos7 root: aaaaa
```

**总结:**

**服务器使用 udp 协议,客户端使用的配置文件中这一行只能有一个@**
***.* @192.168.2.71:514**

**服务器使用 tcp 协议,客户端使用的配置文件中这一行必须有两个@@**
***.* @@192.168.2.71:514

## 三、利用mysql存储日志信息

### 3.1 目标

利用rsyslog日志服务,将收集的日志记录于MySQL中

### 3.2 环境准备

两台主机

一台:rsyslog日志服务器,IP:10.0.0.8

一台:mariadb数据库服务器,IP:10.0.0.18

### 3.3 实现步骤

#### 3.3.1 在rsyslog服务器上安装连接mysql模块相关的程序包

```
[root@Rocky8 ~]#yum install rsyslog-mysql

[root@Rocky8 ~]#rpm -ql rsyslog-mysql
/usr/lib/.build-id
/usr/lib/.build-id/d7
/usr/lib/.build-id/d7/77fc839aa07e92f0a8858cf3f122996436c7df
/usr/lib64/rsyslog/ommysql.so
/usr/share/doc/rsyslog/mysql-createDB.sql

#查看sql脚本文件内容
[root@Rocky8 ~]#cat /usr/share/doc/rsyslog/mysql-createDB.sql
CREATE DATABASE Syslog;
USE Syslog;
CREATE TABLE SystemEvents
(
       ID int unsigned not null auto_increment primary key,
       CustomerID bigint,
       ReceivedAt datetime NULL,
       DeviceReportedTime datetime NULL,
       Facility smallint NULL,
       Priority smallint NULL,
       FromHost varchar(60) NULL,
       Message text,
       NTSeverity int NULL,
       Importance int NULL,
       EventSource varchar(60),
       EventUser varchar(60) NULL,
       EventCategory int NULL,
       EventID int NULL,
       EventBinaryData text NULL,
       MaxAvailable int NULL,
          CurrUsage int NULL,
       MinUsage int NULL,
       MaxUsage int NULL,
       InfoUnitID int NULL ,
       SysLogTag varchar(60),
       EventLogType varchar(60),
       GenericFileName VarChar(60),
       SystemID int NULL
);
CREATE TABLE SystemEventsProperties
(
       ID int unsigned not null auto_increment primary key,
       SystemEventID int NULL ,
       ParamName varchar(255) NULL ,
       ParamValue text NULL
);

#将sql脚本复制到数据库服库上
[root@Rocky8 ~]#scp /usr/share/doc/rsyslog/mysql-createDB.sql 10.0.0.18:/data
```

#### 3.3.2 准备MySQL Server

```
[root@Rocky8 ~]#yum install mariadb-server

#在mariadb数据库服务器上创建相关数据库和表,并授权rsyslog能连接至当前服务器
[root@mysql ~]#mysql -uroot
[root@mysql~]#mysql>source /data/mysql-createDB.sql
[root@mysql~]#mysql>GRANT ALL ON Syslog.* TO 'rsyslog'@'10.0.0.%' IDENTIFIED BY '1';
[root@mysql ~]#systemctl enable --now mariadb
```

#### 3.3.3 配置日志服务器将日志发送至指定数据库

```sh
#配置rsyslog将日志保存到mysql中
[root@Rocky8 ~]#vim /etc/rsyslog.conf
#
####MODULES####
#在 MODULES 语言下面,如果是 Rocky8 加下面行
module(load="ommysql")

#在 MODULES 语言下面,如果是 CentOS 7,6 加下面行
$ModLoad ommysql

#在RULES语句块加下面行的格式
#facility.priority   :ommysql:DBHOST,DBNAME,DBUSER, PASSWORD 
*.info :ommysql:10.0.0.18,Syslog,rsyslog,1

[root@Rocky8 ~]#systemctl restart rsyslog.service
```

#### 3.3.4 测试

```
#在日志服务器上生成日志
[root@Rocky8 ~]#logger "this is a test log"

#在数据库上查询到上面的测试日志

MariaDB [mysql]> use Syslog

MariaDB [Syslog]> SELECT COUNT(*) FROM SystemEvents;
+----------+
| COUNT(*) |
+----------+
|        8 |
+----------+
1 row in set (0.00 sec)

MariaDB [Syslog]> SELECT COUNT(*) FROM SystemEvents;
+----------+
| COUNT(*) |
+----------+
|        9 |
+----------+
1 row in set (0.00 sec)
```

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值