-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
78 lines (72 loc) · 2.67 KB
/
main.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
import json
from discord_webhook import DiscordWebhook, DiscordEmbed
import requests
import time
from pydo import Client
def get_second():
return int(str(int(time.time()))[-1]) # see this logic ? its so complex. yet so simple
logo_link = "https://raw.githubusercontent.com/SurajBhari/droplet_maintainer/main/256_droplet_maintainer.png"
project_name = "Droplet Maintainer"
count = 1
last_second = get_second()
while True:
while last_second == get_second():
time.sleep(0.1)
last_second = get_second()
config = json.load(open('config.json', "r"))
for instance in config.keys():
if count % config[instance]['interval'] != 0:
print(f"{count}. Not time to check {instance}...")
continue # Skip if not time to check
print(f"Checking {instance}...")
location = config[instance]['location']
url = location['url']
headers = location['headers']
tolerance = config[instance]['tolerance']
timeout = location['timeout']
ok = False
while tolerance > 0:
time.sleep(1)
try:
response = requests.get(url, headers=headers, timeout=timeout)
except Exception as e:
print(e)
print(f"{instance} is down!")
tolerance -= 1
continue
if response.status_code == location["response_code"]:
print(f"{instance} Responded with correct code!")
ok = True
break
print(f"{instance} said {response.status_code}!")
tolerance -= 1
if ok:
continue
if config[instance]['discord']:
embed = DiscordEmbed(
title=f"{url} is down!",
color=0xf54242,
description=f"{instance} is down! Restarting now..."
)
webhook = DiscordWebhook(
username = project_name,
avatar_url=logo_link,
url=config[instance]['discord'],
embeds=[embed]
)
response = webhook.execute()
droplet = Client(token=config[instance]['digitalocean_token'])
droplet.droplet_actions.post(instance, {'type': 'power_cycle'})
if config[instance]['discord']:
embed = DiscordEmbed(
title=f"{instance} has been restarted!",
color=0x00ab00,
)
webhook = DiscordWebhook(
username = project_name,
avatar_url=logo_link,
url=config[instance]['discord'],
embeds=[embed]
)
response = webhook.execute()
count += 1