shell腳本編程學習之路-分支與循環結構

1.if語句

(1)if條件語句語法:單分支結構mysql

if [ 條件 ]

then

指令

fi

或

if [ 條件 ];then

指令

fi

if 單分支條件中文編程形象語法:web

若是 [ 你有房 ]

那麼

我就嫁給你

果如

前面的文件條件表達式[ -f 「$file1」 ]&& echo 1 就至關於下面的if語句sql

if [ -f 「$file1」 ];then

   echo 1

fi

(2)雙分支結構shell

語法:數據庫

if [ 條件 ]

then

   指令集1

else

   指令集2

fi

上面的就至關於文件條件表達式[ -f 「$file1」 ]&&echo 1||echo 0apache

if雙分支中文編程語法形象描述:編程

若是 [ 你有房 ]

那麼

   我就嫁給你

不然

   Goodbye

果如

(3)多分支結構bash

語法:服務器

if [ 條件1 ]

then

   指令1

elif [ 條件2 ]

then 

   指令2

else

   指令3

fi
------------------------多個elif--------------------------
if [ 條件1 ]

then
   指令1

elif [  條件2 ]

then

   指令2

elif [ 條件3 ]

then

   指令3
…………
else

   指令4

fi

多分支if語句中文編程語法形象描述:app

若是 [ 你有房 ]  <==有錢

   那麼

我就嫁給你

或者若是[ 你爸是李剛 ] <==有權

   那麼

我就嫁給你

或者若是[ 你很努力很吃苦 ]<==有潛力

   那麼

咱們能夠先處對象

   不然

不鳥你<==遭淘汰

果如

2.範例

監控系統內存並報警企業案例腳本開發實戰

問題:開發shell腳本判斷系統剩餘內存的大小,若是低於100M就郵件報警給管理員,而且加入系統定時任務每3分鐘執行一次。

解答:重視問題的解決過程,第一步、第二部、第三部

實戰操做:

(1)先把命令行條件取出來

[root@shellbiancheng ~]# free -m
 total   used   free sharedbuffers cached
Mem:   981123857  0 12 36
 -/+ buffers/cache:       75    905 
Swap: 1983  0   1983 
[root@shellbiancheng ~]# free -m|awk -F "[ ]+" 'NR==3{print $4}'
906

(2)編寫腳本,發送郵件。發送郵件經常使用的有mail或mutt;服務端有sendmail服務(Centos5),postfix服務(Centos6默認),本地常見的郵件服務有:

Centos5 默認使用sendmail郵件服務,開啓方式/etc/init.d/sendmail start

Centos6默認使用postfix郵件服務,開啓方式/etc/init.d/postfix start

這裏不使用本地的郵件服務而是使用本地的mail客戶端。以及第三方的郵件服務器商如:163(須要提早註冊用戶)利用這個郵件帳號來接收報警人發送的郵件。發送smtp端口25,接收pop3端口110

[root@linzhongniao ~]# tail -2 /etc/mail.rc 
set from=xxxxxxxx@163.com smtp=smtp.163.com
set smtp-auth-user=xxxxx@163.com smtp-auth-password=xxxxxxxx  smtp-auth=login
[root@linzhongniao ~]# cat free.sh   
#!/bin/bash
export PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/bin/passwd:/usr/bin/passwd:/root/bin"
cur_free="`free -m|awk -F "[ ]+" 'NR==3{print $4}'`"
chars="current memory is $cur_free."
mails="/bin/mail"
if [ $cur_free -le 800 ];then
echo "$chars"|${mails} -s "一級告警" xxxxxxxxx@163.com
fi

查看郵件室友發送成功在命令行用mailq命令

3.拓展:監控磁盤,NFS系統,MYSQL,WEB<--監控資源

(1)監控磁盤

先讀取命令行而後再判斷磁盤使用率是否低於設定的值,若是低於設定值發郵件報警。

[root@localhost ~]# df -h
filesystemSize  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
   18G  816M   16G   5% /
tmpfs 491M 0  491M   0% /dev/shm
/dev/sda1 477M   33M  419M   8% /boot
[root@localhost ~]# df -h|awk -F "[ ]+" 'NR==3 {print $3}'
16G

(2)監控mysql服務

能夠根據mysql服務的端口存在不存在判斷mysql是否啓動

注意:不要將端口值取出來,wc –l統計端口個數就完事兒了,將問題簡單化。

[root@localhost ~]# netstat -lnt|grep 3306
tcp0  0 0.0.0.0:33060.0.0.0:*   LISTEN 
[root@localhost ~]# netstat -lnt|grep 3306|wc -l
1

4.實戰:

用if雙分支實現對apache或mysql服務是否正常判斷,使用進程數、端口、URL的方式中的一種;若是進程沒啓動,就把進程啓動。

(1)使用端口判斷mysql(本地)

[root@localhost ~]# cat mysql.sh 
#!/bin/bash
a=$(netstat -lntup|grep mysql|wc -l)
echo $a
 if [ "$a" -eq "1" ];then
      echo "mysql is start"
    else
        echo "mysql is stop.starting "
        /etc/init.d/mysqld start        
 Fi

(2)使用端口判斷apache(本地)

[root@localhost ~]# cat apache.sh 
#!/bin/bash

apache=$(netstat -lntup|grep httpd|wc -l)
echo $apache

if [ "$apache" -eq "1" ];then
        echo "apache is starting..."
    else
        echo 「starting is not starting...」
        /usr/local/apache/bin/apachectl start
Fi

5.用if語句比較兩個數的大小

[root@localhost ~]# cat read3.sh 
#!/bin/bash
read -p "please input nun1 num2:" a b
if [ "$a" -eq "$b" ];then
    echo "$a 等於 $b"
elif [ "$a" -gt "$b" ];then
    echo "$a 大於 $b"
elif [ "$a" -lt "$b" ];then
    echo "$a 小於 $b" 
fi

6.監控web和mysql服務

監控web服務和mysql服務是否正常,不低於5中思路,監控思路Web服務和mysql服務都適用。

(1)端口

本地:netstat/ss/lsof/ps

遠程:telnet/nmap/nc 不在一臺機器上

(2)進程(本地)ps –ef|grep mysql|wc -l

(3)wget/curl(http方式,判斷數據返回值或者返回內容)

(4)header(http方式,根據狀態碼判斷)

(5)數據庫特有,經過mysql客戶端鏈接,根據返回值或者返回內容。

6.1 監控mysql服務

6.1.1 本地

(1)netstat –lnup|grep 3306|wc –l

shell腳本編程學習之路-分支與循環結構

注意這個端口必須是惟一的,不惟一系統上的mysql端口是多少就寫多少

(2)"netstat -lnt|grep 3306|awk -F "[ :]+" '{print $5}'``" = "3306" 取值比較是否等於3306

(3)netstat –lntup|grep mysqld|wc –l 計算mysql服務的數量

(4)[ps -ef|grep mysql|grep -v grep|wc -l -gt 0 ]

查看mysql進程若是是多實例的話就不要grep mysql了,直接過濾它惟一值的那個端口的名字。

(6)ss -lntup|grep 3306|wc –l

(7)lsof -i :3306|grep mysql|wc –l 沒有lsof命令能夠yum安裝

6.1.2 遠程查看服務端口開啓狀況

查看遠端端口通常不多使用telnet,推薦使用nmap查看遠端端口的open的狀態來肯定端口是否有開啓。若是沒nmap用yum安裝一下。端口開放服務不必定正常,端口不開服務必定不正常。因此當服務器數量比較多通常都會判斷端口。生產環境中用的比較多的是nmap。

(1) nmap 192.168.1.113 -p 3306 2&gt;/dev/null|grep open|wc –l

shell腳本編程學習之路-分支與循環結構

(2) echo -e "\n"|telnet 192.168.1.113 3306|grep Connected|wc -l

(3) nc -v -w 2 192.168.1.113 -z 3306 2&gt;/dev/null |grep succeeded|wc –l

咱們在執行nc -v -w 2 192.168.1.113 -z 3306這條命令是可能會出現這樣的報錯F?jHost '192.168.1.108' is not allowed to connect to this MySQL server。出現這個錯誤的緣由是不容許遠程訪問mysql,因此咱們要建立遠程登陸用戶並受權。

shell腳本編程學習之路-分支與循環結構

6.1.3 總結查看mysql服務是否開啓的方法

經過本地和遠端查看端口判斷服務的啓停,若是服務沒有啓動就啓動服務。一共七種方法,以下圖所示。

shell腳本編程學習之路-分支與循環結構

6.2 監控web服務

Mysql查看本地和遠程端口的方法,web服務也一樣適用。這裏就不詳細說明了,只說一下curl和wget兩種方法。查看web服務是否開啓的全部方法,以下圖所示:

shell腳本編程學習之路-分支與循環結構

下圖爲用curl監控web服務的五種方法

shell腳本編程學習之路-分支與循環結構

下面爲wget監控web服務的方法

[root@shellbiancheng ~]# cat check_web3.sh
#!/bin/sh
wget -T 10 -q --spider http://192.168.1.113 &>/dev/null
if [ $? -eq 0 ];then
echo "httpd is started"
else
echo "httpd is starting..... "
ssh -p 22 root@192.168.1.113 '/etc/init.d/httpd start'
fi

6.3 小結

本地:ss,netstat,lsof,ps

`netstat –lntup|grep mysqld|wc –l`

`ss -lntup|grep 3306|wc –l`

`lsof -i :3306|grep mysql|wc –l`

ps -ef|grep mysql|grep -v grep|wc -l -gt 0

遠程:telnet,nmap,nc,curl,wget

echo -e "\n"|telnet 192.168.1.113 3306|grep Connected|wc –l

nmap 192.168.1.113 -p 3306 2>/dev/null|grep open|wc –l

nc -v -w 2 192.168.1.113 -z 3306 2>/dev/null |grep succeeded|wc –l

header(http code)curl –I 監控web服務,web地址返回200就ok

curl -I -m 10 -o /dev/null -s -w "%{http_code}\n" www.baidu.com
相關文章
相關標籤/搜索