Junos Automation (Scripting)
Junos Automation (Scripting)

Find deactivated (inactive) configuration

‎08-02-2015 04:05 PM

How might I go about finding and outputting all configuration statements that are 'deactivate'd ?

 

joshTX@SRX1# show interfaces ge-0/0/3 | display xml 
<rpc-reply xmlns:junos="http://xml.juniper.net/junos/12.1X47/junos">
    <configuration junos:changed-seconds="1435118752" junos:changed-localtime="2015-06-24 04:05:52 UTC">
            <interfaces>
                <interface inactive="inactive">
                    <name>ge-0/0/3</name>
                    <description>SRX1-SRX3</description>
                    <unit>
                        <name>0</name>
                        <family>
                            <inet>
                                <address>
                                    <name>192.168.3.1/27</name>
                                </address>
                                <address>
                                    <name>10.0.0.1/30</name>
                                </address>
                            </inet>
                        </family>
                    </unit>
                </interface>
            </interfaces>
    </configuration>
    <cli>
        <banner>[edit]</banner>         
    </cli>
</rpc-reply>

Basically, I'm looking for any nodes with the attribute inactive="inactive" , like interface ge-0/0/3 in the example above.

9 REPLIES 9
Junos Automation (Scripting)

Re: Find deactivated (inactive) configuration

‎08-03-2015 09:58 AM

Something along the following lines could be used:

 

.
.
.
for-each ($cfg//@inactive) { <xnm-warning> { call jcs:edit-path(); <message> "Found to be inactive"; } } .
.
. ----------------------------------------- > op test [edit system syslog host 192.168.0.38 inactive] warning: Found to be inactive

Regards,

Andy

Junos Automation (Scripting)

Re: Find deactivated (inactive) configuration

‎08-03-2015 10:43 AM

Andy, I wasn't aware of this kind of wildcard notation (//), nor the attribute notation (@), thanks for that.  I'm not able to get this to work, however.  I suspect it has something to do with how I'm defining $cfg?

 

var $cfg=.;
    /* Check for deactivated config */
    for-each ($cfg//@inactive) {
      <xnm-warning> {
        call jcs:edit-path();
        <message> "Found to be inactive";
      }
    }

This is syntactically acceptable, but results in no output (meaning the for-each is never matched) even though I have deactivated config both in the commited config as well as the candidate config.

 

I've tried changing $cfg to:

 

var $cfg = <get-configuration>;

however, I recieve an error with this config :

error: Invalid type
error: runtime error: file /var/db/scripts/commit/test.slax line 3 element for-each
error: Failed to evaluate the 'select' expression.

Any ideas?

Junos Automation (Scripting)

Re: Find deactivated (inactive) configuration

‎08-03-2015 11:32 AM

The example I used was as follows:

 

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 / {
  <op-script-results> {
    var $local = jcs:open();
    var $rpc = <get-configuration>;
    var $cfg = jcs:execute( $local, $rpc );

    for-each ($cfg//@inactive) {
      <xnm:warning> {
        call jcs:edit-path();
        <message> "Found to be inactive";
      } 
      expr jcs:close($local);
    }
  }
}

Regards,

Andy

Junos Automation (Scripting)

Re: Find deactivated (inactive) configuration

‎08-03-2015 12:24 PM

Do you know what the difference between:

 

match / {

and 

match configuration {

is?  I'm using this in a existing commit script where I had match configuration, and trying to figure out how to fold in your recommendation here.  I can reproduce what you've done in a standalone script (both op and commit), but this appears to be one of a couple differences.  In my original script I used match configuration, and also defined $cfg using:

 

var $cfg = .;

Is there a best practice here that I'm not following?

 

Thanks again,

-Josh

Junos Automation (Scripting)

Re: Find deactivated (inactive) configuration

‎08-03-2015 02:26 PM

Hi,

 

Op scripts would typically use this

match /  {
   ...
}

Commit scripts would typically use this

match configuration  {
   ...
}

My example was an Op script rather than a commit script.

 

I'll take a look at a commit script version tomorrow.

 

Regards,

Andy

 

Junos Automation (Scripting)

Re: Find deactivated (inactive) configuration

‎08-03-2015 03:29 PM

I believe that the problem is that the input configuration your commit script consumes will simply not see any inactivated portions.


When you fetch a committed configuration with your op script, you *will* see the bits with the @inactive attribute.

 

You can confirm this with a simple test:

Write a commit script that just outputs all the interface names (see below) and add it to the config. 

Deactivate one of the interfaces and commit.

When the commit script runs, it won't emit that interface name.

match configuration {
    <xnm:warning> { <message> "running test commit script";}
    for-each (interfaces/interface/name) {
        <xnm:warning> { <message> .;}
    }
}

 

/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
Junos Automation (Scripting)

Re: Find deactivated (inactive) configuration

‎08-03-2015 03:37 PM

Doug, yes that makes sense and jives with what I'm seeing.

 

How do I get a config that includes the inactive nodes?

 

-josh

Junos Automation (Scripting)

Re: Find deactivated (inactive) configuration

[ Edited ]
‎08-03-2015 04:08 PM

Hopefully someone smarter than I will pipe in here with the definitive answer, but here's an "approach".  <*cough*>

 

Your commit script can also do an explicit get-configuration> for the candidate config, and for some reason that resulting XML will contain the @inactive attribute.  

Check this example: 1st section operates on a get-configuration result.  The 2nd time through we just examin the commit script *input.


Maybe the explicit <get-configuration database="candidate"> approach will work for you.

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 configuration {

    var $local = jcs:open();
    var $rpc = <get-configuration database="candidate">;
    var $cfg = jcs:execute($local, $rpc);
    
    <xnm:warning> { <message> "checking deactivated interfaces using get-config of candidate config";}
    for-each($cfg/interfaces/interface/@inactive) {    
        <xnm:warning> { 
            <message> ../name _ " is inactive";  
        }
    }
    
    <xnm:warning> { <message> "Checking for ANY interfaces in current commit script input config.";}
    for-each (interfaces/interface/name){    
        <xnm:warning> { <message> . _  " found.";}
    }
}

 

Update:  in my previous reply I mentioned doing a <get-configuration> on a committed config.  What you reallly want is the *candidate* configuration -- and that actually seems to contain all of the @inactive attributes -- as opposed to the config that is getting read in automatically by the commit script.

 

Like I said, hopefully someone will pipe in and clarify *why* that is -- I don't know, 

 

/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
Junos Automation (Scripting)

Re: Find deactivated (inactive) configuration

‎03-08-2019 09:17 AM

I think my previous example was wrong, but I'll leave it.


If you want to be able to detect 'inactive' interfaces from the configuration, you have to do a get-config from the *committed* database:

	var $get-if-rpc = <get-configuration database="committed"> {
        <configuration> {
		    <interfaces>;
        }
    }
var $ifs = jcs:execute($con, $get-if-rpc);
...
var $inactive_count = count($ifs//interface[@inactive]);

...

 

--
"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