#!/usr/bin/env python3 """ Test script to verify connection to monitoring server """ import requests import socket import sys # Configuration MONITORING_SERVER = "http://rpi-ansible:80/logs" def test_connection(): """Test if monitoring server is reachable""" print(f"Testing connection to: {MONITORING_SERVER}\n") # Get local info try: hostname = socket.gethostname() device_ip = socket.gethostbyname(hostname) print(f"Local hostname: {hostname}") print(f"Local IP: {device_ip}\n") except Exception as e: print(f"Error getting local info: {e}\n") hostname = "unknown" device_ip = "0.0.0.0" # Test 1: Simple GET request print("1. Testing simple GET request...") try: response = requests.get(MONITORING_SERVER, timeout=5) print(f" ✓ GET successful (Status: {response.status_code})") print(f" Response: {response.text[:200]}\n") except Exception as e: print(f" ✗ GET failed: {e}\n") # Test 2: POST with test data print("2. Testing POST with test data...") try: test_data = { "hostname": hostname, "device_ip": device_ip, "nume_masa": "TEST_STATION", "log_message": "Test message from client" } print(f" Payload: {test_data}") response = requests.post(MONITORING_SERVER, json=test_data, timeout=5) print(f" ✓ POST successful (Status: {response.status_code})") print(f" Response: {response.text}\n") except requests.exceptions.ConnectionError as e: print(f" ✗ Connection error: {e}") print(f" - Server may be unreachable at {MONITORING_SERVER}\n") except requests.exceptions.Timeout: print(f" ✗ Request timeout (5s)") print(f" - Server may be offline or slow\n") except Exception as e: print(f" ✗ POST failed: {e}\n") # Test 3: Ping server IP print("3. Testing network connectivity to server...") import subprocess try: server_ip = "192.168.1.103" result = subprocess.run( ["ping", "-c", "1", server_ip], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=5 ) if result.returncode == 0: print(f" ✓ Server IP {server_ip} is reachable\n") else: print(f" ✗ Server IP {server_ip} is NOT reachable\n") except Exception as e: print(f" ✗ Ping failed: {e}\n") if __name__ == "__main__": test_connection()