Quick script to generate XML for each network interface

#!/bin/bash
# @file statistics.sh
# @author Ron Brash (<a href="mailto:[email protected]">[email protected]</a>)
# @date Jan 2017
# @purpose Collect network interface statistics and other information and output
# it in XML format instead of arbitrary formatting
#

PATH_DIR=/sys/class/net
INTF_WILD="e*"

FIRST_LEVEL=( "address" "dev_id" "mtu" "duplex" "speed" "operstate" )
STATISTICS_LEVEL=( "rx_bytes" "rx_errors" "rx_dropped" "tx_bytes" "tx_errors" "tx_dropped" )
ONE_SPACE=" "
TWO_SPACE="  "
THREE_SPACE="   "
FOUR_SPACE="   "

function setup_xml_hdr() {
        echo "<xml>"
        echo "$ONE_SPACE<data>"
        echo "$TWO_SPACE<interfaces>"
}

function setup_xml_trl() {
        echo "$TWO_SPACE</interfaces>"
        echo "$ONE_SPACE</data>"
}

function stats_per_intf() {

        for i in "${STATISTICS_LEVEL[@]}"
        do
        :
                echo "$FOUR_SPACE<$i>`cat $1/statistics/$i`</$i>"
        done
}

function per_interfaces() {
        for f in $PATH_DIR/$INTF_WILD
        do
                echo "$THREE_SPACE<interface>"
                INTF=`echo $f |  cut -d/ -f5`
                echo "$FOUR_SPACE<name>$INTF</name>"
               
                for i in "${FIRST_LEVEL[@]}"
                do
                        :
                        echo "$FOUR_SPACE<$i>`cat $f/$i`</$i>"
                done
               
                stats_per_intf $f
                echo "$THREE_SPACE</interface>"

        done
}

setup_xml_hdr
per_interfaces
setup_xml_trl

exit 0

Blog tags: 

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <python> <c>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.