2018-4-23 17 weeks 4 lessons shell alarm system script configuration

20.20 Alarm System Main Script


Put the shell script in the /usr/local/sbin directory for easy search

[root@localhost ~]# cd /usr/local/sbin/
[root@localhost sbin]# mkdir mon/
[root@localhost sbin]# cd mon/
[root@localhost mon]# mkdir bin conf shares mail log
[root@localhost mon]# ls
bin  conf  log  mail  shares
[root@localhost mon]# cd bin/
[root@localhost bin]# vim main.sh

1.png

Main script main.sh parameter address: http://note.youdao.com/noteshare?id=ef94586704b208ee6d7c7d1e5f04f644&sub=08E66A3760DF4961B29C74DED78E09F1


export send The switch of whether to send mail, 1 is to send, if it is in maintenance state, the alarm should be turned off

addr local ip (change ens33 or other names according to the actual network card name)

last_dir /bin/directory

conf_file ../conf/mon.conf relative to /bin/

grep -q returns as long as it matches, used for if logical judgment






20.21 Alarm System Configuration File


my.conf

## to config the options if to monitor

## Define the server address, port, user, password of 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 it is 1, it will be monitored, if it is 0, it will not be monitored

to_mon_httpd=0

## php If it is 1, it will be monitored, if it is 0, it will not be monitored

to_mon_php_socket=0

## http_code_502 Need to define the path of the access log

to_mon_502=1

logfile=/data/log/xxx.xxx.com/access.log

## request_count defines the log path and domain name

to_mon_request_count=0

req_log=/data/log/www.discuz.net/access.log

domainname=www.discuz.net





20.22 Alarm system monitoring items


·load.sh content

#! /bin/bash

##Writen by alex##

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`

be

echo "`date +%T` load is $load"


·502.sh content

#! /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 ##502 count is included in the log

     /bin/bash ../mail/mail.sh $addr\_502 $c_502 ../log/502.tmp ##Send Mail

be

echo "`date +%T` 502 $c_502"


disk.sh content

#! /bin/bash

##Writen by alex##

rm -f ../log/disk.tmp

LANG = en

for r in `df -h |awk -F '[ %]+' '{print $5}'|grep -v Use` ##use one or more spaces or % as delimiters

do

    if [ $r -gt 90 ] && [ $send -eq "1" ]

then

    echo "$addr `date +%T` disk useage is $r" >>../log/disk.tmp

be

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"

be


[ %]+ means one or more spaces or %

[root@localhost shares]# echo "12:aaa#sadfsad:111#3333" |awk -F '[:#]' '{print $3}'

sadfsad ##With: or # as the separator, so the third segment is sadfsad

[root@localhost shares]# echo "12:aaa#sadfsad:111#3333" |awk -F '[:#]' '{print NF}'

5 ##With: or # as the delimiter, there are 5 paragraphs

[root@localhost shares]# echo "12:aaa#sadfsad:111##3333" |awk -F '[:#]' '{print NF}'

6 ##With: or # as the delimiter, there are 6 paragraphs, because there is a ##

[root@localhost shares]# echo "12:aaa#sadfsad:111##3333" |awk -F '[:#]+' '{print NF}'

5 ##With one or more : or # as the delimiter, there are only 5 paragraphs






Guess you like

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