-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_uptime.py
53 lines (40 loc) · 1.41 KB
/
check_uptime.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
import requests
import json
import os
from datetime import datetime
def check_uptime(url):
try:
response = requests.get(url, timeout=5)
return response.status_code == 200
except requests.RequestException:
return False
def update_uptime_data(new_entries, current_urls, max_entries=1000):
filename = "uptime_data.json"
if os.path.exists(filename):
with open(filename, "r") as f:
data = json.load(f)
else:
data = {}
# Remove URLs that are no longer in the workflow file
data = {url: entries for url, entries in data.items() if url in current_urls}
for url, entry in new_entries.items():
if url not in data:
data[url] = []
data[url].append(entry)
data[url] = data[url][-max_entries:]
with open(filename, "w") as f:
json.dump(data, f, indent=2)
def get_urls_from_env():
sites_env = os.environ.get("SITES", "")
return [url.strip() for url in sites_env.split("\n") if url.strip()]
def main():
urls = get_urls_from_env()
timestamp = datetime.now().isoformat()
new_entries = {}
for url in urls:
is_up = check_uptime(url)
new_entries[url] = {"timestamp": timestamp, "status": "up" if is_up else "down"}
print(f"Uptime check completed for {url}. Status: {'Up' if is_up else 'Down'}")
update_uptime_data(new_entries, urls)
if __name__ == "__main__":
main()