-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.py
More file actions
114 lines (96 loc) · 4 KB
/
monitor.py
File metadata and controls
114 lines (96 loc) · 4 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python3
"""
Subscription Tracker Health Monitor
A simple script to monitor the health of the subscription tracker application
"""
import requests
import time
import sys
import argparse
from datetime import datetime
def check_health(url, timeout=30):
"""Check the health endpoint"""
try:
response = requests.get(f"{url}/health", timeout=timeout)
if response.status_code == 200:
data = response.json()
print(f"✓ Health check passed: {data.get('status', 'unknown')}")
return True
else:
print(f"✗ Health check failed with status {response.status_code}")
return False
except requests.exceptions.Timeout:
print(f"✗ Health check timed out after {timeout} seconds")
return False
except requests.exceptions.ConnectionError:
print("✗ Cannot connect to the application")
return False
except Exception as e:
print(f"✗ Health check failed: {e}")
return False
def check_subscription_save(url, username, password, timeout=30):
"""Test subscription saving functionality"""
session = requests.Session()
try:
# Login first
login_response = session.post(
f"{url}/login",
data={'username': username, 'password': password},
timeout=timeout
)
if login_response.status_code != 200:
print("✗ Login failed")
return False
# Try to access dashboard (this triggers currency conversion)
dashboard_response = session.get(f"{url}/dashboard", timeout=timeout)
if dashboard_response.status_code == 200:
print("✓ Dashboard loads successfully")
return True
else:
print(f"✗ Dashboard failed with status {dashboard_response.status_code}")
return False
except requests.exceptions.Timeout:
print(f"✗ Request timed out after {timeout} seconds")
return False
except Exception as e:
print(f"✗ Test failed: {e}")
return False
def main():
parser = argparse.ArgumentParser(description='Monitor Subscription Tracker health')
parser.add_argument('--url', default='http://localhost:5000', help='Application URL')
parser.add_argument('--username', default='admin', help='Username for testing')
parser.add_argument('--password', default='changeme', help='Password for testing')
parser.add_argument('--timeout', type=int, default=30, help='Request timeout in seconds')
parser.add_argument('--interval', type=int, default=60, help='Check interval in seconds')
parser.add_argument('--once', action='store_true', help='Run once instead of continuously')
args = parser.parse_args()
print(f"Monitoring Subscription Tracker at {args.url}")
print(f"Timeout: {args.timeout}s, Interval: {args.interval}s")
print("-" * 50)
while True:
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(f"\n[{timestamp}] Running health checks...")
# Basic health check
health_ok = check_health(args.url, args.timeout)
# Functional test
if health_ok:
functional_ok = check_subscription_save(args.url, args.username, args.password, args.timeout)
else:
functional_ok = False
# Overall status
if health_ok and functional_ok:
print("✓ Overall status: HEALTHY")
exit_code = 0
else:
print("✗ Overall status: UNHEALTHY")
exit_code = 1
if args.once:
sys.exit(exit_code)
print(f"Next check in {args.interval} seconds...")
time.sleep(args.interval)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\nMonitoring stopped by user")
sys.exit(0)