Automation
Automation

Scripting How-To: Use op-rpd-mem to display rpd statistics in simple horizontal bar graphs

by Cordelia on ‎08-14-2015 10:03 AM - edited on ‎09-07-2017 03:36 PM by Administrator Administrator (888 Views)

Displaying rpd Statistics in Simple Horizontal Bar Graphs

 

For SLAX version 1.0 and higher, you can use the op-rpd-mem script to display rpd statistics in simple horizontal bar graphs. This op script uses the command "show task memory detail". The library lib-graph.slax is content independent (resuable).

 

Source Code and GitHub Links

 

The source code below is also available from op-rpd-mem in Junoscriptorium on GitHub

and the following links:

 

Example Input

 
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
param $screen-width = 80;
08
param $separator = " : ";
09
param $debug;
10
 
11
var $stars =
12
"**********************************************************************";
13
var $bangs =
14
"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
15
 
16
template star-graph($stats, $name = "Name", $value = "Value") {
17
    param $header-format = "%-8s %-20s%s";
18
    param $format = "%8s %-20s";
19
 
20
    var $max = $stats/total;
21
    var $threshold = floor($max * 0.80);
22
    var $low = floor($max div 100);
23
 
24
    <output> jcs:printf($header-format, $value, $name, " Graph");
25
    for-each ($stats/item) {
26
    <xsl:sort select="value" data-type="number" order="descending">;
27
    if (value >= $low) {
28
        var $title = jcs:printf($format,
29
                    value, substring(name, 0, 20));
30
        call star-graph-line($title, $title-width = 30, $value = value,
31
                $max, $threshold);
32
    }
33
    }
34
    <output> "Total: " _ $max;
35
}
36
 
37
template star-graph-line($title, $title-width = 10, $value, $max, $threshold,
38
            $max-width = $screen-width) {
39
    var $width = $max-width - $title-width - string-length($separator);
40
    var $per = $max div $width;
41
    var $num = floor($value div $per);
42
 
43
    var $threshold-num = floor($threshold div $per);
44
    var $bang-num = {
45
    if ($value < $threshold) {
46
        expr 0;
47
    } else {
48
        expr $num - $threshold-num + 1;
49
    }
50
    }
51
 
52
    var $star-num = $num - $bang-num;
53
    var $star-string = substring($stars, 0, $star-num + 1);
54
    var $bang-string = substring($bangs, 0, $bang-num + 1);
55
 
56
    var $extra = {
57
    if ($value - (floor($value div $per) * $per) >= $per div 2) {
58
        if ($value > $threshold) {
59
        expr ":";
60
        } else {
61
        expr "-";
62
        }
63
    }
64
    }
65
 
66
    <output> jcs:printf("%-*s%s%s%s%s\n", $title-width, $title, $separator,
67
                $star-string, $bang-string, $extra);
68
}
 
Example Output
01 user@cli> op op-rpd-mem
02 Bytes   Task            Graph
03 114888  KRT         : *******'
04  97309  --System--      : ******
05  76319  OSPFv2          : *****
06  74748  BGP RT Background   : *****
07  70940  --Anonymous--       : *****
08  66876 OSPFv2 I/O./var/run  : ****'
09  66704 LDP I/O./var/run/pp  : ****'
10  65555 BFD I/O./var/run/bf  : ****
11  65544 BGP rsync        : ****
12  65535 MPLSOAMD I/O./var/r  : ****
13  10620 SNMP Subagent ./var/ : '
14   9988 -- sockaddr --       : '
15 Total: 678884
 
SLAX Script Contents
01 version 1.0;
02  
03 ns junos = "http://xml.juniper.net/junos/*/junos";
07  
08 import "lib-graph.slax";
09  
10 param $logical-router;
11  
12 match / {
13     <op-script-results> {
14     var $raw-stats = {
15         call get-rpd-stats();
16     }
17     var $stats = ext:node-set($raw-stats);
18     call star-graph($stats = $stats/graph,
19             $name = "Task", $value = "Bytes");
20     }
21 }
22  
23 template get-rpd-stats () {
24     var $rpc = <command> {
25     expr "show task memory detail";
26     if ($logical-router) {
27         expr " logical-router " _ $logical-router;
28     }
29     }
30     var $out = jcs:invoke($rpc);
31  
32     for-each ($out/task-memory-malloc-usage-report) {
33     <graph> {
34         for-each (task-malloc-list/task-malloc) {
35         <item> {
36             <name> tm-name;
37             <value> tm-max-alloc-bytes;
38         }
39         }
40         <total> task-memory-total-bytes;
41     }
42     }
43 }
 
XML Script Contents
 
01 <?xml version="1.0"?>
02 <script>
03   <title>op-rpd-mem.slax</title>
04   <author>phil</author>
05   <synopsis>
06     Display rpd statistics in simple horizontal bar graphs
07   </synopsis>
08   <coe>op</coe>
09   <type>display</type>
10  
11   <description>
12 This op script uses the command "show task memory detail". The library lib-graph.slax is content independent (resuable).
13  
14   </description>
15  
16   <example>
17     <title>Sample output</title>
18     <description>Display output of "op op-rpd-mem" command</description>
19     <input>example-1.input</input>
20     <output>example-1.output</output>
21   </example>
22  
23   <xhtml:script xmlns:xhtml="http://www.w3.org/1999/xhtml"
24                 src="../../../../../web/leaf.js"
25             type="text/javascript"/>
26 </script>