-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgimmeCRC.py
74 lines (60 loc) · 2.33 KB
/
gimmeCRC.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env Python
#
# cpaggen
#
# no error checks - this is a very simple REST API 'poster' that pushes data to APIC
#
import requests
import sys
import json
from pprint import pprint
import time
http_header={"User-Agent" : "Chrome/17.0.963.46",
"Accept" : "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,text/png,*/*;q=0.5",
"Accept-Language" : "en-us,en;q=0.5",
"Accept-Charset" : "ISO-8859-1",
"Content-type": "application/x-www-form-urlencoded"
}
def getAPICCookie(ip_addr, username, password):
url = 'http://'+ip_addr+'/api/aaaLogin.xml'
http_header["Host"]=ip_addr
xml_string = "<aaaUser name='%s' pwd='%s'/>" % (username, password)
req = requests.post(url, data=xml_string, headers=http_header, verify=False)
rawcookie=req.cookies['APIC-cookie']
return rawcookie
def sendAPICRequest(ip_addr, cookie, apicurl):
url = 'http://'+ip_addr+apicurl
http_header["Host"]=ip_addr
cookies = {}
cookies['APIC-cookie'] = cookie
req = requests.get(url,headers=http_header,cookies=cookies, timeout=60)
return req.text
#################
# MAIN MODULE #
#################
if len(sys.argv) != 8:
ip=raw_input("IP address of APIC? ")
user=raw_input("Admin username? ")
password=raw_input("Password? ")
leafnum=raw_input("Leaf number [i.e. 101, 1103, etc.]? ")
intf=raw_input("Interface [i.e. 1/1, 1/48, etc.]? ")
metric=raw_input("What to monitor [i.e. pkts,cRCAlignErrors, octets, broadcastPkts, etc.]? ")
interval=raw_input("Polling interval in seconds? ")
else:
ip,user,password,leafnum,intf,metric,interval = sys.argv[1:]
switchport = intf.split('/')
baseDN='/api/node/mo/topology/pod-1/node-{}/sys/phys-[eth{}/{}]/dbgEtherStats.json?query-target=self'.format(leafnum,switchport[0],switchport[1])
print("Querying {}".format(baseDN))
cookie=getAPICCookie(ip, user, password)
if cookie:
print("Cookie: {}\n".format(cookie))
while True:
r=sendAPICRequest(ip, cookie, baseDN)
if r:
parsed_json = json.loads(r)
parsedmetric = parsed_json['imdata'][0]['rmonEtherStats']['attributes'][metric]
print("\n{} = {}".format(metric, parsedmetric))
else:
print "That didn't work, we received no response back!"
sys.exit(1)
time.sleep(1)