shell script-5

1. The main script of the alarm system

1. Create each script directory

2, main script main.sh

#!/bin/bash
#Written by aming.
# Whether to send mail or not
export send=1
# Filter ip address
export addr=`/sbin/ifconfig |grep -A1 "ens33: "|awk '/inet/ {print $2 }'`
dir=`pwd`
main.sh content
# Only the last level directory name is
needed last_dir=`echo $dir|awk -F'/' '{print $NF}'`
# The purpose of the following judgment is to ensure the execution When scripting, we are in the bin directory, otherwise monitoring scripts, emails and logs are likely to not find
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 #First
check whether 502 needs to be monitored in the configuration file
if grep -q 'to_mon_502=1' $conf_file; then
export log=`grep 'logfile=' $conf_file |awk -F '=' '{print $2}' |sed 's/ //g'`
/bin/bash  ../shares/502.sh
fi

 

2. Alarm system configuration file

1、main.conf

## to config the options if to monitor
## Define mysql server address, port and user, password
to_mon_cdb=0? ?##0 or 1, default 0,0 not monitor, 1 monitor
db_ip=192.168.134.130
db_port=3306
db_user=username
db_pass=passwd
## httpd? ? If it is 1, it is monitored, if it is 0, it will not be monitored
to_mon_httpd=0
## php If it is 1, it will be monitored, and if it is 0, it will not be monitored
to_mon_php_socket=0
## http_code_502?? Need to define the access log Path
to_mon_502=1
logfile=/data/log/xxx.xxx.com/access.log
## request_count? ? Define log path and domain name
to_mon_request_count=0
req_log=/data/log/www.discuz.net/access.log
domainname =www.discuz.net

For the convenience of monitoring and the strong applicability of the script, the log file can be placed in the configuration file for easy modification.

3. Alarm system monitoring project

1、load.sh

#! /bin/bash
##Writen by aming##
load=`uptime |awk -F 'average:' '{print $2}'|cut -d',' -f1|sed 's/ //g' |cut -d. -f1`
if [ $load -gt 10 ] && [ $send -eq "1" ]
then
echo "$addr `date +%T` load is $load" >../log/load.tmp
/bin/bash ../mail/mail.sh *******@163.com "$addr\_load:$load" `cat ../log/load.tmp`
fi
echo "`date +%T` load is $load"

2、502.sh

[root@localhost shars]# vim 502.sh

#! /bin/bash
d=`date -d "-1 min" +%H:%M`
c_502=`grep :$d:  $log  |grep ' 502 '|wc -l`
if [ $c_502 -gt 10 ] && [ $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 、 disk.sh

#! /bin/bash
##Writen by aming##
rm -f ../log/disk.tmp
for r in `df -h |awk -F '[ %]+' '{print $5}'|sed '1'd`                    #抓取磁盘的利用率
do
    if [ $r -gt 90 ] && [ $send -eq "1" ]
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

[:#]+ Multiple delimiters are allowed in awk, [:#] is delimited by : and #, and [:#]+ : and # are multiple.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324738334&siteId=291194637