main.py 2.4 KB

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