1.if语句

(1)if条件语句语法:单分支结构

if [ 条件 ]

then

指令

fi

或

if [ 条件 ];then

指令

fi

if 单分支条件中文编程形象语法:

如果 [ 你有房 ]

那么

我就嫁给你

果如

前面的文件条件表达式[ -f “$file1” ]&& echo 1 就相当于下面的if语句

if [ -f “$file1” ];then

   echo 1

fi

(2)双分支结构

语法:

if [ 条件 ]

then

   指令集1

else

   指令集2

fi

上面的就相当于文件条件表达式[ -f “$file1” ]&&echo 1||echo 0

if双分支中文编程语法形象描述:

如果 [ 你有房 ]

那么

   我就嫁给你

否则

   Goodbye

果如

(3)多分支结构

语法:

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语句中文编程语法形象描述:

如果 [ 你有房 ]  <==有钱

   那么

我就嫁给你

或者如果[ 你爸是李刚 ] <==有权

   那么

我就嫁给你

或者如果[ 你很努力很吃苦 ]<==有潜力

   那么

我们可以先处对象

   否则

不鸟你<==遭淘汰

果如

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 [email protected] smtp=smtp.163.com
set [email protected] 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 "一级告警" [email protected]
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

注意这个端口必须是唯一的,不唯一系统上的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>/dev/null|grep open|wc –l

(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>/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,所以我们要创建远程登录用户并授权。

6.1.3 总结查看mysql服务是否开启的方法

通过本地和远端查看端口判断服务的启停,如果服务没有启动就启动服务。一共七种方法,如下图所示。

6.2 监控web服务

Mysql查看本地和远程端口的方法,web服务也同样适用。这里就不详细说明了,只说一下curl和wget两种方法。查看web服务是否开启的所有方法,如下图所示:

下图为用curl监控web服务的五种方法

下面为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 [email protected] '/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