07-27-2010 07:44 PM
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.
Solved! Go to Solution.
07-28-2010 11:39 AM
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"
08-29-2010 12:05 AM
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"
action 1.4.1 foreach VAR "$_cli_result" "\n"
action 1.4.2 if $i gt 9 goto 1.8
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"
07-28-2010 11:39 AM
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"
07-28-2010 01:00 PM
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
07-28-2010 01:53 PM
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 "mailid@xxx.com" from "xxx@xxx.com" 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
07-28-2010 02:12 PM
WOOHOOO solved it
Cheers mate
08-27-2010 04:41 PM
Why don't you write how did you solved it?
08-27-2010 06:08 PM
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.
08-28-2010 05:55 AM
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
08-28-2010 05:28 PM
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.
08-28-2010 11:26 PM
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
08-29-2010 12:05 AM
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"
action 1.4.1 foreach VAR "$_cli_result" "\n"
action 1.4.2 if $i gt 9 goto 1.8
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"
08-29-2010 12:34 AM
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.
08-29-2010 05:04 AM
Now all makes sense!
Thank you
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide