Seventeen four classes

Seventeen four classes

20.20 Alarm system main script
20.21 Alarm system configuration file
20.22 Alarm system monitoring items

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       

20.21 Alarm System Configuration File

Alarm system configuration file

  • Shell project - alarm system mon.conf content
  • Configuration file (must be placed in the conf directory, the name must be mon.conf - PS: cannot be changed at will, because it has been set in the main script)
    • Define some switches, define some corresponding log paths, or monitor the username and password of mysql, and the IP address port port, etc.
[root@hf-01 bin]# pwd
/usr/local/sbin/mon/bin
[root@hf-01 bin]# cd ..
[root@hf-01 mon]# vim conf/mon.conf

 ## to config the options if to monitor
## 定义mysql的服务器地址、端口以及user、password
to_mon_cdb=0   
 ##cdb等于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   如果是1则监控,为0不监控
to_mon_httpd=0
## php 如果是1则监控,为0不监控
to_mon_php_socket=0
## http_code_502  需要定义访问日志的路径
to_mon_502=1
logfile=/data/log/xxx.xxx.com/access.log
## request_count   定义日志路径以及域名
to_mon_request_count=0
req_log=/data/log/www.discuz.net/access.log
domainname=www.discuz.net

  • For the purpose of extracting the request log, you must consider that if you want to standardize and standardize the writing of the shell, you must consider that there must be more than one machine to monitor; if you want to make the script universal and highly compatible, you need to put The logs of all the services that need to be monitored are loaded into the configuration file, which is convenient to change, and it is too troublesome to modify the corresponding scripts one by one in the later stage.

20.22 Alarm system monitoring items

Alarm system monitoring project

  • Defining subscripts is the monitoring project

The first load.sh is necessary, because any machine has been defined in the main script to monitor the system load

[root@hf-01 mon]# cd shares/
[root@hf-01 shares]# pwd
/usr/local/sbin/mon/shares
[root@hf-01 shares]# vim 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 [email protected] "$addr\_load:$load" `cat ../log/load.tmp`
fi
echo "`date +%T` load is $load"

502.sh content (502 definitely needs a log), 502 involves an access log, and the access log is designed to a time

  • Because the script monitoring main script is executed once a minute, so 502 monitoring, you must see the time and information of the access log 1 minute ago
[root@hf-01 shares]# 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
#mail就是定义发送给谁,发送的主题,发送的内容
fi
echo "`date +%T` 502 $c_502"

disk.sh content (disk usage)

  • disk, the idea is to look at the partitions one by one
[root@hf-01 shares]# vim disk.sh

#! /bin/bash
##Writen by aming##
rm -f ../log/disk.tmp
for r in `df -h |awk -F '[ %]+' '{print $5}'|grep -v Use`  
#[ %]+  以 多个,空格 或者 %   作为分隔符,+号表示一个或多个;因为系统默认是英文,所以grep -v Use 过滤掉的就是 已用
do
    if [ $r -gt 90 ] && [ $send -eq "1" ]
then
    echo "$addr `date +%T` disk useage is $r" >>../log/disk.tmp
fi
done

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

  • awk specify multiple delimiters
[root@hf-01 shares]# echo "12:aa#123bb:22#ww" |awk -F '[:#]' '{print $3}'
123bb

[root@hf-01 shares]# echo "12:aa#123bb:22#ww" |awk -F '[:#]' '{print NF}'
5
[root@hf-01 shares]# echo "12:aa#123bb:22##ww" |awk -F '[:#]' '{print NF}'
6
[root@hf-01 shares]# echo "12:aa#123bb:22##ww" |awk -F '[:#]+' '{print NF}'
5
[root@hf-01 shares]# 

Friendly link: A Ming linux

Guess you like

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