Automation
Automation

Scripting How-To: Get a report of missing configuration statements

by Cordelia on ‎08-10-2015 11:28 AM - edited on ‎09-18-2017 03:24 PM by Administrator Administrator (950 Views)

Overview

Report missing configuration statements. This applies to SLAX version 1.0 and higher.

 

Description

 

This script detects missing configuration statements and reports them. Often devices fail because someone has deleted valuable configuration data that is vital to the device. These statements may vary by customer, but the script can be altered to match the environment.

 

Implementation


The error-if-missing() reports the given message if the particular statement is missing from the configuration. Tailor these calls to your specific need.


Source Code


GitHub Links


The source code below is also available from GitHub at the following locations:

Example Configuration

 

01	system {
02	    scripts {
03	    commit {
04	        file no-nukes.xsl;
05	    }
06	    }
07	}
08	interfaces {
09	    fxp0 {
10	        disable;    # This will generate an error
11	        unit 0 {
12	            family inet {
13	                address 10.0.0.1/24;
14	            }
15	        }
16	    }
17	}

 

Example Output

 

1	[edit interfaces interface fxp0 disable]
2	  'disable;'
3	    phil, you're fired.  Again.
4	protocols bgp
5	  missing mandatory configuration statement
6	error: 2 errors reported by commit scripts
7	error: commit script failure

 

SLAX Script Contents

 

01	/* Machine Crafted with Care (tm) by slaxWriter */
02	version 1.0;
03	 
04	 
05	/*
06	- $Id: no-nukes.slax,v 1.1 2007/10/17 18:37:04 phil Exp $
07	-
08	- Copyright (c) 2004-2005, Juniper Networks, Inc.
09	- All rights reserved.
10	-
11	 */
12	ns junos = "http://xml.juniper.net/junos/*/junos";
13	ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
14	ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
15	 
16	import "../import/junos.xsl";
17	param $user;
18	 
19	/*
20	- This example detects missing configuration statement and reports them.
21	 */
22	match configuration {
23	    call error-if-missing($must = interfaces/interface[name == "fxp0"]/unit[name == "0"]/family/inet/address, $statement = "interfaces fxp0 unit 0 family inet address");
24	    call error-if-present($must = interfaces/interface[name == "fxp0"]/disable | interfaces/interface[name == "fxp0"]/unit[name == "0"]/disable) {
25	        with $message = {
26	            expr $user;
27	            expr ", you're fired.  Again.";
28	         }
29	    }
30	    call error-if-missing($must = protocols/bgp, $statement = "protocols bgp");
31	}
32	 
33	template error-if-missing ($must, $statement = "unknown", $message = "missing mandatory configuration statement") {
34	     
35	    if (not($must)) {
36	        <xnm:error> {
37	            <edit-path> {
38	                copy-of $statement;
39	            }
40	            <message> {
41	                copy-of $message;
42	            }
43	        }
44	    }
45	}
46	 
47	template error-if-present ($must = 1, $message = "invalid configuration statement") {
48	    /* give error if param missing */
49	     
50	    for-each ($must) {
51	        <xnm:error> {
52	            call jcs:edit-path();
53	            call jcs:statement();
54	            <message> {
55	                copy-of $message;
56	            }
57	        }
58	    }
59	}

 

XML Script Contents

 

01	<?xml version="1.0"?>
02	<script version="0.1">
03	  <title>no-nukes.slax</title>
04	  <alternate>no-nukes.xsl</alternate>
05	  <author>phil</author>
06	  <synopsis>Report missing configuration statements</synopsis>
07	  <keyword>channelization</keyword>
08	  <keyword>per-unit-scheduler</keyword>
09	  <description>
10	    This script detects missing configuration statements and report them.
11	    Often devices fail because some newbie has deleted valueable configuration
12	    data that is vital to the device.  These statements may vary by
13	    customer, but the script can be altered to match the environment.
14	  </description>
15	  <implementation>
16	    error-if-missing() reports the given message if the particular
17	    statement is missing from the configuration.  Tailor these calls
18	    to your specific need.
19	  </implementation>
20	  <example>
21	    <config>no-nukes.conf</config>
22	    <title>A simple configuration</title>
23	    <description>
24	      This is a simple example of how this script is invoked
25	    </description>
26	    <errors>no-nukes.output</errors>
27	  </example>
28	  <xhtml:script xmlns:xhtml="http://www.w3.org/1999/xhtml"
29	                src="../../../../../web/leaf.js" type="text/javascript"/>
30	</script>