Преглед изворни кода

feat: script for optional network stats configuration update README and

TheHuman00 пре 8 месеци
родитељ
комит
dbcab52f43
2 измењених фајлова са 44 додато и 18 уклоњено
  1. 14 3
      README.md
  2. 30 15
      chrony-network-stats.sh

+ 14 - 3
README.md

@@ -18,11 +18,14 @@ Demo here : [https://thehuman00.github.io/demo-chrony-stats.github.io/](https://
 ## Prerequisites
 
 **Install dependencies** (on Debian/Ubuntu-based systems):
+
+#### **For full functionality (Chrony + Network stats):**
    ```bash
    sudo apt update
    sudo apt install vnstat vnstati rrdtool chrony
    ```
-**Configure vnStat**:
+
+**Configure vnStat**
    Ensure `vnstat` is monitoring the correct network interface (e.g., `eth0`):
    Find your interface here :
    ```bash
@@ -34,6 +37,11 @@ Demo here : [https://thehuman00.github.io/demo-chrony-stats.github.io/](https://
    ``` 
    **If not eth0** : ⚠️ Change your network interface in [Configuration](#configuration) section !!
 
+#### **For Chrony-only monitoring (set `ENABLE_NETWORK_STATS="no"`):**
+   ```bash
+   sudo apt update
+   sudo apt install rrdtool chrony
+   ```
 
 ## Installation
 
@@ -57,7 +65,10 @@ The script includes a configuration section at the top of `chrony-network-stats.
 
    ```bash
    [...]
-   #### Configuration ####
+   ####################### Configuration ######################
+
+   # Enable or disable network statistics generation using vnStat
+   ENABLE_NETWORK_STATS="yes"
 
    # ⚠️ IMPORTANT: Replace "eth0" with your actual interface 
    #    (e.g., ens33, enp0s3, wlan0, ...)
@@ -83,7 +94,7 @@ The script includes a configuration section at the top of `chrony-network-stats.
    ## You can display or not the link to the chrony-stats GitHub repository in the HTML page
    ## It's completely optional by default - feel free to use it however you want, no credit needed! 👍
    GITHUB_REPO_LINK_SHOW="no"
-   #########################
+   ##############################################################
    [...]
    ```
    Close with Ctrl+X --> Y --> Enter

+ 30 - 15
chrony-network-stats.sh

@@ -2,28 +2,25 @@
 set -e
 
 ####################### Configuration ######################
-INTERFACE="eth0"
+
+ENABLE_NETWORK_STATS="yes" ## Enable or disable network statistics generation using vnStat
+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"
-HTML_FILENAME="index.html"
+OUTPUT_DIR="/var/www/html/chrony-network-stats" ## Output directory for HTML and images
+HTML_FILENAME="index.html" ## Output HTML file name
 
 ENABLE_LOGGING="yes"
 LOG_FILE="/var/log/chrony-network-stats.log"
 RRD_DIR="/var/lib/chrony-rrd"
 RRD_FILE="$RRD_DIR/chrony.rrd"
-WIDTH=800
-HEIGHT=300
-TIMEOUT_SECONDS=5
 
-## When chrony restarts, it can generate abnormally high statistical values (e.g., 12M packets)
-## This parameter filters out values above the threshold,
-## creating gaps in the graph instead of displaying misleading spikes.
-SERVER_STATS_UPPER_LIMIT=100000
+WIDTH=800   ## Width of the generated graphs
+HEIGHT=300  ## Height of the generated graphs
+TIMEOUT_SECONDS=5
+SERVER_STATS_UPPER_LIMIT=100000 ## When chrony restarts, it generate abnormally high values (e.g., 12M) | This filters out values above the threshold
 
-## You can display the link to the repo 'chrony-stats' in the HTML footer
-## Not required | Default: no
-GITHUB_REPO_LINK_SHOW="no"
+GITHUB_REPO_LINK_SHOW="no" ## You can display the link to the repo 'chrony-stats' in the HTML footer | Not required | Default: no
 
 ##############################################################
 
@@ -46,7 +43,12 @@ validate_numeric() {
 }
 
 check_commands() {
-    local commands=("vnstati" "rrdtool" "chronyc" "sudo" "timeout")
+    local commands=("rrdtool" "chronyc" "sudo" "timeout")
+    
+    if [[ "$ENABLE_NETWORK_STATS" == "yes" ]]; then
+        commands+=("vnstati")
+    fi
+    
     for cmd in "${commands[@]}"; do
         if ! command -v "$cmd" &>/dev/null; then
             log_message "ERROR" "Command '$cmd' not found in PATH."
@@ -70,6 +72,11 @@ setup_directories() {
 }
 
 generate_vnstat_images() {
+    if [[ "$ENABLE_NETWORK_STATS" != "yes" ]]; then
+        log_message "INFO" "Network stats disabled, skipping vnStat image generation..."
+        return 0
+    fi
+    
     log_message "INFO" "Generating vnStat images for interface '$INTERFACE'..."
     local modes=("s" "d" "t" "h" "m" "y")
     for mode in "${modes[@]}"; do
@@ -577,6 +584,10 @@ generate_html() {
                     </div>
                 </div>
             </section>
+EOF
+
+    if [[ "$ENABLE_NETWORK_STATS" == "yes" ]]; then
+        cat >>"$OUTPUT_DIR/$HTML_FILENAME" <<EOF
 
             <section id="vnstat-graphs">
                 <h2>vnStati Graphs</h2>
@@ -597,6 +608,10 @@ generate_html() {
                     </tbody>
                 </table>
             </section>
+EOF
+    fi
+
+    cat >>"$OUTPUT_DIR/$HTML_FILENAME" <<EOF
 
             <section id="chrony-stats">
                 <h2>Chrony - NTP Statistics</h2>
@@ -643,7 +658,7 @@ EOF
 }
 
 main() {
-    log_message "INFO" "Starting vnstati script..."
+    log_message "INFO" "Starting chrony-network-stats script..."
     validate_numeric "$WIDTH" "WIDTH"
     validate_numeric "$HEIGHT" "HEIGHT"
     validate_numeric "$TIMEOUT_SECONDS" "TIMEOUT_SECONDS"