当前位置:Gxlcms > 数据库问题 > mysql和nginx服务是否正常监控脚本

mysql和nginx服务是否正常监控脚本

时间:2021-07-01 10:21:17 帮助过:3人阅读

web服务器和mysql方法

1.监控端口(nginx也相同)

1.1本地监控

netstat -tunlp|grep 3306|wc -l

ss -tunlp|grep 3306|wc -l

 

1.2远程监控

nmap 10.117.33.98 -p 3306|grep open|wc -l

echo -e "\n"|telnet 10.117.33.98 3306 2>/dev/null |grep Connected|wc -l

 

2.监控进程(nginx同)

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

 

3.wgetcurl

wget --spider --timeout=10 --tries=2 www.baidu.com &>/dev/null

echo $?返回值0表示成功,否则失败

注:--spider 模拟爬取,--timeout 超时时间 --tries 测试两次

 

wget -T 10 -q --spider http://www.baidu.com &>/dev/null

echo $?返回值0表示成功,否则失败

注:-T 超时时间 -q 安静模式,此法与上面相同


curl -s -o /dev/null http://www.baidu.com

echo $?返回值0表示成功,否则失败

注:-s 安静模式 -o 重定向

 

二.监控脚本举例

1.监控mysql是否启动举例:

[root@fengxiaoli41 ~]# cat testmysql.sh

#!/sbin/bash

#if [ `netstat -tunlpa|grep mysqld|wc -l` -gt 0 ]本地监控

[ `rpm -qa nmap|wc -l` -lt 1 ] && yum install -y nmap &>/dev/null远程监控

if [ `nmap 10.117.33.130 -p 3306|grep open|wc -l` -gt 0 ]远程监控

then

echo "mysql is running!"

else

echo "mysql is stoped"

#/etc/init.d/mysqld start本地启动

fi

 

2.监控mysql连接是否正常

cat testmysql.php 编写php连接脚本

<?php

$link_id=mysql_connect(‘localhost‘,‘root‘,‘oldboy‘)or mysql_error();

if($link_id){

echo "mysql successful by oldboy!";

}

else{

echo mysql_error();

}

?>

php testmysql.php 执行该脚本,但要保证php已安装

mysql successful by oldboy! 通过grep过滤出关键字

 

3.通过端口监控nginx服务器是否正常

[root@fengxiaoli41 ~]# cat testnginx.sh

#!/sbin/bash

#if [ `netstat -tunlpa|grep nginx|wc -l` -gt 0 ]本地

[ `rpm -qa nmap|wc -l` -lt 1 ] && yum install -y nmap &>/dev/null远程

if [ `nmap 10.117.33.130 -p 80|grep open|wc -l` -gt 0 ]远程

then

echo "nginx is running!"

else

echo "nginx is stoped"

fi

 

4.通过curl监控nginx服务器是否正常

[root@fengxiaoli41 ~]# cat curlnginx.sh

#!/sbin/bash

if [ `curl -I http://10.117.33.130 2>/dev/null |head -1|egrep "200|302|301"|wc -l` -eq 1 ]

then

echo "nginx is running!"

else

echo "nginx is stoped!"

fi

 


本文出自 “feng” 博客,请务必保留此出处http://fengxiaoli.blog.51cto.com/12104465/1949514

mysql和nginx服务是否正常监控脚本

标签:mysql nginx监控

人气教程排行