(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
若是 [ 你有房 ] <==有钱 那么 我就嫁给你 或者若是[ 你爸是李刚 ] <==有权 那么 我就嫁给你 或者若是[ 你很努力很吃苦 ]<==有潜力 那么 咱们能够先处对象 不然 不鸟你<==遭淘汰 果如
监控系统内存并报警企业案例脚本开发实战
问题:开发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命令
(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
用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
[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
监控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客户端链接,根据返回值或者返回内容。
(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安装
查看远端端口通常不多使用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,因此咱们要建立远程登陆用户并受权。
经过本地和远端查看端口判断服务的启停,若是服务没有启动就启动服务。一共七种方法,以下图所示。
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 root@192.168.1.113 '/etc/init.d/httpd start' fi
本地: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