Python 監控supervisor cesi集羣 進程狀態

需求背景:

因服務商有時候會進行網絡維護,網絡間歇性異常,連接redis相關的任務中斷,supervisor不斷的重新拉起進程,當到達一定次數,就會停止拉起,任務無法繼續進行.

# -*- coding: utf-8 -*-

import requests
import json
import sys
import os
import smtplib
from email.mime.text import MIMEText
from email.header import Header
# python2 和 python3的兼容代碼
try:
    # python2 中
    import cookielib
    print(f"user cookielib in python2.")
except:
    # python3 中
    import http.cookiejar as cookielib
    print(f"user cookielib in python3.")

# session代表某一次連接
mafengwoSession = requests.session()
# 因爲原始的session.cookies 沒有save()方法,所以需要用到cookielib中的方法LWPCookieJar,這個類實例化的cookie對象,就可以直接調用save方法。
mafengwoSession.cookies = cookielib.LWPCookieJar(filename = "mafengwoCookies.txt")

userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36"
header = {
    #"Accept": "application/json, text/plain, */*",
    "Content-Type": "application/json;charset=UTF-8",
		"Origin": "http://cesi.sasha-lab.com",
		"Proxy-Authorization": "Basic MTYyMDM1ODYzMEAyNDE3NjpmMGI5ZGEyZTkwZGZiY2MxNzBhNGE2Mjg3ODJlOTAwMQ==",
		"Proxy-Connection": "keep-alive",
		"Referer": "http://cesi.sasha-lab.com/",
    "User-Agent": userAgent,
}


# 模仿 登錄
def mafengwoLogin(account, password):
    print(f"開始模擬登錄---賬號:{account},密碼:{password}")
    postUrl = "http://cesi.sasha-lab.com/api/v2/auth/login/"
    postData = {
        "username": account,
        "password": password,
    }

    # 使用session直接post請求
    responseRes = mafengwoSession.post(postUrl, data = json.dumps(postData), headers = header)
    # 無論是否登錄成功,狀態碼一般都是 statusCode = 200
    statusCode = responseRes.status_code
    if statusCode == 200:
    	print('登陸成功!')
    # 登錄成功之後,將cookie保存在本地文件中,好處是,以後再去獲取馬蜂窩首頁的時候,就不需要再走mafengwoLogin的流程了,因爲已經從文件中拿到cookie了
    mafengwoSession.cookies.save()

# 發送郵件
def send_email():
		# 第三方 SMTP 服務
		mail_host ='smtp.mxhichina.com' #設置服務器
		mail_user ='[email protected]'   #用戶名
		mail_pass ='12345adim'  #口令
		sender = '[email protected]'
		receivers = ['[email protected]']  # 接收郵件,可設置爲你的QQ郵箱或者其他郵箱
		content = open('test.txt', 'rb').read()
		# 三個參數:第一個爲文本內容,第二個 plain 設置文本格式,第三個 utf-8 設置編碼
		message = MIMEText(content, 'plain', 'utf-8')
		message['From'] = Header('[email protected]', 'utf-8')   # 發送者
		message['To'] =  Header('[email protected]', 'utf-8')        # 接收者
		subject = 'supervisor process monitor'
		message['Subject'] = Header(subject, 'utf-8')
		try:
		    smtpObj = smtplib.SMTP()
		    smtpObj.connect(mail_host, 25)    # 25 爲 SMTP 端口號
		    smtpObj.login(mail_user,mail_pass)
		    smtpObj.sendmail(sender, receivers, message.as_string())
		    print("郵件發送成功")
		except smtplib.SMTPException:
		    print("Error: 無法發送郵件")

if __name__ == "__main__":

    # 嘗試用帳號登錄
    mafengwoLogin("admin", "Cadmin283")
    resp = mafengwoSession.get("http://cesi.sasha-lab.com/api/v2/nodes/", headers = header, allow_redirects = False)
    print(f"resp.status = {resp.status_code}")
    jsonData = json.loads(resp.text)
    #sysout= open ('test.txt', 'wt')
    nodes = jsonData.get('nodes')
    for val in nodes:
    	processes = val.get('processes')
    	for pro in processes:
    		statename = pro.get('statename')
    		if statename == 'FATAL':
    			send_email()
    			print(f"{pro.get('group')} FATAL")

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章