Junos Automation (Scripting)
Highlighted
Junos Automation (Scripting)

Trggering an event script...please help

[ Edited ]
‎05-29-2015 01:06 PM

I want to trigger an event script when a switch reboots either by design or power outage. First, is this possible and if so, how do I do it? Here is what I have in my config.

********************************************************************************

event-options {
policy chassisd {
events SYSTEM;
attributes-match {
SYSTEM.message matches .*chassis-control.*;
}
then {
event-script clearmacaddr.slax;
}
}
event-script {
file clearmacaddr.slax;
}
}

*******************************************************************************

and here is my script

*******************************************************************************

version 1.0;
ns junos = "http://xml.juniper.net/junos/*/junos";
ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";

import "../import/junos.xsl";

match / {

<event-script-results> {

var $cmd = <command> "clear ethernet-switching table";
var $res = jcs:invoke($cmd);

/* Send syslog infomational message */
expr jcs:syslog("external.info", "MAC Address Table has been cleared.");
}

}

****************************************************************************************

Any help will be greatly appreciated.

 

3 REPLIES 3
Highlighted
Junos Automation (Scripting)

Re: Trggering an event script...please help

‎06-05-2015 12:04 AM
The only way I can think of is matching on log message which is generated only after reboot, but this is exactly what you have done in the config you published.
Highlighted
Junos Automation (Scripting)

Re: Trggering an event script...please help

‎06-05-2015 08:44 AM

Hi,

 

You should also be able to use the following in your event policy.

 

<attributes-match> {
        <from-event-attribute> "system.message";
        <condition> "matches";
        <to-event-attribute-value> "Starting of initial processes complete";
}

Regards,

Andy

Highlighted
Junos Automation (Scripting)

Re: Trggering an event script...please help

‎06-08-2015 06:57 AM

I've always used the technique that Andy described (event policy that watches for the system event "Starting of initial processes complete").    That has always worked for me.

 

However, I will also give you this bit of advice: 

If Junos is restarting, then there may be an *awful lot* of things going on.  Your event script action *might* get delayed, or just flat-out fail sometimes due to a flurry of higher-priority tasks going on.  (I have mostly run across this issue in  scripts that needed to do something with the SNMP agent, or on devices with thousands of IFLs to bring up...)

 

That said,

1. make sure that you check the status for any/all rpc operations you do in your event script.   Then act accordingly.   I.e., retry the command some number of times. 

2. Log execution to syslog.  I.e. use jcs:syslog().   It'll make troubleshooting a *lot* easier.  

 

See this article that I wrote for the Juniper TechWiki a while back, for more info on this.

 

/doug

 

 

--
"There he goes. One of God's own prototypes. A high-powered mutant of some kind never even considered for mass production. Too weird to live, and too rare to die." --HST