-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.py
238 lines (182 loc) · 6.6 KB
/
init.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
from config import *
from flask import Flask, render_template, url_for, request, jsonify, Response, copy_current_request_context
import random, time, json, urllib2, requests
from threading import Thread, current_thread, Event
from contextlib import closing
from Queue import Queue
from flask_socketio import SocketIO, send, emit
async_mode = None
if async_mode is None:
try:
import eventlet
except ImportError:
pass
if async_mode is None:
try:
from gevent import monkey
async_mode = 'gevent'
except ImportError:
pass
if async_mode is None:
async_mode = 'threading'
print "async_mode is ", async_mode
if async_mode == 'eventlet':
import eventlet
eventlet.monkey_patch()
elif async_mode == 'gevent':
from gevent import monkey
monkey.patch_all()
app = Flask(__name__)
thread = None
threads = {}
#Renders the home page of the application server.
@app.route('/')
@app.route('/home')
def home():
#print url_for('static', filename='../js/statistics.js')
return render_template('home.html', title="Home")
""" Renders an html page which shows the list of containers in the docker swarm,
and if the user provides a container_id also then the info about that container is displayed.
"""
@app.route('/container/')
@app.route('/container/<container_id>')
def container_info(container_id=None):
if container_id != None:
print "Rendering graph template with id : ", container_id
return render_template('container_info.html', title="Container Info", container_id=container_id)
else:
print "Rendering to display all container ids"
return render_template('container_info.html', title="Container Info")
@app.route('/threshold/')
def threshold():
return render_template('threshold.html')
@app.route('/error_report/')
def error_report():
return render_template('error_report.html')
""" THIS NEEDS TO BE REPLACED BY SOCKETIO METHOD"""
""" Rest api that serves the json data for various ajax requests """
@app.route('/jsondata/')
@app.route('/jsondata/<container_id>')
def json_data(continer_id=None):
if container_id != None:
data = requests.get(SERVER_ADDRESS + '/containers/' + container_id + '/json')
data = data.json()
return Response(json.dumps(data), mimetype='application/json')
else:
time.sleep(1)
data = {'cpu_usage': [random.randrange(0, 100) for x in range(2)], 'memory_usage': random.randrange(0, 100), 'network_usage' : random.randrange(0, 100), 'io':random.randrange(0, 100)}
print data
return jsonify(data)
@app.route('/jsondata_containers')
def json_data_containers():
data = requests.get(SERVER_ADDRESS + '/containers/json?all=1')
data = data.json()
data = Response(json.dumps(data), mimetype='application/json')
emit('container_ids', data, broadcast=True)
return data
@app.route('/container_stats_one/<container_id>')
def container_stats_one(container_id=None):
if container_id != None:
resp = requests.get(SERVER_ADDRESS + '/containers/' + container_id + '/stats?stream=false')
data = resp.json()
return Response(json.dumps(data), mimetype='application/json')
else:
return jsonify('{}')
@app.route('/container_stats_stream/<container_id>')
def container_stats_stream(container_id=None):
if container_id != None:
resp = requests.get(SERVER_ADDRESS + '/containers/' + container_id + '/stats?stream=true')
data = resp.json()
return Response(json.dumps(data), mimetype='application/json')
else:
return jsonify('{}')
socketio = SocketIO(app, async_mode=async_mode)
threads = {}
@socketio.on('channel_container_info_stream')
def collect_container_info_stream(req):
print "Request received from browser for container info stream: ", req
container_id = req
if threads.has_key(container_id):
print "Request is already being served"
return
@copy_current_request_context
def background_worker_container_info_stream():
start = time.time()
if True:
print "Running on the worker thread for container stream : "+container_id
with closing(requests.get(SERVER_ADDRESS + '/containers/' + container_id + '/stats?stream=true', stream=True)) as rest_resp:
for line in rest_resp.iter_lines():
if line:
data = line
end = time.time()
print current_thread()," : ", end - start, " sec"
start = end
emit('channel_container_info_stream' + '_'+container_id, data, broadcast=True)
lthread = Thread(target=background_worker_container_info_stream)
lthread.daemon = True
lthread.start()
threads[container_id] = lthread
@socketio.on('channel_container_info_json')
def collect_channel_container_info_json(req):
print "Request received from browser for intial json data: ", req
container_id = req
@copy_current_request_context
def background_worker_channel_container_info_json():
if True:
print "Running on the worker thread that exits after giving json data"
data = requests.get(SERVER_ADDRESS + '/containers/' + container_id + '/stats?stream=false')
data = data.json()
print type(data), data['memory_stats']['stats']
for key, val in data.iteritems():
print key
if data['memory_stats']['stats'] == None:
data = 'false'
print data
emit('channel_container_info_json', data)
lthread = Thread(target=background_worker_channel_container_info_json)
lthread.daemon = True
lthread.start()
""" For listing all the containers"""
@socketio.on('channel_container_ids_req')
def collect_container_ids(req):
print "Request received from browser: ", req
@copy_current_request_context
def background_worker():
if True:
print "Running on the worker thread"
data = requests.get(SERVER_ADDRESS + '/containers/json?all=1')
data = data.json()
print "Got response"
#print data
emit('channel_container_ids_resp', data)
lthread = Thread(target=background_worker)
lthread.daemon = True
lthread.start()
from dbhelper import MConnection
def save_to_db_worker(db_con):
while True:
data = requests.get(SERVER_ADDRESS + '/containers/json')
data = data.json()
container_id_dict = {}
for container in data:
status = container['Status']
if status.startswith('Up'):
container_id = container['Id']
container_id_dict[container_id] = 1
for container_id, status in container_id_dict.iteritems():
if status != 0:
data = requests.get(SERVER_ADDRESS + '/containers/' + container_id + '/stats?stream=false')
data = data.content
if data.startswith("No such container"):
container_id_dict[continer_id] = 0
else:
db_con.save_data(container_id, data)
time.sleep(60)
if __name__ == "__main__":
db_con = MConnection(debug=2)
db_thread = Thread(target=save_to_db_worker, args=(db_con,))
db_thread.daemon = True
db_thread.start()
socketio.run(app, host='', port=5000)
#app.debug = True
#app.run('', port=3000)