Files
Varaverk/unRAID Essentials/zfs_memory_snapshot.sh
T
2026-03-27 16:50:20 +00:00

92 lines
2.6 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
set -e
# ----------------------------------------------------------------------------------------------
# --------------------------- ZFS ARC & Memory Snapshot for Unraid -----------------------------
# ----------------------------------------------------------------------------------------------
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/../Master.conf"
source "$SCRIPT_DIR/../common.sh"
parse_args "$@"
# STATUS MODE
if [[ "$SHOW_STATUS" == true ]]; then
echo
echo "=================================================="
echo " ️ ZFS + MEMORY SNAPSHOT STATUS"
echo "--------------------------------------------------"
echo " 📊 Mode: Read-only diagnostics"
echo " 🧪 Dry Run: $DRY_RUN"
echo "=================================================="
exit 0
fi
# HEADER
echo
echo "============================================================"
echo " 📊 ZFS ARC + MEMORY SNAPSHOT"
echo "------------------------------------------------------------"
echo " 🕒 Time: $(date)"
echo " 🧪 Dry Run: $DRY_RUN"
echo "============================================================"
log "️ Collecting ZFS ARC + memory statistics"
# FUNCTIONS
show_zfs_arc() {
echo
echo "==================== ZFS ARC STATS ===================="
if [[ ! -r /proc/spl/kstat/zfs/arcstats ]]; then
warn "🔴 ZFS arcstats not available on this system"
return
fi
grep -iE '^(c|size|hits|misses|arc_meta_used|demand_metadata_misses|mru_ghost_metadata|mfu_ghost_metadata)' \
/proc/spl/kstat/zfs/arcstats 2>/dev/null || warn "🟡 Unable to read ARC stats"
}
show_memory_status() {
echo
echo "==================== MEMORY STATUS ===================="
if command -v free >/dev/null 2>&1; then
free -h | awk '
NR==1 {print $0}
/Mem:/ {print $0}
/Swap:/ {print $0}'
else
warn "🔴 free command not available"
fi
}
# EXECUTION
START_TIME=$(date +%s)
if [[ "$DRY_RUN" == true ]]; then
warn "🧪 Dry run enabled - no system data collected"
else
show_zfs_arc
show_memory_status
fi
END_TIME=$(date +%s)
DURATION=$((END_TIME - START_TIME))
# SUMMARY
echo
echo "============================================================"
echo " 📊 SNAPSHOT SUMMARY"
echo "------------------------------------------------------------"
echo " ⏱ Duration: ${DURATION}s"
echo " 📡 Mode: READ-ONLY"
echo " 🧠 Source: ZFS ARC + system memory"
echo "============================================================"
info "️ Snapshot complete"