With memory usage on Linux Bash Script Monitoring

There are many open source monitoring tools on the market that can be used to monitor the performance of Linux systems. When the system reaches a specified threshold limit, it can send e-mail alerts. It can monitor CPU utilization, memory utilization, swap all content utilization, disk space utilization.

If you only have a few systems and they want to monitor, then write a small shell script can make your task much easier.

In this tutorial, we added two shell scripts to monitor memory usage on Linux systems. When the system reaches a given threshold, it will send an email to a specific email address.

Method-1: The Linux Bash script to monitor memory usage and send e-mail

If you want to achieve in the system to obtain the current memory utilization percentages given threshold by mail, please use the following script.

This is a very straightforward one-line script. In most cases, I prefer to use this method.

When your system memory utilization of 80%, it would trigger an e-mail.

*/5 * * * * /usr/bin/free | awk '/Mem/{printf("RAM Usage: %.2f%\n"), $3/$2*100}' |  awk '{print $3}' | awk '{ if($1 > 80) print $0;}' | mail -s "High Memory Alert" [email protected]
复制代码

** Note: ** You need to change your email address instead of using our e-mail address. In addition, you can change the memory utilization thresholds according to your requirements.

Output **: ** You will receive an email alert similar to the following.

High Memory Alert: 80.40%
复制代码

We have added a number of useful shell scripts. If you want to see them, please navigate to the following link.

* How to automate daily activities using the shell script?

Method-2: The Linux Bash script to monitor memory usage and send e-mail

If you want to get more information about memory utilization in the mail alert. Use the following script, including those based on topcommand and psdetails maximum memory utilization and process orders.

This will immediately let you know the operation of the system.

When you reach the system memory utilization "80%", it will trigger an e-mail.

** Note: ** You need to change your email address instead of using our e-mail address. In addition, you can change the memory utilization thresholds according to your requirements.

# vi /opt/scripts/memory-alert.sh

#!/bin/sh
ramusage=$(free | awk '/Mem/{printf("RAM Usage: %.2f\n"), $3/$2*100}'| awk '{print $3}')

if [ "$ramusage" > 20 ]; then

  SUBJECT="ATTENTION: Memory Utilization is High on $(hostname) at $(date)"
  MESSAGE="/tmp/Mail.out"
  TO="[email protected]"
  echo "Memory Current Usage is: $ramusage%" >> $MESSAGE
  echo "" >> $MESSAGE
  echo "------------------------------------------------------------------" >> $MESSAGE
  echo "Top Memory Consuming Process Using top command" >> $MESSAGE
  echo "------------------------------------------------------------------" >> $MESSAGE
  echo "$(top -b -o +%MEM | head -n 20)" >> $MESSAGE
  echo "" >> $MESSAGE
  echo "------------------------------------------------------------------" >> $MESSAGE
  echo "Top Memory Consuming Process Using ps command" >> $MESSAGE
  echo "------------------------------------------------------------------" >> $MESSAGE
  echo "$(ps -eo pid,ppid,%mem,%Memory,cmd --sort=-%mem | head)" >> $MESSAGE
  mail -s "$SUBJECT" "$TO" < $MESSAGE
  rm /tmp/Mail.out
fi
复制代码

Finally, add a cron job to do this automatically. It will run every 5 minutes.

# crontab -e
*/5 * * * * /bin/bash /opt/scripts/memory-alert.sh
复制代码

** Note: ** As the script is scheduled to run every five minutes, so you will receive an email alert (but not five minutes, depending on time) at up to 5 minutes.

For example, if your system up to a given limit of 8.25, then you will receive an email alert within 5 minutes. Hopefully by now clear.

Output **: ** You will receive an email alert similar to the following.

Memory Current Usage is: 80.71%

+------------------------------------------------------------------+
Top Memory Consuming Process Using top command
+------------------------------------------------------------------+
top - 12:00:58 up 5 days,  9:03,  1 user,  load average: 1.82, 2.60, 2.83
Tasks: 314 total,   1 running, 313 sleeping,   0 stopped,   0 zombie
%Cpu0  :  8.3 us, 12.5 sy,  0.0 ni, 75.0 id,  0.0 wa,  0.0 hi,  4.2 si,  0.0 st
%Cpu1  : 13.6 us,  4.5 sy,  0.0 ni, 81.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu2  : 21.7 us, 21.7 sy,  0.0 ni, 56.5 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu3  : 13.6 us,  9.1 sy,  0.0 ni, 77.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu4  : 17.4 us,  8.7 sy,  0.0 ni, 73.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu5  : 20.8 us,  4.2 sy,  0.0 ni, 70.8 id,  0.0 wa,  0.0 hi,  4.2 si,  0.0 st
%Cpu6  :  9.1 us,  0.0 sy,  0.0 ni, 90.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu7  : 17.4 us,  4.3 sy,  0.0 ni, 78.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem : 16248588 total,  5015964 free,  6453404 used,  4779220 buff/cache
KiB Swap: 17873388 total, 16928620 free,   944768 used.  6423008 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
17163 daygeek   20     2033204 487736 282888 S  10.0   3.0   8:26.07 /usr/lib/firefox/firefox -contentproc -childID 15 -isForBrowser -prefsLen 9408 -prefMapSize 184979 -parentBuildID 20190521202118 -greomni /u+
 1121 daygeek   20     4191388 419180 100552 S   5.0   2.6 126:02.84 /usr/bin/gnome-shell
 1902 daygeek   20     1701644 327216  82536 S  20.0   2.0 153:27.92 /opt/google/chrome/chrome
 2969 daygeek   20     1051116 324656  92388 S  15.0   2.0 149:38.09 /opt/google/chrome/chrome --type=renderer --field-trial-handle=10346122902703263820,11905758137655502112,131072 --service-pipe-token=1339861+
 1068 daygeek   20     1104856 309552 278072 S   5.0   1.9 143:47.42 /usr/lib/Xorg vt2 -displayfd 3 -auth /run/user/1000/gdm/Xauthority -nolisten tcp -background none -noreset -keeptty -verbose 3
27246 daygeek   20      907344 265600 108276 S  30.0   1.6  10:42.80 /opt/google/chrome/chrome --type=renderer --field-trial-handle=10346122902703263820,11905758137655502112,131072 --service-pipe-token=8587368+

+------------------------------------------------------------------+
Top Memory Consuming Process Using ps command
+------------------------------------------------------------------+
  PID  PPID %MEM %CPU CMD
 8223     1  6.4  6.8 /usr/lib/firefox/firefox --new-window
13948  1121  6.3  1.2 /usr/bin/../lib/notepadqq/notepadqq-bin
 8671  8223  4.4  7.5 /usr/lib/firefox/firefox -contentproc -childID 5 -isForBrowser -prefsLen 6999 -prefMapSize 184979 -parentBuildID 20190521202118 -greomni /usr/lib/firefox/omni.ja -appomni /usr/lib/firefox/browser/omni.ja -appdir /usr/lib/firefox/browser 8223 true tab
17163  8223  3.0  0.6 /usr/lib/firefox/firefox -contentproc -childID 15 -isForBrowser -prefsLen 9408 -prefMapSize 184979 -parentBuildID 20190521202118 -greomni /usr/lib/firefox/omni.ja -appomni /usr/lib/firefox/browser/omni.ja -appdir /usr/lib/firefox/browser 8223 true tab
 1121  1078  2.5  1.6 /usr/bin/gnome-shell
17937  8223  2.5  0.8 /usr/lib/firefox/firefox -contentproc -childID 16 -isForBrowser -prefsLen 9410 -prefMapSize 184979 -parentBuildID 20190521202118 -greomni /usr/lib/firefox/omni.ja -appomni /usr/lib/firefox/browser/omni.ja -appdir /usr/lib/firefox/browser 8223 true tab
 8499  8223  2.2  0.6 /usr/lib/firefox/firefox -contentproc -childID 4 -isForBrowser -prefsLen 6635 -prefMapSize 184979 -parentBuildID 20190521202118 -greomni /usr/lib/firefox/omni.ja -appomni /usr/lib/firefox/browser/omni.ja -appdir /usr/lib/firefox/browser 8223 true tab
 8306  8223  2.2  0.8 /usr/lib/firefox/firefox -contentproc -childID 1 -isForBrowser -prefsLen 1 -prefMapSize 184979 -parentBuildID 20190521202118 -greomni /usr/lib/firefox/omni.ja -appomni /usr/lib/firefox/browser/omni.ja -appdir /usr/lib/firefox/browser 8223 true tab
 9198  8223  2.1  0.6 /usr/lib/firefox/firefox -contentproc -childID 7 -isForBrowser -prefsLen 8604 -prefMapSize 184979 -parentBuildID 20190521202118 -greomni /usr/lib/firefox/omni.ja -appomni /usr/lib/firefox/browser/omni.ja -appdir /usr/lib/firefox/browser 8223 true tab
复制代码

via: www.2daygeek.com/linux-bash-…

Author: Magesh Maruthamuthu topics: lujun9972 Translator: wxy proofread: wxy

This article from the LCTT original compiler, Linux China is proud

Guess you like

Origin juejin.im/post/5d0f9cb8e51d455d88219f21