In [1]:
from netmiko import ConnectHandler
%matplotlib inline
import pandas, matplotlib
from datetime import datetime
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
from matplotlib.lines import Line2D
from pandas import Series, DataFrame, Panel
from IPython.display import display
import ipywidgets as widgets
from io import StringIO
from pandas.tools.plotting import table
import numpy as np
import time
df1 = pandas.read_csv('Info.txt', names = ["Equipo","IP","User","Pass"])
df2 = pandas.read_csv('Commands.txt', names = ["Comando"])
In [2]:
command_list = df2.to_csv(None, header=False, index=False).split('\n')
def f(command_str):
command_list = command_str.split(',')
return [float(command_list[0]), int(command_list[1])]
command_list=command_list[:-1]
device_list = df1['Equipo'].to_csv(None, header=False, index=False).split('\n')
def f(device_str):
device_list = device_str.split(',')
return [float(device_list[0]), int(device_list[1])]
device_list=device_list[:-1]
ip_list = df1['IP'].to_csv(None, header=False, index=False).split('\n')
def f(ip_str):
ip_list = ip_str.split(',')
return [float(ip_list[0]), int(ip_list[1])]
ip_list=ip_list[:-1]
user_list = df1['User'].to_csv(None, header=False, index=False).split('\n')
def f(user_str):
user_list = user_str.split(',')
return [float(user_list[0]), int(user_list[1])]
user_list=user_list[:-1]
pass_list = df1['Pass'].to_csv(None, header=False, index=False).split('\n')
def f(pass_str):
pass_list = pass_str.split(',')
return [float(pass_list[0]), int(pass_list[1])]
pass_list=pass_list[:-1]
#command_list
In [3]:
a=0
for j in device_list:
# Create a dictionary representing the device.
cisco_881 = {
'device_type': 'cisco_ios',
'ip': ip_list[a],
'username': user_list[a],
'password': pass_list[a],
#'port' : 8022, # optional, defaults to 22
#'secret': 'secret', # optional, defaults to ''
#'verbose': False, # optional, defaults to True
}
equi=device_list[a]
net_connect = ConnectHandler(**cisco_881)
today = datetime.today().strftime("%y%m%d")
for row in command_list:
filename=row
filename = filename.replace('/', '')
time.sleep(2)
output = net_connect.send_command(str(row))
text_file = open(equi+'_'+filename+'.txt', "w")
text_file.write(output)
text_file.close()
a=a+1
#print (output)
In [50]:
a=0
v=0
dict1 = ["===","====","====","====","====","====","===="]
for j in device_list:
equi=j
s = StringIO()
with open(equi+'_show crash list.txt') as f:
for line in f:
v=v+1
if v is 1:
fecha=line
elif not (line.startswith(' ') or line.startswith('=')):
while ' ' in line:
line = line.replace(' ', ' ')
s.write(line)
s.seek(0) # "rewind" to the beginning of the StringIO object
h= pandas.read_csv(s, names = ["#","Time", "Process","Card/CPU/PID","SW Version","HW_SER_NUM"], sep=' ')
#h.columns=h[:1]
if (h[-1:].Time == "crash").item():
h[-1:]=["No crash","No crash","No crash","No crash","No crash","No crash"]
f=h[-1:]
else:
f=h[-3:-1:]
f.insert(0, 'EQUIPO', equi)
f.loc[-1]=dict1
#print(f)
if a is 0:
a=a+1
result=f.copy()
else:
result1 = result.append(f, ignore_index=True)
result=result1
result
Out[50]:
In [48]:
dict1 = ["===","====","====","===="]
a=0
for j in device_list:
equi=j
s = StringIO()
with open(equi+'_show session disconnect-reasons.txt') as f:
for line in f:
if not (line.startswith(' ') or line.startswith('=')):
while ' ' in line:
line = line.replace(' ', ' ')
s.write(line)
s.seek(0)#ewind" to the beginning of the StringIO object
h= pandas.read_csv(s, skiprows=5, sep=' ')
j=(h.groupby(['Disconnect Reason'], as_index=False).sum()).sort('Percentage', ascending=False)[:5]
j.insert(0, 'EQUIPO', equi)
j.loc[-1]=dict1
result.append(dict1, ignore_index=True)
#print(f)
if a is 0:
a=a+1
result=j.copy()
else:
result1 = result.append(j, ignore_index=True)
result=result1
result
j
Out[48]:
In [68]:
dict1 = ["===","====","====","===="]
a=0
for j in device_list:
equi=j
s = StringIO()
with open(equi+'_show context all.txt') as f:
for line in f:
if not (line.startswith(' ') or line.startswith('-')):
while ' ' in line:
line = line.replace(' ', ' ')
s.write(line)
s.seek(0)#ewind" to the beginning of the StringIO object
h= pandas.read_csv(s, header=1, sep=' ')
#j=(h.groupby(['Disconnect Reason'], as_index=False).sum()).sort('Percentage', ascending=False)[:5]
h.columns=["Context Name","ContextID", "State","D","DA"]
j=h[['Context Name','ContextID', 'State']]
j.insert(0, 'EQUIPO', equi)
j.loc[-1]=dict1
#result.append(dict1, ignore_index=True)
#print(f)
if a is 0:
a=a+1
result=j.copy()
else:
result1 = result.append(j, ignore_index=True)
result=result1
result
Out[68]:
In [291]:
index1= ["ASU-ASR5K5-1","COR-ASR5K-1","JO2-ASR5K5-1","ROS-ASR5K-1","TOR-ASR5K5-1","ASUGGSN1","MONGGSN1"]
val = widgets.RadioButtons(
options=index1,
# value='2',
description='Contador:',
)
display(val)
In [296]:
i=val.value
s = StringIO()
s.write("Endpoint")
a=0
b=0
with open(i+'_show diameter peer full all.txt') as f:
for line in f:
if (line.startswith('Context:') or (a==0)):
s = StringIO()
a=1
while ' ' in line:
line = line.replace(' ', ' ')
line = line.replace('Context:', '')
line = line.replace('Endpoint:', '-')
line = line.replace('\n', '')
context=line
s.write(line)
elif (line.startswith(' Peer Hostname:')):
line = line.replace('Peer Hostname: ', '')
line = line.replace('\n', '')
s.write(line)
peer=line
elif (line.startswith(' Local Hostname:')):
line = line.replace('Local Hostname: ', '')
line = line.replace('\n', '')
s.write(line)
host=line
elif (line.startswith(' State:')):
line = line.replace('State:', '')
line = line.replace('[TCP]', '')
line = line.replace('\n', '')
s.write(line)
state=line
elif (line.startswith(' CPU:')):
line = line.replace('CPU:', '')
line = line.replace('Task:', '-')
line = line.replace('\n', '')
s.write(line)
cpu=line
elif (line.startswith(' Peer Backoff Timer running:')):
s.seek(0)#ewind" to the beginning of the StringIO object
h= pandas.read_csv(s)
h1=h.T
#print(h1)
h1.insert(0,'CPU - Task', cpu)
h1.insert(0,'State', state)
h1.insert(0,'Peer Hostname', host)
h1.insert(0,'Local Hostname', peer)
h1.insert(0,'Context - Endpoint', context)
h1.insert(0, 'EQUIPO', i)
#a=0
s = StringIO()
s.seek(0)
s.write(context)
#h1.columns=["context","peer","local","state","CPU","a","b","c","d"]
#print(h1)
if b is 0:
b=1
result=h1.copy()
else:
result1 = result.append(h1, ignore_index=True)
result=result1
#h.columns=["Context Name/Endpoint"]
result2=(result.sort(['State','Context - Endpoint','Local Hostname' ], ascending=True))
result4=result2.copy()
result4['Total Peers']=1
result3=(result4.groupby(['State'], as_index=False).sum()).sort('State', ascending=False)
result3
Out[296]:
In [71]:
#i=sm3.value
s = StringIO()
dict1 = ["===","===="]
b=0
result=[]
result1=[]
result3=[]
resultfinal=[]
for j in device_list:
equi=j
s = StringIO()
with open(j+'_show snmp trap history verbose.txt') as f:
for line in f:
if not (line.startswith(' ') or line.startswith('-')):
#line = line.replace('Internal', '|')
#line = line.replace(') ', '|')
lin=line[line.find("(")+1:line.find(")")]
#lin1=line[line.find(")")+1:]
#lin2=line[:line.find("Internal")]
totaline= [equi,lin]
#print (totaline)
b=b+1
if b is 5:
h=pandas.DataFrame(totaline)
h1=h.T
result=h1.copy()
#result.loc[-1]=dict1
elif b > 5:
h=pandas.DataFrame(totaline)
h1=h.T
result1 = result.append(h1, ignore_index=False)
result=result1
#result.loc[-1]=dict1
result.columns=["Device","Facility"]
resultfinal=result.drop_duplicates(subset=['Device','Facility'])
resultfinal
Out[71]:
In [11]:
index1=device_list
sm3 = widgets.RadioButtons(
options=index1,
# value='2',
description='Contador:',
)
display(sm3)
In [12]:
i=sm3.value
s = StringIO()
b=0
result=[]
result1=[]
with open(i+'_show snmp trap history verbose.txt') as f:
equi=i
for line in f:
if not (line.startswith(' ') or line.startswith('-')):
#line = line.replace('Internal', '|')
#line = line.replace(') ', '|')
lin=line[line.find("(")+1:line.find(")")]
lin1=line[line.find(")")+1:]
lin2=line[:line.find("Internal")]
totaline= [equi,lin2,lin,lin1]
#print (totaline)
b=b+1
if b is 5:
h=pandas.DataFrame(totaline)
h1=h.T
result=h1.copy()
elif b > 5:
h=pandas.DataFrame(totaline)
h1=h.T
result1 = result.append(h1, ignore_index=False)
result=result1
result.columns=["Equipo","Time","Facility","Description"]
index1=result['Facility'].drop_duplicates().values.tolist()
sm2 = widgets.SelectMultiple(
options=index1,
description='Warning:')
display(sm2)
In [13]:
resulttotal=result[result['Facility'].isin(sm2.value)]
resulttotal
Out[13]:
In [55]:
a=0
s = StringIO()
b=0
result=[]
result1=[]
for j in device_list:
equi=j
s = StringIO()
with open(equi+'_show version.txt') as f:
for line in f:
if (line.startswith(' Image Version')):
line = line.replace(' Image Version: ', '')
line = line.replace('\n', '')
s.write(line)
version=line
elif (line.startswith(' Boot Image')):
s = StringIO()
s.seek(0)
a=1
line = line.replace(' Boot Image: ', '')
line = line.replace('\n', '')
s.write(line)
boot=line
totaline= [equi,version,boot]
b=b+1
if b < 2:
h=pandas.DataFrame(totaline)
h1=h.T
result=h1.copy()
else :
h=pandas.DataFrame(totaline)
h1=h.T
result1 = result.append(h1, ignore_index=False)
result=result1
result.columns=["Device","Version","Boot Image"]
result
Out[55]:
In [41]:
# result=[]
dict1 = ["===","====","====","====","===","====","====","====","===="]
b=0
for j in device_list:
equi=j
path = '_show session recovery status verbose.txt'
col_specification =[(1, 5), (5, 13), (14, 21), (21, 29), (30, 37), (37, 45), (46, 53), (54, 80),]
data = pandas.read_fwf(equi+path, colspecs=col_specification)
h1=data.iloc[7:]
h1.columns=data.loc[5]
h1.insert(0, 'EQUIPO', j)
h1.loc[-1]=dict1
b=b+1
if b < 2:
#h=pandas.DataFrame(totaline)
#h1=h.T
result=h1.copy()
else :
#h=pandas.DataFrame(totaline)
#h1=h.T
result1 = result.append(h1, ignore_index=False)
result=result1
#result.columns=["Device","Version","Boot Image"]
result
Out[41]:
In [37]:
b=0
j=0
a=0
dict1 = ["===","====","====","====","===","==="]
for d in device_list:
equi=d
s = StringIO()
b=0
j=0
n=0
with open(equi+'_show alarm outstanding verbose.txt') as f:
for line in f:
j=j+1
if (line.startswith('Informational') or line.startswith('Minor') or line.startswith('Major') or line.startswith('Warning') or line.startswith('Critical')):
s.write(line)
col_names = ['Severity','Objects','Timestamp','Alarm ID']
col_specification =[(0, 8), (9, 25), (25, 59), (60, 80),]
data = pandas.read_fwf(equi+'_show alarm outstanding verbose.txt', colspecs=col_specification , nrows=1, skiprows=j-1, names=col_names)
b=1
elif b is 1:
col_names = ['Details']
col_specification =[(0, 80),]
data1 = pandas.read_fwf(equi+'_show alarm outstanding verbose.txt', colspecs=col_specification , nrows=1, skiprows=j-1, names=col_names)
b=0
data.insert(4, 'Description', data1)
data.insert(0, 'Equipo', equi)
if a is 0:
result=data.copy()
n=1
a=1
else :
result1 = result.append(data, ignore_index=False)
result=result1
n=1
result
Out[37]:
In [52]:
b=0
dict1 = ["===","====","====","====","===","==="]
for j in device_list:
equi=j
path = '_show card table.txt'
col_specification =[(0, 11), (13, 52), (53, 67), (68, 73), (75, 81),]
col_names = ['Slot','Card Type','Oper State','SPOF','Attach']
data = pandas.read_fwf(equi+path, colspecs=col_specification, names=col_names)
h1=data.iloc[3:]
h1.insert(0, 'EQUIPO', j)
b=b+1
if b < 2:
#h=pandas.DataFrame(totaline)
#h1=h.T
h1.loc[-1]=dict1
result=h1.copy()
else :
#h=pandas.DataFrame(totaline)
#h1=h.T
h1.loc[-1]=dict1
result1 = result.append(h1, ignore_index=False)
result=result1
#result.columns=["Device","Version","Boot Image"]
result
filtered = result[(result['Card Type']!='None')]
filtered
Out[52]:
In [286]:
dict1 = ["===","====","====","====","===","===","==="]
b=0
for u in device_list:
equi=u
with open(equi+'_show resources.txt') as f:
for line in f:
if (line.startswith(' ')):
if (line.startswith(' In Use')):
line1 = line.replace(' In Use : ', '')
line1 = line1.replace('\n', '')
line1 = line1.replace(' ', '')
pandas.to_numeric(line1, errors='coerce')
a=1
elif (line.startswith(' Max Used') & (a is 1)):
line2 = line.replace(' Max Used : ', '')
line2 = line2.replace('\n', '')
line2 = line2.replace(' ', '')
#pandas.to_numeric(line2, errors='coerce')
d=1
elif (line.startswith(' Limit') & (d is 1)):
line3 = line.replace(' Limit : ', '')
line3 = line3.replace('\n', '')
line3 = line3.replace(' ', '')
pandas.to_numeric(line3, errors='coerce')
f=1
if f is 1:
l4=int(line1)
l5=int(line3)
l6=((l4/l5)*100)
l7=int(l6)
totaline= [line1,line2,line3,l7,equi]
h=pandas.DataFrame(totaline)
h1=h.T
h1.colums=["InUse","MaxUsed","Limit","Utilization","Equipo"]
f=0
resu=[]
resu = pandas.concat([h3, h1], axis=1)
resu.columns=["Service","Type","InUse","MaxUsed","Limit","Utilization","Equipo"]
#print(h3)
#print(h1)
if b is 0:
b=b+1
result=resu.copy()
else :
result1 = result.append(resu, ignore_index=False)
result=result1
#print (result)
elif (line.startswith(' ')):
a=0
d=0
f=0
s = StringIO()
line = line.replace('Service:', '|Servi')
s.write(line)
s.seek(0)
h3= pandas.read_csv(s, names = ["Servicio","Serv"], sep='|')
#result.loc[len(df)]=dict1
resultfinal=result[['Equipo','Service','InUse','MaxUsed','Limit','Utilization']]
resultfinal
Out[286]:
In [29]:
dict1 = ["===","====","====","====","===","===","==="]
b=0
s = StringIO()
for u in device_list:
equi=u
h=[]
with open(equi+'_directory hd-raid.txt') as f:
for line in f:
if (line.startswith('/dev/')):
while ' ' in line:
line = line.replace(' ', ' ')
s.write(line)
s.seek(0) # "rewind" to the beginning of the StringIO object
h= pandas.read_csv(s, names = ["Filesystem","Used", "Available","Use%","Mounted on"], sep=' ')
with open(equi+'_show hd raid verbose.txt') as f:
for line in f:
if (line.startswith(' State :')):
line = line.replace(' State : ', '')
line = line.replace('\n', '')
h['State']=line
elif (line.startswith(' Degraded :')):
line = line.replace(' Degraded : ', '')
line = line.replace('\n', '')
h['Degraded']=line
h['Device']=equi
if b is 0:
b=b+1
result=h.copy()
else :
result1 = result.append(h, ignore_index=True)
result=result1
resultfinal=result[['Device','Filesystem','Used', 'Available','Use%','Mounted on','State','Degraded']]
resultfinal
Out[29]:
In [122]:
#frame.a.str.contains
h=[]
dict1 = ["===","====","====","====","===","===","==="]
b=0
s = StringIO()
for u in device_list:
equi=u
h=[]
i=0
with open(equi+'_show rct stats.txt') as f:
for line in f:
if (line.endswith('sec\n')):
i=i+1
while ' ' in line:
line = line.replace(' ', ' ')
s.write(line)
s.seek(0) # "rewind" to the beginning of the StringIO object
h= pandas.read_csv(s, names = ["Action","Type","From","To","Start Time", "Duration", "Sec"], sep=' ')
h.insert(0, 'EQUIPO', equi)
if i >= 5:
h3=h.iloc[-5:]
else:
h3=h.iloc[-i:]
h2 = h3[(h3['Sec']=='sec')]
h1=h2[['EQUIPO','Action','Type','From','To','Start Time', 'Duration']]
b=b+1
if b < 2:
#h1.loc[-1]=dict1
result=h1.copy()
else :
#h1.loc[-1]=dict1
result1 = result.append(h1, ignore_index=False)
result=result1
result
Out[122]:
In [ ]:
b=0
dict1 = ["===","====","====","====","===","==="]
for j in device_list:
equi=j
path = '_show card table.txt'
col_specification =[(0, 11), (13, 52), (53, 67), (68, 73), (75, 81),]
col_names = ['Slot','Card Type','Oper State','SPOF','Attach']
data = pandas.read_fwf(equi+path, colspecs=col_specification, names=col_names)
h1=data.iloc[3:]
h1.insert(0, 'EQUIPO', j)
b=b+1
if b < 2:
#h=pandas.DataFrame(totaline)
#h1=h.T
h1.loc[-1]=dict1
result=h1.copy()
else :
#h=pandas.DataFrame(totaline)
#h1=h.T
h1.loc[-1]=dict1
result1 = result.append(h1, ignore_index=False)
result=result1
#result.columns=["Device","Version","Boot Image"]
result
filtered = result[(result['Card Type']!='None')]
filtered
In [96]:
b=0
for j in device_list:
equi=j
path = '_show session counters historical all.txt'
col_specification =[(1, 4), (6, 26), (27, 37), (38, 48), (49, 59), (60, 70), (71, 81), (82, 95), (96, 109), (110,123),]
col_names = ['Intv','Timestamp','Arrived','Rejected','Connected','Disconn','Failed','Handoffs','Renewals','CallOps']
data = pandas.read_fwf(equi+path, colspecs=col_specification, names=col_names)
h1=data.iloc[4:10]
h1.insert(0, 'EQUIPO', j)
h1.Connected = h1.Connected.astype(np.int64)
h1.Arrived = h1.Arrived.astype(np.int64)
#l6=((l4/l5)*100)
h1[equi]=(h1['Connected'] / h1['Arrived']*100)
h2=h1[['Timestamp',equi]]
h3=h2.T
h4=h3[1:]
b=b+1
if b < 2:
f= h1.Timestamp.tolist()
h4.columns=f
result=h4.copy()
else :
f= h1.Timestamp.tolist()
h4.columns=f
#h=pandas.DataFrame(totaline)
#h1=h.T
#h1.loc[-1]=dict1
result1 = result.append(h4, ignore_index=False)
result=result1
result2=result.T
result2=result2.reset_index()
result2.columns.values[0] = "Date"
result2
result3=(result2.groupby(['Date'], as_index=False).sum()).sort('Date', ascending=False)
result3.fillna('-')
result3
Out[96]:
In [ ]: