-
Notifications
You must be signed in to change notification settings - Fork 10
/
switchport_web.py
216 lines (192 loc) · 5.59 KB
/
switchport_web.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
from collections import Counter
from flask import Flask, render_template
from flask_bootstrap import Bootstrap
import switchdb
app = Flask(__name__)
@app.route("/", methods=["GET"])
def switch_inventory():
"""
Main web page, displays summary statistics of all switches
"""
lastupdate = getLastUpdate()
switchdata = getSwitchInfo()
return render_template("main.html", switches=switchdata, lastupdate=lastupdate)
@app.route("/<serial>", methods=["GET"])
def switch_info(serial):
"""
This page shows detailed stats on an individual switch
queried by serial number
"""
detail = getSwitchDetail(serial)
intdetail = getInterfaceDetail(serial)
try:
raw_data = open(f"raw_output/{serial}.txt", "r").read().splitlines()
except:
raw_data = "None collected yet"
return render_template(
"detail.html",
title=serial,
switch=detail,
interfaces=intdetail,
raw_data=raw_data,
)
@app.route("/network-wide", methods=["GET"])
def network_wide():
"""
This page shows a summary of all port counts, etc
across the entire network
"""
network = getNetworkWide()
return render_template("network-wide.html", network=network)
@app.route("/lastupdate", methods=["GET"])
def getLastUpdate():
"""
Check DB for last runtime of backend script
This is published on the main page to see when stats were last updated
"""
swDB = switchdb.DB()
lastupdate = swDB.getLastUpdate()
swDB.close()
return lastupdate
def getSwitchInfo():
"""
Query DB for summary info on all
switches currently monitored
"""
swDB = switchdb.DB()
raw_info = swDB.getAllSummary()
switchList = []
for row in raw_info:
row = list(row)
switch = {}
switch["name"] = row[0]
switch["serial"] = row[1]
switch["swver"] = row[2]
switch["ip"] = row[3]
switch["check"] = row[4]
switch["total"] = row[5]
switch["up"] = row[6]
switch["down"] = row[7]
switch["disabled"] = row[8]
if switch["total"] == 0:
switch["capacity"] = 0
else:
switch["capacity"] = (switch["up"] / switch["total"]) * 100
switchList.append(switch)
swDB.close()
return switchList
def getSwitchDetail(serial):
"""
Query DB for details on one specific device
by serial number
"""
swDB = switchdb.DB()
raw_info = swDB.getSwitchDetail(serial)
switch = {}
for row in raw_info:
switch["name"] = row[0]
switch["serial"] = row[1]
switch["model"] = row[2]
switch["swver"] = row[3]
switch["ip"] = row[4]
switch["check"] = row[5]
switch["total"] = row[6]
switch["up"] = row[7]
switch["down"] = row[8]
switch["disabled"] = row[9]
switch["int10m"] = row[10]
switch["int100m"] = row[11]
switch["int1g"] = row[12]
switch["int10g"] = row[13]
switch["int25g"] = row[14]
switch["int40g"] = row[15]
switch["int100g"] = row[16]
switch["copper"] = row[17]
switch["sfp"] = row[18]
switch["virtual"] = row[19]
if switch["total"] == 0:
switch["capacity"] = 0
else:
switch["capacity"] = int((switch["up"] / switch["total"]) * 100)
swDB.close()
return switch
def getInterfaceDetail(serial):
"""
Query DB for interface details on one specific device
by management IP
"""
swDB = switchdb.DB()
raw_info = swDB.getInterfaceDetail(serial)
interfaceList = []
for row in raw_info:
row = list(row)
interface = {}
interface["name"] = row[0]
interface["description"] = row[1]
interface["physical_address"] = row[2]
interface["oper_status"] = row[3]
interface["oper_speed"] = row[4]
interface["oper_duplex"] = row[5]
interfaceList.append(interface)
return interfaceList
def getNetworkWide():
"""
Query DB for all switch statistcs,
then tally results & return to web page
"""
swDB = switchdb.DB()
result = swDB.getNetworkWideStats()
swDB.close()
network = {
"models": [],
"swvers": [],
"total": 0,
"up": 0,
"down": 0,
"disabled": 0,
"int10m": 0,
"int100m": 0,
"int1g": 0,
"int10g": 0,
"int25g": 0,
"int40g": 0,
"int100g": 0,
"copper": 0,
"sfp": 0,
"virtual": 0,
}
modellist = []
swlist = []
for row in result:
if "N/A" not in row[0]:
modellist.append(row[0])
if "N/A" not in row[1]:
swlist.append(row[1])
network["total"] += row[2]
network["up"] += row[3]
network["down"] += row[4]
network["disabled"] += row[5]
network["int10m"] += row[6]
network["int100m"] += row[7]
network["int1g"] += row[8]
network["int10g"] += row[9]
network["int25g"] += row[10]
network["int40g"] += row[11]
network["int100g"] += row[12]
network["copper"] += row[13]
network["sfp"] += row[14]
network["virtual"] += row[15]
# Get 5 most common models / software versions
network["models"] = Counter(modellist).most_common(5)
network["swvers"] = Counter(swlist).most_common(5)
return network
def deleteDevice(serial):
"""
Call to DB to delete a device by serial number
"""
swDB = switchdb.DB()
swDB.deleteBySerial(serial)
swDB.close()
if __name__ == "__main__":
Bootstrap(app)
app.run(debug=True)