最简的办法端口判断法==>仅适合数据库本地使用
法1:if条件判断方法
代码如下 | 复制代码 |
[root@oldboy scripts]# cat check_db01.sh #!/bin/sh #created by oldboy #mail:oldboy521@gmail.com PortNum=`netstat -lnt|grep 3306|wc -l` if [ $PortNum -eq 1 ] then echo "mysqld is running." else echo "mysqld is stoped." fi |
例2模拟用户(包括web server)访问方法==>适合在远端服务器使用(本地也适合)
代码如下 | 复制代码 |
[root@oldboy scripts]# cat check_db04.sh #!/bin/sh #created by oldboy #mail:oldboy521@gmail.com mysql -S /data/3306/mysql.sock -e "select version();" >/dev/null 2>&1 if [ $? -eq 0 ] then echo "mysqld is running." else echo "mysqld is stoped." fi |
技巧:
1)使用web 连接账号模拟访问。
例3
代码如下 | 复制代码 |
#!/bin/sh MailList="xxx@test.com" DBDataDir="/usr/local/mysql/var" DBPort="3306" IP=`ifconfig | grep 'inet addr:' -m 1 | grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'`; ErrFile="$DBDataDir/$HOSTNAME.err" ErrLine=`cat $ErrFile | wc -l` GetDate=`date '+%Y-%m-%d %H:%M:%S'` if [ ! -e /tmp/dberrline$DBPort ] then echo "$ErrLine" >/tmp/dberrline$DBPort echo "Run it first!" exit fi OldLine=`cat /tmp/dberrline$DBPort` echo "$ErrLine" >/tmp/dberrline$DBPort if [ "$ErrLine" -gt "$OldLine" ] then TailLine=`expr $ErrLine - $OldLine` ErrStr=`tail -$TailLine "$ErrFile"` LeftStr=`echo "$ErrStr" | sed '/InnoDB: ERROR: the age of the last checkpoint is/d' | sed '/InnoDB: which exceeds the log group capacity/d' |sed '/InnoDB: If you are using big BLOB or TEXT rows, you must set the/d' | sed '/InnoDB: combined size of log files at least 10 times bigger than the/d' | sed '/InnoDB: largest such row./d'` if [ "$LeftStr" != "" ] then echo "$LeftStr" |mutt -s "Something wrong with $HOSTNAME-$IP($DBPort)!" ${MailList} echo "$LeftStr" fi fi |
上面的代码不能做什么其它及时提醒工具,如果需要我还可参考下面的办法
时间: 2024-07-31 16:59:34