网站首页 > 技术文章 正文
Python脚本监控管理nginx
在Linux环境中,Python脚本可以用来监控和管理Nginx服务器的进程状态、端口监听情况以及日志分析等。以下是一个基础的Python脚本示例,用于检查Nginx主进程是否运行,并在进程未运行时发送警报:
用python代码实现过程如下:
import subprocess
import psutil
import smtplib
import time
from email.mime.text import MIMEText
# 检查Nginx主进程是否存在函数
def is_nginx_running():
try:
# 使用`pgrep`命令查找Nginx主进程,检查 Nginx 进程是否在运行
result = subprocess.run(['pgrep', '-f', 'nginx: master process'], capture_output=True, text=True)
return bool(result.stdout.strip())
except Exception as e:
print(f"Error checking Nginx process: {e}")
return False
如果发现Nginx服务没有运行,重启 Nginx
try:
subprocess.run(['nginx', '-s', 'reload'], check=True)
print("Nginx restarted successfully.")
except subprocess.CalledProcessError as e:
print(f"Error restarting Nginx: {e}")
# 检查nginx的状态
########### 这部分代码也可以使用,根据自己的爱好
def check_nginx_status():
# 发送HTTP请求到Nginx服务器,检查其状态
try:
response = requests.get('http://192.168.10.1/nginx_status')
if response.status_code == 200:
return True
else:
return False
except requests.exceptions.RequestException as e:
print(e)
return False
###########
# 发送邮件通知函数
def send_email(subject, message):
sender = 'your_email@example.com'
receiver = 'admin_email@example.com'
password = 'your_smtp_password' # 或者使用环境变量存储密码
smtp_server = 'smtp.example.com'
msg = MIMEText(message)
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = receiver
try:
with smtplib.SMTP(smtp_server, 587) as server:
server.starttls()
server.login(sender, password)
server.sendmail(sender, receiver, msg.as_string())
except Exception as e:
print(f"Failed to send email: {e}")
# 主程序逻辑
if __name__ == "__main__":
if not is_nginx_running():
message = "Nginx主进程未运行,请尽快检查!"
send_email("Nginx服务警告", message)
else:
print("Nginx主进程正在运行。")
# 获取CPU使用率
cpu_usage = psutil.cpu_percent()
print('CPU Usage:', cpu_usage)
time.sleep(60) # 每分钟检查一次 Nginx 进程
# 如果需要更复杂的监控,比如定期检查或检查工作进程数,可以扩展此脚本功能
猜你喜欢
- 2024-12-18 使用Python监控文件内容变化代码 python 监控文件内容
- 2024-12-18 118.Python——PyQt窗体上显示监控视频画面
- 2024-12-18 Python 全栈开发 -- 监控篇 python监控程序
- 2024-12-18 使用Python实现自动化网络安全监控与防护
- 2024-12-18 Python调用Prometheus监控数据并计算
- 2024-12-18 使用Python实现智能电网监控与优化系统
- 2024-12-18 实时监控文件系统:探索Python Watchdog库的神奇之处!
- 2024-12-18 使用Python实现智能农业设备控制与监控
- 2024-12-18 Watchfiles vs Watchgod:Python 文件监控库的真实体验对比?
- 2024-12-18 如何用Python脚本来监控服务器(一)——自动化监控之Agent方式
- 05-25Python 3.14 t-string 要来了,它与 f-string 有何不同?
- 05-25Python基础元素语法总结
- 05-25Python中的变量是什么东西?
- 05-25新手常见的python报错及解决方案
- 05-2511-Python变量
- 05-2510个每个人都是需要知道Python问题
- 05-25Python编程:轻松掌握函数定义、类型及其参数传递方式
- 05-25Python基础语法
- 257℃Python短文,Python中的嵌套条件语句(六)
- 257℃python笔记:for循环嵌套。end=""的作用,图形打印
- 256℃PythonNet:实现Python与.Net代码相互调用!
- 251℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 251℃Python实现字符串小写转大写并写入文件
- 106℃原来2025是完美的平方年,一起探索六种平方的算吧
- 90℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 81℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)