Alarm System
First, the alarm system main script
(Script storage Address:/usr/local/sbin/)
1. Create a script to store the directory: mkdir /usr/local/sbin/mon/
1.1 creating directories for each functional module---storing scripts
CD /usr/local/sbin/mon/
mkdir bin conf shares log
/bin/storing the main script
/conf/Storing the configuration file
/shares Storing individual monitoring scripts
/log/Storing logs
The main script is the gateway to the entire system, which The script is placed in the /usr/local/sbin/mon/bin/directory
2. Main script content:
main.sh Content
#!/bin/bash
#Written by Aming.
# whether to send mail switch, when the system maintenance, this switch is very useful to prevent the alarm when the alarm also send mail
Export send=1
# filter IP address, specifically which machine alarm
Export addr= '/sbin/ifconfig |grep-a1 ' ens33: ' |awk '/inet/{print $} '
Dir= ' pwd '
# requires only the last level of directory name
Last_dir= ' echo $dir |awk-f '/' {print $NF} '
# The purpose of this is to ensure that we are in the bin directory when the script is executed, otherwise the monitoring scripts, mails, and logs may not be found
if [$last _dir = = "Bin"] | | [$last _dir = = "bin/"]; Then
Conf_file= ". /conf/mon.conf "
Else
echo "You shoud cd bin dir"
Exit
Fi
exec 1>>. /log/mon.log 2>> /log/err.log
echo "' Date +"%F%T "' Load average '
/bin/bash. /shares/load.sh
#先检查配置文件中是否需要监控502, whether this monitoring 502 is defined in the configuration file script
If Grep-q ' to_mon_502=1 ' $conf _file; Then
Export log= ' grep ' logfile= ' $conf _file |awk-f ' = ' {print $} ' |sed ' s///g '
/bin/bash. /shares/502.sh
Fi
2.1: Where the script is Dir=pwd, is to get the path of the current file; Last_dir= ' echo $dir |awk-f '/' {print $NF} ' is the last level of directory under the current file path
2.2 conf_file= ". /conf/mon.conf "is the definition of the configuration file path
second, alarm system configuration file
Configuration file Contents: (actually very big content is each use)
The contents and values in the configuration file are based on your actual situation.
mon.conf Content
# to config-the options if to monitor
# # Defines the server address, port, and user, password for MySQL
To_mon_cdb=0 # #0 or 1, default 0,0 not monitor, 1 monitor
db_ip=10.20.3.13
db_port=3315
Db_user=username
db_pass=passwd
# # HTTPD If 1 is monitored, 0 not monitored
To_mon_httpd=0
# # PHP If it is 1 monitor, 0 not monitor
To_mon_php_socket=0
# # http_code_502 need to define the path to the access log
To_mon_502=1
Logfile=/data/log/xxx.xxx.com/access.log
# # Request_count defines the log path and domain name, which are based on your own monitoring content
To_mon_request_count=0
Req_log=/data/log/www.discuz.net/access.log
Domainname=www.discuz.net
The meaning of these to_mon_xxx=0/1 defined here is to determine whether or not to monitor in the master file.
third, alarm system monitoring Project
1.load.sh Content
#! /bin/bash
# #Writen by # #
load= ' uptime |awk-f ' average: ' {print $} ' |cut-d ', '-f1|sed ' s///g ' |cut-d.-f1 '
If [$load-gt] && [$send-eq "1"]
Then
echo "$addr ' date +%t ' load is $load" ... /log/load.tmp
/bin/bash. /mail/mail.sh [email protected] "$addr \_load: $load" ' Cat. /log/load.tmp '
# #发送邮件的主题内容
Fi
Fi
echo "' Date +%t ' load is $load" # #输出当前负载值
2. Monitoring Project 502.sh content
#! /bin/bash
# #获得一分钟前时间戳, in order to get 502 times in a minute
D= ' date-d '-1 min "+%h:%m"
C_502= ' grep: $d: $log |grep ' 502 ' |wc-l '
If [$c _502-gt] && [$send = = 1]; Then
echo "$addr $d 502 count is $c _502" ... /log/502.tmp
/bin/bash. /mail/mail.sh $addr \_502 $c _502. /log/502.tmp
Fi
echo "' Date +%t ' 502 $c _502"
3. The monitoring item obtains the disk usage condition:
disk.sh Content
#! /bin/bash
# #Writen by aming##
Rm-f. /log/disk.tmp
For R in ' Df-h |awk-f ' [%]+ ' {print $} ' |grep-v use '
Do
If [$r-gt] && [$send-eq "1"]//set to meet the conditions
Then
echo "$addr ' date +%t ' disk useage is $r" >> /log/disk.tmp
Fi
If [-F. /LOG/DISK.TMP]
Then
Df-h >>. /log/disk.tmp
/bin/bash. /mail/mail.sh $addr \_disk $r. /log/disk.tmp
echo "' Date +%t ' disk useage is Nook"
Else
echo "' Date +%t ' disk useage is OK"
Fi
which
Df-h |awk-f ' [%]+ ' {print $} ' |grep-v use to get Current disk usage (grep-v use is used for the English language (lang=en) in the current system )
Awk-f ' [%]+ ' here is a regular statement, with one or more spaces or% as delimiters, more precise separation, convenient to intercept the required fields (from the content can be found that there is a lot of space characters)
Shell (4) Alarm system-Main script, configuration file, monitoring project