Shell Scripts monitor Linux system memory usage (not using Nagios to monitor Linux) _linux shell

Source: Internet
Author: User
Tags chmod memory usage server memory

First, install the following Linux mail client msmtp software (similar to a foxmail tool)
1. Download and install:

Copy Code code as follows:

# 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 for the mail domain name, mail username test, password 123456)
Copy Code code as follows:

# 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 the default installation mutt)

Copy Code code as follows:

# 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, the mail sends the test (S-mail title, a table plus annex)

Copy Code code as follows:

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


Monitoring system memory using free command:

1, use the free command to view the Linux system memory usage: (in m)

Copy Code code as follows:

# free-m

(Execution results are the following style)
Copy Code code as follows:

Total used free Sharedbuffers Cached
mem:3952 34145380168484
-/+ buffers/cache:2760 1191
SWAP:8191 86 8105

2, view the Intercept the remaining memory Free value command:

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

3, write the Memory monitoring script file:

Copy Code code as follows:

# vim/scripts/free-mem.sh
#!/bin/bash
#使用free命令监控linux系统内存变化

#取系统当前时间 (write file as append >>)
Date >>/scripts/date-time.txt

#抓取物理内存free值 (write file as append >>)
echo mem-free: ' Free-m | grep Mem | awk ' {print $} ' M >>/scripts/mem-free.txt

#抓取缓冲区的free值 (write file as append >>)
echo buffers/cache-free: ' Free-m | grep-| awk ' {print $} ' m>>/scripts/buffers-free.txt

#抓取Swap分区free值 (write file as append >>)
echo swap-free: ' Free-m | grep Swap | awk ' {print $} ' m>>/scripts/swap-free.txt

#逐行连接上面的时间和内存相关行数据 (each time the file is rewritten >)
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 results file:
# Cat/scripts/freemem.txt
5. Write free result file mail Send script:
# vim/scripts/sendmail-mem.sh
#!/bin/bash
#把生成的freemem. txt file is sent to the user by mail

#提取本服务器的IP地址信息
ip= ' Ifconfig eth0 | grep "inet addr" | Cut-f 2-d ":" | Cut-f 1-d "" '

#提取当前日期时间
today= ' date-d ' 0 day ' +%y year%m month%d '

#发送内存监控结果邮件
echo "This is the memory monitoring report for the $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, the monitoring system swap partition Swap, when the use of more than 80% of the time to send a warning message:

Copy Code code as follows:

# vim/scripts/swap-warning.sh
#!/bin/bash

#提取本服务器的IP地址信息
ip= ' Ifconfig eth0 | grep "inet addr" | Cut-f 2-d ":" | Cut-f 1-d "" '

#系统分配的交换分区总量
Swap_total= ' Free-m | grep Swap | awk ' {print$2} '

#当前剩余的交换分区free大小
Swap_free= ' Free-m | grep Swap | awk ' {print$4} '

#当前已使用的交换分区used大小
Swap_used= ' Free-m | grep Swap | awk ' {print$3} '

if (($swap _used!= 0)); Then

#如果交换分区已被使用, calculates the percentage of the total amount of free for the current remaining swap partition, in decimal notation, to make an integer digit 0 before the decimal point
Swap_per=0 ' echo ' scale=2 $swap _free/$swap _total "| BC '

#设置交换分区的告警值为20% (that is, when using more than 80% alarm).
swap_warn=0.20

#当前剩余交换分区百分比与告警值进行比较 (1 is returned when it is greater than the alarm value (i.e., more than 20%), and 0 is returned when less than (i.e., less than 20% remaining)
swap_now= ' expr $swap _per \> $swap _warn '

#如果当前交换分区使用超过80% (that is, the remaining is less than 20%, the return value above equals 0), send an email alert immediately
if (($swap _now = 0)); Then
echo "$IP server swap partition is left $swap _free M unused, remaining less than 20%, the utilization rate has exceeded 80%, please deal with it in time. " | Mutt-s "$IP server memory Alert" test@126.com
Fi

Fi
# chmod a+x/scripts/swap-warning.sh


Four, join the task plan: Memory every 10 minutes of detection, alarm is immediately send mail (10 minutes), memory test results mail every morning 8 point send a

Copy Code code as follows:

# CRONTAB-E

*/10 * * * */scripts/free-mem.sh
*/10 * * * */scripts/swap-warning.sh
0 8 * * */scripts/sendmail-mem.sh

# Service Crond Restart

Related Article

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.