From 23289acabdf6c5358d4eac06030b44bf89fa5909 Mon Sep 17 00:00:00 2001 From: "Valeriano A.R." Date: Wed, 6 Aug 2025 14:03:57 +0200 Subject: [PATCH] Nethogs-Monitor: Add Nethogs monitor service with installation scripts, dashboard, and reporting utilities --- monitor-service/README.md | 28 + .../nethogs-monitor-dashboard-template.html | 711 ++++++++++++++++++ monitor-service/nethogs-monitor-install.sh | 22 + monitor-service/nethogs-monitor-report.sh | 136 ++++ monitor-service/nethogs-monitor.service | 31 + monitor-service/nethogs-monitor.sh | 33 + 6 files changed, 961 insertions(+) create mode 100644 monitor-service/README.md create mode 100644 monitor-service/nethogs-monitor-dashboard-template.html create mode 100644 monitor-service/nethogs-monitor-install.sh create mode 100644 monitor-service/nethogs-monitor-report.sh create mode 100644 monitor-service/nethogs-monitor.service create mode 100644 monitor-service/nethogs-monitor.sh diff --git a/monitor-service/README.md b/monitor-service/README.md new file mode 100644 index 0000000..9424d49 --- /dev/null +++ b/monitor-service/README.md @@ -0,0 +1,28 @@ +# Nethogs monitor + +Use nethogs as a background service to monitor network usage. + +## Installation + +```sh +sh nethogs-monitor-install.sh +``` + +## Usage + +Enable service: +```sh +systemctl enable nethogs-monitor +``` + +Start service: +```sh +systemctl start nethogs-monitor +``` + +View report: +```sh +nethogs-monitor-report.sh +``` + + diff --git a/monitor-service/nethogs-monitor-dashboard-template.html b/monitor-service/nethogs-monitor-dashboard-template.html new file mode 100644 index 0000000..3040493 --- /dev/null +++ b/monitor-service/nethogs-monitor-dashboard-template.html @@ -0,0 +1,711 @@ + + + + + + Nethogs Network Monitor + + + + +
+
+

🌐 Network Monitor Dashboard

+

+ System network traffic monitoring +

+
+ Generated on: GENERATION_TIME_PLACEHOLDER + | Data from last HOURS_BACK_PLACEHOLDER hours + | 0 records +
+
+ +
+
+
-
+
Registered Connections
+
+
+
-
+
Total Download
+
+
+
-
+
Total Upload
+
+
+
-
+
Unique Processes
+
+
+ +
+
+
+ + +
+ +
+ + +
+ + +
+
+ +
+
+
Traffic by Application
+ +
+ +
+
Traffic by Interface
+ +
+ +
+
Traffic Evolution Over Time
+ +
+
+ +
+
+ 📊 Connection Details + 0 records +
+
+ + + + + + + + + + + + + + + + + + +
TimestampProcessPIDUIDInterfaceDownloadUploadTotal
Processing data...
+
+
+
+ + + + \ No newline at end of file diff --git a/monitor-service/nethogs-monitor-install.sh b/monitor-service/nethogs-monitor-install.sh new file mode 100644 index 0000000..2190b9b --- /dev/null +++ b/monitor-service/nethogs-monitor-install.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +# Create directories +sudo mkdir -p /var/log/nethogs +sudo mkdir -p /opt/nethogs-monitor + +# Move files +sudo cp ../src/nethogs /opt/nethogs-monitor/ +sudo cp nethogs-monitor.sh /opt/nethogs-monitor/ +sudo cp nethogs-monitor-dashboard-template.html /opt/nethogs-monitor/ +sudo cp nethogs-monitor.service /etc/systemd/system/ +sudo cp nethogs-monitor-report.sh /opt/nethogs-monitor/ + +# Set execution permissions +sudo chmod +x /opt/nethogs-monitor/nethogs-monitor.sh +sudo chmod +x /opt/nethogs-monitor/nethogs-monitor-report.sh + +# Link in local bins +sudo ln -sf /opt/nethogs-monitor/nethogs-monitor-report.sh /usr/local/bin/nethogs-monitor-report.sh + +# Reload daemons +sudo systemctl daemon-reload diff --git a/monitor-service/nethogs-monitor-report.sh b/monitor-service/nethogs-monitor-report.sh new file mode 100644 index 0000000..f3e2bfd --- /dev/null +++ b/monitor-service/nethogs-monitor-report.sh @@ -0,0 +1,136 @@ +#!/bin/bash + +set -e + +# Configuration +NETHOGS_LOG="/var/log/nethogs/nethogs.jsonl" +TEMPLATE_FILE="/opt/nethogs-monitor/nethogs-monitor-dashboard-template.html" +TEMP_DIR=$(mktemp -d) +HOURS_BACK="${1:-24}" +OUTPUT_FILE="${2:-${PWD}/nethogs-dashboard.html}" +MAX_LINES=10000 + +log() { + echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1" >&2 +} + +cleanup() { + rm -rf "$TEMP_DIR" +} + +trap cleanup EXIT + +# Function to extract recent data from JSONL +extract_recent_data() { + local hours_back=$1 + local output_file="$TEMP_DIR/recent_data.jsonl" + + if [ ! -f "$NETHOGS_LOG" ]; then + log "Warning: File $NETHOGS_LOG not found" + echo "[]" > "$output_file" + return + fi + + # Calculate timestamp limit + local time_limit=$(date -d "$hours_back hours ago" +%s) + + # Extract last lines and filter by time + echo "[" > "$output_file" + tail -n $MAX_LINES "$NETHOGS_LOG" | while IFS= read -r line; do + if [ -n "$line" ]; then + # Extract timestamp and convert to epoch + local timestamp_iso=$(echo "$line" | jq -r '.timestamp // empty' 2>/dev/null || echo "") + if [ -n "$timestamp_iso" ]; then + local timestamp_epoch=$(date -d "$timestamp_iso" +%s 2>/dev/null || echo "0") + if [ "$timestamp_epoch" -ge "$time_limit" ]; then + echo "${line}," + fi + fi + fi + done >> "$output_file" + echo "]" >> "$output_file" + + # If no data, create valid empty file + if [ ! -s "$output_file" ]; then + echo "[]" > "$output_file" + fi +} + +# Function to generate HTML with embedded data +generate_html() { + local data_file="$1" + local output="$2" + + # Verify template exists + if [ ! -f "$TEMPLATE_FILE" ]; then + log "Error: Template not found at $TEMPLATE_FILE" + exit 1 + fi + + # Generate generation timestamp + local generation_time=$(date -Iseconds) + + # Copy template and replace placeholders + cp "$TEMPLATE_FILE" "$output" + + #sed -i "s|JSON_DATA_PLACEHOLDER|$json_data|g" "$output" + sed -i "/JSON_DATA_PLACEHOLDER/{ + r $data_file + d + }" "$output" + sed -i "s|GENERATION_TIME_PLACEHOLDER|$generation_time|g" "$output" + sed -i "s|HOURS_BACK_PLACEHOLDER|$HOURS_BACK|g" "$output" + + log "Dashboard HTML generated: $output" +} + +# Main function +main() { + log "Starting dashboard generation" + log "Extracting data from last $HOURS_BACK hours..." + + # Check dependencies + if ! command -v jq &> /dev/null; then + log "Error: jq is not installed. Install with: sudo apt install jq" + exit 1 + fi + + # Extract recent data + extract_recent_data "$HOURS_BACK" + + # Verify data exists + local data_file="$TEMP_DIR/recent_data.jsonl" + local record_count=$(wc -l < "$data_file") + log "Processing $record_count records" + + # Generate HTML + log "Generating dashboard HTML..." + generate_html "$data_file" "$OUTPUT_FILE" + + # Create output directory if it doesn't exist + mkdir -p "$(dirname "$OUTPUT_FILE")" + + log "Dashboard generated successfully: $OUTPUT_FILE" + log "File size: $(du -h "$OUTPUT_FILE" | cut -f1)" + + xdg-open "$OUTPUT_FILE" +} + +# Show help +if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then + echo "Usage: $0 [HOURS] [OUTPUT_FILE]" + echo + echo "HOURS: Number of hours to look back for data (default: 24)" + echo "OUTPUT_FILE: Output HTML file (default: ${PWD}/nethogs-dashboard.html)" + echo + echo "Examples:" + echo " $0 # Last 24 hours, default output" + echo " $0 6 # Last 6 hours" + echo " $0 168 /var/www/html/week.html # Last week" + echo + echo "Dependencies: jq" + exit 0 +fi + +# Execute main function +main "$@" \ No newline at end of file diff --git a/monitor-service/nethogs-monitor.service b/monitor-service/nethogs-monitor.service new file mode 100644 index 0000000..1a6e600 --- /dev/null +++ b/monitor-service/nethogs-monitor.service @@ -0,0 +1,31 @@ +[Unit] +Description=Nethogs Network Monitor Service +Documentation=man:nethogs(8) +After=network.target +Wants=network-online.target + +[Service] +Type=simple +User=root +Group=root +ExecStart=/opt/nethogs-monitor/nethogs-monitor.sh +ExecStop=/bin/kill -TERM $MAINPID +PIDFile=/run/nethogs-monitor.pid +Restart=always +RestartSec=10 +StandardOutput=journal +StandardError=journal + +# Configuración de seguridad +NoNewPrivileges=true +ProtectSystem=strict +ReadWritePaths=/var/log/nethogs /run +ProtectHome=true +PrivateTmp=true + +# Límites de recursos +LimitNOFILE=65536 +MemoryMax=512M + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/monitor-service/nethogs-monitor.sh b/monitor-service/nethogs-monitor.sh new file mode 100644 index 0000000..97806d2 --- /dev/null +++ b/monitor-service/nethogs-monitor.sh @@ -0,0 +1,33 @@ +#!/bin/bash + +# Configuration +LOGFILE="/var/log/nethogs/nethogs.jsonl" +PIDFILE="/run/nethogs-monitor.pid" +NETHOGS_BIN="/opt/nethogs-monitor/nethogs" + +cleanup() { + echo "Stopping nethogs monitor..." + if [ -f "$PIDFILE" ]; then + rm -f "$PIDFILE" + fi + exit 0 +} +trap cleanup SIGTERM SIGINT + +# Write PID +echo $$ > "$PIDFILE" + +# Ensure logfile directory +mkdir -p "$(dirname "$LOGFILE")" + +# Main loop +while true; do + # Execute nethogs + "$NETHOGS_BIN" -j -v 6 -z -C -d 10 >> "$LOGFILE" 2>&1 + + # Si nethogs falla, esperar antes de reintentar + if [ $? -ne 0 ]; then + echo "$(date): Error executing nethogs, retrying in 10 seconds ..." + sleep 10 + fi +done \ No newline at end of file