#!/bin/bash # ----------------------------------------------------------------------------------------------- # --------------------------------- ZFS Memory Snapshot ---------------------------------------- # ----------------------------------------------------------------------------------------------- # Captures a point-in-time snapshot of ZFS ARC statistics and system memory usage. # Read-only diagnostic tool — no changes are made to the system regardless of flags. # Dry run mode still collects and displays data since no modifications occur. # ----------------------------------------------------------------------------------------------- SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "$SCRIPT_DIR/../Master.conf" source "$SCRIPT_DIR/../common.sh" parse_args "$@" # ----------------------------------------------------------------------------------------------- # ━━━ $ICON_GEAR Setup ━━━ # ----------------------------------------------------------------------------------------------- echo "" echo "━━━ $ICON_GEAR Setup ━━━" info "$ICON_ZFS ZFS ARC + Memory Snapshot" info "$ICON_TIME $(date)" # ----------------------------------------------------------------------------------------------- # ━━━ $ICON_SUMMARY Status ━━━ # ----------------------------------------------------------------------------------------------- if [[ "$SHOW_STATUS" == true ]]; then echo "" echo "━━━━━ $ICON_SUMMARY STATUS ━━━━━" echo "$ICON_ZFS Mode: Read-only diagnostics" echo "$ICON_MEM Source: ZFS ARC + system memory" echo "$ICON_GEAR Dry Run: $DRY_RUN" echo "━━━━━━━━━━━━━━━━━━━━━━━" exit 0 fi # ----------------------------------------------------------------------------------------------- # FUNCTIONS # ----------------------------------------------------------------------------------------------- # Reads ZFS ARC statistics from the kernel stats interface. # Filters for the most useful ARC metrics — size, hits, misses and metadata. # Skips gracefully if ZFS is not available on this system. show_zfs_arc() { echo "" echo "━━━ $ICON_ZFS ZFS ARC Stats ━━━" if [[ ! -r /proc/spl/kstat/zfs/arcstats ]]; then warn "ZFS arcstats not available on this system — is ZFS loaded?" 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" } # Reads current system memory and swap usage using free. # Skips gracefully if free is not available. show_memory_status() { echo "" echo "━━━ $ICON_MEM Memory Status ━━━" if ! command -v free >/dev/null 2>&1; then warn "free command not available on this system" return fi free -h | awk ' NR==1 { print $0 } /Mem:/ { print $0 } /Swap:/ { print $0 }' } # ----------------------------------------------------------------------------------------------- # ━━━ $ICON_ZFS Snapshot ━━━ # ----------------------------------------------------------------------------------------------- echo "" echo "━━━ $ICON_ZFS Snapshot ━━━" echo "$ICON_ZFS Source: ZFS ARC + system memory" echo "$ICON_GEAR Dry Run: $DRY_RUN" echo "" [[ "$DRY_RUN" == true ]] && warn "DRY RUN — this script is read-only, data will still be collected" START=$(date +%s) show_zfs_arc show_memory_status END=$(date +%s) # ----------------------------------------------------------------------------------------------- # ━━━ $ICON_SUMMARY Summary ━━━ # ----------------------------------------------------------------------------------------------- echo "" echo "━━━━━ $ICON_SUMMARY SNAPSHOT SUMMARY ━━━━━" echo "$ICON_ZFS Source: ZFS ARC + system memory" echo "$ICON_GEAR Mode: READ-ONLY — no changes made" echo "$ICON_TIME Duration: $(format_duration $((END - START)))" echo "$ICON_DONE Status: $ICON_SUCCESS DONE" echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"