Blog Viewer

Scripting How-To: Display rpd statistics in horizontal bar graphs

By Erdem posted 11-19-2015 12:36

  
This applies to SLAX version 1.0 and higher.
 

Overview

Display rpd statistics in simple horizontal bar graphs.

 

Description

 

This op script uses the command "show task memory detail". The library lib-graph.slax is content independent (resuable).

 

Source

 

GitHub Links

 

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

 

 

Example Input

 

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

param $screen-width = 80;
param $separator = " : ";
param $debug;

var $stars =
"**********************************************************************";
var $bangs =
"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";

template star-graph($stats, $name = "Name", $value = "Value") {
    param $header-format = "%-8s %-20s%s";
    param $format = "%8s %-20s";

    var $max = $stats/total;
    var $threshold = floor($max * 0.80);
    var $low = floor($max div 100);

    <output> jcs:printf($header-format, $value, $name, " Graph");
    for-each ($stats/item) {
	<xsl:sort select="value" data-type="number" order="descending">;
	if (value >= $low) {
	    var $title = jcs:printf($format,
				    value, substring(name, 0, 20));
	    call star-graph-line($title, $title-width = 30, $value = value,
			    $max, $threshold);
	}
    }
    <output> "Total: " _ $max;
}

template star-graph-line($title, $title-width = 10, $value, $max, $threshold,
		    $max-width = $screen-width) {
    var $width = $max-width - $title-width - string-length($separator);
    var $per = $max div $width;
    var $num = floor($value div $per);

    var $threshold-num = floor($threshold div $per);
    var $bang-num = {
	if ($value < $threshold) {
	    expr 0;
	} else {
	    expr $num - $threshold-num + 1;
	}
    }

    var $star-num = $num - $bang-num;
    var $star-string = substring($stars, 0, $star-num + 1);
    var $bang-string = substring($bangs, 0, $bang-num + 1);

    var $extra = {
	if ($value - (floor($value div $per) * $per) >= $per div 2) {
	    if ($value > $threshold) {
		expr ":";
	    } else {
		expr "-";
	    }
	}
    }

    <output> jcs:printf("%-*s%s%s%s%s\n", $title-width, $title, $separator,
			    $star-string, $bang-string, $extra);
}

 

Example Output

 

user@cli> op op-rpd-mem 
Bytes	Task			Graph
114888	KRT			: *******'
 97309	--System--		: ******
 76319	OSPFv2			: *****
 74748	BGP RT Background	: *****
 70940	--Anonymous--		: *****
 66876 OSPFv2 I/O./var/run	: ****'
 66704 LDP I/O./var/run/pp	: ****'
 65555 BFD I/O./var/run/bf	: ****
 65544 BGP rsync		: ****
 65535 MPLSOAMD I/O./var/r	: ****
 10620 SNMP Subagent ./var/	: '
  9988 -- sockaddr --		: ' 
Total: 678884

 

SLAX Script Contents

 

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";
ns ext = "http://xmlsoft.org/XSLT/namespace";

import "lib-graph.slax";

param $logical-router;

match / {
    <op-script-results> {
	var $raw-stats = {
	    call get-rpd-stats();
	}
	var $stats = ext:node-set($raw-stats);
	call star-graph($stats = $stats/graph,
			$name = "Task", $value = "Bytes");
    }
}

template get-rpd-stats () {
    var $rpc = <command> {
	expr "show task memory detail";
	if ($logical-router) {
	    expr " logical-router " _ $logical-router;
	}
    }
    var $out = jcs:invoke($rpc);

    for-each ($out/task-memory-malloc-usage-report) {
	<graph> {
	    for-each (task-malloc-list/task-malloc) {
		<item> {
		    <name> tm-name;
		    <value> tm-max-alloc-bytes;
		}
	    }
	    <total> task-memory-total-bytes;
	}
    }
}

 

XML Script Contents

 

<?xml version="1.0"?>
<script>
  <title>op-rpd-mem.slax</title>
  <author>phil</author>
  <synopsis>
	Display rpd statistics in simple horizontal bar graphs
  </synopsis>
  <coe>op</coe>
  <type>display</type>

  <description>
This op script uses the command "show task memory detail". The library lib-graph.slax is content independent (resuable).

  </description>

  <example>
    <title>Sample output</title>
    <description>Display output of "op op-rpd-mem" command</description>
    <input>example-1.input</input>
    <output>example-1.output</output>
  </example>

  <xhtml:script xmlns:xhtml="http://www.w3.org/1999/xhtml"
                src="../../../../../web/leaf.js" 
	        type="text/javascript"/>
</script>

#How-To
#Slax
#opscript
#ScriptingHow-To

Permalink