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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章