help with a completely pointless applet

Answered Question
Apr 4th, 2012

Hi,

This is my very second applet (first was an hour ago) so please go easy on me.  I watched a great webex earlier today and I've got a bit overly excited about EEM, especially now I don't have to learn TCL.  I still have an unopned TCL scripting bible on my shelf that I just have never brought myself to pick up!

I'm trying to understand the use of 'while/end' and 'if/else' statements.  I've created a completely pointless applet that tells me off everytime I shutdown the loopback100 interface on my router.  It worked perfectly and I even managed to get variable name email accounts based on who's logged in.


What I'd like to do now is only send an email if the user persists and shuts the loopback100 interface 3 times.

event manager environment _email_server X.X.X.X

event manager environment company @company.com
event manager environment counter 3


no event manager applet lo100protector
event manager applet lo100protector
event syslog pattern "Interface Loopback100, changed state to administratively down" period 1

action 1.0 while $counter gt 0
action 2.1 cli command "enable"
action 2.2 cli command "show users"
action 2.3 regexp "[a-z]+\.[a-z]+" "$_cli_result" username                                         !regexp not particularly relavent
action 2.4 syslog msg "hello $username"
action 2.5 syslog msg "Please don't shutdown loopback100!"
action 2.6 cli command "enable"
action 2.7 cli command "config t"
action 2.8 cli command "int lo100"
action 2.9 cli command "no shut"
action 3.0 cli command "exit"
action 3.1 cli command "exit"
action 3.2 subtract $counter 1
action 3.3 puts $counter                                             !i've put this in to "troubleshoot" the issue (if you can call it that).  Always returns a value of 3
action 3.4 end
action 4.0 cli command "enable"
action 4.1 cli command "show run | inc hostname"
action 4.2 regexp  "[a-z]+_[a-z]+[0-1]_f[0-9]_[a-z]+[0-1]" "$_cli_result" hostname          !nor is this one.
action 5.0 mail server "$_email_server" to "$username$company" from "$hostname$company" subj "This email was generated by EEM on $hostname" body "Please will you stop trying to shutdown the loopback100 interface!  It's not big or clever." source-int vlan1
action 5.1 syslog msg "email sent to $username"
action 6.0 set counter 3

Soooo.  The first problem I have is that my $counter variable never seems to go decrement.

The second problem I have is that it seems to be looping the while statement.  I see the "Please don't shutdown loopback100!" statement multiple times.  Although it doesn't seem to go on forever, just a few times.

I've tested it all without a while loop in it and all seems to be good.  Would someone be kind enough to put me in the right direction please?

A couple of other random questions. 

Is there a way I can put a charater directly after a variable?  For example in action 2.4, I don't seem to be able to put an "!" after the $username variable without a space a'la "hello $username!"

action 5.0 mail server command has just about hit the line charater limit.  Is there a way that I can break the command into it's components?

Thanks you all very much in advance


A slightly too excited, Alex

I have this problem too.
0 votes
Correct Answer by Joseph Clarke about 2 years 1 week ago

Thanks for attending the webinar.  I'm glad you found it informative.

Congrats on your first applet.  What you want to do is possible, but you cannot set environment variables locally.  Those can only be set by going back into config t mode and resetting them.  There are other counters you can use, though.  One idea is to use an EEM counter.  This would require another policy, but you could accomplish what you want with the following.

event manager applet lo100protector

event syslog pattern "Interface Loopback100, changed state to administratively down" period 1

action 1.0 counter name lo100cnt op dec 1

action 1.1 if $_counter_value_remain gt 0

action 2.1  cli command "enable"

action 2.2  cli command "show users"

action 2.3  regexp "[a-z]+\.[a-z]+" "$_cli_result"   username

action 2.4  syslog msg "hello $username"

action 2.5  syslog msg "Please don't shutdown loopback100!"

action 2.7  cli command "config t"

action 2.8  cli command "int lo100"

action 2.9  cli command "no shut"

action 3.0  cli command "end"

action 3.4 else

action 4.1  info type routername

action 5.0  mail server "$_email_server" to "$username$company"  from  "$_info_routername$company" subj "This email was generated by EEM  on $_info_routername"  body "Please will you stop trying to shutdown the  loopback100  interface!  It's not big or clever." source-int vlan1

action 5.1  syslog msg "email sent to $username"

action 6.0 end

event manager applet lo100counter

event counter name lo100cnt entry-op le entry-val 0 exit-val ge exit-val 0
action 1.0 counter name lo100cnt op set value 3

Applet variable processing is fairly primative.  So you will not be able to follow some variable names with certain characters.  The '!' is one such character.  You'll need a space.

Regarding command length, at some point you'll have to break that Tcl book out :-).  Applets are powerful, but as you start to build more complex and larger applications, you will need a more fully-featured programming language.

  • 1
  • 2
  • 3
  • 4
  • 5
Average Rating: 5 (1 ratings)
Correct Answer
Joseph Clarke Wed, 04/04/2012 - 13:19

Thanks for attending the webinar.  I'm glad you found it informative.

Congrats on your first applet.  What you want to do is possible, but you cannot set environment variables locally.  Those can only be set by going back into config t mode and resetting them.  There are other counters you can use, though.  One idea is to use an EEM counter.  This would require another policy, but you could accomplish what you want with the following.

event manager applet lo100protector

event syslog pattern "Interface Loopback100, changed state to administratively down" period 1

action 1.0 counter name lo100cnt op dec 1

action 1.1 if $_counter_value_remain gt 0

action 2.1  cli command "enable"

action 2.2  cli command "show users"

action 2.3  regexp "[a-z]+\.[a-z]+" "$_cli_result"   username

action 2.4  syslog msg "hello $username"

action 2.5  syslog msg "Please don't shutdown loopback100!"

action 2.7  cli command "config t"

action 2.8  cli command "int lo100"

action 2.9  cli command "no shut"

action 3.0  cli command "end"

action 3.4 else

action 4.1  info type routername

action 5.0  mail server "$_email_server" to "$username$company"  from  "$_info_routername$company" subj "This email was generated by EEM  on $_info_routername"  body "Please will you stop trying to shutdown the  loopback100  interface!  It's not big or clever." source-int vlan1

action 5.1  syslog msg "email sent to $username"

action 6.0 end

event manager applet lo100counter

event counter name lo100cnt entry-op le entry-val 0 exit-val ge exit-val 0
action 1.0 counter name lo100cnt op set value 3

Applet variable processing is fairly primative.  So you will not be able to follow some variable names with certain characters.  The '!' is one such character.  You'll need a space.

Regarding command length, at some point you'll have to break that Tcl book out :-).  Applets are powerful, but as you start to build more complex and larger applications, you will need a more fully-featured programming language.

alexsteer Thu, 04/05/2012 - 01:34

Hi joseph,

Thanks very much.  It was very good.  I'd love another more hands one at some point in the future.  I think I am going to start looking at some beginners TCL scripting.  I do quite like the fact that applets are more visible in the config though.  My brief and very uneventful experience of TCL scripts is that it's not entirely clear to your average administrator that TCL scripts are configured or running.

Anyway,

I'm not sure the version of EEM I have on my router supports all the commands in your second applet.  My router is running 151-3.T2.

event manager applet lo100counter
event counter name lo100cnt entry-op le entry-val 0 exit-val ge exit-val 0

It doesn't seem to like the "ge" statement in there and is expecting a numeric value.  I'm not sure if that just means I need to upgrade or if the first "exit-val" statement should be an "exit-op"?

I can't see how the two applets are linked either.  If the first the if statement looks at $_counter_value_remain.  Where is that variable declared and modified in the second applet, or doesn't it need to be?

Would you mind giving me a brief description?

Thanks for your help Joseph


Alex

Joseph Clarke wrote:

Thanks for attending the webinar.  I'm glad you found it informative.

Congrats on your first applet.  What you want to do is possible, but you cannot set environment variables locally.  Those can only be set by going back into config t mode and resetting them.  There are other counters you can use, though.  One idea is to use an EEM counter.  This would require another policy, but you could accomplish what you want with the following.

event manager applet lo100protector
 event syslog pattern "Interface Loopback100, changed state to administratively down" period 1
 action 1.0 counter name lo100cnt op dec 1
 action 1.1 if $_counter_value_remain gt 0
 action 2.1  cli command "enable"
 action 2.2  cli command "show users"
 action 2.3  regexp "[a-z]+\.[a-z]+" "$_cli_result"   username
 action 2.4  syslog msg "hello $username"
 action 2.5  syslog msg "Please don't shutdown loopback100!"
 action 2.7  cli command "config t"
 action 2.8  cli command "int lo100"
 action 2.9  cli command "no shut"
 action 3.0  cli command "end"
 action 3.4 else
 action 4.1  info type routername
 action 5.0  mail server "$_email_server" to "$username$company"  from  "$_info_routername$company" subj "This email was generated by EEM  on $_info_routername"  body "Please will you stop trying to shutdown the  loopback100  interface!  It's not big or clever." source-int vlan1
 action 5.1  syslog msg "email sent to $username"
 action 6.0 end

event manager applet lo100counter
 event counter name lo100cnt entry-op le entry-val 0 exit-val ge exit-val 0
 action 1.0 counter name lo100cnt op set value 3

Applet variable processing is fairly primative.  So you will not be able to follow some variable names with certain characters.  The '!' is one such character.  You'll need a space.

Regarding command length, at some point you'll have to break that Tcl book out :-).  Applets are powerful, but as you start to build more complex and larger applications, you will need a more fully-featured programming language.

Joseph Clarke wrote:

Thanks for attending the webinar.  I'm glad you found it informative.

Congrats on your first applet.  What you want to do is possible, but you cannot set environment variables locally.  Those can only be set by going back into config t mode and resetting them.  There are other counters you can use, though.  One idea is to use an EEM counter.  This would require another policy, but you could accomplish what you want with the following.

event manager applet lo100protector
 event syslog pattern "Interface Loopback100, changed state to administratively down" period 1
 action 1.0 counter name lo100cnt op dec 1
 action 1.1 if $_counter_value_remain gt 0
 action 2.1  cli command "enable"
 action 2.2  cli command "show users"
 action 2.3  regexp "[a-z]+\.[a-z]+" "$_cli_result"   username
 action 2.4  syslog msg "hello $username"
 action 2.5  syslog msg "Please don't shutdown loopback100!"
 action 2.7  cli command "config t"
 action 2.8  cli command "int lo100"
 action 2.9  cli command "no shut"
 action 3.0  cli command "end"
 action 3.4 else
 action 4.1  info type routername
 action 5.0  mail server "$_email_server" to "$username$company"  from  "$_info_routername$company" subj "This email was generated by EEM  on $_info_routername"  body "Please will you stop trying to shutdown the  loopback100  interface!  It's not big or clever." source-int vlan1
 action 5.1  syslog msg "email sent to $username"
 action 6.0 end

event manager applet lo100counter
 event counter name lo100cnt entry-op le entry-val 0 exit-val ge exit-val 0
 action 1.0 counter name lo100cnt op set value 3

Applet variable processing is fairly primative.  So you will not be able to follow some variable names with certain characters.  The '!' is one such character.  You'll need a space.

Regarding command length, at some point you'll have to break that Tcl book out :-).  Applets are powerful, but as you start to build more complex and larger applications, you will need a more fully-featured programming language.

Joseph Clarke Thu, 04/05/2012 - 10:06

Yep, that was a typo.  The line should read:

event counter name lo100cnt entry-op le entry-val 0 exit-op ge exit-val 0

In applet one, action 1.0 performs a decrement operation on the counter named lo100cnt.  This is what links to the two policies.  This action implicitly sets the $_counter_value_remain variable to the new value of the lo100cnt counter.  The second applet monitors that counter and will reset its value to three when it drops to or below 0.

alexsteer Fri, 04/06/2012 - 15:43

Thanks Joseph,


Awesome advice.  I'm going to man up and get on with TCL now.

Kind regards

Alex

Actions

Login or Register to take actions

This Discussion

Posted April 4, 2012 at 12:48 PM
Stats:
Replies:4 Avg. Rating:5
Views:509 Votes:0
Shares:0
Tags: No tags.
Categories: EEM for IOS-XR
+

Discussions Leaderboard