Blog Viewer

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

By Erdem posted 08-14-2015 10:03

  

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