#!/usr/bin/env python3 """ Complete Smart Intersection System Status Checker Comprehensive status check for MQTT, InfluxDB, and Grafana """ import subprocess import socket import time import json from datetime import datetime import sys import os class SmartIntersectionSystemChecker: def __init__(self): self.services = { 'mqtt': {'port': 1883, 'name': 'MQTT Broker (Mosquitto)', 'status': 'unknown'}, 'influxdb': {'port': 8086, 'name': 'InfluxDB', 'status': 'unknown'}, 'grafana': {'port': 3000, 'name': 'Grafana', 'status': 'unknown'} } self.system_status = {} def check_port_listening(self, port, host='localhost'): """Check if a port is listening""" try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(5) result = sock.connect_ex((host, port)) sock.close() return result == 0 except: return False def check_process_running(self, process_names): """Check if any of the given processes are running""" try: if sys.platform == "win32": # Windows output = subprocess.check_output('tasklist', shell=True, text=True) for process_name in process_names: if process_name.lower() in output.lower(): return True else: # Linux/Unix output = subprocess.check_output(['ps', 'aux'], text=True) for process_name in process_names: if process_name in output: return True return False except: return False def get_system_info(self): """Get basic system information""" try: if sys.platform == "win32": # Windows system info hostname = subprocess.check_output('hostname', shell=True, text=True).strip() os_info = subprocess.check_output('ver', shell=True, text=True).strip() else: # Linux system info hostname = subprocess.check_output(['hostname'], text=True).strip() os_info = subprocess.check_output(['uname', '-a'], text=True).strip() self.system_status = { 'hostname': hostname, 'os_info': os_info, 'timestamp': datetime.now().isoformat(), 'platform': sys.platform } except: self.system_status = { 'hostname': 'unknown', 'os_info': 'unknown', 'timestamp': datetime.now().isoformat(), 'platform': sys.platform } def check_service_status(self): """Check status of all services""" print(f"πŸ” CHECKING SERVICE STATUS...") print(f"{'='*50}") # Check MQTT print(f"πŸ“‘ Checking MQTT Broker (port 1883)...") if self.check_port_listening(1883): self.services['mqtt']['status'] = 'running' print(f" βœ… MQTT Broker is RUNNING") else: self.services['mqtt']['status'] = 'stopped' print(f" ❌ MQTT Broker is NOT RUNNING") # Check InfluxDB print(f"πŸ“Š Checking InfluxDB (port 8086)...") if self.check_port_listening(8086): self.services['influxdb']['status'] = 'running' print(f" βœ… InfluxDB is RUNNING") else: self.services['influxdb']['status'] = 'stopped' print(f" ❌ InfluxDB is NOT RUNNING") # Check Grafana print(f"πŸ“ˆ Checking Grafana (port 3000)...") if self.check_port_listening(3000): self.services['grafana']['status'] = 'running' print(f" βœ… Grafana is RUNNING") else: self.services['grafana']['status'] = 'stopped' print(f" ❌ Grafana is NOT RUNNING") def check_service_processes(self): """Check if service processes are running""" print(f"\nπŸ” CHECKING SERVICE PROCESSES...") print(f"{'='*40}") process_checks = { 'MQTT': ['mosquitto', 'mosquitto.exe'], 'InfluxDB': ['influxd', 'influxd.exe', 'influxdb'], 'Grafana': ['grafana-server', 'grafana-server.exe', 'grafana'] } for service_name, process_names in process_checks.items(): if self.check_process_running(process_names): print(f" βœ… {service_name} process is running") else: print(f" ❌ {service_name} process not found") def generate_service_urls(self): """Generate service access URLs""" print(f"\n🌐 SERVICE ACCESS URLS:") print(f"{'='*30}") running_services = [] for service_key, service_info in self.services.items(): if service_info['status'] == 'running': if service_key == 'mqtt': print(f"πŸ“‘ MQTT Broker: mqtt://localhost:1883") print(f" - Use MQTT client to connect") print(f" - Topics: smart-intersection/*") running_services.append('MQTT') elif service_key == 'influxdb': print(f"πŸ“Š InfluxDB UI: http://localhost:8086") print(f" - API: http://localhost:8086/api/v2") print(f" - Organization: smart-intersection-org") running_services.append('InfluxDB') elif service_key == 'grafana': print(f"πŸ“ˆ Grafana Dashboard: http://localhost:3000") print(f" - Username: admin / Password: admin") print(f" - API: http://localhost:3000/api") running_services.append('Grafana') return running_services def check_service_connectivity(self): """Test connectivity to running services""" print(f"\nπŸ”— TESTING SERVICE CONNECTIVITY:") print(f"{'='*40}") for service_key, service_info in self.services.items(): if service_info['status'] == 'running': port = service_info['port'] name = service_info['name'] print(f"πŸ”Œ Testing {name}...") # Test basic TCP connection if self.check_port_listening(port): print(f" βœ… TCP connection successful") # Service-specific tests if service_key == 'mqtt': print(f" πŸ“‘ MQTT: Ready for pub/sub operations") elif service_key == 'influxdb': print(f" πŸ“Š InfluxDB: HTTP API accessible") elif service_key == 'grafana': print(f" πŸ“ˆ Grafana: Web interface accessible") else: print(f" ❌ TCP connection failed") def provide_troubleshooting_tips(self): """Provide troubleshooting tips for failed services""" print(f"\nπŸ”§ TROUBLESHOOTING GUIDE:") print(f"{'='*40}") failed_services = [k for k, v in self.services.items() if v['status'] != 'running'] if not failed_services: print(f"βœ… All services are running properly!") return for service in failed_services: if service == 'mqtt': print(f"\nπŸ“‘ MQTT Broker (Mosquitto) not running:") print(f" 1. Start services: run 'start_services.bat'") print(f" 2. Check if Mosquitto is installed") print(f" 3. Check port 1883 is not blocked") print(f" 4. Review Mosquitto logs") elif service == 'influxdb': print(f"\nπŸ“Š InfluxDB not running:") print(f" 1. Start services: run 'start_services.bat'") print(f" 2. Check if InfluxDB executable exists") print(f" 3. Check port 8086 is not blocked") print(f" 4. Review InfluxDB logs") print(f" 5. Check disk space for database") elif service == 'grafana': print(f"\nπŸ“ˆ Grafana not running:") print(f" 1. Start services: run 'start_services.bat'") print(f" 2. Check if Grafana executable exists") print(f" 3. Check port 3000 is not blocked") print(f" 4. Review Grafana logs") print(f" 5. Check configuration files") def explain_system_architecture(self): """Explain how the components interact""" print(f"\nπŸ—οΈ SYSTEM ARCHITECTURE OVERVIEW:") print(f"{'='*50}") print(f""" 🚦 Smart Intersection Traffic Monitoring System β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Qt Desktop β”‚ β”‚ MQTT Broker β”‚ β”‚ InfluxDB β”‚ β”‚ App │◄──►│ (Mosquitto) │◄──►│ (Time Series) β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β€’ Video Analysisβ”‚ β”‚ β€’ Message Queue β”‚ β”‚ β€’ Data Storage β”‚ β”‚ β€’ Detection β”‚ β”‚ β€’ Pub/Sub β”‚ β”‚ β€’ Measurements β”‚ β”‚ β€’ VLM Insights β”‚ β”‚ β€’ Real-time β”‚ β”‚ β€’ Retention β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ └──────────────►│ Grafana β”‚β—„β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ (Dashboards) β”‚ β”‚ β”‚ β”‚ β€’ Visualization β”‚ β”‚ β€’ Alerts β”‚ β”‚ β€’ Analytics β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ πŸ“Š DATA FLOW: 1. Qt App detects vehicles/violations β†’ MQTT topics 2. MQTT Broker routes messages β†’ InfluxDB via Telegraf 3. InfluxDB stores time-series data 4. Grafana queries InfluxDB β†’ Real-time dashboards πŸ“‘ MQTT TOPICS: β€’ smart-intersection/camera-{id}/detection/vehicles β€’ smart-intersection/camera-{id}/violation/red-light β€’ smart-intersection/camera-{id}/performance/fps β€’ smart-intersection/camera-{id}/status/health πŸ“Š INFLUXDB MEASUREMENTS: β€’ detection_events (vehicle counts, types) β€’ violation_events (red light, crosswalk) β€’ performance_metrics (fps, processing time) β€’ device_status (health, uptime) """) def run_comprehensive_check(self): """Run complete system status check""" print(f"🚦 SMART INTERSECTION SYSTEM STATUS CHECKER") print(f"{'='*60}") print(f"Check Time: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}") # Get system info self.get_system_info() print(f"Hostname: {self.system_status['hostname']}") print(f"Platform: {self.system_status['platform']}") # Check all services self.check_service_status() self.check_service_processes() # Generate URLs for running services running_services = self.generate_service_urls() # Test connectivity self.check_service_connectivity() # Show architecture self.explain_system_architecture() # Provide troubleshooting self.provide_troubleshooting_tips() # Summary print(f"\nπŸ“‹ SYSTEM STATUS SUMMARY:") print(f"{'='*40}") total_services = len(self.services) running_count = len([s for s in self.services.values() if s['status'] == 'running']) print(f"πŸ”’ Services Status: {running_count}/{total_services} running") print(f"βœ… Running: {', '.join(running_services) if running_services else 'None'}") failed_services = [v['name'] for v in self.services.values() if v['status'] != 'running'] if failed_services: print(f"❌ Failed: {', '.join(failed_services)}") if running_count == total_services: print(f"\nπŸŽ‰ ALL SERVICES ARE HEALTHY!") print(f"πŸš€ Your Smart Intersection system is ready!") else: print(f"\n⚠️ Some services need attention") print(f"πŸ’‘ Run 'start_services.bat' to start all services") def main(): """Main function""" checker = SmartIntersectionSystemChecker() checker.run_comprehensive_check() print(f"\nπŸ”§ QUICK COMMANDS:") print(f"{'-'*25}") print(f"Start all services:") print(f" > cd services/scripts") print(f" > start_services.bat") print(f"") print(f"Check individual components:") print(f" > python check_mqtt_status.py") print(f" > python check_influxdb_status.py") print(f" > python check_grafana_status.py") if __name__ == "__main__": main()