Need help on EEM (Version 3.0 CLI script)

Answered Question
Jul 27th, 2010
User Badges:

Hi Guys,


I am trying to get the first 15 lines of my interfaces output through EEM.


I have made this script to do that task, but the problem with it is that this script will view only the first 5 lines correctly, then it duplicate the rest of the lines...


here is the configuration:


event manager applet EEM
event syslog pattern "INTERFACE"
action 1.1   cli command "enable"
action 1.2   cli command "show ip int bri"

action 1.3   set i "0"
action 1.4   foreach VAR "$_cli_result" "\n"
action 1.5    if $i gt 10
action 1.6     break
action 1.6.1  end
action 1.7    append OUT "$VAR"
action 1.7.1  increment i
action 1.9    syslog msg "$OUT"
action 2.1   end




Here is the output of the syslog $OUT




TPT_HO_KhobarR01#send log INTERFACE
TPT_HO_KhobarR01#
%SYS-2-LOGMSG: Message from 514(nourmaint): INTERFACE
%HA_EM-6-LOG: EEM:
Interface                  IP-Address      OK? Method Status                Protocol
FastEthernet0/0            172.30.1.22     YES NVRAM  up                    up     
FastEthernet0/1            unassigned      YES NVRAM  administratively down down   
FastEthernet0/0/0          unassigned      YES unset  up                    up     
FastEthernet0/0/1          unassigned      YES unset  up                    up     
FastEthernet0/0/2          unassigned      YES unset  up                    up     
FastEthernet0/0/2          unassigned      YES unset  up                    up     
FastEthernet0/0/2          unassigned      YES unset  up                    up     
FastEthernet0/0/2          unassigned      YES unset  up                    up     
FastEthernet0/0/2          unassigned      YES unset  up                    up     
FastEthernet0/0/2          unassigned      YES unset  up                    up    



I wish someone could direct me to the right solution.

Correct Answer by Joe Clarke about 6 years 10 months ago

The break statement has no effect on an if clause.  The break statement causes the immediate loop

to be terminated.  It does NOT terminate the script execution.  I did test the script as I have written on 15.0(1)M, and it works exactly as I expect.  That said, I did file a bug a while back after I noticed the break statement was not working as expected.  See CSCtb37673.  This bug affects 12.4(24)T, and causes break to actually terminate the execution of the applet.  Using continue is not the proper workaround in this case.  To workaround this bug, you need to use "goto."


If you are running an affected version of code, then this applet should do what you want:


action 1.3   set i "0"<br/>action 1.4.1 foreach VAR "$_cli_result" "\n"<br/>action 1.4.2  if $i gt 9 goto 1.8<br/>action 1.4.4  end<br/>action 1.5    append OUT "$VAR"<br/>action 1.6    increment i<br/>action 1.7   end<br/>action 1.8 syslog msg "$OUT"<br/>
Correct Answer by Joe Clarke about 6 years 12 months ago

Syslog is a bad choice for this as you can overflow your message.  Consider email instead.  However, you have a bug in your code.  You send the syslog message each time through the loop.  Instead, you want:


event manager applet EEM
event syslog pattern "INTERFACE"
action 1.1   cli command "enable"
action 1.2   cli command "show ip int bri"
action 1.3   set i "0"
action 1.4   foreach VAR "$_cli_result" "\n"
action 1.5    if $i gt 10
action 1.6     break
action 1.6.1  end
action 1.7    append OUT "$VAR"
action 1.7.1  increment i
action 1.8   end
action 1.9   syslog msg "$OUT"
  • 1
  • 2
  • 3
  • 4
  • 5
Overall Rating: 5 (2 ratings)
Loading.
Correct Answer
Joe Clarke Wed, 07/28/2010 - 11:39
User Badges:
  • Cisco Employee,
  • Hall of Fame,

    Founding Member

Syslog is a bad choice for this as you can overflow your message.  Consider email instead.  However, you have a bug in your code.  You send the syslog message each time through the loop.  Instead, you want:


event manager applet EEM
event syslog pattern "INTERFACE"
action 1.1   cli command "enable"
action 1.2   cli command "show ip int bri"
action 1.3   set i "0"
action 1.4   foreach VAR "$_cli_result" "\n"
action 1.5    if $i gt 10
action 1.6     break
action 1.6.1  end
action 1.7    append OUT "$VAR"
action 1.7.1  increment i
action 1.8   end
action 1.9   syslog msg "$OUT"
Mohammed Khair ... Wed, 07/28/2010 - 13:00
User Badges:

Hello Joseph,


Many thanks for your response, I struggled a lot yesterday trying to tweek this EEM script (originally it's based on one of yours ).. without you the EEM wouldn't been as useful as it is now!


I will try to use this on one of my routers with email action.


Best Regards

Mohammed Khair ... Wed, 07/28/2010 - 13:53
User Badges:

Hi Joseph,


I tried this solution and unfortunately its not working still.


Here is the script that I tried:


event manager applet TEST
event syslog pattern "TEST"
action 1.1   cli command "enable"
action 1.2   cli command "show process cpu sorted 5min"
action 1.3   set i "0"
action 1.4.1 foreach VAR "$_cli_result" "\n"
action 1.4.2  if $i gt 9
action 1.4.3   break
action 1.4.4  end
action 1.5    append OUT "$VAR"
action 1.6    increment i
action 1.7   end
action 1.9.5 mail server "10.0.0.46" to "[email protected]" from "[email protected]" subject "TEST 10.0.0.46" body "$OUT" source-interface Loopback0
action 1.9.6 syslog msg "$OUT"




once the execution of actions reach the syslog msg "$output" or send email action, it doesn't execute.. (seems the problem is that the end terminating the whole action process. so that the parser will terminate the execution of the sequence before reaching the syslog msg or mail server action)..


please tell me what could be wrong in it..


Thanks

Mohammed Khair ... Fri, 08/27/2010 - 18:08
User Badges:

You need to use continue to break out of the loop in the IF statement. otherwise your script will be looped and you won't get the right values.

ivan.cavar Sat, 08/28/2010 - 05:55
User Badges:

Do you mean like this?


action 1.3   set i "0"

action 1.4.1 foreach VAR "$_cli_result" "\n"

action 1.4.2  if $i gt 9

action 1.4.3   continue    //instead of break?

action 1.4.4  end

action 1.5    append OUT "$VAR"

action 1.6    increment i

action 1.7   end


But with the break statement it would be more optimized because in this way in needs to go through all the lines of cli_result and with break it would end up after 10th line.

Or not?


Regards

Joe Clarke Sat, 08/28/2010 - 17:28
User Badges:
  • Cisco Employee,
  • Hall of Fame,

    Founding Member

Actually, I don't understand the original comment about the script not working.  The way the loop is structured, you want a break to stop the loop from executing, then fall out to the line that sends the syslog message.  The code you posted will continue to iterate through the loop, but not append further lines to the $OUT variable.  Okay, that works, but it takes longer that it needs.  This should be sufficient:


action 1.3   set i "0"

action 1.4.1 foreach VAR "$_cli_result" "\n"

action 1.4.2  if $i gt 9

action 1.4.3   break

action 1.4.4  end

action 1.5    append OUT "$VAR"

action 1.6    increment i

action 1.7   end

action 1.8 syslog msg "$OUT"


What happens hereis that the loop will operate on the first 10 lines of $_cli_result.  At line 11, the loop will terminate then send a syslog message containing value of the variable $OUT.


But, as I said in my first post, syslog is a bad mechanism for doing this since syslog messages are really supposed to be that large.  Instead, replacing action 1.8 with an email action will ensure the full text is reported.

Mohammed Khair ... Sat, 08/28/2010 - 23:26
User Badges:

/* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin-top:0in; mso-para-margin-right:0in; mso-para-margin-bottom:10.0pt; mso-para-margin-left:0in; line-height:115%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:"Times New Roman"; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:Arial; mso-bidi-theme-font:minor-bidi;}

Hi Joseph,

I think break will break the execution of the whole script not only the IF clause, while the continue means “if the IF clause meets the condition, then continue executing the next lines “the lines after IF”…

Now we can use goto function also and specify the syslog msg or mail in order to be sent.

Anyway I recommend that you really test this and see the result yourself through a mail server, and then you will see that the break is not doing its job as you expect.

Best Regards



Correct Answer
Joe Clarke Sun, 08/29/2010 - 00:05
User Badges:
  • Cisco Employee,
  • Hall of Fame,

    Founding Member

The break statement has no effect on an if clause.  The break statement causes the immediate loop

to be terminated.  It does NOT terminate the script execution.  I did test the script as I have written on 15.0(1)M, and it works exactly as I expect.  That said, I did file a bug a while back after I noticed the break statement was not working as expected.  See CSCtb37673.  This bug affects 12.4(24)T, and causes break to actually terminate the execution of the applet.  Using continue is not the proper workaround in this case.  To workaround this bug, you need to use "goto."


If you are running an affected version of code, then this applet should do what you want:


action 1.3   set i "0"<br/>action 1.4.1 foreach VAR "$_cli_result" "\n"<br/>action 1.4.2  if $i gt 9 goto 1.8<br/>action 1.4.4  end<br/>action 1.5    append OUT "$VAR"<br/>action 1.6    increment i<br/>action 1.7   end<br/>action 1.8 syslog msg "$OUT"<br/>
Mohammed Khair ... Sun, 08/29/2010 - 00:34
User Badges:

Now thats make things clear for me. actually I tried the script on 12.4(24)T, and tried the break many times hence I got the conclusion of my previous answer.


Anyway thanks for the clarification Joseph.

Actions

This Discussion

Related Content