python 檢測nginx服務郵件報警的指令碼

2022-09-29 06:36:09 字數 2969 閱讀 3855

$ cat checkserver.py

#!/usr/bin/python

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

import os

import socket

import smtplib

from email.mime.text import mimetext

from email.header import header

mail_host = "smtp.exmail.qq.com"

mail_user = "[email protected]"

mail_pass = "yne8dcsx"

sender = '[email protected]'

receivers = ['[email protected]']

def checkserverdown():

#三個引數:第乙個為文字內容,第二個 plain 設定文字格式,第三個 utf-8 設定編碼

message = mimetext('192.168.71.200 nginx is down','plain','utf-8')

message['from'] = header("nginx is down ", 'utf-8') # 傳送者

message['to'] = header("李鑫亮", 'utf-8')   # 接收者

subject = '192.168.71.200 nginx is down'

message['subject'] = header(subject,'utf-8')

try:

smtpobj = smtplib.smtp()

smtpobj.connect(mail_host,25)

smtpobj.login(mail_user,mail_pass)

smtpobj.sendmail(sender,receivers,message.as_string())

print("郵件傳送成功")

except smtplib.smtpexception:

print("error: 無法傳送郵件")

def checkserverstilldown():

#三個引數:第乙個為文字內容,第二個 plain 設定文字格式,第三個 utf-8 設定編碼

&nbs = mimetext('192.168.71.200 nginx is still down','plain','utf-8')

message['from'] = header("nginx is still down ", 'utf-8') # 傳送者

message['to'] = header("李鑫亮", 'utf-8')   # 接收者

subject = '192.168.71.200 nginx is still down'

message['subject'] = header(subject,'utf-8')

try:

smtpobj = smtplib.smtp()

smtpobj.connect(mail_host,25)

smtpobj.login(mail_user,mail_pass)

smtpobj.sendmail(sender,receivers,message.as_string())

print("郵件傳送成功")

except smtplib.smtpexception:

print("error: 無法傳送郵件")

def checkserverup():

#三個引數:第乙個為文字內容,第二個 plain 設定文字格式,第三個 utf-8 設定編碼

message = mimetext('192.168.71.200 nginx is  up','plain','utf-8')

message['from'] = he程式設計客棧ader("nginx is up ", 'utf-8') # 傳送者

message['to'] = header("李鑫亮", 'utf-8')   # 接收者

subject = '192.168.71.200 nginx is up'

messawlmliage['subject'] = header(subject,'utf-8')

try:

smtpobj = smtplib.smtp()

smtpobj.connect(mail_host,25)

smtpobj.login(mail_user,mail_pass)

smtpobj.sendmail(sender,receivers,message.as_string())

print("郵件傳送成功")

except smtplib.smtpexception:

wlmlia      print("error: 無法傳送郵件")  

# 判斷 nginx 程序輸出內容來確定是否要進行程序啟動

file = "/tmp/nginx.txt"

os.system("""ps -ef  |grep nginx  |grep -ev "grep|vim" > %s""" % file)

print (os.path.getsize(file))

if os.path.getsize(file) == 0:

checkserverdown()

os.system("/usr/sbin/nginx")

print (os.path.getsize(file))

os.system("""ps -ef  |grep nginx  |grep -ev "grep|vim" > %s""" % file)

if os.path.getsize(file) == 0:

&程式設計客棧nbsp;      checkserverstilldown()

os.system("/usr/sbin/nginx")

else:

checkserverup()

檢測Nginx服務是否正常執行(詳解)

需求 檢測nginx服務是否正常的執行,如果不在執行那麼就啟動nginx伺服器,那麼如果啟動nginx失敗,那麼通過郵件報警的方式通知我們 一 分析 1 監測nginx服務是否正在執行如下 root yankerp netstat anput grep nginx tcp 0 0 0.0.0.0 8...

NGINX心跳檢測

nginx心跳檢測 在30s內 fail timeout,預設值為10s nginx與中介軟體通訊失敗2次 max fails,預設值為1,設定為0則認為服務端一直可用 則認為中介軟體不可用 不可用中介軟體在30s內與nginx通訊成功2次,則認為中介軟體恢復。當upstream中的一台中介軟體響應...

nginx啟動及檢測nginx配置是否正確

檢查配置檔案 上面的結果說明nginx.conf 58行有問題 啟動nginx nginx的sbin目錄下 執行.nginx ps ef grep nginx後顯示 root 28775 1 0 18 24 00 00 00 nginx master process nginx nobody 287...