Showing posts with label statistics. Show all posts
Showing posts with label statistics. Show all posts

Saturday, October 19, 2013

Create your own command with op script. Show ldp statics from an mpls label.


My customer asked me whether JUNOS supports a function about ldp traffic statistics per mpls label. You can do show ldp statistics, but you have to know the prefix. I didn't find any command about the function and our firewall filter only supports label exp flag. Can we write some op script tho support it?
I created an op script that will do a reverse lookup of the prefix from the label. It combines the following commands:

show route x.x.x.x table inet.3
show ldp traffic-statistics

Basically it looks for the label that is in the inet.3 table and finds the associated route. It then looks for the ldp-traffic stats from the route.


See below on how it works:

user@router# show system                                         
host-name router;
scripts {
    op {
        file label-stats.slax;
    }
}

[edit]
user@router# run op label-stats label 300048                     
label 300048 prefix 202.0.0.0/24
202.0.0.0/24 Transit 114533658740 5726682937000 No.
202.0.0.0/24 Ingress 0 0 No.

[edit]
user@router# run show route 202.0.0.0/24 table inet.3            

inet.3: 9 destinations, 9 routes (9 active, 0 holddown, 0 hidden)
+ = Active Route, - = Last Active, * = Both

202.0.0.0/24       *[LDP/9] 22:28:18, metric 1
                    > to 10.2.3.3 via xe-3/0/0.0, Push 300048

[edit]
user@router# run show ldp traffic-statistics | find 202.0.0.0    
 202.0.0.0/24        Transit        114543762951      5727188147550    No   
                     Ingress                   0                  0    No   
 202.0.1.0/24        Transit           110333454         5516672700    No   
                     Ingress                   0                  0    No   
 202.0.2.0/24        Transit           110333454         5516672700    No   
                     Ingress                   0                  0    No   
 202.0.3.0/24        Transit           110333453         5516672650    No   
                     Ingress                   0                  0    No   

SOURCE CODE:
------------------------------------------------------------------------------------------
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";

import "../import/junos.xsl";
var $arguments = <argument> {
    <name> "label";
    <description> "mpls label";
}
param $label;
param $indent = "  TEXT";
param $newline = "\n";

match / {
    <op-script-results> {
        var $result = jcs:invoke("get-route-information");
        var $test = ($result);
        <output method = "text"> {
            
            for-each ($result/route-table) {
                if (table-name == "inet.3") {
                    var $content = .;
                    
                    for-each ($content/rt/rt-entry/nh) {
                        var $check = mpls-label;
                        
                        if (contains($check, $label)) {
                            var $route = normalize-space(../../rt-destination);
                            
                            expr "label " _ $label _ " prefix " _ $route _ "\n";
                            var $stats = jcs:invoke("get-ldp-traffic-statistics-information");
                            
                            for-each ($stats/ldp-traffic-statistics) {
                                var $compare = normalize-space(./ldp-prefix);
                                
                                if ($compare == $route) {
                                    expr normalize-space(.);
                                    expr ".\n";
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}