#!/bin/bash

# Memory state with use of "free"
MEM_TOTAL=$(free -m | awk '/^Mem:/ { print $2 } ')
MEM_USED=$(free -m | awk '/^Mem:/ { print $3 } ')

# ARC state with use of "/proc/spl/kstat/zfs/arcstats"
FLOAT_ARC_USED=$(awk '/^size/ { print $3 / 1048576 }' < /proc/spl/kstat/zfs/arcstats)
INT_ARC_USED=${FLOAT_ARC_USED%.*}

# Calculated values
MEM_SYSTEM=$(($MEM_USED-INT_ARC_USED))
MEM_AVAILABLE=$(($MEM_TOTAL-$MEM_SYSTEM))

# output
echo -e "\nMemory overview:\n"
printf "\nTotal Memory:,Used Memory:,Available Memory:,Memory ARC:,Memory Sys & KVM:\n$MEM_TOTAL MiB,$MEM_USED MiB,$MEM_AVAILABLE MiB,$INT_ARC_USED MiB,$MEM_SYSTEM MiB\n\n" | column -t -s ','

echo -e "\nOutput definition:"

echo -e "Total Memory:	Memory installed on system
Available Memory:	['Total Memory' - 'Memory Sys & KVM']
Memory ARC:	Memory used by ZFS RAM cache (ARC)
Memory Sys & KVM:	Memory used by containers, VMs and system\n" | column -t -s '	'

