-
Notifications
You must be signed in to change notification settings - Fork 55
/
httpserver.py
163 lines (138 loc) · 4.87 KB
/
httpserver.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
#!/usr/bin/env python
#
# Copyright (C) 2013 Cisco Systems Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# This script implements the BaseHTTPServer class available within Python
# and displays a status page on your Nexus 9000 running on port 8081.
#
# This sample displays the route that is used to reach the device contacting
# the web server, and also displays the ping latency to the host
#
import cli
import json
import re
import BaseHTTPServer
class Route():
def printroute(self, j, d=0):
o = ''
whitelist = ['vrf-name-out', 'addrf', 'ipprefix', 'ucast-nhops', 'mcast-nhops', 'attached', 'uptime', 'pref', 'metric', 'clientname', 'ubest']
if isinstance(j, list):
for i in j:
o += self.printroute(i, d + 1)
else:
for k, v in j.items():
if k in whitelist:
o += '%s %s: %s\n' % ('-' * d, k, v)
if isinstance(v, dict) or isinstance(v, list):
o += self.printroute(v, d + 1)
return o
def title(self):
return 'Route to your IP'
def data(self, **kwargs):
s = kwargs['s']
ip = s.client_address[0]
r = json.loads(cli.clid('show ip route %s vrf management' % ip))
return self.printroute(r)
class Latency():
def title(self):
return 'Latency to your IP'
def data(self, **kwargs):
s = kwargs['s']
ip = s.client_address[0]
a = ''.join(cli.cli('ping %s vrf management count 1' % ip).split('\n'))
m = re.match('.*time=([0-9\.]+).*', a)
return float(m.group(1))
def HTMLBuilder():
return '''
<html>
<head>
<title>Nexus 9000</title>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" language="javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var options = {
title: 'Latency',
curveType: 'function',
legend: { position: 'bottom' }
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(google.visualization.arrayToDataTable(window.items), options);
}
$(document).ready(function() {
window.items = [['Time', 'Latency'], [getTimeHHMMSS(), 0]];
$.getJSON( "/route", function( data ) {
$("#route_div").html('<p><pre>' + data + '</pre></p>');
});
fetchData();
});
function padInt(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
function getTimeHHMMSS() {
var now = new Date();
var h = now.getHours();
var m = now.getMinutes();
var s = now.getSeconds();
m = padInt(m);
s = padInt(s);
return h + ":" + m + ":" + s;
}
function fetchData() {
$.getJSON( "/latency", function( data ) {
window.items.push( [getTimeHHMMSS(), data] );
});
// console.log(items);
drawChart();
if (window.items.length > 20) {
window.items.splice(1, 1);
}
setTimeout(fetchData, 1000);
}
</script>
</head>
<body>
<div id="chart_div" style="width: 800px; height: 400px;"></div>
<div id="route_div" style="width: 800px; height: 100px; top: 500;">Route</div>
</body>
<html>
'''
class httphandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET(s):
s.send_response(200)
s.send_header('Content-type', 'text/html')
s.end_headers()
if s.path == '/':
s.wfile.write(HTMLBuilder())
elif s.path == '/latency':
lat = Latency().data(s=s)
s.wfile.write(json.dumps(lat))
elif s.path == '/route':
rout = Route().data(s=s)
s.wfile.write(json.dumps(rout))
if __name__ == '__main__':
httpserver = BaseHTTPServer.HTTPServer
httpd = httpserver(('0.0.0.0', 8081), httphandler)
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()