Pak Jiddat - Innovative Software Services

Read: In the name of thy Lord Who createth, Createth man from a clot. Read: And thy Lord is the Most Bounteous, Who teacheth by the pen, Teacheth man that which he knew not. Nay, but verily man is rebellious That he thinketh himself independent!. Lo! unto thy Lord is the return. (Sura Alalaq 96:8)

Monitoring RAM usage on server

Created On: 10 Feb, 2017: 11:42:29 - Tags : server management

Url: https://www.centos.org/docs/5/html/5.1/Deployment_Guide/s2-proc-meminfo.html

The RAM on a server can be monitored using /proc/meminfo and awk commands. /proc/ is a special file system created by the Linux kernel which contains kernel related information such as CPU and memory usage. /proc/meminfo contains information about memory usage. The following bash script can be used to extract memory usage information from /proc/meminfo. It uses the awk text processing command for parsing memory information. It uses bc command for performing floating point operations. The bash scripting language does not support arithmetic operations on floating point numbers so an external tool like bc is needed


# the total memory. the MemTotal row is extracted using awk. the second column of this row is then extracted and saved to total variable
total=$( awk '/MemTotal/ {print $2}' < /proc/meminfo )
# the available memory. the MemAvailable row is extracted using awk. the second column of this row is then extracted and saved to available variable
available=$( awk '/MemAvailable/ {print $2}' < /proc/meminfo )
# the formula for calculating percentage of available memory is piped to bc command. the output of bc command is saved to memory_available variable
memory_available=$(echo "($available*100/$total)" | bc)
# if the available memory is less than 30%
if [ "$memory_available" -lt "30" ];
then
    # the body of the alert email is saved to a temprary file
    echo "Hello Admin. The Memory usage on server is $memory_available%. Please check!" > /tmp/mail_body.txt
    # the mailx command is used to sent the email. it sends the email using gmail server.
    # this is useful on servers that have limited access to email servers such as Google Compute instance
    mailx -s "Memory usage on server is more than 70%. Please check!." -r "reply_email" -S smtp="smtp.gmail.com:587" -S    \
smtp-use-starttls -S smtp-auth=login -S smtp-auth-user="gmail_address" -S smtp-auth-password="gmail_password" -S \
ssl-verify=ignore recepiant_email < /tmp/mail_body.txt # the temprary file is removed rm -rf /tmp/mail_body fi

Related Articles

  1. Using Rsync over custom SSH port
  2. Upgrading Postgresql from version 9.4 to 9.6
  3. Installing latest version of Php
  4. Upgrading MySQL server from version 5.6 to 8.0
  5. Upgrading R from version 3.0 to 3.5
  6. Backing up and restoring LXD containers
  7. Resizing Linux root partitions without unmount on Google Cloud
  8. Running multiple versions of mailx command
  9. Getting /etc/rc.local file to work on Debian 9 (Stretch)
  10. Increasing number of concurrent connections for Proftp
  11. Changing ownership of symbolic link
  12. Configuring Apache to process html files as php
  13. Php server APIs
  14. Apache Multi Processing Modules (MPMs)
  15. Calculating the number of simultaneous connections supported by Apache
  16. Problem with copying large files to USB
  17. Finding CPU usage using top command
  18. Format of crontab file
  19. Force grub2 to not use UUID
  20. Automate scp command using sshpass