Sunday, June 15, 2014

Python script - check for interface errors remotely.

In my last blog post I created a slax script that would tell you which interfaces that had errors on it. I recreated this script again using Python so a NetOps engineer could check it remotely. This could be useful if you have multiple networking equipment and want to track down what may be causing packet loss.

Somethings to consider. I'm using the juniper python library from GITHUB https://github.com/jeremyschulman/py-junos-eznc

There are a few things to note. The library to see the relevant python methods can be found here.

/Library/Python/2.7/site-packages/jnpr/junos/op

There is a YAML file phyport.yml which shows what each rpc and attributes are supported.


PhyPortErrorTable:
  rpc: get-interface-information
  args:
    extensive: True
    interface_name: '[fgx]e*'
  args_key: interface_name
  item: physical-interface
  view: PhyPortErrorView

PhyPortErrorView:
  groups:
    ts: traffic-statistics
    rxerrs: input-error-list
    txerrs: output-error-list

  # fields that are part of groups are called
  # "fields_<group-name>"

  fields_ts:
    rx_bytes: { input-bytes: int }
    rx_packets: { input-packets: int }
    tx_bytes: { output-bytes: int }
    tx_packets: { output-packets: int }

  fields_rxerrs:
    rx_err_input: { input-errors: int }
    rx_err_drops: { input-drops: int }
    rx_err_frame: { framing-errors: int }
    rx_err_runts: { input-runts: int }
    rx_err_discards: { input-discards: int }
    rx_err_l3-incompletes: { input-l3-incompletes: int }
    rx_err_l2-channel: { input-l2-channel-errors: int }
    rx_err_l2-mismatch: { input-l2-mismatch-timeouts: int }
    rx_err_fifo: { input-fifo-errors: int }
    rx_err_resource: { input-resource-errors: int }

  fields_txerrs:
    tx_err_carrier-transitions: { carrier-transitions: int }
    tx_err_output: { output-errors: int }
    tx_err_collisions: { output-collisions: int }
    tx_err_drops: { output-drops: int }
    tx_err_aged: { aged-packets: int }
    tx_err_mtu: { mtu-errors: int }
    tx_err_hs-crc: { hs-link-crc-errors: int }
    tx_err_fifo: { output-fifo-errors: int }
    tx_err_resource: { output-resource-errors: int }


script in action:

[mylaptop:~/scripts/PYTHON/] user% python error.py
host: 192.168.1.1
Interface    Error
xe-0/0/1     tx_err_carrier-transitions 6
xe-0/0/3     tx_err_carrier-transitions 7
ge-1/0/0     tx_err_drops 35994597
ge-1/0/0     tx_err_carrier-transitions 23
ge-1/1/0     tx_err_carrier-transitions 25
ge-1/1/1     tx_err_carrier-transitions 29
ge-1/1/3     rx_err_frame 1
ge-1/1/3     tx_err_carrier-transitions 31
ge-1/1/3     rx_err_input 1
ge-1/1/5     tx_err_carrier-transitions 11
ge-1/1/8     tx_err_carrier-transitions 8


-----------
SOURCE CODE
----------

from pprint import pprint as pp

from lxml import etree

from jnpr.junos import Device as Junos

from jnpr.junos.op.phyport import *

import json
import itertools

login = dict(user='user', host='192.168.1.1', password='password')

rtr = Junos(**login)

rtr.open()

ports = PhyPortTable(rtr).get()
stats = PhyPortErrorTable(rtr).get()


print "host: " + rtr.hostname
print "Interface\tError"



for port,stat in map(None,ports,stats):
   for attr in stat.FIELDS:
     if 'err' in attr:
       if getattr(stat,attr) != 0:
         print port.name.ljust(12), attr.ljust(12), getattr(stat, attr)



rtr.close()

No comments:

Post a Comment