Shell Script Monitors Server status

Source: Internet
Author: User
Tags server website server memory

First, the installation of Mutt

1 yum-y Install postfix2 #需要安装sendmail并开启防火墙的25端口, if you need to receive mail 110 port also open 3 yum-y install Mutt

  Second, configuration information

About the configuration information, there is a point to explain, many online tutorials are said to edit/root/.muttrc to modify the configuration file, I want to say, I after the installation is complete, the/root directory does not have. muttrc This hidden file, you can copy it from somewhere else, or create a new file yourself. Here I am the copy.

You can find the muttrc this file by using the Find command, command the Find/-name muttrc, and then rename it to./root after the command cp/etc/muttrc/root/.muttrc copy to MUTTRC, then you can edit the configuration Piece of it.

1 #如果你收到的邮件乱码, set the following information 2 set charset= "Utf-8" 3 set Rfc2047_parameters=yes4 #如果你想自定义发件人信息, you need to set the following settings 5 set ENVELOPE_FROM=YES6 Set USE_FROM=YES7 Set [email protected]8 set realname= "Itdhz"

After you finish installing the Mail program

vi/etc/script/sys-warning.sh

#!/bin/bash# Unified Write down the mailbox receiving the monitoring mail mail_account= "[email protected]" #监控系统负载与CPU, memory, hard disk, number of logged on users, beyond the alert to send an email alarm. #提取本服务器的IP地址信息IP = '/sbin/ifconfig eth0 | grep "inet addr" | Cut-f 2-d ":" | Cut-f 1-d "" # 1, monitoring system load changes, out-of-the-box email alarm: #抓取cpu的总核数cpu_num = ' grep-c ' model name '/proc/cpuinfo ' #抓取当前系统15分钟的平均负载值load_15 = ' Uptime | awk ' {print $1.0} ' #计算当前系统单个核心15分钟的平均负载值, with a result of less than 0 before the single digit in the previous number. Average_load= ' echo ' scale=2;a= $load _15/$cpu _num;if (Length (a) ==scale (a)) print 0;print a "| BC ' #取上面平均负载值的个位整数average_int = ' echo $average _load | Cut-f 1-d "." ' #设置系统单个核心15分钟的平均负载的告警值为0.70 (i.e. alarm when using more than 70%). load_warn=0.70# when the average load value of a single core 15 minutes is greater than or equal to 1.0 (that is, the digit number is greater than 0), the message alarm is sent directly, if less than 1.0 is two comparisons if (($average _int > 0)); Thenecho "$IP Server 15 minutes of the system average load of $average_load, exceeding the alert value of 1.0, please immediately handle!!! " | Mutt-s "$IP Server system load critical alarm!!! "$mail _accountelse# current system 15-minute average load value compared to the alarm value (when the alarm value is greater than 0.70 will return 1, less than 0) load_now= ' expr $average _load \> $load _warn ' # If the average load value for a single core 15 minutes is greater than the alarm value of 0.70 (the return value is 1), send an email to the Administrator # if (($load _now = = 1)); Then#echo "$IP Server 15 minutes of the average system load reached $average _load, exceeding the alert value of 0.70, please timely processing. " | MutT-S "$IP Server system load Alarm" $mail _account#fifi# 2, monitor the CPU of the system, when using more than 80% alarm message: #取当前空闲cpu百份比值 (take only the integer part) cpu_idle= ' Top-b-N 1 | grep Cpu | awk ' {print $} ' | Cut-f 1-d "." ' #设置空闲cpu的告警值为20%, if the current CPU is using more than 80% (i.e. the remainder is less than 20%), immediately send an email alert if (($cpu _idle < 20)); Thenecho "$IP Server CPU remaining $cpu_idle%, the utilization rate has exceeded 80%, please timely processing. " | Mutt-s "$IP Server CPU Alarm" $mail _accountfi# 3, monitoring System swap partition swap situation, when the use of more than 80% when the alarm message: #系统分配的交换分区总量swap_total = ' Free-m | grep Swap | awk ' {print $} ' #当前剩余的交换分区free大小swap_free = ' free-m | grep Swap | awk ' {print $4} ' #当前已使用的交换分区used大小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 fill an integer digit before the decimal point 0swap_per=0 ' echo ' scale=2; $swap _free/$swap _total "| BC ' #设置交换分区的告警值为20% (i.e. alarm when using more than 80%). swap_warn=0.20# the current remaining swap partition percentage is compared to the alarm value (1 is returned when it is greater than the alarm value (i.e., more than 20% remaining), and 0 is returned when it is less than (i.e., less than 20%) swap_now= ' expr $swap _per \> $swap _ Warn ' #如果当前交换分区使用超过80% (i.e. the remainder is less than 20%, the above return value equals 0), immediately send an email alarm if (($swap _now = = 0)); Thenecho "$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 "$mail _accountfifi# 4, monitor the system hard disk partition use situation, when use more than 80% when the alarm message: For i in ' df-h | Grep-v Filesystem | awk ' {print $} ' | Cut-f 1-d '% '; Doif ($i >=); Thenecho "$IP server hard disk usage has exceeded 80%, please handle it in time. " | Mutt-s "$IP Server hard disk Alarm" $mail _accountfidone# the percentage of the current root partition (/DEV/SDA3) used (take only the integer portion) #disk_sda2 = ' Df-h | Grep/dev/sda2 | awk ' {print $} ' | Cut-f 1-d "%" ' #设置空闲硬盘容量的告警值为80%, if the current hard disk uses more than 80%, immediately send an email alarm # if (($disk _sda2 > 80)); Then#echo "$IP server/root partition utilization has exceeded 80%, please handle it in time. " | Mutt-s "$IP Server hard Drive Alarm" $mail _account#fi######## #usr分区监控 ########### #disk_sda5 = ' Df-h | Grep/dev/sda5 | awk ' {print $} ' | Cut-f 1-d "%" ' #设置空闲硬盘容量的告警值为80%, if the current hard disk uses more than 80%, immediately send an email alarm # if (($disk _sda5 > 80)); Then#echo "$IP server/USR utilization has exceeded 80%, please handle it in time. " | Mutt-s "$IP Server hard Drive Alarm" $mail _account#fi######## #home分区监控 ########### #disk_sda8 = ' Df-h | Grep/dev/sda8 | awk ' {print $} ' | Cut-f 1-d "%" ' #设置空闲硬盘容量的告警值为80%, if the current hard disk uses more than 80%, immediately send an email alarm # if (($disk _sda8 > 80)); Then#echo "$IP Server/home usage has exceeded 80%, please handle it in time. " | Mutt-s "$IP Server hard Drive Warning" $mail _account#fi############################################################################################################## ################ #5, monitoring System User login situation, when the number of users more than 3 when the alarm message: #取当前用户登录数 (only take the numeric part) users= ' uptime | awk ' {print $6} ' #设置登录用户数的告警值为3个 if the current number of users exceeds 3, immediately send an email alert if (($users >= 3)); Thenecho "The number of $IP server users has reached $users, please deal with it in a timely manner. " | Mutt-s "$IP Server users Alarm" $mail _accountfi#6, monitoring network Connections, the number of connections is greater than 2000 alarm message conn_num= ' Netstat-nat | Grep-i "80" | Wc-l ' #设置网络连接数告警值为2000, if the number of connections exceeds 2000, immediately send an email alarm if (($conn _num >= 2000)); Thenecho "The number of network connections to the $IP server has reached $conn_num, please deal with it in a timely manner. " | Mutt-s "$IP Server concurrent Connections" $mail _accountfi#7, Monitoring Server Account table, encrypted with md5sum method, stored in a file, two contrast, if the mismatch issued alarm default_md5= ' CAT/SCRIPT/MD5 ' check_md5= ' md5sum/etc/passwd | awk ' {print $ ' #用户表发生变化, understand alarm if ["$Check _md5" x! = "$default _md5" x]; Thenecho "$IP Server user table is changed, please handle it in time. " | Mutt-s "$IP Server User table alarm" $mail _accountfi #8, Monitoring Server password table, encrypted with md5sum method, stored in a file, two contrast, if the mismatch issued alarm default_passwd5= ' cat/script/ Passwdmd5 ' check_passwd5= ' Md5sum/etc/shadow | awk ' {print $ ' #用户表发生变化, understanding alarm If ["$Check _passwd5 "X! =" $default _passwd5 "x"; Thenecho "$IP Server password changes, please timely processing. " | Mutt-s "$IP Server User table alarm" $mail _accountfi#9, monitoring web site files to see if there is an exception file, if found immediately reported abno_num= ' php/root/chk.php scan/home/wwwroot/| Grep-v "XX" | Grep-v "chk.php" | Wc-l ' #判断个数, because there is no exception after execution will output a blank line, so to determine a few, and then determine whether the exception file if (($abno _num > 1)); thenphp/root/chk.php scan/home/wwwroot/| Grep-v "XX" | Grep-v "chk.php" >/script/abno_fileecho "$IP Server Web site found exception file $abno_num, please deal with it in a timely manner.  " | Mutt-s "$IP Server website exception file Warning" $mail _accountfi

  

Shell Script Monitors Server status

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.