shell学习十四--编写监控mysql脚本

前端之家收集整理的这篇文章主要介绍了shell学习十四--编写监控mysql脚本前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
shell编写监控MysqL脚本

判断服务存活方法
1、端口
[root@node01 ~]# netstat -lntup|grep 330
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 127641/MysqLd

2、进程判断
[root@node01 ~]# ps -ef |grep MysqLd
root 127539 1 0 17:58 pts/2 00:00:00 /bin/sh /usr/bin/MysqLd_safe --datadir=/var/lib/MysqL --socket=/var/lib/MysqL/MysqL.sock --pid-file=/var/run/MysqLd/MysqLd.pid --basedir=/usr --user=MysqL
MysqL 127641 127539 0 17:58 pts/2 00:00:00 /usr/libexec/MysqLd --basedir=/usr --datadir=/var/lib/MysqL --user=MysqL --log-error=/var/log/MysqLd.log --pid-file=/var/run/MysqLd/MysqLd.pid --socket=/var/lib/MysqL/MysqL.sock
root 127670 127096 0 17:59 pts/2 00:00:00 grep MysqLd

3、登录,看返回值
[root@node01 ~]# MysqL -uroot -p1323 -e "select version();"
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@node01 ~]# echo $?
1
[root@node01 ~]# MysqL -uroot -p123 -e "select version();"
+-----------+
| version() |
+-----------+
| 5.1.73 |
+-----------+
[root@node01 ~]# echo $?
0
[root@node01 ~]#

脚本提示:不要用3306去判断,还要去用整数判断,一旦为空就报错了(比如没起来),可以取字符串(过程太长,过程太复杂)

最好的方式是用wc -l 转行为行数判断

===============================================================

例子1: [root@node01 day5]# vi MysqL_chk.sh #!/bin/bash port="`netstat -lntup |grep 3306|wc -l`" if [ $port -eq 1 ];then echo "MysqL start" else /etc/init.d/MysqLd start fi 例子2: [root@node01 day5]# vi MysqL_chk.sh #!/bin/bash port="`netstat -lntup |grep 3306|wc -l`" if [ $port -ne 1 ];then /etc/init.d/MysqLd start fi 例子3: #!/bin/bash port="`ps -ef |grep MysqL|grep -v grep|wc -l`" if [ $port -ne 2 ];then /etc/init.d/MysqLd start fi [root@node01 day5]# ps -ef |grep MysqL|grep -v grep|wc -l 2 [root@node01 day5]# sh -x MysqL_chk4.sh 脚本里不要出现名字 ++ wc -l ++ grep -v grep ++ grep MysqL ++ ps -ef + port=4 + '[' 4 -ne 2 ']' 这里有问题 + /etc/init.d/MysqLd start Starting MysqLd: [ OK ] 例子4: [root@node01 day5]# vi MysqL_chk1.sh #!/bin/bash SERVER="`netstat -lntup | grep 3306`" if [ -n "$SERVER" ];then echo "MysqLd is running" else /etc/init.d/MysqLd start fi 例子5: [root@node01 day5]# vi MysqL_chk2.sh #!/bin/bash processcount=`ps -ef|grep MysqL|grep -v grep|grep -v MysqL_chk2.sh|wc -l` 切记这里脚本带MysqL,一定要过滤或者脚本名字不带MysqL port=`netstat -lntup|grep 3306|wc -l` if [ $processcount -eq 2 ] && [ $port -eq 1 ] then echo "MysqL running" else /etc/init.d/MysqLd start fi [root@node01 day5]# sh MysqL_chk2.sh MysqL running [root@node01 day5]# pkill MysqLd [root@node01 day5]# pkill MysqLd [root@node01 day5]# pkill MysqLd [root@node01 day5]# sh MysqL_chk2.sh Starting MysqLd: [ OK ] [root@node01 day5]# sh MysqL_chk2.sh Starting MysqLd: [ OK ] [root@node01 day5]# sh MysqL_chk2.sh MysqL running [root@node01 day5]# sh MysqL_chk2.sh MysqL running [root@node01 day5]# 例子6: [root@node01 day5]# vi check_db_client.sh #!/bin/bash MysqL -uroot -p123 -e "select version();" &>/dev/null if [ $? -ne 0 ] then /etc/init.d/MysqLd start else echo "MysqL is running" fi [root@node01 day5]# sh check_db_client.sh MysqL is running [root@node01 day5]# pkill MysqLd [root@node01 day5]# sh check_db_client.sh Starting MysqLd: [ OK ] [root@node01 day5]# pkill MysqLd [root@node01 day5]# sh check_db_client.sh ^[[AStarting MysqLd: [ OK ] 利用MysqL启动原理来写监控脚本 [root@node01 day5]# vi db_chk.sh #!/bin/bash pidfile="/var/run/MysqLd/MysqLd.pid" datadir="/var/lib/MysqL" if [ ! -f $pidfile ];then /bin/sh /usr/bin/MysqLd_safe --datadir=$datadir --pid-file=$pidfile & touch /var/run/MysqLd else echo "MysqL is running" fi 原文链接:https://www.f2er.com/bash/390523.html

猜你在找的Bash相关文章