Skip to content

Commit

Permalink
use logrotate to cleanup and archive audit logs (#3039)
Browse files Browse the repository at this point in the history
* use logrotate to cleanup and archive audit logs

* Update audit-log.md

* Update audit-log.md

* comment fix

* Update audit-log.md

* Update audit-log.md

* Update audit-log.md
  • Loading branch information
abby-cyber authored Oct 13, 2023
1 parent aa7ba54 commit 8e7db95
Showing 1 changed file with 107 additions and 0 deletions.
107 changes: 107 additions & 0 deletions docs-2.0/5.configurations-and-logs/2.log-management/audit-log.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,113 @@
|`QUERY_STATUS`| 查询状态。`0`表示成功,其他数字代表不同的错误信息。|
|`QUERY_MESSAGE`| 如果查询出错,会显示报错信息。|

## 使用 logrotate 进行审计日志轮转

用户可以使用 Linux 系统中的 [logrotate](https://github.com/logrotate/logrotate) 工具,对审计日志进行轮转以定期归档和销毁审计日志,避免日志文件过大。

以下为使用`logrotate`来定时清理{{nebula.name}}审计日志的操作步骤:

!!! note

需要使用 root 用户或者具有 sudo 权限的用户来安装 logrotate 或者运行 logrotate。

1. 安装 logrotate。

- Debian/Ubuntu:

```bash
sudo apt-get install logrotate
```

- CentOS/RHEL:

```bash
sudo yum install logrotate
```

2. 创建 logrotate 配置文件。

`/etc/logrotate.d`目录下,为审计日志创建一个新的 logrotate 配置文件,例如创建一个名为`audit`的文件并其中添加以下内容:

```bash
# 创建 audit 文件
sudo vim /etc/logrotate.d/audit
```

```bash
# 在 audit 文件中添加配置以设置日志轮转规则
/usr/local/nebula/logs/audit/audit.log {
daily
rotate 5
copytruncate
nocompress
missingok
notifempty
create 644 root root
dateext
dateformat .%Y-%m-%d-%s
maxsize 1k
}
```

示例中的`/usr/local/nebula/logs/audit/audit.log`为{{nebula.name}}的默认审计日志文件(`audit.log`)的路径,如果日志路径不同,需根据实际情况修改配置文件中的路径。以下为示例配置文件中各个参数的解释:

|参数|说明|
|:--|:--|
|`daily`| 每天轮转日志。可用的时间单位有:`hourly``daily``weekly``monthly``yearly`|
|`rotate 5`| 在删除前日志文件前,其被轮转的次数。即保留最近生成的 5 个日志文件。|
|`copytruncate`| 将当前日志文件复制一份,然后清空当前日志文件。|
|`nocompress`| 不压缩旧的日志文件。|
|`missingok`| 如果日志文件丢失,不报告错误。|
|`notifempty`| 如果日志文件为空,不进行轮转。|
|`create 644 root root`| 创建新的日志文件,并设置适当的权限和所有者。|
|`dateext`| 在日志文件名中添加日期后缀。<br\>默认是当前日期。默认是`-%Y%m%d`的后缀。可用`dateformat`选项扩展配置。|
|`dateformat .%Y-%m-%d-%s`| 必须配合`dateext`使用,紧跟在下一行出现,定义文件切割后的文件名。<br\>在V3.9.0 之前,只支持`%Y``%m``%d``%s`参数。在V3.9.0 及之后,支持 %H 参数。<br\>**注意**:文件名称中禁止出现分号(:) ,因为文件名称不能含有分号。|
|`maxsize 1k`| 当日志文件大小超过`1`千字节(`1024`字节)或者超过设定的周期(如`daily`)时,进行日志轮转。可用的大小单位有:`k``M`,默认单位为字节。|

用户可以根据实际需求修改配置文件中的参数。更多关于参数的配置及解释,参见 [logrotate](https://man7.org/linux/man-pages/man8/logrotate.8.html)。

3. 测试 logrotate 配置。

为了验证 logrotate 的配置是否正确,可以使用以下命令来进行测试:

```bash
sudo logrotate --debug /etc/logrotate.d/audit
```

4. 运行 logrotate。

尽管`logrotate`通常由 Cron 作业自动执行,但也可以手动执行以下命令,以立即进行日志轮转:

```bash
sudo logrotate -fv /etc/logrotate.d/audit
```

`-fv``f`表示强制执行,`v`表示打印详细信息。

5. 查看日志轮转结果。

日志轮转后,会在`/usr/local/nebula/logs/audit`目录下看到新的日志文件,例如`audit.log.2022-04-07-1649298693`。原始日志内容会被清空,但文件会被保留,新日志继续写入。当日志数量超过`rotate`值时,最旧的日志将被删除。

当日志文件数量超过`rotate`设置的值时,会删除最旧的日志文件。例如,`rotate 5`表示保留最近生成的 5 个日志文件,当日志文件数量超过 5 个时,会删除最旧的日志文件。

```bash
[test@test audit]$ ll
-rw-r--r-- 1 root root 0 10月 12 11:15 audit.log
-rw-r--r-- 1 root root 1436 10月 11 19:38 audit.log-202310111697024305 # 保留的日志文件中最旧的一个,当日志文件数量超过设定数量 5 时,会删除该文件。
-rw-r--r-- 1 root root 286 10月 12 11:05 audit.log-202310121697079901
-rw-r--r-- 1 root root 571 10月 12 11:05 audit.log-202310121697079940
-rw-r--r-- 1 root root 571 10月 12 11:14 audit.log-202310121697080478
-rw-r--r-- 1 root root 571 10月 12 11:15 audit.log-202310121697080536
[test@test audit]$ ll
-rw-r--r-- 1 root root 571 10月 12 11:18 audit.log
-rw-r--r-- 1 root root 286 10月 12 11:05 audit.log-202310121697079901
-rw-r--r-- 1 root root 571 10月 12 11:05 audit.log-202310121697079940
-rw-r--r-- 1 root root 571 10月 12 11:14 audit.log-202310121697080478
-rw-r--r-- 1 root root 571 10月 12 11:15 audit.log-202310121697080536
-rw-r--r-- 1 root root 571 10月 12 11:17 audit.log-202310121697080677 # 新生成的日志文件。
```

## 视频

* [{{nebula.name}}的审计日志](https://www.bilibili.com/video/BV17F41157JB)(3 分 53 秒)
Expand Down

0 comments on commit 8e7db95

Please sign in to comment.