Initial server monitoring system with port 80 support

Features:
- Device management and monitoring dashboard
- Remote command execution on devices via port 80
- Auto-update coordination for multiple devices
- Database reset functionality with safety confirmations
- Server logs filtering and dedicated logging interface
- Device status monitoring and management
- SQLite database for comprehensive logging
- Web interface with Bootstrap styling
- Comprehensive error handling and logging

Key components:
- server.py: Main Flask application with all routes
- templates/: Complete web interface templates
- data/database.db: SQLite database for device logs
- UPDATE_SUMMARY.md: Development progress documentation
This commit is contained in:
2025-08-14 15:53:00 +03:00
commit 42989aa8df
14 changed files with 2029 additions and 0 deletions

View File

@@ -0,0 +1,93 @@
---
### `logging_module.py`
```python
import logging
import os
import socket
import requests
from datetime import datetime, timedelta
# Configure logging
LOG_FILE_PATH = './data/log.txt'
logging.basicConfig(filename=LOG_FILE_PATH, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# Function to delete old logs
def delete_old_logs():
log_dir = './data/'
log_file = 'log.txt'
log_path = os.path.join(log_dir, log_file)
if os.path.exists(log_path):
file_mod_time = datetime.fromtimestamp(os.path.getmtime(log_path))
if datetime.now() - file_mod_time > timedelta(days=10):
os.remove(log_path)
log_info_with_server(f"Deleted old log file: {log_file}")
else:
log_info_with_server(f"Log file is not older than 10 days: {log_file}")
else:
log_info_with_server(f"Log file does not exist: {log_file}")
# Function to read the name (idmasa) from the file
def read_name_from_file():
try:
with open("./data/idmasa.txt", "r") as file:
### How to Use the Module in Another App
1. Save the script as `logging_module.py` in a directory accessible to your other Python applications.
2. Import the module and use its functions in your other app. For example:
```python
# Import the logging module
from logging_module import log_info_with_server, delete_old_logs
# Example usage
log_info_with_server("This is a test log message.")
delete_old_logs()
```
---
### Explanation of the Script:
1. **Encapsulation**:
- The script encapsulates all logging-related functionality, including:
- Deleting old logs ([`delete_old_logs`](command:_github.copilot.openSymbolFromReferences?%5B%22%22%2C%5B%7B%22uri%22%3A%7B%22scheme%22%3A%22file%22%2C%22authority%22%3A%22%22%2C%22path%22%3A%22%2Fhome%2Fpi%2FDesktop%2Fprezenta%2Fapp.py%22%2C%22query%22%3A%22%22%2C%22fragment%22%3A%22%22%7D%2C%22pos%22%3A%7B%22line%22%3A16%2C%22character%22%3A4%7D%7D%2C%7B%22uri%22%3A%7B%22scheme%22%3A%22file%22%2C%22authority%22%3A%22%22%2C%22path%22%3A%22%2Fhome%2Fpi%2FDesktop%2Fprezenta%2Fapp.py%22%2C%22query%22%3A%22%22%2C%22fragment%22%3A%22%22%7D%2C%22pos%22%3A%7B%22line%22%3A68%2C%22character%22%3A0%7D%7D%2C%7B%22uri%22%3A%7B%22scheme%22%3A%22file%22%2C%22authority%22%3A%22%22%2C%22path%22%3A%22%2Fhome%2Fpi%2FDesktop%2Fprezenta%2Fapp.py%22%2C%22query%22%3A%22%22%2C%22fragment%22%3A%22%22%7D%2C%22pos%22%3A%7B%22line%22%3A16%2C%22character%22%3A4%7D%7D%5D%2C%229495c72a-c9b2-498f-9787-1c43fb524282%22%5D "Go to definition")).
- Reading the [`idmasa`](command:_github.copilot.openSymbolFromReferences?%5B%22%22%2C%5B%7B%22uri%22%3A%7B%22scheme%22%3A%22file%22%2C%22authority%22%3A%22%22%2C%22path%22%3A%22%2Fhome%2Fpi%2FDesktop%2Fprezenta%2Fapp.py%22%2C%22query%22%3A%22%22%2C%22fragment%22%3A%22%22%7D%2C%22pos%22%3A%7B%22line%22%3A31%2C%22character%22%3A29%7D%7D%2C%7B%22uri%22%3A%7B%22scheme%22%3A%22file%22%2C%22authority%22%3A%22%22%2C%22path%22%3A%22%2Fhome%2Fpi%2FDesktop%2Fprezenta%2Fapp.py%22%2C%22query%22%3A%22%22%2C%22fragment%22%3A%22%22%7D%2C%22pos%22%3A%7B%22line%22%3A31%2C%22character%22%3A29%7D%7D%5D%2C%229495c72a-c9b2-498f-9787-1c43fb524282%22%5D "Go to definition") value from a file ([`read_name_from_file`](command:_github.copilot.openSymbolFromReferences?%5B%22%22%2C%5B%7B%22uri%22%3A%7B%22scheme%22%3A%22file%22%2C%22authority%22%3A%22%22%2C%22path%22%3A%22%2Fhome%2Fpi%2FDesktop%2Fprezenta%2Fapp.py%22%2C%22query%22%3A%22%22%2C%22fragment%22%3A%22%22%7D%2C%22pos%22%3A%7B%22line%22%3A32%2C%22character%22%3A4%7D%7D%2C%7B%22uri%22%3A%7B%22scheme%22%3A%22file%22%2C%22authority%22%3A%22%22%2C%22path%22%3A%22%2Fhome%2Fpi%2FDesktop%2Fprezenta%2Fapp.py%22%2C%22query%22%3A%22%22%2C%22fragment%22%3A%22%22%7D%2C%22pos%22%3A%7B%22line%22%3A32%2C%22character%22%3A4%7D%7D%5D%2C%229495c72a-c9b2-498f-9787-1c43fb524282%22%5D "Go to definition")).
- Sending logs to a remote server ([`send_log_to_server`](command:_github.copilot.openSymbolFromReferences?%5B%22%22%2C%5B%7B%22uri%22%3A%7B%22scheme%22%3A%22file%22%2C%22authority%22%3A%22%22%2C%22path%22%3A%22%2Fhome%2Fpi%2FDesktop%2Fprezenta%2Fapp.py%22%2C%22query%22%3A%22%22%2C%22fragment%22%3A%22%22%7D%2C%22pos%22%3A%7B%22line%22%3A42%2C%22character%22%3A4%7D%7D%2C%7B%22uri%22%3A%7B%22scheme%22%3A%22file%22%2C%22authority%22%3A%22%22%2C%22path%22%3A%22%2Fhome%2Fpi%2FDesktop%2Fprezenta%2Fapp.py%22%2C%22query%22%3A%22%22%2C%22fragment%22%3A%22%22%7D%2C%22pos%22%3A%7B%22line%22%3A42%2C%22character%22%3A4%7D%7D%5D%2C%229495c72a-c9b2-498f-9787-1c43fb524282%22%5D "Go to definition")).
- Logging messages locally and remotely ([`log_info_with_server`](command:_github.copilot.openSymbolFromReferences?%5B%22%22%2C%5B%7B%22uri%22%3A%7B%22scheme%22%3A%22file%22%2C%22authority%22%3A%22%22%2C%22path%22%3A%22%2Fhome%2Fpi%2FDesktop%2Fprezenta%2Fapp.py%22%2C%22query%22%3A%22%22%2C%22fragment%22%3A%22%22%7D%2C%22pos%22%3A%7B%22line%22%3A60%2C%22character%22%3A4%7D%7D%2C%7B%22uri%22%3A%7B%22scheme%22%3A%22file%22%2C%22authority%22%3A%22%22%2C%22path%22%3A%22%2Fhome%2Fpi%2FDesktop%2Fprezenta%2Fapp.py%22%2C%22query%22%3A%22%22%2C%22fragment%22%3A%22%22%7D%2C%22pos%22%3A%7B%22line%22%3A25%2C%22character%22%3A12%7D%7D%5D%2C%229495c72a-c9b2-498f-9787-1c43fb524282%22%5D "Go to definition")).
2. **Reusability**:
- The script is designed to be imported and reused in other Python applications.
3. **Configuration**:
- The log file path (`LOG_FILE_PATH`) and server URL ([`server_url`](command:_github.copilot.openSymbolFromReferences?%5B%22%22%2C%5B%7B%22uri%22%3A%7B%22scheme%22%3A%22file%22%2C%22authority%22%3A%22%22%2C%22path%22%3A%22%2Fhome%2Fpi%2FDesktop%2Fprezenta%2Fapp.py%22%2C%22query%22%3A%22%22%2C%22fragment%22%3A%22%22%7D%2C%22pos%22%3A%7B%22line%22%3A52%2C%22character%22%3A8%7D%7D%5D%2C%229495c72a-c9b2-498f-9787-1c43fb524282%22%5D "Go to definition")) can be customized as needed.
4. **Error Handling**:
- The script includes error handling for file operations and HTTP requests.
---
### Testing the Module
1. Create a new Python file (e.g., `test_logging.py`) and import the module:
```python
from logging_module import log_info_with_server, delete_old_logs
# Test the logging functions
log_info_with_server("Testing logging from another app.")
delete_old_logs()
```
2. Run the `test_logging.py` script:
```bash
python3 test_logging.py
```
3. Verify that:
- The log message is written to [`./data/log.txt`](command:_github.copilot.openRelativePath?%5B%7B%22scheme%22%3A%22file%22%2C%22authority%22%3A%22%22%2C%22path%22%3A%22%2Fhome%2Fpi%2FDesktop%2Fprezenta%2Fdata%2Flog.txt%22%2C%22query%22%3A%22%22%2C%22fragment%22%3A%22%22%7D%2C%229495c72a-c9b2-498f-9787-1c43fb524282%22%5D "/home/pi/Desktop/prezenta/data/log.txt").
- The log message is sent to the remote server (if configured correctly).
- Old logs are deleted if they are older than 10 days.
This approach ensures that the logging functionality is modular and reusable across multiple Python applications.

View File

@@ -0,0 +1,60 @@
import logging
import os
import socket
import requests
from datetime import datetime, timedelta
# Configure logging
LOG_FILE_PATH = './data/log.txt'
logging.basicConfig(filename=LOG_FILE_PATH, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# Function to delete old logs
def delete_old_logs():
log_dir = './data/'
log_file = 'log.txt'
log_path = os.path.join(log_dir, log_file)
if os.path.exists(log_path):
file_mod_time = datetime.fromtimestamp(os.path.getmtime(log_path))
if datetime.now() - file_mod_time > timedelta(days=10):
os.remove(log_path)
log_info_with_server(f"Deleted old log file: {log_file}")
else:
log_info_with_server(f"Log file is not older than 10 days: {log_file}")
else:
log_info_with_server(f"Log file does not exist: {log_file}")
# Function to read the name (idmasa) from the file
def read_name_from_file():
try:
with open("./data/idmasa.txt", "r") as file:
n_masa = file.readline().strip()
return n_masa
except FileNotFoundError:
logging.error("File ./data/idmasa.txt not found.")
return "unknown"
# Function to send logs to a remote server
def send_log_to_server(log_message, n_masa):
try:
hostname = socket.gethostname()
device_ip = socket.gethostbyname(hostname)
log_data = {
"hostname": str(hostname),
"device_ip": str(device_ip),
"nume_masa": str(n_masa),
"log_message": str(log_message)
}
server_url = "http://rpi-ansible:80/logs" # Replace with your server's URL
print(log_data) # Debugging: Print log_data to verify its contents
response = requests.post(server_url, json=log_data, timeout=5)
response.raise_for_status()
logging.info("Log successfully sent to server: %s", log_message)
except requests.exceptions.RequestException as e:
logging.error("Failed to send log to server: %s", e)
# Wrapper for logging.info to also send logs to the server
def log_info_with_server(message):
n_masa = read_name_from_file() # Read name (idmasa) from the file
logging.info(message)
send_log_to_server(message, n_masa)