main.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import os
  2. import requests
  3. from flask import Flask
  4. from flask import jsonify
  5. from flask_cors import CORS, cross_origin
  6. def get_result():
  7. services_up = 0
  8. failure_list = []
  9. returnResponse = {"style":"", "title":"", "content":""}
  10. statping_endpoint = os.getenv('STATPING_ENDPOINT')
  11. try:
  12. print('%sapi/services/' % statping_endpoint)
  13. response = requests.get('%sapi/services/' % statping_endpoint, verify=False)
  14. # If we get a response
  15. if response.status_code == 200:
  16. # Get the total number of services returned
  17. total_services = len(response.json())
  18. # Iterate through the response
  19. for key in response.json():
  20. # If the service is online, incrememnt the up count
  21. if key["online"] == True:
  22. services_up += 1
  23. # Otherwise append the name to the naughty list
  24. else:
  25. failure_list.append(key["name"])
  26. # Check the number of up services is equal to the total number of services
  27. if total_services == services_up:
  28. # if so, return a success object
  29. returnResponse["style"] = "is-success"
  30. returnResponse["title"] = "All good in the hood boss"
  31. returnResponse["content"] = "<b>%s / %s Services showing as online. Check <a href=%s target=_blank>here</a> for more info. </br> <p>" % (services_up, total_services, statping_endpoint)
  32. return(returnResponse)
  33. else:
  34. # return a numpty message
  35. returnResponse["style"] = "is-danger"
  36. returnResponse["title"] = "Halp"
  37. # need to build the return message to include the failures as a formatted list
  38. numptyList = "<ul>"
  39. for i in failure_list:
  40. numptyList += "<li>" + i + "</li>"
  41. numptyList += "</ul>"
  42. returnResponse["content"] = "<b>%s / %s Services showing as online. Check <a href=%s target=_blank>here</a> for more info. </br> <p> <br>Here are the numpties that are letting the side down:</br> %s" % (services_up, total_services, numptyList, statping_endpoint)
  43. return(returnResponse)
  44. except:
  45. returnResponse["style"] = "is-danger"
  46. returnResponse["title"] = "I have the sads"
  47. returnResponse["content"] = "Like, man cannot even holla at Statping."
  48. return(returnResponse)
  49. app = Flask(__name__)
  50. app.config["JSON_SORT_KEYS"] = False
  51. cors = CORS(app)
  52. app.config['CORS_HEADERS'] = 'Content-Type'
  53. @app.route("/endpoint/", methods=["GET"])
  54. @cross_origin()
  55. def welcome():
  56. return jsonify(get_result())
  57. if __name__ == "__main__":
  58. app.run(host="0.0.0.0", port=8099)