Linux system load and CPU, memory, hard disk, number of users monitoring script [marked]

Source: Internet
Author: User
Tags bz2 server memory

Reprint the original source of the article

In the absence of nagios monitoring, as long as the server can be on the Internet, you can send e-mail to remind administrators of the use of system resources.

First, write the Linux system warning mail script

# vim/scripts/sys-warning.sh

#!/bin/Bash#monitor the system load and CPU, memory, hard disk, log on the number of users, beyond the alert to send email alerts. #Extract IP address information for this server IP= ' Ifconfig eth0 | grep "inet addr" | Cut-f 2-d ":" | Cut-f 1-d ""`   # 1, monitor the change of the system load, send an email alarm when exceeding:#total number of cores to fetch CPU Cpu_num= ' grep-c ' model name '/proc/Cpuinfo '#captures the average load value of the current system for 15 minutes#load_15 = ' Uptime | awk ' {Print"(with ' {PrintIt's not accurate enough, if you use awk to take the 12th field, the result might be empty, and the $NF Table outputs the last paragraph of the content. Thanks to Hpsfpcca Netizen's correction) load_15= ' Uptime | awk ' {Print$NF} "#计算当前系统单个核心15分钟的平均负载值, the result is less than 1.0 o'clock in front of the single digit complement 0. Average_load=`Echo"Scale=2;a= $load _15/$cpu _num;if (Length (a) ==scale (a)) print 0;print a"| BC '#Single -digit integer average_int of the above average load value=`Echo$average _load | Cut-f 1-d "."`   #设置系统单个核心15分钟的平均负载的告警值为0.70 (i.e. alarm when using more than 70%). Load_warn=0.70 #当单个核心15分钟的平均负载值大于等于1.0 (that is, single-digit integers greater than 0), direct email alerts, if less than 1.0 compared to two timesif(($average _int > 0)); Then        Echo"$IP Server 15-minute system average load of $average_load, exceeding the alert value of 1.0, please immediately handle!!! " | Mutt-s "$IP Server system load critical alarm!!! "[Email protected]comElse#当前系统15分钟平均负载值与告警值进行比较 (when the alarm value is greater than 0.70 o'clock returns 1, less than 0) Load_now= ' Expr $average _load \> $Load_warn '#如果系统单个核心15分钟的平均负载值大于告警值0.70(return value is 1), send an email to the Administratorif(($load _now = = 1)); Then      Echo"$IP Server 15-minute system average load reached $average _load, exceeding the alert value of 0.70, please timely processing. " | Mutt-s "$IP Server system load Alarm" [email protected]com fi fi# 2, monitor the system CPU, when using more than 80% of the time to send alarm messages:#The current idle CPU percentage (only integer part) Cpu_idle= ' Top-b-N 1 | grep Cpu | awk ' {Print$ $} ' | Cut-f 1-d "."`   #set the idle CPU alarm value to 20%, if the current CPU uses more than 80% (that is, the remaining less than 20%), send an email alert immediatelyif(($cpu _idle < 20)); Then      Echo"$IP Server CPU remaining $cpu_idle%, the usage rate has exceeded 80%, please handle in time." " | Mutt-s "$IP Server CPU Alarm" [email protected]com fi# 3, monitoring system swap partition swap situation, when using more than 80% of the time to send alarm messages:#total number of swap partitions allocated by the system Swap_total= ' Free-m | grep Swap | awk ' {Print$} '#the current remaining swap partition free size Swap_free= ' Free-m | grep Swap | awk ' {Print$4} '#the swap partition used size that is currently in use swap_used= ' Free-m | grep Swap | awk ' {Print$} 'if(($swap _used! = 0)); Then#if the swap partition is already in use, calculate the percentage of the total of the current remaining swap partition free, in decimal notation, to complement the decimal point with an integer digit 0 swap_per=0 'Echo"scale=2, $swap _free/$swap _total"| BC '#设置交换分区的告警值为20% (i.e. alarm when using more than 80%). Swap_warn=0.20 #当前剩余交换分区百分比与告警值进行比较 (1 is returned when it is greater than the alarm value (i.e. 20% or more), less than (i.e., less than 20% remaining)will return 0) Swap_now= ' Expr $swap _per \> $Swap_warn '#if the current swap partition uses more than 80% (that is, the remaining less than 20%, the above return value equals 0), send an email alert immediatelyif(($swap _now = = 0)); Then      Echo"$IP server swap partition only left $swap _free M unused, the remaining less than 20%, the utilization rate has exceeded 80%, please timely processing. " | Mutt-s "$IP server memory Alarm" [email protected]com fi fi# 4, monitor the system hard disk root partition use situation, when use more than 80% time sends the warning message:#取当前根分区 (/dev/sda3) The percentage used (take only the integer portion) Disk_sda3= ' Df-h | Grep/dev/sda3 | awk ' {Print$ $} ' | Cut-f 1-d "%"`   #set an alarm value of 80% for free hard disk capacity and send an email alert immediately if the current drive is using more than 80%if(($disk _sda3 > 80)); Then        Echo"$IP server/root partition utilization has exceeded 80%, please handle it in time. " | Mutt-s "$IP Server hard drive Alarm" [email protected]com fi# 5, monitor the system user login situation, when the number of users more than 3 times to send an alarm message:#number of current user logins (only numeric part) users= ' Uptime | awk ' {Print$6} '#set the number of logged on users alarm value is 3, if the current number of users more than 3, immediately send an email alertif(($users >= 3)); Then        Echo"The number of $IP server users has reached $users, please deal with it in a timely manner. " | Mutt-s "$IP Server user Alarm" [email protected]com fi

# chmod a+x/scripts/sys-warning.sh

Second, to join the mission plan: every 10 minutes to detect, there is an alarm immediately send mail (10 minutes to send).

# CRONTAB-E

*/10 * * * *  

# Service Crond Restart

Third, to enable the server to send mail, you must open the SendMail service or install Linux under a mail client msmtp software (similar to a foxmail tool)

The installation process is described here, or here

The step shape is as follows:

1. Download and install:

# wget Http://downloads.sourceforge.net/msmtp/msmtp-1.4.30.tar.bz2?big_mirror=0

# tar JXVF msmtp-1.4.30.tar.bz2

# CD msmtp-1.4.30
#./configure--PREFIX=/USR/LOCAL/MSMTP
# make
# make Install

2, create MSMTP configuration file and log file (host for mail domain name, mail user name test, password 123456)
# Vim ~/.MSMTPRC

Account Default   126. com from  [email protected]126. com  auth login  user test  123456  

# chmod ~/.MSMTPRC

# Touch ~/.msmtp.log

3, Mutt installation configuration: You can use which mutt to view the path of mutt installation, if not installed can use the # Yum install Mutt
# VIM/ETC/MUTTRC

Set sendmail= "/usr/local/msmtp/bin/msmtp" set use_from=Yes set realname= "Memory"   set from [email protected]. com  set Envelope_from=Yes set rfc2047_parameters=Yes set charset 

4. Mail sending test (-S message header)

# echo "Mail content 123456" | Mutt-s "Mail title test mail" [email protected]

Linux system load and CPU, memory, hard disk, number of users monitoring script [marked]

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.