Use shell scripts to monitor linux system memory

Source: Internet
Author: User

These days I have been studying shell scripts and writing some memory monitoring script programs. Without nagios monitoring software, as long as the server can access the Internet, you can send an email to remind the administrator of the system memory usage.
 
1. Install a mail client msmtp software (similar to a foxmail tool) in linux)
 
1, download installation: http://downloads.sourceforge.net/msmtp/msmtp-1.4.16.tar.bz2? Modtime = 1217206451 & 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 the msmtp configuration file and log file (host is the mail domain name, mail username 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: (mutt is installed by default in linux)
# 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. Mail sending test (-s mail title,-a table with attachments)
# Echo "Mail content 123456" | mutt-s "mail Title Test mail"-a/scripts/test.txt test@126.com
 
 
2. Use the free command to monitor the system memory:
 
1. Run the free command to view memory usage in linux: (MB)
# Free-m
(The execution result is as follows)
Total used free shared buffers cached
Mem: 3952 3414 538 0 168 484
-/+ Buffers/cache: 2760 1191
Swap: 8191 86 8105
 
 
2. Run the following command to view the free value of the remaining memory:
(1) physical memory free value: # free-m | grep Mem | awk '{print $4 }'
(2) free value of the buffer: # free-m | grep-| awk '{print $4 }'
(3) free value of Swap partition: # free-m | grep Swap | awk '{print $4 }'
 
 
3. Compile a script file for memory monitoring:
# Vim/scripts/free-mem.sh
 
#! /Bin/bash # use the free command to monitor linux system memory changes # obtain the current system time (write files in append mode>) date>/scripts/date-time.txt # capture the physical memory free value (write the file in append mode>) echo Mem-free: 'free-m | grep Mem | awk' {print $4} ''M>/scripts/mem-free.txt # capture the free value of the buffer (write the file in append mode>) echo buffers/cache-free: 'free-m | grep-| awk' {print $4} ''M>/scripts/buffers-free.txt # capture the Swap partition free value (write the file in append mode>) echo Swap-free: 'free-m | grep Swap | awk '{print $4} ''M>/scripts/swap-free.txt # connect the above time and memory-related row data row by row (each rewrite file>) paste/scripts/date-time.txt/scripts/mem-free.txt/scripts/buffers-free.txt/scripts/swap-free.txt>/scripts/freemem.txt # chmod a + x/scripts/free-mem.sh
#/Scripts/free-mem.sh
 
4. view the memory Monitoring Result file:
# Cat/scripts/freemem.txt
 
5. Compile the mail sending script for the free result file:
# Vim/scripts/sendmail-mem.sh
 
#! /Bin/bash handler sends the generated freemem.txt file to the user by email # extract the IP address information of the server to IP address = 'ifconfig eth0 | grep "inet addr" | cut-f 2-d ": "| cut-f 1-d" "'# extract the current date and time today = 'date-d" 0 day "+ % Y % m month % dday' # Send memory monitoring result email echo "this is the memory monitoring report for $ IP server $ today, 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
 
 
 
3. Monitor the situation of swap partition swap. Send an alarm email when the usage exceeds 80%:
# Vim/scripts/swap-warning.sh
 
#! /Bin/bash # extract the IP address of the server IP address = '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}'' # remaining swap_free = 'free-m | grep Swap | awk '{print $4} ''# The used size of the currently used Swap partition swap_used = 'free-m | grep Swap | awk '{print $3} ''if (swap_used! = 0); then # If the swap partition has been used, calculate the percentage of the free total amount of the remaining swap partition, expressed in decimal places, add an integer 0 swap_per = 0 'echo "scale = 2 before the decimal point; $ swap_free/$ swap_total "| bc '# sets the swap partition's alarm value to 20% (that is, when the value exceeds 80% ). Swap_warn = 0.20 # Compare the percentage of the remaining swap partitions with the alarm value (if the value is greater than the alarm value (that is, the remaining value is more than 20%), 1 is returned, less than (that is, less than 20% remaining) will return 0) swap_now = 'expr $ swap_per \> $ swap_warn' # If the current swap partition is used more than 80% (that is, the remaining is less than 20%, and the above return value is equal to 0 ), send an email immediately if ($ swap_now = 0); then echo "$ IP server swap partition only $ swap_free M not used, remaining less than 20%, usage has exceeded 80%, please handle it in time. "| Mutt-s" $ IP server memory alert "test@126.com fi # chmod a + x/scripts/swap-warning.sh
 
 
4. Join the task plan: the memory is detected every ten minutes. If an alarm is triggered, an email is sent immediately (every ten minutes). The memory detection result email is sent at every morning.
 
# Crontab-e
 
*/10 */scripts/free-mem.sh */10 */scripts/swap-warning.sh 0 8 */scripts/sendmail-mem.sh # service crond restart
 
This article is from the "My O & M path" blog

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.