Blogs

Scripting How-To: Get a statistics report for all fe interfaces

By Erdem posted 08-10-2015 11:16

  

Overview

Show ethernet statistics in a table format. This applies to SLAX version 1.0 and higher.

 

Description

 

Use this script to quickly get a report table that shows the statistics for all fast Ethernet (fe) interfaces. Displaying the statistics in a table format saves time and makes analysis quick and easy.

This script gathers statistics from each of the fe interfaces and reformats them into a table display, making it easier to read and analyze the information quickly.

 

Set Up


  1. Copy the op-ether-table.slax script to the /var/db/scripts/op directory on the router.
  2. Enable the script by adding the file statement and script filename to the [edit system scripts op] hierarchy level in the configuration. Only superusers can enable scripts in the configuration.

    1	[edit system scripts op]
    2	user@host# set file op-ether-table.slax
  3. Commit the configuration:

    1	[edit]
    2	user@host# commit and-quit

How to Run


  1. To run this script, issue the command op op-ether-table from the CLI operational mode:

    1	user@host> op op-ether-table
  2. The output will appear similar to this:

    01	user@cli> op op-ether-table
    02	Interface   RT      0-64    65-127   128-255   256-511  512-1023 1024-1518
    03	fe-0/0/0    rx     13613     17476         0         0         0         0
    04	            tx       224         0         0         0         0         0
    05	fe-0/0/1    rx     64661         0         0         0         0         0
    06	            tx         0         0         0         0         0         0
    07	fe-0/0/2    rx     64791     20023       443        82       317       121
    08	            tx       144     18606        75        16       317       121
    09	fe-0/0/3    rx         0         0         0         0         0         0
    10	            tx         0         0         0         0         0         0
    11	fe-1/3/0    rx     69567     63748      2935        20       318       123
    12	            tx      4418     89221      2910        17       318       122
    13	fe-1/3/1    rx     64780     15477         2         0         0         0
    14	            tx       121     15388        94         0         0         0
    15	fe-1/3/2    rx     64784     15396        45         1         0         0
    16	            tx       120     15402        43         2         0         0
    17	fe-1/3/3    rx         0         0         0         0         0         0
    18	            tx         0         0         0         0         0         0
    19	...

 

1	system {
2	    scripts {
3	        op {
4	            file op-ether-table.slax;
5	        }
6	    }
7	}

 

Example Output

 

01	user@cli> op op-ether-table
02	Interface   RT      0-64    65-127   128-255   256-511  512-1023 1024-1518
03	fe-0/0/0    rx     13613     17476         0         0         0         0
04	            tx       224         0         0         0         0         0
05	fe-0/0/1    rx     64661         0         0         0         0         0
06	            tx         0         0         0         0         0         0
07	fe-0/0/2    rx     64791     20023       443        82       317       121
08	            tx       144     18606        75        16       317       121
09	fe-0/0/3    rx         0         0         0         0         0         0
10	            tx         0         0         0         0         0         0
11	fe-1/3/0    rx     69567     63748      2935        20       318       123
12	            tx      4418     89221      2910        17       318       122
13	fe-1/3/1    rx     64780     15477         2         0         0         0
14	            tx       121     15388        94         0         0         0
15	fe-1/3/2    rx     64784     15396        45         1         0         0
16	            tx       120     15402        43         2         0         0
17	fe-1/3/3    rx         0         0         0         0         0         0
18	            tx         0         0         0         0         0         0
19	...

 

SLAX Script Contents

 

001	version 1.0;
002	 
003	ns junos = "http://xml.juniper.net/junos/*/junos";
004	ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
005	ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
006	ns ext = "http://xmlsoft.org/XSLT/namespace";
007	 
008	import "../import/junos.xsl";
009	 
010	/*
011	 * This script gets "FE" statistics from the PFE and displays
012	 * the chip counters for packet histogram data in a table. 
013	 * The chips records transmit and receive sizes for the following buckets:
014	 *
015	 *     0-64    65-127   128-255   256-511  512-1023 1024-1518
016	 *
017	 * This script was developed as a demo for a presentation at the
018	 * J-Tech Forum in 2007.
019	 */
020	 
021	var $arguments = {
022	    <argument> {
023	        <name> "interface";
024	        <description> "FE interface for which to record stats";
025	    }
026	}
027	 
028	param $interface;
029	param $file = "ether-stats.ap";
030	param $layout = "ether-stats";
031	param $trap;
032	 
033	match / {
034	    <op-script-results> {
035	    var $fmt = "%-12.12s%2.2s%10s%10s%10s%10s%10s%10s";
036	    <output> jcs:printf($fmt, "Interface", "RT", "0-64", "65-127",
037	                "128-255", "256-511", "512-1023", "1024-1518");
038	 
039	    if ($interface) {
040	        call do-stats($interface);
041	    } else {
042	        var $rpc = <get-interface-information> {
043	        <terse>;
044	        }
045	        var $if = jcs:invoke($rpc);
046	 
047	        for-each ($if/physical-interface/name[starts-with(., "fe-")]) {
048	        call do-stats($interface = .);
049	        }
050	    }
051	    }
052	}
053	 
054	template do-stats($interface) {
055	    var $stats-raw = {
056	    call get-stats($interface);
057	    }
058	    var $stats = ext:node-set($stats-raw);
059	 
060	    var $fmt = "%-12.12s%2.2s%10s%10s%10s%10s%10s%10s";
061	 
062	    <output> jcs:printf($fmt, $interface, "rx",
063	        $stats/etherStatsPkts64Octets,
064	        $stats/etherStatsPkts65to127Octets,
065	        $stats/etherStatsPkts128to255Octets,
066	        $stats/etherStatsPkts256to511Octets,
067	        $stats/etherStatsPkts512to1023Octets,
068	        $stats/etherStatsPkts1024to1518Octets);
069	 
070	    <output> jcs:printf($fmt, $interface, "tx",
071	        $stats/etherStatsPkts64Octets_TX,
072	        $stats/etherStatsPkts65to127Octets_TX,
073	        $stats/etherStatsPkts128to255Octets_TX,
074	        $stats/etherStatsPkts256to511Octets_TX,
075	        $stats/etherStatsPkts512to1023Octets_TX,
076	        $stats/etherStatsPkts1024to1518Octets_TX);
077	}
078	 
079	template get-stats ($interface) {
080	    var $if = jcs:regex("fe-([0-9]+)/([0-9]+)/([0-9]+)", $interface);
081	 
082	    if ($if[1]) {
083	    var $fpc = "fpc" _ $if[2];
084	    var $pic = $if[3];
085	    var $port = $if[4];
086	 
087	    var $rpc = <request-pfe-execute> {
088	        <target> $fpc;
089	        <command> "show fe-pic " _ $pic _ " stats " _ $port;
090	    }
091	    var $result = jcs:invoke($rpc);
092	    var $lines = jcs:break-lines($result);
093	 
094	    for-each ($lines) {
095	        var $pattern = "(etherStatsPkts[to0-9]+Octets[_TX]*) += +([0-9]+)";
096	        var $res = jcs:regex($pattern, .);
097	        if ($res[1]) {
098	        <xsl:element name=$res[2]> $res[3];
099	        }
100	    }
101	 
102	    } else {
103	    <xnm:error> {
104	        <message> "invalid interface name: " _ $interface;
105	    }
106	    }
107	}

 

XML Script Contents

 

01	<?xml version="1.0"?>
02	<script>
03	  <title>op-ether-table.slax</title>
04	  <author>phil.shafer</author>
05	  <synopsis>
06	    Show ethernet statistics in a table format
07	  </synopsis>
08	  <coe>op</coe>
09	  <type>display</type>
10	 
11	  <description>
12	This script gets "FE" statistics from the PFE and displays
13	the chip counters for packet histogram data in a table.
14	The chips records transmit and receive sizes for the following buckets:
15	  0-64    65-127   128-255   256-511  512-1023 1024-1518
16	 
17	This script was developed as a demo for a presentation at the
18	J-Tech Forum in 2007.
19	 
20	  </description>
21	 
22	<example>
23	  <title>Sample output</title>
24	  <config>example-1.conf</config>
25	  <output>example-1.output</output>
26	</example>
27	 
28	  <xhtml:script xmlns:xhtml="http://www.w3.org/1999/xhtml"
29	                src="../../../../../web/leaf.js"
30	            type="text/javascript"/>
31	</script>

 


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