Last active
July 13, 2021 22:33
-
-
Save ilyaevseev/8806593b7a7da8fa53e9dbf4bf2f57df to your computer and use it in GitHub Desktop.
Flush Linux swap back to RAM, suppresses alerting in Zabbix - useless for actual performance tuning
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
test "0" = "$(grep -vc '^Filename' /proc/swaps)" && exit 0 # ..no swaps | |
SWAP_FREE="$(awk ' | |
/^SwapTotal:/ { total = $2 } | |
/^SwapFree:/ { unused = $2 } | |
END { printf "%d\n", 100 * unused / total }' /proc/meminfo)" | |
test "$SWAP_FREE" -lt "${SWAP_NEED_FREE:-55}" || exit 0 | |
MEM_ENOUGH="$(awk ' | |
/^SwapTotal:/ { total = $2 } | |
/^SwapFree:/ { unused = $2 } | |
/^MemAvailable:/ { avail = $2 } | |
END { printf "%d\n", (avail > (total - unused)) }' /proc/meminfo)" | |
test "$MEM_ENOUGH" = "1" || { | |
logger -p "user.warn" -t "${0##*/}.nomem" -- "swap free ${SWAP_FREE}%, but no memory for flush" | |
exit 1 | |
} | |
logger -p "user.notice" -t "${0##*/}" -- "swap free ${SWAP_FREE}%, TRY TO FLUSH" | |
swapoff -a 2>&1 | logger -e -p "user.err" -t "${0##*/}.swapoff" | |
swapon -a 2>&1 | logger -e -p "user.err" -t "${0##*/}.swapon" | |
## END ## |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment