cisco-vpn: Daily log rotation with 7-day retention
Some checks failed
Test / test (push) Has been cancelled
Some checks failed
Test / test (push) Has been cancelled
- Logs now saved to /var/log/cisco-vpn/YYYY-MM-DD.log - Automatic cleanup of logs older than 7 days - Each day gets its own log file
This commit is contained in:
@@ -26,9 +26,20 @@ TOTP_SECRET="${VPN_TOTP_SECRET:-}"
|
|||||||
VPN_HOST="${VPN_HOST:-vpn-ord1.dovercorp.com}"
|
VPN_HOST="${VPN_HOST:-vpn-ord1.dovercorp.com}"
|
||||||
TARGET_IP="${TARGET_IP:-10.35.33.230}"
|
TARGET_IP="${TARGET_IP:-10.35.33.230}"
|
||||||
|
|
||||||
# Log file
|
# Log directory and file (date-based rotation)
|
||||||
LOG_FILE="/var/log/cisco-vpn.log"
|
LOG_DIR="/var/log/cisco-vpn"
|
||||||
mkdir -p "$(dirname "$LOG_FILE")" 2>/dev/null
|
LOG_RETENTION_DAYS=7
|
||||||
|
mkdir -p "$LOG_DIR" 2>/dev/null
|
||||||
|
|
||||||
|
# Function to get current log file (changes daily)
|
||||||
|
get_log_file() {
|
||||||
|
echo "$LOG_DIR/$(date '+%Y-%m-%d').log"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Cleanup old log files (older than LOG_RETENTION_DAYS)
|
||||||
|
cleanup_old_logs() {
|
||||||
|
find "$LOG_DIR" -name "*.log" -type f -mtime +$LOG_RETENTION_DAYS -delete 2>/dev/null
|
||||||
|
}
|
||||||
|
|
||||||
# Colors
|
# Colors
|
||||||
RED='\033[0;31m'
|
RED='\033[0;31m'
|
||||||
@@ -51,15 +62,16 @@ SKIP_AUTO_LOGIN=false
|
|||||||
DO_CONNECT=false
|
DO_CONNECT=false
|
||||||
DO_DISCONNECT=false
|
DO_DISCONNECT=false
|
||||||
|
|
||||||
# Logging function with timestamp - writes to both console and file
|
# Logging function with timestamp - writes to both console and daily log file
|
||||||
log() {
|
log() {
|
||||||
local level="$1"
|
local level="$1"
|
||||||
local msg="$2"
|
local msg="$2"
|
||||||
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
|
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
|
||||||
local timestamp_short=$(date '+%H:%M:%S')
|
local timestamp_short=$(date '+%H:%M:%S')
|
||||||
|
local log_file=$(get_log_file)
|
||||||
|
|
||||||
# Write to log file (plain text, no colors)
|
# Write to log file (plain text, no colors)
|
||||||
echo "[$timestamp] [$level] $msg" >> "$LOG_FILE"
|
echo "[$timestamp] [$level] $msg" >> "$log_file"
|
||||||
|
|
||||||
# Write to console (with colors)
|
# Write to console (with colors)
|
||||||
case $level in
|
case $level in
|
||||||
@@ -716,9 +728,12 @@ parse_args() {
|
|||||||
# Main
|
# Main
|
||||||
parse_args "$@"
|
parse_args "$@"
|
||||||
|
|
||||||
|
# Cleanup old logs and start fresh
|
||||||
|
cleanup_old_logs
|
||||||
|
|
||||||
# Log script start
|
# Log script start
|
||||||
echo "" >> "$LOG_FILE"
|
echo "" >> "$(get_log_file)"
|
||||||
echo "========================================" >> "$LOG_FILE"
|
echo "========================================" >> "$(get_log_file)"
|
||||||
log INFO "cisco-vpn script started"
|
log INFO "cisco-vpn script started"
|
||||||
log DEBUG "VPN_EMAIL=$EMAIL"
|
log DEBUG "VPN_EMAIL=$EMAIL"
|
||||||
log DEBUG "VPN_HOST=$VPN_HOST"
|
log DEBUG "VPN_HOST=$VPN_HOST"
|
||||||
|
|||||||
Reference in New Issue
Block a user