mysql和nginx服務是否正常監控指令碼

來源:互聯網
上載者:User

標籤:mysql nginx監控

一.監控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.wget,curl

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是否啟動舉例:

[[email protected] ~]# 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伺服器是否正常

[[email protected] ~]# 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伺服器是否正常

[[email protected] ~]# 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服務是否正常監控指令碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.