20.20 Alarm System Main Script

Alarm system main script

 main.sh内容
 #!/bin/bash
#Written by aming.
# 是否发送邮件的开关
export send=1
# 过滤ip地址
export addr=`/sbin/ifconfig |grep -A1 "ens33: "|awk '/inet/ {print $2}'`
dir=`pwd`
# 只需要最后一级目录名
last_dir=`echo $dir|awk -F'/' '{print $NF}'`
# 下面的判断目的是,保证执行脚本的时候,我们在bin目录里,不然监控脚本、邮件和日志很有可能找不到
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
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

Alarm system main script

  • Define each directory of the monitoring system, and then define the main script. Because it is distributed, each machine needs to be defined. Create each script and each directory in advance, and then copy the script directly, and then go to make some changes
  • All shell scripts are placed in the /usr/local/sbin/ directory for easy search
  1. Switch to the /usr/local/sbin/ directory and create a subdirectory
[root@hf-01 ~]# cd /usr/local/sbin/
[root@hf-01 sbin]# mkdir mon
[root@hf-01 sbin]# cd mon
[root@hf-01 mon]# ls
[root@hf-01 mon]# mkdir bin conf shares log mail
[root@hf-01 mon]# ls
bin  conf  log  mail  shares
[root@hf-01 mon]# 
  1. Switch to the bin directory (the main script is placed in the bin directory, the main script is used as an entry, you should judge the configuration file, check whether a monitoring item needs to be monitored, and you need to call each sub-script that needs to be monitored)
[root@hf-01 mon]# cd bin
[root@hf-01 bin]# ls
[root@hf-01 bin]# vim main.sh

 #!/bin/bash
#Written by aming.
# 是否发送邮件的开关,
export send=1
#只要把send 改成了1 ,就会给下面所有的监控的项目都会发送邮件,export表示所有的变量会应用在所有的子脚本里(若是系统处于维护状态,就需要关闭所有的服务,这时候就需要先把告警关闭,否则会一直发邮件)
# 过滤ip地址;可以加定义一个hostname,这样可以知道是哪台机器
export addr=`/sbin/ifconfig |grep -A1 "eno6777736: "|awk '/inet/ {print $2}'`
dir=`pwd`
# 找一下当前脚本所在的目录
# 只需要最后一级目录名
last_dir=`echo $dir|awk -F'/' '{print $NF}'`
# 下面的判断目的是,保证执行脚本的时候,我们在bin目录里,不然监控脚本、邮件和日志很有可能找不到
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,到配置文件中遍历一遍,看看是否需要监控502
if grep -q 'to_mon_502=1' $conf_file; then
    export log=`grep 'logfile=' $conf_file |awk -F '=' '{print $2}' |sed 's/ //g'`
#找出log的路径
    /bin/bash  ../shares/502.sh
fi       

Guess you like

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