improve log system
This commit is contained in:
Vendored
+30
-5
@@ -14,12 +14,37 @@ DEFAULT_GATEWAY="${GATEWAY_ENV:-10.10.0.1}" # Use GATEWAY_ENV if provided, othe
|
||||
DNS_SERVERS="8.8.8.8 8.8.4.4"
|
||||
LOG_FILE="/var/log/set_static_ip.log"
|
||||
NETPLAN_DIR="/etc/netplan"
|
||||
MAX_LOG_SIZE_KB=1024 # 1MB
|
||||
|
||||
# --- Functions ---
|
||||
# --- Funções Auxiliares ---
|
||||
|
||||
# Function to log messages
|
||||
# Função de Log (modificada para usar logger)
|
||||
log() {
|
||||
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
|
||||
local message="$1"
|
||||
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
|
||||
|
||||
# Escreve no ficheiro de log
|
||||
echo "$timestamp: $message" | tee -a "$LOG_FILE"
|
||||
|
||||
# Envia para o syslog (opcional)
|
||||
logger -t "set_static_ip" "$message"
|
||||
}
|
||||
|
||||
# Função para limitar o tamanho do log
|
||||
limit_log_size() {
|
||||
local log_file="$1"
|
||||
local max_size_kb="$2"
|
||||
local max_size_bytes=$((max_size_kb * 1024))
|
||||
|
||||
if [ -f "$log_file" ]; then
|
||||
local current_size=$(stat -c %s "$log_file" 2>/dev/null || wc -c < "$log_file" 2>/dev/null)
|
||||
|
||||
if [ "$current_size" -gt "$max_size_bytes" ]; then
|
||||
log "Aviso: Ficheiro de log $log_file excedeu $max_size_kb KB. A truncar..."
|
||||
tail -n 500 "$log_file" > "${log_file}.tmp" && mv "${log_file}.tmp" "$log_file"
|
||||
log "Ficheiro de log truncado. As últimas 500 linhas foram mantidas."
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to fix netplan permissions
|
||||
@@ -231,8 +256,8 @@ configure_static_ip() {
|
||||
local interface
|
||||
local dns="$DNS_SERVERS"
|
||||
|
||||
# Create log directory if it doesn't exist
|
||||
mkdir -p /var/log
|
||||
mkdir -p "$(dirname "$LOG_FILE")"
|
||||
limit_log_size "$LOG_FILE" "$MAX_LOG_SIZE_KB" # Verifica o tamanho do log no início
|
||||
|
||||
# Get active interface
|
||||
if ! interface=$(get_active_interface); then
|
||||
|
||||
Reference in New Issue
Block a user