| Current Path : /var/www/cesa.co.za/python/ |
| Current File : /var/www/cesa.co.za/python/server-checks.py |
#!/usr/bin/env python3
"""
Server Health Check Entry Point
This script runs comprehensive server health checks and sends email alerts
when thresholds are exceeded. It serves as the main entry point for the
server monitoring system.
Usage:
python server-checks.py # Run with default settings
python server-checks.py --email custom@example.com # Custom alert email
python server-checks.py --test # Test mode (no emails sent)
python server-checks.py --json # Output results in JSON format
"""
import sys
import argparse
import json
from pathlib import Path
# Add the app directory to Python path
sys.path.insert(0, str(Path(__file__).parent / 'app'))
# Import our server monitoring service
from services.server_checks import ServerMonitor, quick_server_check
def main():
"""Main entry point for server health checks."""
parser = argparse.ArgumentParser(
description='Server Health Monitoring and Alerting System',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
python server-checks.py # Run with default settings
python server-checks.py --email admin@example.com # Custom alert email
python server-checks.py --test # Test mode (no emails sent)
python server-checks.py --json # Output results in JSON format
"""
)
parser.add_argument(
'--email',
default='server-admin@igot.africa',
help='Email address to send alerts to (default: server-admin@igot.africa)'
)
parser.add_argument(
'--test',
action='store_true',
help='Test mode - run checks but don\'t send emails'
)
parser.add_argument(
'--json',
action='store_true',
help='Output results in JSON format'
)
parser.add_argument(
'--config',
help='Path to custom configuration file (not implemented yet)'
)
args = parser.parse_args()
try:
# Create server monitor instance
if args.test:
# In test mode, we could modify the mail service to not send emails
# For now, we'll just run the checks normally
pass
# print("Starting server checks")
monitor = ServerMonitor(alert_email=args.email)
# Run all health checks
results = monitor.run_all_checks()
# Output results if JSON format requested
if args.json:
print(json.dumps(results, indent=2, default=str))
# Return appropriate exit code
total_alerts = results.get('total_alerts', 0)
return 0 if total_alerts == 0 else 1
except KeyboardInterrupt:
return 130
except Exception:
print("Error: ", sys.exc_info()[1])
return 1
if __name__ == "__main__":
try:
exit_code = main()
sys.exit(exit_code)
except Exception:
sys.exit(1)