
EEM(Embedded Event Manager)作為一個自動化的腳本部署在設備上,可以根據指定的trigger來自動完成提前布置的任務,
如信息的收集或特定的操作。一個完整的EEM需要包含Name、Trigger、Action,以下為EEM部署的示例,僅供參考。
注意:Catalyst 9000 需要思科DNA許可證才能支持EEM。
-
基本構成
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的字段,如果有,則打印匹配結果。
-
配置示例
當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設備,如果不是,則主備切換。







































