内存/CPU 达到阈值时收集信息
内存/CPU 达到阈值时收集信息
简介
当 FortiGate CPU 利用率达到阈值或设备内存占用过高进入保护模式时,客户有可能为了快速恢复业务而重启设备,重启完成后,可以收集到的故障信息较少,难以定位。
自动化功能可以在 FortiGate CPU 利用率达到阈值或内存进入保护模式时,触发相关的 CLI 命令(CLI script action)来收集故障时的设备状态信息,以方便技术人员进行问题定位。
重要
CLI 类型的动作(Action)可以收集的信息输出结果最大支持 192000 个字符,超过 192000 个字符的内容会被截断。
CLI script action 配置
config system automation-action
edit <name>
set action-type cli-script
set output-size <integer>
set timeout <integer>
next
endoutput-size <integer>:最大输出大小。单位为 megabytes,范围 1 ~ 1024,默认为 10。timeout <integer>:执行 CLI 的超时时间,超过这个时间 CLI 未执行完成,则结束 action。单位为秒,范围 0 ~ 300,默认为 0,表示持续等待 CLI 执行完成。
内存保护模式
网络需求
当 FortiGate 进入内存保护模式后,触发 CLI 动作收集设备当前的运行信息,并将信息发送至指定邮箱。
配置步骤
内存保护模式阈值配置。
config system global set memory-use-threshold-extreme <percent> set memory-use-threshold-green <percent> set memory-use-threshold-red <percent> endmemory-use-threshold-red:进入内存保护模式的阈值,默认为 88%。memory-use-threshold-extreme:内存保护的 extreme 模式,新建会话会被丢弃,默认为 95%。memory-use-threshold-green:退出内存保护模式的低阈值,默认为 82%。
首先配置告警邮件发送的配置(参考 日志 → 邮件告警章节,也可使用默认配置)。
进入 FortiGate 的“Security Fabric → 自动化 → 工作流”页面,点击新建按钮,新建一个工作流。

配置工作流的名称,点击“添加触发器”按钮,在右侧的“选择条目”中点击“新建”按钮。

在右侧弹出的窗口中,找到“系统”分类,点击“保护模式”选项。

配置触发器(Trigger)的名称,点击确认下发配置。

config system automation-trigger edit "Conserve_Mode" set event-type low-memory next end在选择条目中选择上步创建的触发器(Trigger),点击应用按钮。

点击“添加动作”按钮,在右侧弹出的“选择条目”中点击“新建”按钮。

在右侧弹出的窗口中,找到“通用”分类,点击“CLI 脚本”选项。

配置动作(Action)的名称,配置 CLI 脚本内容为 FortiGate 进入内存保护模式后收集信息需要执行的 CLI 命令,点击确认下发配置。

config system automation-action
edit "Conserve_Mode_Status_Collect"
set action-type cli-script
set script "diagnose sys top 5 99 3
diagnose sys session full-stat
get system performance status
diagnose sys session full-stat
diagnose hardware sysinfo shm
diagnose hardware sysinfo memory
diagnose hardware sysinfo slab
diagnose debug crashlog read"
set accprofile "super_admin"
next
end- 在选择条目中选择上步创建的动作(Action),点击应用按钮。

再次点击“添加动作”按钮,在右侧弹出的“选择条目”中点击“新建”按钮。

在右侧弹出的窗口中,找到“通知”分类,点击“Email”选项。

配置动作(Action)的名称,以及邮件通知的收件人、邮件标题,邮件内容填写
%%results%%,这是一个变量,表示使用前序动作 CLI 命令执行的结果作为邮件正文,点击确认下发配置。
- 最小间隔:如果 Trigger 频繁触发了,Action 执行的最小间隔。
- 源:指定邮件通知的发件人,默认为 DoNotReply@notification.fortinet.net。
- 目的:配置邮箱的收件人,可以配置多个收件人。
- Body:邮件内容。
- 更换信息:可以更换为自定义内容和格式。
config system automation-action edit "Email_Conserve_Mode" set action-type email set email-to "bbai@fortinet.com" set email-subject "FortiGate has entered conserve mode!" set message "%%results%%" next end在选择条目中选择上步创建的动作(Action),点击应用按钮。

检查无误后,点击确认下发配置。

config system automation-stitch edit "Conserve_Mode" set trigger "Conserve_Mode" config actions edit 1 set action "Conserve_Mode_Status_Collect" set required enable next edit 2 set action "Email_Conserve_Mode" set required enable next end next end
配置验证
触发 FortiGate 进入内存保护模式(Conserve Mode)。
触发自动化程序,相关自动化日志如下。

date=2024-03-14 time=16:29:50 eventtime=1710404989676485934 tz="+0800" logid="0100046600" type="event" subtype="system" level="notice" vd="root" logdesc="Automation stitch triggered" stitch="Conserve_Mode" trigger="Conserve_Mode" stitchaction="Conserve_Mode_Status_Collect,Email_Conserve_Mode" from="log" msg="stitch:Conserve_Mode is triggered." date=2024-03-14 time=16:29:49 eventtime=1710404989471003340 tz="+0800" logid="0100022011" type="event" subtype="system" level="critical" vd="root" logdesc="Memory conserve mode entered" service="kernel" conserve="on" total=1487 MB used=1118 MB red="1115 MB" green="1040 MB" msg="Kernel enters memory conserve mode"收件人邮箱收到了告警邮件,邮件内容为通过 CLI 动作收集到的设备状态信息,发件人为默认的 DoNotReply@notification.fortinet.net。

对应的自动化触发 Debug 信息。
FortiGate # diagnose debug application autod -1 FortiGate # diagnose debug enable __action_cli_script_open()-186: cli script action:Conserve_Mode_Status_Collect is called. svc ctx:0xfc044a0 accprof:super_admin script: diagnose sys top 5 99 3 diagnose sys session full-stat get system performance status diagnose sys session full-stat diagnose hardware sysinfo shm diagnose hardware sysinfo memory diagnose debug crashlog read __cli_script_close()-121: cli script: autod.2 output: ########## script name: autod.2 ########## ========== #1, 2024-03-14 16:35:13 ========== Spoke1 $ diagnose sys top 5 99 3 Run Time: 0 days, 0 hours and 13 minutes 0U, 0N, 0S, 100I, 0WA, 0HI, 0SI, 0ST; 1487T, 217F autod 1988 S 1.0 1.7 5 ipshelper 1968 S < 0.5 3.4 7 forticron 1939 S 0.5 2.6 7 ipsengine 2441 S < 0.5 2.2 0 ipsengine 2439 S < 0.5 2.1 4 ...... __action_cli_script_close()-213: cli script action is done. script: diagnose sys top 5 99 3 diagnose sys session full-stat get system performance status diagnose sys session full-stat diagnose hardware sysinfo shm diagnose hardware sysinfo memory diagnose debug crashlog read output: ########## script name: autod.2 ########## ========== #1, 2024-03-14 16:35:13 ========== Spoke1 $ diagnose sys top 5 99 3 Run Time: 0 days, 0 hours and 13 minutes 0U, 0N, 0S, 100I, 0WA, 0HI, 0SI, 0ST; 1487T, 217F autod 1988 S 1.0 1.7 5 ipshelper 1968 S < 0.5 3.4 7 forticron 1939 S 0.5 2.6 7 ipsengine 2441 S < 0.5 2.2 0 ipsengine 2439 S < 0.5 2.1 4 ...... pid:1988-__handle_msg()-414: Subscriber:4 received package. pubid:2 pkgid:73 pkg_index:0 pid:1988-__handle_pkg_logs()-356: Subscriber:4 processing package size:516 logs:1 pickup:1 auto_event_pub_handle_log()-331: publish 32550 to subscr-node pid:1988-__subscr_close_cur_pkg()-140: close package size:516 logs:1 __action_email_hdl()-181: email action (Email_Conserve_Mode) is called. from: to:bbai@fortinet.com; subject:FortiGate has entered conserve mode!发送邮件的 Debug 信息如下。
FortiGate # diagnose debug application alertmail -1 FortiGate # diagnose debug enable Arrived msg(type 9, 46668 bytes):bbai@fortinet.com; FortiGate has entered conserve mode! ########## script name: autod.3 ########## ========== #1, 2024-03-14 16:35:13 ========== Spoke1 $ diagnose sys top 5 99 3 Run Time: 0 days, 0 hours and 13 minutes 0U, 0N, 0S, 100I, 0WA, 0HI, 0SI, 0ST; 1487T, 217F autod 1988 S 1.0 1.7 5 ipshelper 1968 S < 0.5 3.4 7 forticron 1939 S 0.5 2.6 7 ipsengine 2441 S < 0.5 2.2 0 ipsengine 2439 S < 0.5 2.1 4 ...... mail_info: from:notification.fortinet.net user:DoNotReply@notification.fortinet.net mail_info: reverse path:DoNotReply@notification.fortinet.net user name:DoNotReply to[0]:bbai@fortinet.com <==_init_mail_info create session resolve notification.fortinet.net to 1 IP ==> send mail connecting to 208.91.114.151 port 465 send mail 0xfb2cbb0 session 0xfb315e0 session_io_event: creating ssl structure for session 0xfb315e0 create_ssl_ctx create_ssl: 0x7f99cedbd000 sessionn 0xfb315e0, SSL connected session: 0xfb315e0, rsp_state: greeting, code: 220 session: 0xfb315e0, rsp_state: ehlo, code: 250 session: 0xfb315e0, rsp_state: mail, code: 250 session: 0xfb315e0, rsp_state: rcpt, code: 250 session: 0xfb315e0, rsp_state: data, code: 354 === send: ########## script name: autod.3 ########## ========== #1, 2024-03-14 16:35:13 ========== Spoke1 $ diagnose sys top 5 99 3 Run Time: 0 days, 0 hours and 13 minutes 0U, 0N, 0S, 100I, 0WA, 0HI, 0SI, 0ST; 1487T, 217F autod 1988 S 1.0 1.7 5 ipshelper 1968 S < 0.5 3.4 7 forticron 1939 S 0.5 2.6 7 ipsengine 2441 S < 0.5 2.2 0 ipsengine 2439 S < 0.5 2.1 4 ...... 1session: 0xfb315e0, rsp_state: data2, code: 250 session: 0xfb315e0, rsp_state: quit, code: 221 session finined _session_on_destroy <== send mail success, m = 0xfb2cbb0 s = 0xfb315e0
平均 CPU 使用率高
网络需求
当 FortiGate 的 CPU 多个核心的平均利用率达到设定的阈值后,触发 CLI 动作收集设备当前的运行信息,并将信息发送至指定邮箱。
配置步骤
CPU 阈值配置。
config system global set cpu-use-threshold 85 endcpu-use-threshold:CPU 所有核心的平均利用率阈值,默认为 90%。
首先配置告警邮件发送的配置(参考 日志 → 邮件告警章节,也可使用默认配置)。
进入 FortiGate 的“Security Fabric → 自动化 → 工作流”页面,点击新建按钮,新建一个工作流。

配置工作流的名称,点击“添加触发器”按钮,在右侧的“选择条目”中点击“新建”按钮。

在右侧弹出的窗口中,找到“系统”分类,点击“高 CPU 使用率”选项。

配置触发器(Trigger)的名称,点击确认下发配置。

config system automation-trigger edit "CPU_High" set event-type high-cpu next end在选择条目中选择上步创建的触发器(Trigger),点击应用按钮。

点击“添加动作”按钮,在右侧弹出的“选择条目”中点击“新建”按钮。

在右侧弹出的窗口中,找到“通用”分类,点击“CLI 脚本”选项。

配置动作(Action)的名称,配置 CLI 脚本内容为 FortiGated 的 CPU 利用率达到阈值后收集信息需要执行的 CLI 命令,点击确认下发配置。

config system automation-action
edit "CPU_High_Status_Collect"
set action-type cli-script
set script "diagnose sys cmdb info
diagnose sys vd list | grep fib
diagnose sys top 5 99 3
diagnose sys session full-stat
diagnose sys session list | grep \"\\<dirty\\>\" -c
get system performance status
diagnose sys session full-stat
diagnose hardware sysinfo memory"
set accprofile "super_admin"
next
end在选择条目中选择上步创建的动作(Action),点击应用按钮。

再次点击“添加动作”按钮,在右侧弹出的“选择条目”中点击“新建”按钮。

在右侧弹出的窗口中,找到“通知”分类,点击“Email”选项。

配置动作(Action)的名称,以及邮件通知的收件人、邮件标题,邮件内容填写
%%results%%,这是一个变量,表示使用前序动作 CLI 命令执行的结果作为邮件正文,点击确认下发配置。
- 最小间隔:如果 Trigger 频繁触发了,Action 执行的最小间隔。
- 源:指定邮件通知的发件人,默认为 DoNotReply@notification.fortinet.net。
- 目的:配置邮箱的收件人,可以配置多个收件人。
- Body:邮件内容。
- 更换信息:可以更换为自定义内容和格式。
config system automation-action edit "Email_CPU_High" set action-type email set email-to "bbai@fortinet.com" set email-subject "FortiGate CPU High!" set message "%%results%%" next end在选择条目中选择上步创建的动作(Action),点击应用按钮。

检查无误后,点击确认下发配置。

config system automation-stitch edit "CPU_High" set trigger "CPU_High" config actions edit 1 set action "CPU_High_Status_Collect" set required enable next edit 2 set action "Email_CPU_High" set required enable next end next end
配置验证
触发 FortiGate 的 CPU 利用率达到设定的阈值(此例中的 FortiGate CPU 高阈值设置为 85%)。
触发自动化程序,相关自动化日志如下。

date=2024-03-14 time=17:35:07 eventtime=1710408906768114390 tz="+0800" logid="0100046600" type="event" subtype="system" level="notice" vd="root" logdesc="Automation stitch triggered" stitch="CPU_High" trigger="CPU_High" stitchaction="CPU_High_Status_Collect,Email_CPU_High" from="log" msg="stitch:CPU_High is triggered." date=2024-03-14 time=17:35:06 eventtime=1710408897701912754 tz="+0800" logid="0100040705" type="event" subtype="system" level="notice" vd="root" logdesc="CPU usage statistics" action="cpu-usage" cpu=88 msg="CPU usage reaches: 88"收件人邮箱收到了告警邮件,邮件内容为通过 CLI 动作收集到的设备状态信息,发件人为默认的 DoNotReply@notification.fortinet.net。

查看自动化触发 Debug 信息(略,参考内存保护模式自动化的 Debug 信息)。
FortiGate # diagnose debug application autod -1 FortiGate # diagnose debug enable查看发送邮件的 Debug 信息(略,参考内存保护模式自动化动作的邮件告警 Debug 信息)。
FortiGate # diagnose debug application alertmail -1 FortiGate # diagnose debug enable
单核 CPU 使用率高
重要
该功能在 7.2.4 及以上版本支持。
网络需求
很多情况下,FortiGate 的某个 CPU 持续高也会影响到正常运行,这种情况下是无法触发上个章节中平均 CPU 高触发器的,此章节可以实现单个 CPU 核利用率超过配置的阈值时,触发 CLI 动作收集设备当前的运行信息,并将信息发送至指定邮箱。
配置步骤
首先配置告警邮件发送的配置(参考 日志 → 邮件告警章节,也可使用默认配置)。
在 FortiGate 的 CLI 中开启日志记录单核 CPU 高的功能,开启后,
cpu-use-threshold也会对单个 CPU 核心生效。config system global set log-single-cpu-high enable set cpu-use-threshold 85 end产生的日志示例如下,Log ID 为 40707。
date=2024-03-15 time=14:08:23 eventtime=1710482896991349328 tz="+0800" logid="0100040707" type="event" subtype="system" level="notice" vd="root" logdesc="CPU single core usage statistics" action="cpu-single-core-usage" core=1 cpu=97 msg="CPU core 1 usage reaches: 97"进入 FortiGate 的“Security Fabric → 自动化 → 工作流”页面,点击新建按钮,新建一个工作流。

配置工作流的名称,点击“添加触发器”按钮,在右侧的“选择条目”中点击“新建”按钮。

在右侧弹出的窗口中,找到“其他条件”分类,点击“FortiOS 事件日志”选项。

配置触发器(Trigger)的名称,FortiOS 事件日志中选择“CPU single core usage statistics”,点击确认下发配置。

config system automation-trigger edit "Single_CPU_High" set event-type event-log set logid 40707 next end在选择条目中选择上步创建的触发器(Trigger),点击应用按钮。

点击“添加动作”按钮,在右侧弹出的“选择条目”中点击“新建”按钮。

在右侧弹出的窗口中,找到“通用”分类,点击“CLI 脚本”选项。

配置动作(Action)的名称,配置 CLI 脚本内容为 FortiGated 的 CPU 利用率达到阈值后收集信息需要执行的 CLI 命令,点击确认下发配置。
重要
如果已经确定是哪个 CPU 核心持续高,但出现该单核高的时间不确定,可以在上个章节收集的 CLI 命令后加入如下命令来查看该 CPU 代码级别的 Debug 信息(由于前 2 条命令和后 2 条命令中间需要 1 分钟的收集间隔,需要 2 个 Action 来完成所有的信息收集):
diag sys profile cpumask 0 //抓取特定的CPU核心,这里以CPU核心0为例// diag sys profile start //开启profile程序,等待1分钟,再执行后续命令// diag sys profile stop diag sys profile show order
config system automation-action edit "CPU_High_Status_Collect" set action-type cli-script set script "diagnose sys cmdb info diagnose sys vd list | grep fib diagnose sys top 5 99 3 diagnose sys session full-stat diagnose sys session list | grep "\<dirty\>" -c get system performance status diagnose sys session full-stat diagnose hardware sysinfo memory" --------以下CLI在确认哪个CPU核心高的情况下可用(CLI脚本不包含此行)-------- diag sys profile cpumask 0 //抓取特定的CPU核心,这里以CPU核心0为例// diag sys profile start //开启profile程序,等待1分钟,再执行后续命令// --------以上CLI在确认哪个CPU核心高的情况下可用(CLI脚本不包含此行)-------- set accprofile "super_admin" next end在选择条目中选择上步创建的动作(Action),点击应用按钮。

再次点击“添加动作”按钮,在右侧弹出的“选择条目”中点击“新建”按钮。

在右侧弹出的窗口中,找到“通知”分类,点击“Email”选项。

配置动作(Action)的名称,以及邮件通知的收件人、邮件标题,邮件内容填写
%%results%%,这是一个变量,表示使用前序动作 CLI 命令执行的结果作为邮件正文,点击确认下发配置,发送第一个 CLI Action 执行的输出结果。
- 最小间隔:如果 Trigger 频繁触发了,Action 执行的最小间隔。
- 源:指定邮件通知的发件人,默认为 DoNotReply@notification.fortinet.net。
- 目的:配置邮箱的收件人,可以配置多个收件人。
- Body:邮件内容。
- 更换信息:可以更换为自定义内容和格式。
config system automation-action edit "Email_Single_CPU_High" set action-type email set email-to "bbai@fortinet.com" set email-subject "FortiGate single CPU high!" set message "%%results%%" next end在选择条目中选择上步创建的动作(Action),点击应用按钮。

再次点击“添加动作”按钮,在右侧弹出的“选择条目”中点击“新建”按钮。
重要
参考步骤 11,在确认是哪个 CPU 核心高,但高的时间随机的情况下,才执行此步,否则跳过 17 ~ 25 步。

在右侧弹出的窗口中,找到“通用”分类,点击“CLI 脚本”选项。

配置动作(Action)的名称,配置 CLI 脚本内容为结束 CPU profile 信息收集并打印统计结果,点击确认下发配置。

config system automation-action edit "Single_CPU_High_2" set action-type cli-script set script "diag sys profile stop diag sys profile show order" set accprofile "super_admin" next end在选择条目中选择上步创建的动作(Action),点击应用按钮。

在第 2 个 Action 和第 3 个 Action 之间点击“Add delay”,添加延迟为 60 秒,点击确认按钮下发配置。

再次点击“添加动作”按钮,在右侧弹出的“选择条目”中点击“新建”按钮。

在右侧弹出的窗口中,找到“通知”分类,点击“Email”选项。

配置动作(Action)的名称,以及邮件通知的收件人、邮件标题,邮件内容填写
%%results%%,这是一个变量,表示使用前序动作 CLI 命令执行的结果作为邮件正文,点击确认下发配置。
- 最小间隔:如果 Trigger 频繁触发了,Action 执行的最小间隔。
- 源:指定邮件通知的发件人,默认为 DoNotReply@notification.fortinet.net。
- 目的:配置邮箱的收件人,可以配置多个收件人。
- Body:邮件内容。
- 更换信息:可以更换为自定义内容和格式。
config system automation-action edit "Email_Single_CPU_High" set action-type email set email-to "bbai@fortinet.com" set email-subject "FortiGate single CPU high!" set message "%%results%%" next end在选择条目中选择上步创建的动作(Action),点击应用按钮。

检查无误后,点击确认下发配置。
重要
参考步骤 11,在确认是哪个 CPU 核心高,但高的时间随机的情况下,才配置第 3 和第 4 个 Action。

config system automation-stitch edit "Single_CPU_High" set trigger "Single_CPU_High" config actions edit 1 set action "Single_CPU_High_1" set required enable next edit 2 set action "Email_Single_CPU_High" set required enable next edit 3 set action "Single_CPU_High_2" set delay 60 set required enable next edit 4 set action "Email_Single_CPU_High_1" set required enable next end next end
配置验证
触发 FortiGate 的 CPU 核心 0 利用率达到设定的阈值(此例中的 FortiGate CPU 高阈值设置为 85%)。
触发自动化程序,相关自动化日志如下。

date=2024-03-15 time=15:56:50 eventtime=1710489409476085970 tz="+0800" logid="0100046600" type="event" subtype="system" level="notice" vd="root" logdesc="Automation stitch triggered" stitch="Single_CPU_High" trigger="Single_CPU_High" stitchaction="Single_CPU_High_1,Email_Single_CPU_High,Single_CPU_High_2,Email_Single_CPU_High_1" from="log" msg="stitch:Single_CPU_High is triggered." date=2024-03-15 time=15:56:49 eventtime=1710489408805031532 tz="+0800" logid="0100040707" type="event" subtype="system" level="notice" vd="root" logdesc="CPU single core usage statistics" action="cpu-single-core-usage" core=1 cpu=88 msg="CPU core 1 usage reaches: 88"收件人邮箱收到了 2 封告警邮件,第 1 封为除 CPU profile 统计信息外的收集信息,第 2 封为 CPU profile 的统计信息,发件人为默认的 DoNotReply@notification.fortinet.net。

查看自动化触发 Debug 信息(略,参考内存保护模式自动化的 Debug 信息)。
FortiGate # diagnose debug application autod -1 FortiGate # diagnose debug enable查看发送邮件的 Debug 信息(略,参考内存保护模式自动化动作的邮件告警 Debug 信息)。
FortiGate # diagnose debug application alertmail -1 FortiGate # diagnose debug enable
注意事项
如果 FortiGate 的内存保护模式已经进入了 Extreme 状态,可能无法完成相关 CLI 命令的信息收集。