Peform a Before and After Comparison of "show data"
For SLAX version 1.0 and higher, compare the 'show' output in pre/post-check phases for delta identification. This op delta script is called in two passes:
- On the first pass, the script is invoked without the filename argument and outputs data to be saved.
- Later, the script is invoked a second time, but this time the name of the saved file from the first pass is passed in as an argument.
The data from this file is compared with the current values, and errors can be reported as needed.
The 'show system uptime" is used in a simple example.
Source Code and GitHub Links
The source code below is also available from the following GitHub locations:
Example Output
01 Usage:
02
03 user@cli> op delta | display xml | save /tmp/save.xml
04 Wrote 59 lines of output to '/tmp/save.xml'
05
06 user@cli> op delta filename /tmp/save.xml
07 old 1222115000, new 1222115050, delta 50
08
09 % more /tmp/save.xml
10 <rpc-reply xmlns:junos="http://xml.juniper.net/junos/9.4I0/junos">
11 <system-uptime-information xmlns="http://xml.juniper.net/junos/9.4I0/junos">
12 <current-time>
13 <date-time junos:seconds="1222115000">
14 2008-09-22 16:23:20 EDT
15 </date-time>
16 </current-time>
17 <system-booted-time>
18 <date-time junos:seconds="1222091374">
19 2008-09-22 09:49:34 EDT
20 </date-time>
21 <time-length junos:seconds="23626">
22 06:33:46
23 </time-length>
24 </system-booted-time>
25 <protocols-started-time>
26 <date-time junos:seconds="1222096904">
27 2008-09-22 11:21:44 EDT
28 </date-time>
29 <time-length junos:seconds="18096">
30 05:01:36
31 </time-length>
32 </protocols-started-time>
33 <last-configured-time>
34 <date-time junos:seconds="1222112273">
35 2008-09-22 15:37:53 EDT
36 </date-time>
37 <time-length junos:seconds="2727">
38 00:45:27
39 </time-length>
40 <user>
41 user1
42 </user>
43 </last-configured-time>
44 <uptime-information>
45 <date-time junos:seconds="1222115000">
46 4:23PM
47 </date-time>
48 <up-time junos:seconds="23656">
49 6:34
50 </up-time>
51 <active-user-count junos:format="7 users">
52 7
53 </active-user-count>
54 <load-average-1>
55 0.00
56 </load-average-1>
57 <load-average-5>
58 0.00
59 </load-average-5>
60 <load-average-15>
61 0.00
62 </load-average-15>
63 </uptime-information>
64 </system-uptime-information>
65 <cli>
66 <banner></banner>
67 </cli>
68 </rpc-reply>
SLAX Script Contents
001 version 1.0;
002
003 ns junos = "http://xml.juniper.net/junos/*/junos";
004 ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
005 ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
006
007 import "../import/junos.xsl";
008
009 /*
010 * This script is called in two passes. On the first pass, it is
011 * invoked without the filename argument and outputs data to be
012 * saved. The user is responsible for saving this data. The
013 * typical invocation is:
014 *
015 * op delta | display xml | save /tmp/save.xml
016 *
017 * At a later point, the script is invoked a second time, but this
018 * time the name of the saved file from the first pass is passed in
019 * as the "filename" argument. The data from this file is compared
020 * with the currently values and errors can be reported as needed.
021 *
022 * op delta filename /tmp/save.xml
023 *
024 * In this test case, we record the output of "show system uptime"
025 * on the first pass and compare the saved value for the current-time
026 * during the second pass. The delta is reported.
027 */
028 var $arguments = {
029 <argument> {
030 <name> "phase";
031 <description> "Phase of delta (baseline or compare)";
032 }
033 <argument> {
034 <name> "filename";
035 <description> "Name of baseline file";
036 }
037 }
038
039 param $filename;
040 param $phase = {
041 if ($filename) {
042 expr "compare";
043 } else {
044 expr "baseline";
045 }
046 }
047
048 match / {
049 <op-script-results> {
050 var $rpc = <get-system-uptime-information>;
051 var $new = jcs:invoke($rpc);
052
053 if ($phase == "baseline") {
054 copy-of $new;
055 } else {
056 var $doc = document($filename);
057 var $old = $doc/rpc-reply/ *;
058
059 /*
060 * This is ugly work, but we don't know the
061 * namespace string, so we have to use local-name()
062 * to find the nodes we need.
063 */
064 var $ot = {
065 call extract($node = $old,
066 $path = "current-time/date-time/@seconds");
067 }
068 var $nt = {
069 call extract($node = $new,
070 $path = "current-time/date-time/@seconds");
071 }
072
073 <top> {
074 <output> jcs:printf("old %s, new %s, delta %s",
075 $ot, $nt, $nt - $ot);
076 }
077 }
078 }
079 }
080
081 template extract ($node, $path)
082 {
083 if (not($path)) {
084 copy-of $node;
085
086 } else {
087 var $pre = {
088 if (contains($path, "/")) {
089 expr substring-before($path, "/");
090 } else {
091 expr $path;
092 }
093 }
094 var $post = substring-after($path, "/");
095
096 if (starts-with($pre, "@")) {
097 var $rest = $node/@*[local-name() == substring($pre, 2)];
098 expr $rest;
099
100 } else {
101 var $rest = $node/ *[local-name() == $pre];
102 call extract($node = $rest, $path = $post);
103 }
104 }
105 }
106
107 template remove-namespaces ($node)
108 {
109 <xsl:element name=local-name($node)> {
110 for-each ($node/@*) {
111 <xsl:attribute name=local-name(.)> {
112 expr .;
113 }
114 }
115 for-each ($node/*) {
116 call remove-namespaces($node = .);
117 }
118 expr $node/text();
119 }
120 }
XML Script Contents
01 <?xml version="1.0"?>
02 <script>
03 <title>delta.slax</title>
04 <author>phil</author>
05 <synopsis>
06 Compare the 'show' output in pre/post-check phases for delta identification
07 </synopsis>
08 <coe>op</coe>
09 <type>system</type>
10
11 <description>
12 This script is called in two passes. On the first pass, it is invoked without the filename argument and outputs data to be saved. At a later point, the script is invoked a second time, but this time the name of the saved file from the first pass is passed in as an argument. The data from this file is compared with the currently values and errors can be reported as needed. We use 'show system uptime" here as a simple example
13
14 </description>
15
16 <example>
17 <title>Sample output</title>
18 <description>The delta is reported</description>
19 <output>example-1.output</output>
20 </example>
21
22 <xhtml:script xmlns:xhtml="http://www.w3.org/1999/xhtml"
23 src="../../../../../web/leaf.js"
24 type="text/javascript"/>
25 </script>