In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Preface
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 the specified threshold limit, it can send email alerts. It can monitor CPU utilization, memory utilization, swap utilization, disk space utilization, and so on.
If you have only a few systems and want to monitor them, writing a small shell script can make your task very easy.
In this tutorial, we added two shell scripts to monitor memory utilization on the Linux system. When the system reaches a given threshold, it will send an email to a specific e-mail address.
Method-1: use Linux Bash script to monitor memory utilization and send email
If you only want to get the current memory utilization percentage by email when the system reaches a given threshold, use the following script.
This is a very simple and straightforward one-line script. In most cases, I prefer this method.
When your system reaches 80% of memory utilization, it will trigger an email.
* / 5 * / usr/bin/free | awk'/ Mem/ {printf ("RAM Usage:% .2f%\ n"), $3 and awk'{print $3}'| awk'{if ($1 > 80) print $0;}'| mail-s "High Memory Alert" 2daygeek@gmail.com
Note: you need to change your email address instead of using our email address. In addition, you can change the memory utilization threshold according to your requirements.
Output: you will receive an email reminder similar to the following.
High Memory Alert: 80.40%
We added a lot of useful shell scripts in the past. If you want to view this content, navigate to the link below.
* how can I use shell scripts to automate daily activities?
Method-2: use Linux Bash script to monitor memory utilization and send email
If you want to get more information about memory utilization in the mail alert. Use the following script, which includes maximum memory utilization and process details based on top and ps commands.
This will let you know the operation of the system immediately.
When your system reaches "80%" of memory utilization, it will trigger an email.
Note: you need to change your email address instead of using our email address. In addition, you can change the memory utilization threshold according to your requirements.
# vi / optramusage if if binds if if $(free | awk'/ Mem/ {printf ("RAM Usage:% .2f\ n"), $3 ramusage 2pm 100}'| awk'{CPART $3}') Then SUBJECT= "ATTENTION: Memory Utilization is High on $(hostname) at $(date)" MESSAGE= "/ tmp/Mail.out" TO= "2daygeek@gmail.com" 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.outfi
Finally, add a cron task to automatically perform this operation. It will run every 5 minutes.
# crontab-eBay bin/bash 5 * / bin/bash / opt/scripts/memory-alert.sh
Note: because the script is scheduled to run every 5 minutes, you will receive an email reminder after up to 5 minutes (but not 5 minutes, depending on the time).
For example, if your system reaches the given limit of 8.25, you will receive an email alert within 5 minutes. I hope it's clear now.
Output: you will receive an email reminder 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.83Tasks: 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 stKiB Mem: 16248588 total, 5015964 free, 6453404 used 4779220 buff/cacheKiB Swap: 17873388 total, 16928620 free, 944768 used. 6423008 avail Mem PID USER PR NI VIRT RES SHR S% CPU% MEM TIME+ COMMAND17163 daygeek 20 2033204 487736 282888 S 10.0 3.0 8avail Mem PID USER PR NI VIRT RES SHR S 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 daygeek 02.84 / usr/bin/gnome-shell 1902 daygeek 20 1701644 327216 82536 S 20.02.0 153usr/lib/firefox/firefox / opt/google/chrome/chrome 2969 Daygeek 20 1051116 324656 92388 S 15.0 2.0 149 service-pipe-token=1339861+ daygeek 20 1104856 309552 278072 S 5.0 1.9143 background none 47.42 / usr/lib/Xorg vt2-displayfd 3-auth / run/user/1000/gdm/Xauthority-nolisten tcp-background none-noreset-keeptty-verbose 327246 daygeek 20 907344 265600 108276 S 30.0 1.6 10range 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-window13948 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 tab17163 8223 3.00.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-shell17937 8223 2.50.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 84998223 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 91988223 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: https://www.2daygeek.com/linux-bash-script-to-monitor-memory-utilization-usage-and-send-email/
Author: Magesh Maruthamuthu topic: lujun9972 translator: wxy proofread: wxy
Summary
The above is the whole content of this article. I hope the content of this article has a certain reference and learning value for everyone's study or work. Thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.