Blog Viewer

Scripting How-To: Write rtt details into an accounting profile file periodically

By Erdem posted 08-10-2015 11:11

  

Writing rtt Details into an Accounting Profile File

 
 

For SLAX version 1.0 and higher, the op script runs every 5 minutes, pinging the remote host and writing the rtt details into an accounting profile file. This op script is similar to op-ping-rtt.slax but without using jcs:syslog(), which is available in Junos OS Release 9.0 and later.

 

NOTE: This script is used in conjunction with the event script display-rtt-detail.slax, which runs every hour, reads the same accounting profile file, gets the last hours data, and writes it to the console. Eventd captures the data written to console, writes into file, and uploads to a configured destination.

 

Script Source Code

 

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

log-rtt-detail in Junoscriptorium on GitHub

 

NOTE: Requires the utility script display-rtt-detail (SLAX) or XML version.

 

Example Configuration

 

01	user@cli# show event-options
02	generate-event {
03	    FIVE_MINUTES time-interface 300;
04	    ONE_HOUR time-interface 3600;
05	}
06	policy log-rtt-detail {
07	    events FIVE_MINUTES;
08	    then {
09	        event-script log_rtt_detail {
10	            arguments {
11	                remote-host hostname;
12	                ping-request 4;
13	            }
14	        }
15	    }
16	}
17	policy display-rtt-detail {
18	     events ONE_HOUR;
19	     then {
20	         event-script display_rtt_detail {
21	             output-filename rtt_detail;
22	             destination rtt-destination;
23	         }
24	     }
25	}
26	destinations {
27	     rtt-destination {
28	         archive-sites {
29	             /var/tmp/rtt-detail;
30	         }
31	     }
32	}
33	 
34	user@cli# show system scripts
35	op {
36	     file display_rtt_detail.slax;
37	     file log_rtt_detail.slax;
38	}
39	 
40	user@clo# show accounting-options
41	file rtt-details.prf {
42	     files 3;
43	}

 

SLAX Script Contents

 

01	version 1.0;
02	 
03	ns junos = "http://xml.juniper.net/junos/*/junos";
04	ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
05	ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
06	 
07	import "../import/junos.xsl";
08	 
09	var $arguments = {
10	    <argument> {
11	    <name> "remote-host";
12	    <description> "Host name or ip-address to ping";
13	    }
14	    <argument> {
15	    <name> "ping-request";
16	    <description> "Number of ping requests to send";
17	    }
18	}
19	 
20	param $remote-host;
21	param $ping-request;
22	param $file = "rtt-details.prf";
23	  
24	match / {
25	    var $ping-rpc = {
26	    <ping> {
27	        <host> $remote-host;
28	        <count> $ping-request;
29	    }
30	    }
31	 
32	    var $ping-out = jcs:invoke($ping-rpc);
33	 
34	    if ($ping-out/ping-failure) {
35	 
36	    call store-detail($message = "Ping to host '" _  $remote-host _ "' failed at time " _ $localtime_iso);
37	    } else {
38	    call store-detail($message = "Rtt details for host '" _ $remote-host _ "' at time '" _ $localtime_iso _ "' Minimum = " _ ($ping-out/probe-results-summary/rtt-minimum div 100) _ " Maximum = " _ ($ping-out/probe-results-summary/rtt-maximum div 100)_ " Average = " _ ($ping-out/probe-results-summary/rtt-average div 100));
39	 
40	    }
41	}
42	 
43	template store-detail($message) {
44	 
45	    var $translated = {
46	    call replace-string($text = $message, $from = " ", $to = "~");
47	    }
48	 
49	    var $add-acc-rpc = {
50	    <add-accounting-file-record> {
51	        <file> $file;
52	        <data> $translated;
53	        <fields> "message";
54	        <layout> "message-layout";
55	    }
56	    }
57	 
58	    var $add-acc-out = jcs:invoke($add-acc-rpc);
59	}
60	 
61	template replace-string ($text, $from, $to) {
62	 
63	    if (contains($text, $from)) {
64	        var $before = substring-before($text, $from);
65	        var $after = substring-after($text, $from);
66	        var $prefix = $before _  $to;
67	 
68	        expr $before;
69	        expr $to;
70	        call replace-string($text = $after, $from, $to);
71	 
72	    } else {
73	        expr $text;
74	    }
75	}

 

XML Script Contents

 

01	<?xml version="1.0"?>
02	<script>
03	  <title>log_rtt_detail.slax</title>
04	  <author>rsankar</author>
05	  <synopsis>
06	    Ping the remote host every 5 min and write the rtt details into an accounting profile
07	  </synopsis>
08	  <coe>event</coe>
09	  <type>diagnose</type>
10	 
11	  <description>
12	This op script is similar to op-ping-rtt.slax but without using jcs:syslog(),
13	which is available in JUNOS 9.0 and later. Note this script is used in
14	conjunction with the event script display_rtt_detail.slax, which runs for
15	every hour and read the same accounting profile file and get the last 1 hour
16	data and write to console. Eventd will capture the data written to console
17	and will write into file and upload to a configured destination.
18	 
19	  </description>
20	 
21	  <example>
22	    <title>Sample Configuration</title>
23	    <description>Configuration required for this event script</description>
24	    <config>example-1.conf</config>
25	  </example>
26	 
27	  <xhtml:script xmlns:xhtml="http://www.w3.org/1999/xhtml"
28	                src="../../../../../web/leaf.js"
29	            type="text/javascript"/>
30	</script>

 

Required Utility Script

 

01	version 1.0;
02	 
03	ns junos = "http://xml.juniper.net/junos/*/junos";
04	ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
05	ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
06	ns ext = "http://xmlsoft.org/XSLT/namespace";
07	 
08	import "../import/junos.xsl";
09	 
10	 
11	param $file = "rtt-details.prf";
12	 
13	match / {
14	    /*
15	     * Get the rtt details for the last 60 minutes
16	     */
17	    var $rpc = <get-accounting-file-record-information> {
18	      <file> $file;
19	       <since> "-3600";
20	    }
21	    var $results = jcs:invoke($rpc);
22	 
23	    for-each ($results/file-accounting-records/file-accounting-record/message) {
24	 
25	    var $translated = {
26	        call replace-string($text = ., $from = "~", $to = " ");
27	    }
28	    expr jcs:output($translated);
29	    }
30	}
31	 
32	template replace-string ($text, $from, $to) {
33	 
34	    if (contains($text, $from)) {
35	        var $before = substring-before($text, $from);
36	        var $after = substring-after($text, $from);
37	        var $prefix = $before _  $to;
38	 
39	        expr $before;
40	        expr $to;
41	        call replace-string($text = $after, $from, $to);
42	     
43	    } else {
44	        expr $text;
45	    }
46	}

 


#ScriptingHow-To
#Slax
#rtt
#JunosOS
#How-To
#utilityscript
#xml
#eventscript

Permalink