-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patho.py
executable file
·773 lines (620 loc) · 25.3 KB
/
o.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
#!/usr/bin/env python
'''
both of these api are driven by the same script but distributed as (o), (a), and (b)
(d) is the private central data store for circuits and endpoints
webapp (p) to (o) orchestrator public api and consumse the information collected
orchestrator to endpoint private for circuit provisioning from (o) to endpoint (a) and endpoint (b)
'''
__author__ = 'xsited'
import os
import sys
import json
import argparse
import datetime
import socket
import threading
import select
from time import time, sleep, ctime
from collections import deque
from restapi import OClient
import subprocess as subps
try:
from flask import Flask, jsonify, abort, request, make_response, url_for
except ImportError:
logger.error( 'You will need the Flask python module installed to use this script' )
logger.error( 'You can install it using the following command' )
logger.error( 'sudo pip install Flask' )
sys.exit(1)
try:
from flask.ext.httpauth import HTTPBasicAuth
except ImportError:
logger.error( 'You will need the HTTPBasicAuth python module installed to use this script' )
logger.error( 'You can install it using the following command' )
logger.error( 'sudo pip install Flask-HTTPAuth' )
sys.exit(1)
try:
from flask.ext.restful import reqparse, abort, Api, Resource, request
except ImportError:
logger.error( 'You will need the Flask-RESTful python module installed to use this script' )
logger.error( 'You can install it using the following command' )
logger.error( 'sudo pip install Flask-RESTful' )
sys.exit(1)
try:
import daemonize
except ImportError:
daemonize = None
try:
import ping
except ImportError:
ping = None
logger.error( 'You will need the ping python module installed to use this script' )
logger.error( 'You can install it using the following command' )
logger.error( 'sudo pip install ping' )
sys.exit(1)
class oTimeout(Exception):
pass
class oError(Exception):
pass
app = Flask(__name__, static_url_path = "")
auth = HTTPBasicAuth()
oc = OClient()
pthread = threading.Thread()
@auth.get_password
def get_password(username):
if username == 'user':
return 'password'
return None
@auth.error_handler
def unauthorized():
return make_response(jsonify( { 'error': 'Unauthorized access' } ), 403)
# return 403 instead of 401 to prevent browsers from displaying the default auth dialog
@app.errorhandler(400)
def not_found(error):
return make_response(jsonify( { 'error': 'Bad request' } ), 400)
@app.errorhandler(404)
def not_found(error):
return make_response(jsonify( { 'error': 'Not found' } ), 404)
latency = 35
throughput = 100
circuits = [
{
'id': 1,
'service_type': 'epl',
'start_ip_address': u'192.168.1.1',
'end_ip_address': u'192.168.1.2',
'classifier': u'12',
'active': False,
'self': 'uri'
},
{
'id': 2,
'service_type': 'epl',
'start_ip_address': u'192.168.1.3',
'end_ip_address': u'192.168.1.4',
'classifier': u'red',
'active': False,
'self': 'uri'
},
]
import logging
# create logger ??? global conext or pass to class
logger = logging.getLogger(os.path.basename(__file__))
logger.setLevel(logging.INFO)
def logging_configure(console=False, debug=False):
if console==True:
# create console handler and set level to debug
ch = logging.StreamHandler()
if debug == True:
ch.setLevel(logging.DEBUG)
else:
ch.setLevel(logging.INFO)
# create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# add ch to logger
logger.addHandler(ch)
# add formatter to ch
ch.setFormatter(formatter)
if debug == True:
logger.setLevel(logging.DEBUG)
# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')
return logger
def c_circuit_delete(circuit_id,start_ip, end_ip, circuit_type='gre'):
if circuit_type == 'gre':
ovscfg_sh = '\
#!/bin/bash\n\n\
ovs-vsctl del-port gre0 \n\
'
else:
ovscfg_sh = '\
#!/bin/bash\n\n\
ovs-vsctl del-port vxlan \n\
'
with open('/tmp/ovscfg.sh', 'w') as f:
f.write(ovscfg_sh)
cmd = "/bin/bash /tmp/ovscfg.sh" # %s %s" % (data_start_ip, data_end_ip)
if subps.call(cmd.split(), stderr=file('ovscfg.err', 'w')):
raise oError()
def c_circuit_add(circuit_id,start_ip, end_ip, circuit_type='gre'):
'''
Configure circuit can be confgured at a couple different ReSTful RPC levels
Option 1:
Custom RPC - Call a custom RPC to configure the circuit
Option 2:
Call OVSDB json-rpc ovsdb.py is a PoC of this interface
Option 3:
ODL ReSTful NB rpc (which calls SB ODL OVSDB) - need example
Option 4:
Openstack Neutron ML2 rpc - need example
circuit_type currently is an enum of possibile implmenetation types
gre
vxlan
The endpoints should be addressed by management interfaces so status reporting is possble when link is down
start_ip = 10.0.0.133
end_ip = 10.0.0.134
'''
'''
This is the last minute mess to address the required interfaces because we don't have enough information on about an endpoint
'''
mylist = start_ip.split(".",4)
start_host = mylist[3]
print "start_host = %s" % start_host
mylist = end_ip.split(".",4)
end_host = mylist[3]
print "end_host = %s" % end_host
tunnel_start_ip="192.168.222." + start_host
print "tunnel_start_ip = %s" % tunnel_start_ip
tunnel_end_ip="192.168.222." + end_host
print "tunnel_end_ip = %s" % tunnel_end_ip
data_start_ip="10.36.0." + start_host
print "data_start_ip = %s" % data_start_ip
data_end_ip="10.36.0." + end_host
print "data_end_ip = %s" % data_end_ip
print "Remote Server = ", end_ip
'''
Consider this a CLI driver of sorts
This is the tunnel addressing assumed configuration
ovs-vsctl add-br isolated0
#ifconfig isolated0 192.168.222.133 up
ifconfig isolated0 tunnel_start_ip up
# steal eth2 from default openstack configuration (optional and internal note)
# ovs-vsctl del-port br-eth2 eth2
# ovs-vsctl add-port isolated0 eth2
cpe_interface='gre0'
ovs-vsctl add-port isolated0 cpe_interface
'''
if circuit_type == 'gre':
ovscfg_sh = '\
#!/bin/bash\n\
S=$1\n\
R=$2\n\
ovs-vsctl del-port gre0 \n\
ovs-vsctl add-port isolated0 gre0 -- set interface gre0 type=gre options:remote_ip=$R\n\
'
#ovs-vsctl -vjsonrpc add-port isolated0 gre0 -- set interface gre0 type=gre options:remote_ip=10.36.0.134
else:
ovscfg_sh = '\
#!/bin/bash\n\
S=$1\n\
R=$2\n\
ovs-vsctl del-port vxlan \n\
ovs-vsctl add-port isolated0 vxlan -- set interface vxlan type=vxlan options:key=10 options:local_ip=$L options:remote_ip=$R\n\
'
with open('/tmp/ovscfg.sh', 'w') as f:
f.write(ovscfg_sh)
#cmd = "screen -m -d /bin/bash /tmp/ovscfg.sh %s %s" % (data_start_ip, data_end_ip)
cmd = "/bin/bash /tmp/ovscfg.sh %s %s" % (data_start_ip, data_end_ip)
if subps.call(cmd.split(), stderr=file('ovscfg.err', 'w')):
raise oError()
'''
rpc_faultmonitor_start(circuit_id, interface, end_ip_address);
# ping -I isolated0 192.168.222.134
ping -I interface end_ip_address
ovs-vsctl add-br isolated0
ifconfig isolated0 192.168.222.134 up
# steal eth2 from default openstack configuration (optional and internal note)
# ovs-vsctl del-port br-eth2 eth2
ovs-vsctl add-port isolated0 eth2
if circuit_type == 'gre':
# ovs-vsctl -vjsonrpc add-port isolated0 gre0 -- set interface gre0 type=gre options:remote_ip=10.36.0.133
ovs-vsctl add-port isolated0 gre0 -- set interface gre0 type=gre options:remote_ip=10.36.0.133
else:
ovs-vsctl add-port isolated0 vxlan -- set interface vxlan type=vxlan options:key=10 options:local_ip=192.168.222.134 options:remote_ip=10.36.0.133
rpc_faultmonitor_start(circuit_id, interface, end_ip_address);
# ping -I isolated0 192.168.222.133
ping -I interface end_ip_address
'''
def print_circuit(circuit):
print "service_type = %s" % circuit[0]['service_type']
print "start_ip_address = %s" % circuit[0]['start_ip_address']
print "end_ip_address = %s" % circuit[0]['end_ip_address']
print "classifier = %s" % circuit[0]['classifier']
print "self = %s" % circuit[0]['self']
print "active = %s" % circuit[0]['active']
def make_public_circuit(circuit):
new_circuit = {}
for field in circuit:
if field == 'id':
new_circuit['uri'] = url_for('get_circuit', circuit_id = circuit['id'], _external = True)
else:
# print "field = ",field
new_circuit[field] = circuit[field]
return new_circuit
def make_circuit_status(circuit_id, active):
new_status = {}
if active:
new_status['status'] = "running"
else:
new_status['status'] = "suspended"
'''
JT: id is reserved
'''
def make_circuit_metrics(circuit_id, latency, throughput):
new_metrics = {}
new_metrics['id'] = circuit_id
new_metrics['latency'] = latency
new_metrics['throughput'] = throughput
new_metrics['latency_UnitMeasurement'] = "ms"
new_metrics['throughput_UnitMeasurement'] = "Mbps"
return new_metrics
@app.route('/orchestrator/api/v1.0/circuits', methods = ['GET'])
# @auth.login_required
def get_circuits():
return jsonify( { 'circuits': map(make_public_circuit, circuits) } )
@app.route('/orchestrator/api/v1.0/hello', methods = ['GET'])
# @auth.login_required
def get_hello():
return jsonify( { 'hello': 'world' } )
@app.route('/orchestrator/api/v1.0/circuits/<int:circuit_id>', methods = ['GET'])
# @auth.login_required
def get_circuit(circuit_id):
circuit = filter(lambda t: t['id'] == circuit_id, circuits)
if len(circuit) == 0:
abort(404)
return jsonify( { 'circuit': make_public_circuit(circuit[0]) } )
@app.route('/orchestrator/api/v1.0/circuits', methods = ['POST'])
# @auth.login_required
def create_circuit():
print "Create circuit"
print request.json
# if not request.json : #or not 'service_type' in request.json:
# abort(400)
print "Build circuit"
circuit = {
'id': circuits[-1]['id'] + 1,
'service_type': request.json['service_type'],
'start_ip_address': request.json.get('start_ip_address', ""),
'end_ip_address': request.json.get('end_ip_address', ""),
'classifier': request.json.get('classifier', ""),
'self': request.json.get('self', ""),
'active': False
}
print "Append circuit"
circuits.append(circuit)
print "Call computes to create circuit"
#c_circuit = dict(circuit)
#c_circuit['self']='http://10.0.0.192:5555/orchstrator/api/v1.0/reportstatus/%d'%circuit['id']
oc.c_circuit_create_on_server(circuit, circuit['start_ip_address'])
oc.c_circuit_create_on_server(circuit, circuit['end_ip_address'])
print "Return circuit"
return jsonify( {
'status': 'ok',
'id': circuit['id'],
'circuit': make_public_circuit(circuit)
}
), 201
@app.route('/debug', methods = ['PUT'])
# @auth.login_required
def debug():
print "Debug circuit"
json = request.json
print(json)
# Render template
return jsonify(json)
@app.route('/orchestrator/api/v1.0/circuits/<int:circuit_id>', methods = ['PUT'])
# @auth.login_required
def update_circuits(circuit_id):
circuit = filter(lambda t: t['id'] == circuit_id, circuits)
print "Find circuit"
if len(circuit) == 0:
abort(404)
# print "Validate circuit data"
'''
if not request.json:
abort(400)
if 'service_type' in request.json and type(request.json['service_type']) != unicode:
abort(400)
if 'start_ip_address' in request.json and type(request.json['start_ip_address']) is not unicode:
abort(400)
if 'active' in request.json and type(request.json['active']) is not bool:
abort(400)
'''
# print "Update circuit data"
print_circuit(circuit)
'''
print "service_type = %s" % circuit[0]['service_type']
'''
circuit[0]['start_ip_address'] = request.json.get('start_ip_address', circuit[0]['start_ip_address'])
circuit[0]['self'] = request.json.get('self', circuit[0]['self'])
circuit[0]['end_ip_address'] = request.json.get('end_ip_address', circuit[0]['end_ip_address'])
circuit[0]['classifier'] = request.json.get('classifier', circuit[0]['classifier'])
circuit[0]['service_type'] = request.json.get('service_type', circuit[0]['service_type'])
circuit[0]['active'] = request.json.get('active', circuit[0]['active'])
print "Return something "
return jsonify( {
'status': 'ok',
'result': circuit[0]['id'] ,
'circuit': make_public_circuit(circuit[0])
}
), 201
# return jsonify( { 'circuit': make_public_circuit(circuit[0]) } )
@app.route('/orchestrator/api/v1.0/circuits/<int:circuit_id>', methods = ['DELETE'])
# @auth.login_required
def delete_circuits(circuit_id):
circuit = filter(lambda t: t['id'] == circuit_id, circuits)
if len(circuit) == 0:
abort(404)
print "Call computes to delete circuit"
oc.c_circuit_delete_on_server(circuit_id, circuit[0]['start_ip_address'])
oc.c_circuit_delete_on_server(circuit_id, circuit[0]['end_ip_address'])
circuits.remove(circuit[0])
return jsonify( { 'status':'ok','result': circuit[0]['id'] } )
@app.route('/orchestrator/api/v1.0/reportmetrics/<int:circuit_id>', methods = ['PUT'])
# @auth.login_required
def report_metrics(circuit_id):
global latency
global throughput
circuit = filter(lambda t: t['id'] == circuit_id, circuits)
if len(circuit) == 0:
abort(404)
latency = request.json.get('latency', latency)
throughput = request.json.get('throughput', throughput)
return jsonify( { 'status':'ok', 'result': True } )
@app.route('/orchestrator/api/v1.0/reportstatus/<int:circuit_id>', methods = ['PUT'])
# @auth.login_required
def report_status(circuit_id):
circuit = filter(lambda t: t['id'] == circuit_id, circuits)
if len(circuit) == 0:
abort(404)
tempactive = circuit[0]['active']
s = request.json.get('status', "")
if s == 'running':
print "status ", s
active = circuit[0]['active'] = True
else:
print "status ", s
active = circuit[0]['active'] = False
print "active ", active
if tempactive == active:
print "status no change"
else:
oc.o_report_status_self(circuit_id, circuit[0]['self'], make_circuit_status(circuit_id,circuit[0]['active']))
return jsonify( { 'status':'ok', 'result': True } )
#@app.route('/orchestrator/api/v1.0/performancemetrics/<int:circuit_id>', methods = ['PUT'])
# @auth.login_required
#def report_metrics(circuit_id):
# circuit = filter(lambda t: t['id'] == circuit_id, circuits)
# if len(circuit) == 0:
# abort(404)
# return jsonify( { 'status':'ok', 'result': True } )
@app.route('/orchestrator/api/v1.0/performancemetrics/<int:circuit_id>', methods = ['GET'])
# @auth.login_required
def performance_metrics_get(circuit_id):
circuit = filter(lambda t: t['id'] == circuit_id, circuits)
if len(circuit) == 0:
abort(404)
return jsonify( { 'status':'ok', 'metrics': make_circuit_metrics(circuit_id, latency, throughput) } ), 201
@app.route('/compute/api/v1.0/faultmonitor/<int:circuit_id>', methods = ['POST'])
# @auth.login_required
def compute_faultmonitor_start(circuit_id, start_ip, end_ip):
circuit = filter(lambda t: t['id'] == circuit_id, circuits)
if len(circuit) == 0:
abort(404)
# ping_start (end_ip);
return jsonify( { 'status':'ok', 'result': True } )
@app.route('/compute/api/v1.0/performancemetrics/<int:circuit_id>', methods = ['GET'])
# @auth.login_required
def compute_performance_metrics_get(circuit_id):
circuit = filter(lambda t: t['id'] == circuit_id, circuits)
if len(circuit) == 0:
abort(404)
return jsonify( { 'status':'ok', 'metrics': make_circuit_metrics(circuit_id, latency, throughput) } ), 201
@app.route('/compute/api/v1.0/circuits', methods = ['POST'])
# @auth.login_required
def compute_circuit_create():
global options
if not request.json or not 'service_type' in request.json:
abort(400)
circuit = {
'id': circuits[-1]['id'] + 1,
'service_type': request.json['service_type'],
'start_ip_address': request.json.get('start_ip_address', ""),
'end_ip_address': request.json.get('end_ip_address', ""),
'self': request.json.get('self', ""),
'classifier': request.json.get('classifier', ""),
'active': True
}
#if request.host == circuit['start_ip_address']:
myserver = request.host.split(":",2)
if myserver[0] == circuit['start_ip_address']:
c_circuit_add(circuit['id'], circuit['start_ip_address'], circuit['end_ip_address'])
#if options.pingstart:
start_ping(circuit, circuit['end_ip_address'])
else:
c_circuit_add(circuit['id'], circuit['end_ip_address'], circuit['start_ip_address'])
circuits.append(circuit)
return jsonify( {
'status': 'ok',
'id': circuit['id'],
'circuit': make_public_circuit(circuit)
}
), 201
@app.route('/compute/api/v1.0/circuits/<int:circuit_id>', methods = ['DELETE'])
# @auth.login_required
def compute_circuit_delete(circuit_id):
circuit = filter(lambda t: t['id'] == circuit_id, circuits)
if len(circuit) == 0:
abort(404)
myserver = request.host.split(":",2)
if myserver[0] == circuit[0]['start_ip_address']:
stop_ping()
c_circuit_delete(circuit_id, circuit[0]['end_ip_address'], circuit[0]['start_ip_address'])
circuits.remove(circuit[0])
return jsonify( { 'status':'ok','result': circuit[0]['id'] } )
def get_root():
print 'This script needs to run as root. Please provide your sudo password'
args = ['sudo', sys.executable] + sys.argv + [os.environ]
os.execlpe('sudo', *args)
return os.getuid()
def parse_options():
parser = argparse.ArgumentParser(prog='o.py', description='ReSTFul ochestrator to create circuit')
'''
Alot of these switch were in place to navigate around untested code
'''
parser.add_argument('-q', '--quite', action='store_true',
help='Quite mode, update the terminal only when there is a timeout')
parser.add_argument('-n', '--notify', action='store_true',
help='Notify on')
parser.add_argument('-c', '--compute', action='store_true',
help='Perform Compute role')
parser.add_argument('-p', '--pingstart', action='store_true',
help='Autostart health checking on circuit create')
parser.add_argument('-d', '--daemonize', action='store_true',
help='Run the process in the background as a daemon ')
parser.add_argument('-l', '--logconsole', action='store_true',
help='Add console to logging output ')
parser.add_argument('-v', '--verbosedebug', action='store_true',
help='Add verbose debug statements to logging output ')
parser.add_argument('-s', '--serverip', action='store_true',
help='Endpoint contacts this server to register and provide endpoint information to orchestrator ')
args = parser.parse_args()
return args
def main():
global options
global logger
options = parse_options()
uid = os.getuid()
logger = logging_configure(options.logconsole, options.verbosedebug)
logger.debug ('Options = %s', options )
logger.debug( 'UserID = %s', uid )
if uid != 0:
logger.error ( 'Failed to get root permissions. Exiting!' )
logger.error ( 'You need to run as root to set command and open raw sockets' )
logger.error ( 'sudo python o.py' )
sys.exit(1)
# uid = get_root()
# if uid != 0:
# print 'Failed to get root permissions. Exiting!'
# sys.exit(1)
if options.daemonize and not daemonize:
logger.error ( 'You do not have the necessary library run in daemon mode')
logger.error ( 'Install the library using the following command')
logger.error ( 'sudo pip install daemonize' )
sys.exit(1)
if options.daemonize:
options.notify = True
daemon = daemonize.Daemonize(app='o', pid='/tmp/o.pid', action=main)
daemon.start()
else:
if options.quite:
logger.info('Running in quite mode, will update when a timeout occurs')
app.run('0.0.0.0', 5555, debug=True)
def cls():
os.system(['clear', 'cls'][os.name == 'nt'])
def showNotification(title, body):
print title, body
# PingLoop adopted and modified from check_internet.py, Akshet Pandey argetlam [dot] akshet [at] gmail [dot] com
class PingLoop(threading.Thread):
def __init__(self, options, circuit, endpoint):
self.options = options
self.circuit = circuit
self.endpoint = endpoint
self.loop = True
threading.Thread.__init__(self)
#signal.signal(signal.SIGINT, self.stopLoop)
def stopLoop(self):
if self.loop:
self.loop = False
logging.info ('Received exit signal. Stopping!')
#def runPingLoop(self):
def run(self):
delayList = deque(maxlen=100)
lastTimeout = None
netUp = True
replySuccessCount = 0
replyFailCount = 0
# cls()
while self.loop:
try:
delay = ping.do_one(self.endpoint, 3, 64)
except socket.error:
delay = None
except select.error:
delay = None
if delay:
delayList.append(delay)
replySuccessCount += 1
replyFailCount = 0
else:
lastTimeout = time()
replySuccessCount = 0
replyFailCount += 1
if replySuccessCount >= 10 and not netUp:
netUp = True
if self.options.notify:
showNotification('Net Status', 'Net is back!')
if self.circuit['active'] == False:
self.circuit['active'] = True
oc.o_report_status(circuit_id, make_circuit_status(circuit_id, self.circuit['active']))
elif replyFailCount >= 10 and netUp:
netUp = False
if self.options.notify:
showNotification('Net Status', 'Net is down!')
if self.circuit['active'] == True:
self.circuit['active'] = False
oc.o_report_status(circuit_id, make_circuit_status(circuit_id, self.circuit['active']))
if not self.options.quite or not delay:
# cls()
if len(delayList):
global latency
latency = str(sum(delayList) / len(delayList) * 1000)
print 'Circuit id ', self.circuit['id'], ' status of ', self.circuit['end_ip_address'], 'Average delay of last ', len(delayList), ' pings is ', sum(delayList) / len(delayList) * 1000, ' ms'
if not self.options.quite and lastTimeout:
logging.info( 'Last timeout was ', str(timedelta(seconds=time() - lastTimeout)), 'seconds ago.')
elif lastTimeout:
logging.info( 'Last timeout was at ', ctime(lastTimeout))
#else:
# print 'There has been no timeouts yet! Hurray!'
sleep(2)
def stop_ping():
global pingLoop
pingLoop.stopLoop()
# pingLoop.cancel()
'''
XXX - this ping thread is started with the wrong IP address
'''
def start_ping(circuit, end_ip):
global pingLoop
pingLoop = PingLoop(options, circuit, end_ip)
pingLoop.start()
if __name__ == '__main__':
main()
#if __name__ == '__main__':
# app.run('0.0.0.0', 5555, debug=True)
'''
Request methods using CURL:
0. curl -i -u user:password http://localhost:5555/orchestrator/api/v1.0/hello
1. curl -i -u user:password http://localhost:5555/orchestrator/api/v1.0/circuits
2. curl -i -u user:password http://localhost:5555/orchestrator/api/v1.0/circuits/2
3. curl -i -u user:password -H "Content-Type: application/json" -X POST -d '{ "service_type": "epl", "start_ip_address": "192.168.1.3", "end_ip_address": "192.168.1.4", "classifier": "red", "self": "http://localhost:8888/service_id/1" }' http://localhost:5555/orchestrator/api/v1.0/circuits
4. curl -i -u user:password -H "Content-Type: application/json" -X PUT -d '{ "id": 1, "service_type": "epl", "start_ip_address": "192.168.1.3", "end_ip_address": "192.168.1.4", "classifier": "blue", "self": "http://localhost:8888/service_id/1", "active": true }' http://localhost:5555/orchestrator/api/v1.0/circuits/1
5. curl -i -H "Content-Type: application/json" -X PUT -d '{"start_ip_address":"2.2.2.2","self":"http://local/1","end_ip_address":"1.1.1.1","service_type":"vepl","classifier":"11","active":true}' http://localhost:5555/orchestrator/api/v1.0/circuits/1
6. curl -i -H "Content-Type: application/json" -X PUT -d '{"active":true}' http://localhost:5555/orchestrator/api/v1.0/circuits/1
7. curl -i -u user:password http://localhost:5555/orchestrator/api/v1.0/performancemetrics/2
8. curl -i -u user:password -H "Content-Type: application/json" -X DELETE http://localhost:5555/orchestrator/api/v1.0/circuits/2
9. curl -i -H "Content-Type: application/json" -X PUT -d '{"active":true}' http://localhost:5555/debug
'''