-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathrouteserver-vmss-selfcontained-healthcheck.py
70 lines (62 loc) · 2.13 KB
/
routeserver-vmss-selfcontained-healthcheck.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
import os
import socket, struct
import sys
import time
import warnings
import requests
from flask import Flask
from flask import request
from flask import jsonify
# Gets the web port out of an environment variable, or defaults to 8080
def get_web_port():
web_port=os.environ.get('PORT')
if web_port==None or not web_port.isnumeric():
print("Using default port 8080")
web_port=8080
else:
print("Port supplied as environment variable:", web_port)
return web_port
app = Flask(__name__)
# Flask route for healthchecks
@app.route("/api/healthcheck", methods=['GET'])
def healthcheck():
if request.method == 'GET':
try:
output_stream = os.popen('birdc show protocols | grep rs0 | awk \'{print $6}\'')
rs0_status = output_stream.read()
output_stream = os.popen('birdc show protocols | grep rs1 | awk \'{print $6}\'')
rs1_status = output_stream.read()
rs0_status = rs0_status.rstrip('\n')
rs1_status = rs1_status.rstrip('\n')
if (rs0_status == "Established") and (rs1_status == "Established"):
return_code = 200
else:
return_code = 503
msg = {
'health': 'OK',
'rs0_status': rs0_status,
'rs1_status': rs1_status
}
return jsonify(msg), return_code
except Exception as e:
return jsonify(str(e)), 500
# Flask route to run config
@app.route("/api/config", methods=['GET'])
def config():
if request.method == 'GET':
try:
output_stream = os.popen('/root/routeserver-vmss-selfcontained-config.sh')
output = output_stream.read()
msg = {
'health': 'OK',
'config_output': output.rstrip('\n'),
}
return jsonify(msg)
except Exception as e:
return jsonify(str(e))
# Ignore warnings
with warnings.catch_warnings():
warnings.simplefilter("ignore")
# Set web port
web_port=get_web_port()
app.run(host='0.0.0.0', port=web_port, debug=True, use_reloader=False)