Run the Flask server with optional HTTPS support. Args: host: Host to bind to (default: 0.0.0.0) port: HTTP port (default: 8000) ssl_cert: Path to SSL certificate file (optional) ssl_key: Path to SSL key file (optional) https_port: HTTPS port (defaul
(host='0.0.0.0', port=8000, ssl_cert=None, ssl_key=None, https_port=None)
| 19759 | } |
| 19760 | |
| 19761 | return jsonify(system_status) |
| 19762 | |
| 19763 | except Exception as e: |
| 19764 | logger.error(f"Error getting system status: {e}") |
| 19765 | return jsonify({'error': str(e)}), 500 |
| 19766 | |
| 19767 | @app.route('/api/system/processes') |
| 19768 | def get_processes_api(): |
| 19769 | """Get detailed process information""" |
| 19770 | try: |
| 19771 | if not psutil_available: |
| 19772 | return jsonify({'error': 'Process monitoring not available'}), 503 |
| 19773 | |
| 19774 | processes = [] |
| 19775 | for proc in psutil.process_iter(['pid', 'name', 'cpu_percent', 'memory_percent', 'status', 'create_time']): |
| 19776 | try: |
| 19777 | pinfo = proc.info |
| 19778 | processes.append({ |
| 19779 | 'pid': pinfo['pid'], |
| 19780 | 'name': pinfo['name'], |
| 19781 | 'cpu_percent': round(pinfo['cpu_percent'] or 0, 2), |
| 19782 | 'memory_percent': round(pinfo['memory_percent'] or 0, 2), |
| 19783 | 'status': pinfo['status'], |
| 19784 | 'create_time': pinfo['create_time'] |
| 19785 | }) |
| 19786 | except (psutil.NoSuchProcess, psutil.AccessDenied): |
| 19787 | pass |
| 19788 | |
| 19789 | # Sort by CPU usage |
| 19790 | sort_by = request.args.get('sort', 'cpu') |
| 19791 | if sort_by == 'memory': |
| 19792 | processes.sort(key=lambda x: x['memory_percent'], reverse=True) |
| 19793 | else: |
| 19794 | processes.sort(key=lambda x: x['cpu_percent'], reverse=True) |
| 19795 | |
| 19796 | return jsonify(processes) |
| 19797 | |
| 19798 | except Exception as e: |
| 19799 | logger.error(f"Error getting processes: {e}") |
| 19800 | return jsonify({'error': str(e)}), 500 |
| 19801 | |
| 19802 | @app.route('/api/system/network-stats') |
| 19803 | def get_network_stats_api(): |
| 19804 | """Get network interface statistics""" |
| 19805 | try: |
| 19806 | if not psutil_available: |
| 19807 | return jsonify({'error': 'Network monitoring not available'}), 503 |
| 19808 | |
| 19809 | net_io = psutil.net_io_counters(pernic=True) |
| 19810 | net_connections = psutil.net_connections() |
| 19811 | |
| 19812 | # Count connections by status |
| 19813 | connection_stats = {} |
| 19814 | for conn in net_connections: |
| 19815 | status = conn.status |
| 19816 | connection_stats[status] = connection_stats.get(status, 0) + 1 |
| 19817 | |
| 19818 | network_stats = { |
no test coverage detected