Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small fixes #44

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

- name: Dummy Postgres Database
type: port
host: ec2-54-173-89-248.compute-1.amazonaws.com
host: ec2-54-173-89-228.compute-1.amazonaws.net
port: 5432

- name: Dummy MySQL Database
Expand Down
40 changes: 38 additions & 2 deletions tinystatus.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
import logging
import platform


# Load environment variables
load_dotenv()


# Configuration
MONITOR_CONTINOUSLY = os.getenv('MONITOR_CONTINOUSLY', 'True') == 'True'
CHECK_INTERVAL = int(os.getenv('CHECK_INTERVAL', 30))
Expand All @@ -26,11 +28,13 @@
TEMPLATE_FILE = os.getenv('TEMPLATE_FILE', 'index.html.theme')
HISTORY_TEMPLATE_FILE = os.getenv('HISTORY_TEMPLATE_FILE', 'history.html.theme')
STATUS_HISTORY_FILE = os.getenv('STATUS_HISTORY_FILE', 'history.json')
HTML_OUTPUT_DIRECTORY = os.getenv('HTML_OUTPUT_DIRECTORY', os.getcwd())
HTML_OUTPUT_DIRECTORY = os.getenv('HTML_OUTPUT_DIRECTORY',f"{os.getcwd()}/web")


# Platform Idendifier
PLATFORM = platform.system().lower()


# Service check functions
async def check_http(url, expected_code, selfsigned):
async with aiohttp.ClientSession() as session:
Expand Down Expand Up @@ -97,7 +101,10 @@ async def run_checks(checks):
def load_history():
if os.path.exists(STATUS_HISTORY_FILE):
with open(STATUS_HISTORY_FILE, 'r') as f:
return json.load(f)
try:
return json.load(f)
except json.JSONDecodeError:
return {}
return {}


Expand All @@ -119,10 +126,38 @@ def update_history(results):
save_history(history)


async def download_file(url, filename):
async with aiohttp.ClientSession() as session:
print(f"Starting download file from {url}")
async with session.get(url) as response:
assert response.status == 200
with open(filename, "wb") as f:
while True:
chunk = await response.content.readany()
if not chunk:
break
f.write(chunk)
print(f"Downloaded {filename} from {url}")


async def get_missing_files():
# Downloads (latest version of) missing files
if not os.path.exists(CHECKS_FILE):
await download_file("https://raw.githubusercontent.com/harsxv/tinystatus/refs/heads/master/checks.yaml", CHECKS_FILE)
if not os.path.exists(INCIDENTS_FILE):
await download_file("https://raw.githubusercontent.com/harsxv/tinystatus/refs/heads/master/incidents.md", INCIDENTS_FILE)
if not os.path.exists(TEMPLATE_FILE):
await download_file("https://raw.githubusercontent.com/harsxv/tinystatus/refs/heads/master/index.html.theme", TEMPLATE_FILE)
if not os.path.exists(HISTORY_TEMPLATE_FILE):
await download_file("https://raw.githubusercontent.com/harsxv/tinystatus/refs/heads/master/history.html.theme", HISTORY_TEMPLATE_FILE)


# Main monitoring loop
async def monitor_services():
os.makedirs(HTML_OUTPUT_DIRECTORY, exist_ok=True)

await get_missing_files()

while True:
start_ts = time.monotonic()
down_services = []
Expand Down Expand Up @@ -190,6 +225,7 @@ def main():
with open(os.path.join(HTML_OUTPUT_DIRECTORY, 'index.html'), 'w') as f:
f.write(html)


if __name__ == "__main__":
logging.basicConfig(level=getattr(logging, LOG_LEVEL), format='%(asctime)s - %(levelname)s - %(message)s')
asyncio.run(monitor_services())