Skip to content

Instantly share code, notes, and snippets.

@gilangvperdana
Last active March 14, 2023 04:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gilangvperdana/ecac97b36bd0257dbc7f6691493130fb to your computer and use it in GitHub Desktop.
Save gilangvperdana/ecac97b36bd0257dbc7f6691493130fb to your computer and use it in GitHub Desktop.
Automaticly Enable Disable Swap Based on Utilization of RAM Usage on Linux

General

  • I have a case on a server host where when enabling swap it increases IOwait (because of slow disk) but on the other hand it is the most reliable host (so it often runs out of RAM). This prompted me to automate the use of swap when there is only certain RAM.
  • In this script it is possible to execute swapon /swapfile when the RAM threshold reaches 90% while swap is immediately turned off swapoff -a when RAM is below 80%
  • Besides this script executing predetermined commands regarding swap, this script will also report to the BOT telegram.

Script.sh

#!/bin/bash

# Set the threshold for activating and deactivating swap
ACTIVATE_THRESHOLD=90
DEACTIVATE_THRESHOLD=80

# Set the Telegram bot token and chat ID
BOT_TOKEN="YOUR_BOT_TOKEN"
CHAT_ID="YOUR_CHAT_ID"

HOSTNAME=$(hostname)

# Set the initial status of swap to "unknown"
SWAP_STATUS="unknown"

while true; do
  # Get the total memory usage
  MEMORY=$(free | awk '/Mem/{printf("%.2f"), $3/$2*100}')

  # Check if the memory usage is above the activation threshold
  if (( $(echo "$MEMORY > $ACTIVATE_THRESHOLD" | bc -l) )); then
    # Check if swap is already activated
    if [[ "$SWAP_STATUS" == "activated" ]]; then
      echo "Swap already activated."
    else
      # Activate swap
      swapon /swapfile
      echo "Swap activated. Current memory usage: $MEMORY%."

      # Send a notification to Telegram
      curl -s -X POST "https://api.telegram.org/bot$BOT_TOKEN/sendMessage" \
        -d chat_id="$CHAT_ID" \
        -d text="Swap on $HOSTNAME has been activated. Current memory usage: $MEMORY%."

      # Set the status of swap to "activated"
      SWAP_STATUS="activated"
    fi
  fi

  # Check if the memory usage is below the deactivation threshold
  if (( $(echo "$MEMORY < $DEACTIVATE_THRESHOLD" | bc -l) )); then
    # Check if swap is already deactivated
    if [[ "$SWAP_STATUS" == "deactivated" ]]; then
      echo "Swap already deactivated."
    else
      # Deactivate swap
      swapoff -a
      echo "Swap deactivated. Current memory usage: $MEMORY%."

      # Send a notification to Telegram
      curl -s -X POST "https://api.telegram.org/bot$BOT_TOKEN/sendMessage" \
        -d chat_id="$CHAT_ID" \
        -d text="Swap on $HOSTNAME has been deactivated. Current memory usage: $MEMORY%."

      # Set the status of swap to "deactivated"
      SWAP_STATUS="deactivated"
    fi
  fi

  # Wait for 10 seconds before checking the memory usage again
  sleep 10
done

DLL

You can use it automatically by entering it into crontab or other time automation applications on Linux. in my case, I use the default linux crontab with a configuration like this:

crontab -e
* * * * * /root/script.sh

this will check and execute ram usage and its actions every minute, so we don't need to monitor the server in realtime anymore. because when we have set the amount of swap according to our needs, your host should be safe from OOM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment