20.16/20.17 shell中的函數php
函數就是把一段代碼整理到了一個小單元中,並給這個小單元起一個名字,當用到這段代碼時直接調用這個小單元的名字便可。python
格式: function f_name() {mysql
commandlinux
}sql
函數必需要放在最前面shell
示例腳本1:vim
[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 第一個,第二個參數數組
$# 腳本名稱bash
$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 的第一個、第2、第三個參數
[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 程序來實現,它能夠定義發郵件的服務器、發郵件人以及發件人密碼
輸出日誌:整個監控系統要有日誌輸出。
要求:咱們的機器角色多種多樣,可是全部機器上都要部署一樣的監控系統,也就說全部機器無論什麼角色,整個程序框架都是一致的,不一樣的地方在於根據不一樣的角色,定製不一樣的配置文件。
程序架構:
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 aming_test@163.com "$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 習題
一、編寫shell腳本,計算1-100的和;
二、編寫shell腳本,要求輸入一個數字,而後計算出從1到輸入數字的和,要求,若是輸入的數字小於1,則從新輸入,直到輸入正確的數字爲止;
三、編寫shell腳本,把/root/目錄下的全部目錄(只須要一級)拷貝到/tmp/目錄下;
四、編寫shell腳本,批量創建用戶user_00, user_01, ... user_100而且全部用戶同屬於users組;
五、編寫shell腳本,截取文件test.log中包含關鍵詞 ‘abc’ 的行中的第一列(假設分隔符爲 」:」 ),而後把截取的數字排序(假設第一列爲數字),而後打印出重複次數超過10次的列;
六、編寫shell腳本,判斷輸入的IP是否正確(IP的規則是,n1.n2.n3.n4,其中1<n1<255, 0<n2<255, 0<n3<255, 0<n4<255)。
答案: