Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Shell script to realize the memory of email Monitoring Linux system

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how to use Shell script to achieve mail monitoring Linux system memory", the content is simple and easy to understand, clear organization, I hope to help you solve doubts, let Xiaobian lead you to study and learn "how to use Shell script to achieve mail monitoring Linux system memory" this article bar.

Install a mail client Msmtp software under Linux (similar to a Foxmail tool)

1. Download and install: downloads.sourceforge.net... 206451&big_mirror=0

# tar jxvf msmtp-1.4.16.tar.bz2 # cd msmtp-1.4.16 # ./ configure --prefix=/usr/local/msmtp # make # make install

2. Create msmtp configuration file and log file (host is mail domain name, mail user name test, password 123456)

# vim ~/.msmtprc account default host 126.com from test@126.com auth login user test password 123456 logfile ~/.msmtp.log # chmod 600 ~/.msmtprc # touch ~/.msmtp.log

3, Mutt installation configuration: (general linux has default installation mutt)

# vim ~/.muttrc set sendmail="/usr/local/msmtp/bin/msmtp" set use_from=yes set realname="memory" set from=test@126.com set envelope_from=yes set rfc2047_parameters=yes set charset="utf-8"

4. Email sending test (-s email header, -a table plus attachments)

# echo "Message content 123456"| mutt -s "Message header test message" -a/scripts/test.txt test@126.com

Second, use the Free command to monitor system memory

1. Use the free command to view Linux system memory usage: (in M units)

# free -m total used free shared buffers cached Mem: 3952 3414 538 0 168 484 -/+ buffers/cache: 2760 1191 Swap: 8191 86 8105

2. Check the value command of intercepting the remaining memory free:

(1)Physical memory free value: # free -m| grep Mem |awk '{print $4}'(2) Free value of buffer: # free -m| grep - |awk '{print $4}'(3) Swap partition free value: # free -m| grep Swap | awk '{print $4}'

3. Write a script file for memory monitoring:

# vim /scripts/free-mem.sh #!/ bin/bash #Monitor Linux memory changes using the free command #Get the current system time (write file by appending>>) date >>/scripts/date-time. txt #Grab free value from physical memory (write file by appending>>) echo Mem-free: `free -m| grep Mem | awk '{print $4}'`M >> /scripts/mem-free.txt #free value of grab buffer (write file by appending>>) echo buffers/cache-free: `free -m| grep - | awk '{print $4}'`M >> /scripts/buffers-free.txt #Grab Swap partition free value (write file by appending>>) echo Swap-free: `free -m| grep Swap | awk '{print $4}'`M >> /scripts/swap-free.txt #Concatenate the above time and memory related line data line by line (rewrite file each time>) paste/scripts/date-time.txt/scripts/mem-free.txt/scripts/buffers-free.txt>/scripts/freemem. txt # chmod a+x /scripts/free-mem.sh # /scripts/free-mem.sh

4. View the result file of memory monitoring:

# cat /scripts/freemem.txt

5. Write free result file email sending script:

# vim /scripts/sendmail-mem.sh #!/ bin/bash #Send the generated freem.txt file to the user via email #Extract IP address information of this server IP=`ifconfig eth0| grep "inet addr" | cut -f 2 -d ":" | cut -f 1 -d " "` #extract current date and time today=`date -d "0 day" +%Y year %m month %d day` #Send memory monitoring results email echo "This is the memory monitoring report for $IP server $today, please download the attachment. " |mutt -s "$IP Server $today Memory Monitoring Report" -a/scripts/freemem.txt test@126.com # chmod a+x /scripts/sendmail-mem.sh

Third, monitor the situation of system swap partition, and send alarm mail when the use exceeds 80%.

# vim /scripts/swap-warning.sh #!/ bin/bash #Extract IP address information of this server IP=`ifconfig eth0| grep "inet addr" | cut -f 2 -d ":" | cut -f 1 -d " "` #Total number of swap partitions allocated by the system swap_total=`free -m| grep Swap | awk '{print $2}'` #Current remaining swap partition free size swap_free=`free -m| grep Swap | awk '{print $4}'` #currently used swap partition size swap_used=`free -m| grep Swap | awk '{print $3}'` if ((swap_used != 0)); then #If swap partitions are already used, calculate the percentage of the total number of swap partitions remaining free, expressed as a decimal, with an integer before the decimal 0 swap_per=0`echo "scale=2;$swap_free/$swap_total"| bc` #Set the alarm value of swap partition to 20%(i.e. alarm when usage exceeds 80%). swap_warn=0.20 #Compare the current percentage of swap partitions remaining with the alarm value (1 will be returned if it is greater than the alarm value (i.e. more than 20% remaining), and 0 will be returned if it is less than 20% remaining) swap_now=`expr $swap_per > $swap_warn` #If the current swap partition is more than 80% used (i.e., the remaining is less than 20%, the return value above is equal to 0), immediately send an email warning if (($swap_now == 0)); then echo "$IP server swap swap partition only $swap_free M is not used, the remaining is less than 20%, the utilization rate has exceeded 80%, please handle it in time. " |mutt -s "$IP Server Memory Alert"test@126.com fi fi # chmod a+x /scripts/swap-warning.sh

IV. Join the task plan: memory is detected once every ten minutes, and an email is sent immediately if there is an alarm (once every ten minutes). The email of memory detection results is sent once every morning at 8:00.

# crontab -e */10 * * * * /scripts/free-mem.sh */10 * * * * /scripts/swap-warning.sh 0 8 * * * /scripts/sendmail-mem.sh # service crond restart The above is "How to use Shell script to achieve mail monitoring Linux system memory" All the contents of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report