借助mosquitto“實時”遠程監控服務器數據庫運行狀態


公司的項目還處於開發階段,我把整個后台服務臨時放在阿里雲上供前端測試,用的阿里雲的ECS雲服務器,HTTP請求服務器和數據庫服務都安裝在一台機子上(窮啊,湊合用),做測試用,配置相當低:單核1Gb。其實我對服務器多大配置能承受多大訪問壓力並沒有多大概念。前不久 使用Jmeter進行http接口性能測試 ,發現短時間內訪問量比較大時,總是會請求錯誤,根據返回的結果提示是數據庫錯誤,查看一下數據庫狀態,果真數據庫宕機了。
    
    
    
            
  1. service mysqld status


只要數據庫服務崩潰了,后面的請求就都會出錯,所以想用一種方法來監控服務器數據庫服務的狀態。自己想了幾種方案,大致分為兩類
1.當有請求到來時,如果發生數據庫連接錯誤,就(通過郵件或者短息)推送信息給管理員,管理員手動去重啟數據庫服務。
2.在系統中設置一個定時任務,每隔一段時間檢查一次數據庫服務狀態,如果服務停了就重啟並通知管理員。
這里介紹的是第二種方法,主要通過shell腳本實現,下面具體說明如何實現。
首先檢測Mysql的狀態
   
   
   
           
  1. #!/bin/bash
  2. pgrep -x mysqld &> /dev/null
  3. if [ $? -ne 0 ]
  4. then
  5. echo "At time: `date` :MySQL is stop .">> /var/log/mysql_messages
  6. service mysql start
  7. #echo "At time: `date` :MySQL server is stop."
  8. else
  9. echo "MySQL server is running ."
  10. fi

將上述腳本保存到mysql.sh中,上傳到服務器,運行該腳本可以發現輸出數據庫服務正在運行

    
    
    
            
  1. # sh mysql.sh
  2. MySQL server is running

這里我還出現一個小插曲,就是shell腳本總是運行錯誤,可以參考下面。原來是windows系統和類Unix系統的換行符不一樣。
腳本如果還執行不了的可能性是文件沒有執行權限

每隔一定時間自動運行腳本

linux上定期執行腳本用的是cron進程

命令:
  1.        
           
           
                   
    1. crontab -e
  1. 輸入(如果不能輸入,按鍵盤上的Insert鍵就能輸入了)
  2. */5 * * * * /your_dir/mysql.sh
*/5表示分鍾能被5整除,及每5分鍾執行一次,后面4個*號,分別表示 小時,日,月,星期。
編輯完畢,按ESC鍵退出,輸入:wq保存后退出。
重啟cron就可以了
  1.        
           
           
                   
    1. service cron restart
這樣就會每隔5分鍾,執行一次檢測mysql的腳本。
使用上面的shell腳本並不會推送數據庫狀態消息給管理員,這里就要借助我以前寫的一篇博客了。

Centos7-mqtt消息中間件mosquitto的安裝和配置

借助mosquitto服務可以將消息推送到管理員客戶端。

   
   
   
           
  1. #!/bin/bash
  2. pgrep -x mysqld &> /dev/null
  3. if [ $? -ne 0 ]
  4. then
  5. mosquitto_pub -t mysql_status -m "Failed"
  6. service mysql start
  7. #echo "At time: `date` :MySQL server is stop."
  8. else
  9. mosquitto_pub -t mysql_status -m "Running"
  10. fi
服務器發布主題為“mysql_status”的消息,再使用客戶端來接收該消息。
   
   
   
           
  1. import paho.mqtt.client as mqtt
  2. # The callback for when the client receives a CONNACK response from the server.
  3. def on_connect(client, userdata, flags, rc):
  4. print("Connected with result code "+str(rc))
  5. # Subscribing in on_connect() means that if we lose the connection and
  6. # reconnect then subscriptions will be renewed.
  7. client.subscribe("mysql_status")
  8. #訂閱,第一個參數是訂閱的主題
  9. # The callback for when a PUBLISH message is received from the server.
  10. def on_message(client, userdata, msg):
  11. print(msg.topic+" "+str(msg.payload))
  12. client = mqtt.Client()
  13. client.on_connect = on_connect
  14. client.on_message = on_message
  15. client.connect("主機名或ip", 1883, 60)
  16. #第一個參數為主機名,及Mosquitto所在服務器,第二個參數是端口
  17. # Blocking call that processes network traffic, dispatches callbacks and
  18. # handles reconnecting.
  19. # Other loop*() functions are available that give a threaded interface and a
  20. # manual interface.
  21. client.loop_forever()




參考:









免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM