Last active
March 2, 2018 18:56
-
-
Save mathershifter/61f7ef1a9712f3ea1b2b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# Copyright (c) 2016 Arista Networks, Inc. All rights reserved. | |
# Arista Networks, Inc. Confidential and Proprietary. | |
""" | |
Display PFC counter delta. First run will simply display current values | |
Requires: | |
paramiko (For SSH) - pip install paramiko | |
requests (For eAPI) - pip install requests | |
arcomm (2.0+) - pip install arcomm | |
Caveats: | |
This script does not handle counter resets/flips, so it is possible to get a | |
negative value | |
""" | |
from __future__ import (absolute_import, division, print_function, | |
unicode_literals) | |
import arcomm | |
import argparse | |
import getpass | |
import tempfile | |
import os | |
import json | |
__version__ = '0.1.0' | |
__author__ = 'Jesse Mather <[email protected]' | |
def parse_args(): | |
parser = argparse.ArgumentParser(prog="arcomm") | |
arg = parser.add_argument | |
arg("endpoints", nargs="+", | |
help="Specifies one or more hosts to collect counter data") | |
arg("-v", "--version", action="store_true", help="Display version info") | |
arg("-u", "--username", help="Specifies the username on the switch") | |
arg("-p", "--password", help="Specifies users password.") | |
arg("--protocol", default="ssh", help="Specifies the protocol (ssh or eapi)") | |
return parser.parse_args() | |
def main(): | |
args = parse_args() | |
tempdir = tempfile.gettempdir() | |
if args.version: | |
parser.exit(0, __version__ + "\n") | |
if not args.username: | |
args.username = getpass.getuser() | |
if args.password is None: | |
args.password = getpass.getpass('Password: ') | |
creds = arcomm.BasicCreds(args.username, args.password) | |
script = ['show interfaces priority-flow-control counters'] | |
for response in arcomm.batch(args.endpoints, script, creds=creds, | |
protocol=args.protocol): | |
# generate path to snapshot file | |
filename = 'pfc-counters-snapshot-{}'.format(response.host) | |
path = os.path.join(tempdir, filename) | |
prev = {} | |
if os.path.exists(path): | |
with open(path, 'r') as fh: | |
prev = json.loads(fh.read()) | |
counters = {} | |
for line in str(response[0]).splitlines(): | |
if line.startswith('Port'): | |
continue | |
port, rx, tx = line.split() | |
rx = int(rx) | |
tx = int(tx) | |
prx, ptx = [0, 0] | |
if port in prev: | |
prx, ptx = prev[port] | |
print("{}\t{}\t{}\t{}".format(response.host, port, tx - ptx, rx - prx)) | |
counters[port] = [int(rx), int(tx)] | |
# overwrite the old snapshot | |
with open(path, 'w') as fh: | |
fh.write(json.dumps(counters)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment