#!/bin/sh
# Checks memory usage

# Number of seconds to retest
timer=5

# Main loop
while true; do

    mem_total=$(LANG=C free -b|awk '/^Mem:/{print $2}')
    mem_used=$(LANG=C free -b|awk '/^Mem:/{print $3}')
    swap_total=$(LANG=C free -b|awk '/^Swap:/{print $2}')
    swap_used=$(LANG=C free -b|awk '/^Swap:/{print $3}')


    memperc=$(echo "$mem_used" "$mem_total" |  awk '{ div = 100 * ($1 / $2)} { printf "%.0f ", div }' | tr -d ' ' )
    swapperc=$(echo "$swap_used" "$swap_total" |  awk '{
    if ( $1 == 0 || $2 == 0 ) {
        print "%"
    } else {
    div = 100 * ($1 / $2); 
    printf "%.0f ", div
}
}' | tr -d ' ' )


echo "memperc|string|$memperc"
if [ "$swapperc" = "%" ];then
    echo "swapstate|bool|false"
else
    echo "swapperc|string|$swapperc"
    echo "swapstate|bool|true"
fi
echo ""
sleep "$timer"

done