shell告警系统

shell告警系统

shell告警系统_第1张图片

1、配置main.sh

以后我们写的所有的脚本都放在一个目录下,方便我们以后去查找,这里我们的项目就放在/usr/local/sbin/目录下。

1.1 创建项目主目录
[root@localhost ~]# mkdir /usr/local/sbin/mon
在这里插入图片描述
1.2创建项目各个子目录(注意:在项目主目录里头创建)
[root@localhost mon]# mkdir bin conf shares mail log
在这里插入图片描述
主程序:作为整个脚本的入口,是整个系统的命脉。
先配置主程序:
shell告警系统_第2张图片

**

主程序:
[root@localhost bin]# vi main.sh
#!/bin/bash
#是否发送邮件的开关
export send=1
#过滤ip地址
export addr=`/sbin/ifconfig |grep -A1 "eno16777736: "|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

shell告警系统_第3张图片

**

2、配置mon.conf

配置文件:是一个控制中心,用它来开关各个子程序,指定各个相关联的日志文件。

这个文件就是我们项目各个程序的主开关,负责各个程序是否监控,这里面我们定义了很多参数,有些是我们用不到的,文件放在conf目录下。

配置配置文件:
[root@localhost bin]# cd …/conf/
**

[root@localhost conf]# vi mon.conf
##to config the options if to monitor
##定义mysql的服务器地址、

你可能感兴趣的:(shell告警系统)