Catalyst 9000上EEM的部署方法和示例
发布时间:2023-02-06

EEM(Embedded Event Manager)作为一个自动化的脚本部署在设备上,可以根据指定的trigger来自动完成提前布置的任务,
如信息的收集或特定的操作。一个完整的EEM需要包含Name、Trigger、Action,以下为EEM部署的示例,仅供参考。

 

注意:Catalyst 9000 需要思科DNA许可证才能支持EEM。

 

  • 基本构成
     

01 EEM Name

event manager applet MY-SCRIPT authorization bypass <<<<< 创建一个名字叫MY-SCRIPT的EEM脚本。
使用"authorization bypass"命令来规避权限问题,否则可能由于权限问题导致EEM无法生效。


02 EEM Trigger

a. 周期触发:

  event timer watchdog time 120 maxrun 100 <<<<< 每 120 秒运行一次,每次最多运行 100 秒。

 

b. 特定时间或特定周期触发

   event timer cron cron-entry "1 2 3 4 5" <<<<< 1=分 2=时 3=日 4=月 5=星期(0表示周日)

   Cron Timer Examples:

   "15 9 * * *" <<<<< 每天上午的9点15分触发

   "00 10 * * 1-5" <<<<< 每周一到周五的上午10点触发

 

c. 通过SNMP触发

  event snmp oid 1.3.6.1.4.1.9.2.1.56 get-type next entry-op ge entry-val 50 poll-interval 1

  当CPU超过50%时触发, 1.3.6.1.4.1.9.2.1.56 为 Cat9000 的SNMP CPU OID,其他平台需对应修改。


d. 根据系统日志来触发:

   event syslog pattern "NOPOWER_AVAIL: Interface" ratelimit 60

  当出现的日志中包含"NOPOWER_AVAIL: Interface"关键字后触发,ratelimit为每60s内只触发一次。


e. 系统自带的参数:

   如当接口的total output 增长时触发:

 event interface name Te1/0/1 parameter output_packets_dropped entry-op ge entry-val 1000 poll-interval 1     entry-type value


03 EEM Action

a. CLI Action

   按顺序执行指令,注意所有序号需保持同样位数,如010和100,而不能写10和100.

   action 010 cli command "en"

 action 020 cli command "show version | append bootflash:EEM.txt"

 

b. 生成系统日志:

   action 050 syslog msg "Script has completed" priority 1 <<<<<< 将生成1级日志: "%HA_EM-1-LOG: xxx: Script has    completed"

 

c. 交互式CLI:

   action 430 cli command "clear log" pattern "confirm"

   action 431 cli command ""

   当执行clear log后,系统会出现交互式语句,匹配关键字confirm后输入"",相当于确认进行下一步。

 

d. 匹配SNMP的信息:

   action 010 info type snmp oid 1.3.6.1.4.1.9.2.1.56 get-type next

   action 020 syslog msg "Current CPU: $_info_snmp_value" <<<<< 系统收集当前OID的结果,并通过变量打印至日志中

 

e. If / Else / Elseif 语句

   如果CPU 大于等于 85,则执行“end”之前的语句,否则执行“end”之后的语句。

   action 010 info type snmp oid 1.3.6.1.4.1.9.2.1.56 get-type next

   action 020 if $_info_snmp_value ge "85"

   action 030 syslog msg "High CPU detected inside script!"

   action 040 end

  action 050 syslog msg "This line is now outside the –if- statement"

 

f. 循环语句

   一个循环可以一遍又一遍地做同样的事情而不用写很多额外的行,如打印1到10:

   action 010 set loop_iteration 1 <<<<< 设置变量"loop_iteration" 为 1.

   action 020 while $loop_iteration le 10

   action 030 syslog msg "Iteration: $loop_iteration"

   action 040 increment loop_iteration 1

   action 050 end

   action 060 syslog msg "Script Complete"

 

g. 正则表达式匹配

   正则表达式通常可以应用于特定命令的结果。Regex 还会在调用后创建一些预定义的变量。

   action 010 cli command "show ip interface brief | ex unass"

   action 020 regexp "[123]" $_cli_result match

   action 030 if $_regexp_result eq 1

   action 040 syslog msg "The Regex found 1 or 2 or 3 in $match"

   action 050 end

   在show ip interface brief 中搜索是否存在1或2或3的字段,如果有,则打印匹配结果。

 

  • 配置示例
     

01 使用SNMP监控来触发high cpu的信息收集

当CPU无规律瞬间变高时,我们可以使用如下脚本(OID为catalyst 9000的OID,其他平台需要对象修改)。

event manager applet High_CPU authorization bypass

event snmp oid 1.3.6.1.4.1.9.2.1.56 get-type next entry-op ge entry-val 80 poll-interval 1 ratelimit 60

action 010 info type snmp oid 1.3.6.1.4.1.9.2.1.56 get-type next

action 020 syslog msg "CPU Utilization is high, Current CPU: $_info_snmp_value"

action 030 cli command "enable"

action 040 cli command "show clock"

action 041 regex "([0-9]|[0-9][0-9]):([0-9]|[0-9][0-9]):([0-9]|[0-9][0-9])" $_cli_result match H M S <<<<将匹配表达式中的小时,分钟和秒

action 050 set time $match

action 060 cli command "show clock | append flash:CPU_info$time.txt"

action 070 cli command "show process cpu sorted | append flash:CPU_info$time.txt"

......

action 190 cli command "monitor capture A control-plane in"

action 200 cli command "monitor capture A file location flash:CPU_$H.$M.$S.pcap" pattern "confirm"

action 201 cli command ""

action 210 cli command "monitor capture A start"

action 220 wait 10

action 230 cli command "monitor capture A stop"

!注意: Cat9200需要monitor capture stop之后

运用"monitor capture A export flash:CPU_$H.$M.$S.pcap" pattern "confirm"来将抓包文件保存到机器里面

 

当CPU超过80%时,将生成以下日志:

%HA_EM-6-LOG: test: CPU Utilization is high, Current CPU: 80

 

同时将生成基于时间的文件,例如以下:

switch#dir flash: | i CPU

409606 -rw- 296 Apr 24 2022 12:08:54 +00:00 CPU_12.08.51.pcap <<<抓包文件

262188 -rw- 49487 Apr 24 2022 12:02:38 +00:00 CPU_info12:02:38.txt <<


02 应用正则表达式来监控设备上的变量

当需要监控非系统自定义的参数时,可以使用正则表达式来监控,如以下方式监控TCAM的使用情况:

event manager applet TCAM authorization bypass

event timer watchdog time 3600

action 10 cli command "enable"

action 20 cli command "show platform hardware fed active fwd-asic resource tcam utilization"

action 30 regex "[(6-9)][(0-9)]\.[(0-9)][(0-9)]" $_cli_result

action 40 if $_regexp_result ge "1"

action 50 syslog msg " ***TCAM abnormal detected***" priority 1

action 60 syslog msg "$_cli_result"

action 70 else

action 80 syslog msg "*** TCAM normal ***"

action 90 end

 

当TCAM的利用率超过60%时,将出现告警,同时会将TCAM的具体值通过log打印:

%HA_EM-1-LOG: TCAM: ***TCAM abnormal detected***

Cat9500#$ ac fwd-asic resource tcam utilization

Table Subtype Dir Max Used %Used V4 V6 MPLS Other

----------------------------------------------------------------------------

Mac Address Table EM I 65536 31 60.05% 0 0 0 31


03 使用循环语句和自定义变量来配置EEM

当我们需要配置大量的重复参数时可以使用以下方法来配置:

event manager applet Port authorization bypass

event timer watchdog time 300 maxrun 200

action 100 cli command "en"

action 200 set PORT "1" <<<<< 设置一个变量名为PORT,同时将变量值置为1

action 201 while $PORT le 24 <<<<< 设置循环语句,使得变量小于等于24

action 202 cli command "show interface G1/0/$PORT"

action 204 regexp "err-disabled" "$_cli_result"

action 205 if $_regexp_result eq "1"

action 206 syslog msg "*** Issue detected at PORT_$PORT is err-disabled ***"

action 208 cli command "configure terminal"

action 209 cli command "interface G1/0/$PORT"

action 210 cli command "shutdown"

action 211 cli command "no shutdown"

action 212 cli command "end"

action 231 syslog msg " *** PORT_$PORT is Recovery ***"

action 242 else

action 253 syslog msg "*** No issue detected! PORT_$PORT ***"

action 300 increment PORT

action 301 wait 1

action 403 end

action 404 end

 

设备每300s自动检测接口1-24接口的状态,如果出现err-disabled, 则自动执行shutdown/no shutdown的操作。


04 交互式EEM

当执行一些需要交互的操作时,可能使用到以下方法:

event manager applet Reload authorization bypass

event timer cron cron-entry "1 0 * * *" <<<<< 每天的0:01分自动执行

action 010 cli command "enable"

action 020 cli command "show switch | i aaaa.bbbb.cccc"

action 030 regexp "Active" $_cli_result

action 040 if $_regexp_result eq "1"

action 050 syslog msg "*** Switch 1 is Active ***"

action 070 else

action 080 cli command "wr"

action 090 wait 10

action 100 cli command "redundancy force-switchover" pattern "confirm"

action 110 cli command ""

action 120 end

 

每天的0:01分自动检测aaaa.bbbb.cccc是否为active设备,如果不是,则主备切换。

填写您的信息获取IT方案
在线咨询
需求提交
请您填写下列信息,并点击提交按钮,耐心等待专家代表的电话回访!
称呼:
电话:
公司:
Email:
QQ:
需求:
400-888-4911
投诉建议
请您认真填写投诉/建议内容,我们将第一时间为您处理!
称呼:
电话:
公司:
Email:
QQ:
建议:
400-888-4911
业务合作
请您填写合作内容,相关工作人员将第一时间与您联系!
称呼:
电话:
公司:
Email:
QQ:
合作内容:
400-888-4911