-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
28 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,54 @@ | ||
import requests,sys, os | ||
import requests, sys, os | ||
|
||
|
||
def check(): | ||
exit_code = os.EX_OK | ||
health_url = os.getenv("HEALTH_URL","http://localhost:8000/drugs/") | ||
health_url = os.getenv("HEALTH_URL", "http://localhost:8000/drugs/") | ||
try: | ||
r = requests.get(health_url) | ||
if r.status_code != 200 : | ||
if r.status_code != 200: | ||
raise Exception("The probe has failed") | ||
set_tries("0") | ||
except: | ||
except Exception as ex: | ||
print(ex) | ||
notify() | ||
exit_code = os.EX_SOFTWARE | ||
sys.exit(exit_code) | ||
|
||
|
||
def notify(): | ||
url = os.getenv("HEALTH_ALERT_URL","") | ||
if url!="": | ||
url = os.getenv("HEALTH_ALERT_URL", "") | ||
if url != "": | ||
tries = get_tries() | ||
tries = tries+1 | ||
tries = tries + 1 | ||
headers = {'Content-Type': 'application/json'} | ||
message="Modulector: Health check failed {} times.".format(tries) | ||
if tries==3: | ||
message = "Modulector: Health check failed {} times.".format(tries) | ||
if tries == 3: | ||
message = message + " The container has been marked as unhealthy." | ||
set_tries(0) | ||
data = { "content": message } | ||
data = {"content": message} | ||
try: | ||
r = requests.post(url,headers=headers, json=data) | ||
except: | ||
requests.post(url, headers=headers, json=data) | ||
except Exception as ex: | ||
print(ex) | ||
pass | ||
set_tries(str(tries)) | ||
|
||
|
||
def get_tries(): | ||
tries=0 | ||
tries = 0 | ||
try: | ||
with open("tries.txt","r") as f: | ||
tries=f.read() | ||
with open("tries.txt", "r") as f: | ||
tries = f.read() | ||
except FileNotFoundError: | ||
set_tries(0) | ||
return int(tries) | ||
|
||
|
||
def set_tries(tries): | ||
with open("tries.txt","w") as f: | ||
with open("tries.txt", "w") as f: | ||
f.write(str(tries)) | ||
|
||
if __name__=="__main__": | ||
|
||
if __name__ == "__main__": | ||
check() |