0618课的预习任务

20.16/20.17 shell中的函数

  • 函数就是把一段代码整理到了一个小单元中,并给这个小单元起一个名字,当用到这段代码时直接调用这个小单元的名字即可。

  • 格式: function f_name() {

                                 command

                                 }

函数必须要放在最前面

示例脚本1:

[root@arslinux-01 shell]# vim fun1.sh          //定义 f_name 最好不要和 shell 里面的关键词冲突
#!/bin/bash
inp(){
    echo $1 $2 $3 $0 $#
}
inp 1 a 2
[root@arslinux-01 shell]# sh fun1.sh
1 a 2 fun1.sh 3

$1 $2 第一个,第二个参数

$# 脚本名称

$0 参数数量

·将脚本更改为下面内容,测试结果

#!/bin/bash
inp(){
    echo "The first par is $1"
    echo "The second par is $2"
    echo "The third par is $3"
    echo "The script name is $0"
    echo "The quantity of par is $#"
}
inp 1 a 2 d kjf
[root@arslinux-01 shell]# sh fun1.sh
The first par is 1
The second par is a
The third par is 2
The script name is fun1.sh
The quantity of par is 5

参数可以写在脚本之外

[root@arslinux-01 shell]# vim fun1.sh
#!/bin/bash
inp(){
    echo "The first par is $1"
    echo "The second par is $2"
    echo "The third par is $3"
    echo "The script name is $0"
    echo "The quantity of par is $#"
}

inp $1 $2 $3 //这是指 inp 的第一个、第二、第三个参数

[root@arslinux-01 shell]# sh fun1.sh 1 3            //参数写在脚本外,名称后面,也行
The first par is 1
The second par is 3
The third par is
The script name is fun1.sh
The quantity of par is 2

示例脚本2:

[root@arslinux-01 shell]# vim fun2.sh
#!/bin/bash
sum (){
    s=$[$1+$2]
    echo $s
}
sum 1 10
[root@arslinux-01 shell]# sh -x fun2.sh
+ sum 1 10
+ s=11
+ echo 11
11

示例脚本3:

[root@arslinux-01 shell]# vim fun3.sh
#!/bin/bash
ip() {
    ifconfig |grep -A1 "$1: " |awk '/inet/ {print $2}'
}
read -p "Please input the eth name: " eth
ip $eth
[root@arslinux-01 shell]# sh fun3.sh
Please input the eth name: ens33
192.168.194.130
[root@arslinux-01 shell]# sh fun3.sh
Please input the eth name: ens37
192.168.100.1
[root@arslinux-01 shell]# sh fun3.sh
Please input the eth name: ens38
192.168.174.100

grep -A1 显示关键行及关键行的下一行

课后作业:

输入网卡名,判断是不是空,是不是系统中的网卡

思路:首先解决输入为空的问题,如果输入内容为空,就提示要输入内容并重新循环,

其次要是系统中存在的网卡,而网卡配置文件在/etc/sysconfig/network-scripts/下,并且都以ifcfg-为开头,那么久可以以此判断输入的网卡名为名称的"ifcfg-网卡名”文件是否存在,如果存在则允许下步操作,否则重新循环


20.18 shell中的数组

  • 定义数组 a=(1 2 3 4 5); echo ${a[@]} 数组不一定要是数字

  • echo ${b[*]} 等同于 ${b[@]}  显示整个数组

[root@arslinux-01 shell]# b=(1 2 3)
[root@arslinux-01 shell]# echo ${b[@]}            //可以用 @ 或者 *
1 2 3
[root@arslinux-01 shell]# echo ${b[*]}
1 2 3
  • echo ${b[2]} 读取第三个元素,数组从0开始

[root@arslinux-01 shell]# echo ${b[1]}
2
[root@arslinux-01 shell]# echo ${b[2]}
3
[root@arslinux-01 shell]# echo ${b[0]}
1
  • echo ${#b[@]} 获取数组的元素个数

[root@arslinux-01 shell]# echo ${#b[*]}
3
  • 数组赋值:

·b[1]=100; echo ${b[@]}

[root@arslinux-01 shell]# b[3]=a
[root@arslinux-01 shell]# echo ${b[*]}
1 2 3 a

可以更改已经赋值的数组为新的数值

[root@arslinux-01 shell]# b[3]=aaa
[root@arslinux-01 shell]# echo ${b[*]}
1 2 3 aaa

·a[5]=2; echo ${a[@]} 如果下标不存在则会自动添加一个元素

[root@arslinux-01 shell]# b[5]=ccc
[root@arslinux-01 shell]# echo ${b[*]}
1 2 3 aaa ccc
[root@arslinux-01 shell]# echo ${b[4]}
[root@arslinux-01 shell]#
  • 数组的删除:

uset a; unset a[1]

[root@arslinux-01 shell]# unset b[5]
[root@arslinux-01 shell]# echo ${b[*]}
1 2 3 aaa
[root@arslinux-01 shell]# echo ${b[4]}
[root@arslinux-01 shell]# unset b
[root@arslinux-01 shell]# echo ${b[*]}
  • 数组分片:

·a=(`seq 1 10`)

·echo ${a[@]:0:3} 从第一个元素开始,截取3个

[root@arslinux-01 shell]# c=(`seq 1 10`)
[root@arslinux-01 shell]# echo ${c[*]}
1 2 3 4 5 6 7 8 9 10

要截取4到7,就是第3个元素开始,截取4个元素

[root@arslinux-01 shell]# echo ${c[*]:3:4}
4 5 6 7

要截取倒数第3个元素开始,截取2个元素

[root@arslinux-01 shell]# echo ${c[*]:0-3:2}
8 9
  • 数组替换:

·echo ${a[@]/3/100} 替换的是值

·a=(${a[@]/3/100})

[root@arslinux-01 shell]# echo ${c[*]}
1 2 3 4 5 6 7 8 9 10

把8替换成6

[root@arslinux-01 shell]# echo ${c[*]/8/6}
1 2 3 4 5 6 7 6 9 10

直接替换赋值

[root@arslinux-01 shell]# a=(${c[*]/4/99})
[root@arslinux-01 shell]# echo ${a[*]}
1 2 3 99 5 6 7 8 9 10


20.19 告警系统需求分析

需求:使用 shell 定制各种个性化告警工具,但需要统一化管理、规范化管理。

思路:指定一个脚本包,包含主程序、子程序、配置文件、邮件引擎、输出日志等。

主程序:作为整个脚本的入口,是整个系统的命脉。

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

子程序:这个才是真正的监控脚本,用来监控各个指标。

邮件引擎:是由一个 python 程序来实现,它可以定义发邮件的服务器、发邮件人以及发件人密码

输出日志:整个监控系统要有日志输出。

要求:我们的机器角色多种多样,但是所有机器上都要部署同样的监控系统,也就说所有机器不管什么角色,整个程序框架都是一致的,不同的地方在于根据不同的角色,定制不同的配置文件。

程序架构:

clipboard.png

bin下是主程序

conf下是配置文件

shares下是各个监控脚本

mail下是邮件引擎

log下是日志


20.20 告警系统主脚本

[root@arslinux-01 ~]# cd /usr/local/sbin/
[root@arslinux-01 sbin]# mkdir mon
[root@arslinux-01 sbin]# cd mon/
[root@arslinux-01 mon]# mkdir bin conf shares mail log
[root@arslinux-01 mon]# cd bin/
[root@arslinux-01 bin]# vim main.sh
#!/bin/bash
# 是否发送邮件的开关
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

主脚本main.sh参数地址:http://note.youdao.com/noteshare?id=ef94586704b208ee6d7c7d1e5f04f644&sub=08E66A3760DF4961B29C74DED78E09F1


export send 是否发送邮件的开关,1为发送,如果是维护状态下,就要关闭告警

addr 本机ip(根据实际网卡名称更改ens33或者其他名称)

last_dir /bin/目录

conf_file ../conf/mon.conf 相对于/bin/的位置

grep -q 只要匹配就返回,用于 if 逻辑判断



20.21 告警系统配置文件

[root@arslinux-01 mon]# vim conf/mon.conf
## to config the options if to monitor
## 定义mysql的服务器地址、端口以及user、password
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   如果是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


20.22 告警系统监控项目

  • load.sh 内容(监控系统负载)

#! /bin/bash
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 内容

#! /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"
  • disk.sh 内容(磁盘使用率)

#! /bin/bash
rm -f ../log/disk.tmp
for r in `df -h |awk -F '[ %]+' '{print $5}'|grep -v Use`
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

[ %]+ 表示一个或多个空格或%

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

sadfsad                             //以:或#为分隔符,所以第三段为sadfsad

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

5                                         //以:或#为分隔符,有5段

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

6                                         //以:或#为分隔符,有6段,因为有个##

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

5                                        //以一个或多个:或#为分隔符,就只有5段



20.23/20.24/20.25 告警系统邮件引擎

·mail.sh内容

其中 mail.py 内容到这里 https://note.youdao.com/share/?id=dac98a142b86abba9b118e113969d4c4&type=note#/

mail.sh 为的是做告警手收敛

[root@arslinux-01 mon]# vim mail/mail.sh
log=$1
t_s=`date +%s`
t_s2=`date -d "2 hours ago" +%s`
if [ ! -f /tmp/$log ]
then
    echo $t_s2 > /tmp/$log
fi
t_s2=`tail -1 /tmp/$log|awk '{print $1}'`
    echo $t_s>>/tmp/$log
v=$[$t_s-$t_s2]
echo $v
if [ $v -gt 3600 ]
then
    ./mail.py  $1  $2  $3
    echo "0" > /tmp/$log.txt
else
    if [ ! -f /tmp/$log.txt ]
    then
        echo "0" > /tmp/$log.txt
    fi
    nu=`cat /tmp/$log.txt`
    nu2=$[$nu+1]
    echo $nu2>/tmp/$log.txt
    if [ $nu2 -gt 10 ]
    then
        ./mail.py  $1 "trouble continue 10 min $2" "$3"
        echo "0" > /tmp/$log.txt
    fi
fi

理解:

第一次告警,之前没有告警过,没执行过mail.sh

t_s为现在时间,t_s2为目前时间前2小时,时间差为7200秒

那么如果$log(在mon.conf中定义的logfile)不存在,则把2小时前的时间戳写入到$log中,也就是7200

t_s2为7200,把t_s追加到$log中

差值t_s和t_s2差值v等于7200,大于3600,所有发邮件告警mail.py,并且计数 0 到 $log.txt中

第一次告警结束

mail.sh一分钟执行一次,第二次执行时

t_s2=`tail -1 /tmp/$log|awk '{print $1}'`会把t_s2=`date -d "2 hours ago" +%s`的值覆盖掉,因此

t_s2为1分钟前的时间,因为$log为1分钟前,然后在追加现在时间到$log

差值v是60,小于3600,不告警,直接计数

那么执行else,判断$log.txt是否存在,第二次执行时不存在,所有计数0到$log.txt中,结束判断

因此nu=0,nu2=1,把nu2也就是1计数写入到$log.txt中

判断nu2是否大于10,那么显然不大于10,直接结束判断,结束脚本

第三次执行时,跟第二次类似,只不过计数会变化,nu=1,nu2=2

以此类推,在最后一次告警10分钟后,nu=10,nu2=11>10,那么符合if [ $nu2 -gt 10 ]的判断,执行mail.py告警,并把计数器重新清为0

如果之后没有问题,不会执行mail.sh,如果有问题,每一分钟执行一次mail.sh,十分钟后如果问题没有解决,再发邮件告警

这个告警收敛 10 分钟发一次邮件

如果10分钟内问题自行解决,服务重新上线,再过几分钟又有问题的话,需要1个小时之后才能告警


20.26 运行告警系统

将 main.sh 加入 crontab,一分钟执行一次

[root@arslinux-01 mon]# crontab -e

* * * * * cd /usr/local/sbin/mon/bin;bash main.sh

可以先将写入日志的过程注释掉,再执行

mail.sh 中的 mail.py 后面 $1,$2,$3 就是 mail.py 中的 to=sys.argv[1],subject=sys.argv[2],content=sys.argv[3]  三个参数,$1发送给谁,$2主题,$3内容

·如果load.sh有问题,那么它发告警,那么也是要带三个参数的,给谁发,主题,内容

(第三个参数也可以cat,把内容作为第三个参数)



shell 习题

1、编写shell脚本,计算1-100的和;

2、编写shell脚本,要求输入一个数字,然后计算出从1到输入数字的和,要求,如果输入的数字小于1,则重新输入,直到输入正确的数字为止;

3、编写shell脚本,把/root/目录下的所有目录(只需要一级)拷贝到/tmp/目录下;

4、编写shell脚本,批量建立用户user_00, user_01, ... user_100并且所有用户同属于users组;

5、编写shell脚本,截取文件test.log中包含关键词 ‘abc’ 的行中的第一列(假设分隔符为 ”:” ),然后把截取的数字排序(假设第一列为数字),然后打印出重复次数超过10次的列;

6、编写shell脚本,判断输入的IP是否正确(IP的规则是,n1.n2.n3.n4,其中1<n1<255, 0<n2<255, 0<n3<255, 0<n4<255)。


答案:

http://note.youdao.com/noteshare?id=6f6827ec73819d55fab8c0d45b1fd380&sub=4DA0DE3D8A904C09AF5D5FB94F2E7E52


猜你喜欢

转载自blog.51cto.com/11530642/2410749