Jelajahi Sumber

adding logging rotation in the script

Medowar 1 bulan lalu
induk
melakukan
e01320c398
1 mengubah file dengan 47 tambahan dan 5 penghapusan
  1. 47 5
      chrony-network-stats.sh

+ 47 - 5
chrony-network-stats.sh

@@ -7,7 +7,7 @@ ENABLE_NETWORK_STATS="yes" ## Enable or disable network statistics generation us
 INTERFACE="eth0" ## Network interface to monitor (e.g., eth0, wlan0)
 
 PAGE_TITLE="Network Traffic and Chrony Statistics for ${INTERFACE}"
-OUTPUT_DIR="/var/www/html/chrony-network-stats" ## Output directory for HTML and images
+OUTPUT_DIR="/var/www/html/" ## Output directory for HTML and images
 HTML_FILENAME="index.html" ## Output HTML file name
 
 RRD_DIR="/var/lib/chrony-rrd"
@@ -15,6 +15,14 @@ RRD_FILE="$RRD_DIR/chrony.rrd" ## RRD file for storing chrony statistics
 
 ENABLE_LOGGING="yes"
 LOG_FILE="/var/log/chrony-network-stats.log"
+# In-script log truncation (prevents logfile from growing indefinitely)
+# - LOG_MAX_SIZE_BYTES: when >0 use byte-based truncation (default 10MB)
+# - LOG_MAX_LINES: when >0 use line-based truncation instead of bytes
+# - LOG_TRIM_TO_BYTES / LOG_TRIM_TO_LINES: amount to keep when trimming (defaults to half)
+LOG_MAX_SIZE_BYTES=0 # $((10 * 1024 * 1024))  # 10 MB
+LOG_MAX_LINES=10000
+LOG_TRIM_TO_BYTES=0 # $((LOG_MAX_SIZE_BYTES / 2))
+LOG_TRIM_TO_LINES=9000
 
 AUTO_REFRESH_SECONDS=0 ## Auto-refresh interval in seconds (0 = disabled, e.g., 300 for 5 minutes)
 GITHUB_REPO_LINK_SHOW="no" ## You can display the link to the repo 'chrony-stats' in the HTML footer | Not required | Default: no
@@ -36,9 +44,43 @@ log_message() {
     local level="$1"
     local message="$2"
     if [[ "$ENABLE_LOGGING" == "yes" ]]; then
-    	echo "[$(date '+%Y-%m-%d %H:%M:%S')] [$level] $message" >> "$LOG_FILE"
+        # ensure logfile stays within configured limits before writing
+        rotate_logfile_if_needed
+        printf '[%s] [%s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$level" "$message" >> "$LOG_FILE"
+    fi
+    echo "[$level] $message"
+}
+
+rotate_logfile_if_needed() {
+    # no-op when logging disabled or logfile missing
+    [ "$ENABLE_LOGGING" != "yes" ] && return 0
+    [ -f "$LOG_FILE" ] || return 0
+
+    # Line-based truncation (preferred when configured)
+    if [[ "${LOG_MAX_LINES:-0}" -gt 0 ]]; then
+        local total_lines
+        total_lines=$(wc -l < "$LOG_FILE" 2>/dev/null || echo 0)
+        if [[ "$total_lines" -gt "$LOG_MAX_LINES" ]]; then
+            local keep_lines=${LOG_TRIM_TO_LINES:-$(( LOG_MAX_LINES / 2 ))}
+            (( keep_lines <= 0 )) && keep_lines=$(( LOG_MAX_LINES / 2 ))
+            tail -n "$keep_lines" "$LOG_FILE" > "${LOG_FILE}.tmp" && mv "${LOG_FILE}.tmp" "$LOG_FILE"
+            printf '[%s] [INFO] Log truncated to last %d lines (was %d lines)\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$keep_lines" "$total_lines" >> "$LOG_FILE"
+        fi
+        return 0
+    fi
+
+    # Size-based truncation (bytes)
+    local max_bytes=${LOG_MAX_SIZE_BYTES:-10485760}
+    local filesize
+    if ! filesize=$(stat -c%s "$LOG_FILE" 2>/dev/null); then
+        filesize=$(wc -c < "$LOG_FILE" 2>/dev/null || echo 0)
+    fi
+    if [[ "$filesize" -ge "$max_bytes" ]]; then
+        local keep_bytes=${LOG_TRIM_TO_BYTES:-$(( max_bytes / 2 ))}
+        (( keep_bytes <= 0 )) && keep_bytes=$(( max_bytes / 2 ))
+        tail -c "$keep_bytes" "$LOG_FILE" > "${LOG_FILE}.tmp" && mv "${LOG_FILE}.tmp" "$LOG_FILE"
+        printf '[%s] [INFO] Log truncated to last %d bytes (was %d bytes)\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$keep_bytes" "$filesize" >> "$LOG_FILE"
     fi
-    	echo "[$level] $message"
 }
 
 configure_display_preset() {
@@ -136,7 +178,7 @@ collect_chrony_data() {
     fi
     
     get_html() {
-        timeout "$TIMEOUT_SECONDS"s sudo chronyc $CHRONYC_OPTS "$1" -v 2>&1 | sed 's/&/\&/g;s/</\</g;s/>/\>/g;s/$/<br>/' || {
+        timeout "$TIMEOUT_SECONDS"s sudo chronyc $CHRONYC_OPTS "$1" -v 2>&1 | sed 's/&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g' || {
             log_message "ERROR" "Failed to collect chronyc $1 data"
             return 1
         }
@@ -146,7 +188,7 @@ collect_chrony_data() {
         log_message "ERROR" "Failed to collect chronyc tracking data"
         exit 1
     }
-    CHRONYC_TRACKING_HTML=$(echo "$RAW_TRACKING" | sed 's/&/\&/g;s/</\</g;s/>/\>/g;s/$/<br>/')
+    CHRONYC_TRACKING_HTML=$(echo "$RAW_TRACKING" | sed 's/&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g')
     CHRONYC_SOURCES=$(get_html sources) || exit 1
     CHRONYC_SOURCESTATS=$(get_html sourcestats) || exit 1
     CHRONYC_SELECTDATA=$(get_html selectdata) || exit 1