matlab监控
This commit is contained in:
commit
4ec85bad23
60
Prometheus 和 Grafana监控部署.docx
Normal file
60
Prometheus 和 Grafana监控部署.docx
Normal file
@ -0,0 +1,60 @@
|
||||
第一步:本地机和服务器上安装 Prometheus 和 Grafana
|
||||
下载地址:Download | Prometheus
|
||||
· 根据你的操作系统下载合适的 Prometheus 版本。
|
||||
· 解压下载的文件,并进入 Prometheus 文件夹。
|
||||
Prometheus 会在 http://localhost:9090 上运行。
|
||||
需要开启进程prometheus.exe
|
||||
|
||||
安装 Grafana
|
||||
下载地址:Grafana get started | Cloud, Self-managed, Enterprise
|
||||
* 访问 Grafana 官方下载页面。
|
||||
* 根据你的操作系统下载并安装 Grafana。
|
||||
Grafana 会在 http://localhost:3000 上运行,用户名和密码默认都是 admin。
|
||||
|
||||
|
||||
|
||||
|
||||
第二步骤 本地机和远程服务器 Prometheus的链接
|
||||
1 测试本地是否能远程访问服务器server
|
||||
|
||||
比如
|
||||
http://localhost:9090 也是服务器访问Prometheus的端口
|
||||
如果出现
|
||||
则成功
|
||||
|
||||
如果访问不到端口8080 9100,一般是防火墙的原因,需要在服务器关闭防火墙
|
||||
|
||||
第三步:配置 Prometheus 以抓取 Python 应用的数据
|
||||
打开 Prometheus 的配置文件 prometheus.yml,找到 scrape_configs 部分,添加如下配置:访问服务器 访问本地 端口
|
||||
可本地查看43.138.46:9090
|
||||
Status = up 表示成功
|
||||
没有数据或指标: 如果没有数据,可能是因为 Prometheus 没有正确抓取到该端点。检查 Prometheus 配置文件中的抓取目标是否正确,且 Python 应用是否在正确的端口上运行。
|
||||
Python监听脚本是8001的端口,需要在服务器上的prometheus.yaml改端口
|
||||
9090和8001 都试试
|
||||
|
||||
|
||||
第四步 远程服务器的Python脚本监控:
|
||||
在 Python 中集成 Prometheus
|
||||
安装 Prometheus Python 客户端库 你需要使用 prometheus_client 这个库来将你的 Python 应用与 Prometheus 集成。在终端中输入以下命令:
|
||||
|
||||
cmd 运行需要监控的脚本demo1.py, demo2.py。。。
|
||||
|
||||
监控的脚本循环是否在运行
|
||||
|
||||
服务器的Prometheus + 本地的Prometheu会进行监控
|
||||
结果用Grafana进行可视化
|
||||
|
||||
|
||||
|
||||
|
||||
先在connections创建新的prometheus实例
|
||||
|
||||
Firing如何跳出来
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
query在某个时间段查询
|
||||
hour(vector(time())) >= 11 and hour(vector(time())) < 12
|
||||
|
122
monitorV4.py
Normal file
122
monitorV4.py
Normal file
@ -0,0 +1,122 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Created on Mon Feb 24 22:55:11 2025
|
||||
|
||||
@author: fyx90
|
||||
"""
|
||||
|
||||
import psutil
|
||||
import time
|
||||
import pandas as pd
|
||||
import requests
|
||||
from sqlalchemy import create_engine, text
|
||||
from flask import Flask, Response
|
||||
from prometheus_client import Gauge, generate_latest
|
||||
|
||||
# 创建 Flask 应用
|
||||
app = Flask(__name__)
|
||||
|
||||
# 获取服务器的 IP 地址
|
||||
update_servers = requests.get('http://ifconfig.me/ip', timeout=10).text.strip()
|
||||
|
||||
# 连接到数据库
|
||||
engine_tencent = create_engine(
|
||||
'mysql+pymysql://ainvest_luming:lm18242094506@bj-cdb-3gfxha84.sql.tencentcdb.com:59970/monitor_fund'
|
||||
)
|
||||
|
||||
# 查询数据库中需要监控的 MATLAB 脚本
|
||||
sql_query1 = text(fr'SELECT * FROM `server_scripts` WHERE ip="{update_servers }"')
|
||||
|
||||
with engine_tencent.connect() as connection:
|
||||
df1 = pd.read_sql_query(sql_query1, connection)['scripts_name']
|
||||
|
||||
SCRIPTS_TO_MONITOR = df1.tolist()
|
||||
|
||||
# 定义 Prometheus 指标
|
||||
MATLAB_PROCESS_RUNNING = Gauge('matlab_process_running', 'MATLAB process running status')
|
||||
MATLAB_SCRIPT_RUNNING = Gauge('matlab_script_running', 'MATLAB script running status', ['script_name'])
|
||||
MATLAB_SCRIPT_COUNT = Gauge('matlab_script_count', 'Number of MATLAB scripts running')
|
||||
|
||||
def get_matlab_script_count():
|
||||
"""获取当前正在运行的 MATLAB 脚本数量"""
|
||||
count = 0
|
||||
for proc in psutil.process_iter(['name', 'cmdline']):
|
||||
try:
|
||||
if 'matlab' in proc.info['name'].lower() and any('.m' in part for part in proc.info['cmdline']):
|
||||
count += 1
|
||||
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
|
||||
pass
|
||||
return count
|
||||
|
||||
def check_matlab_process_status():
|
||||
"""检查是否有 MATLAB 进程在运行"""
|
||||
matlab_processes = [
|
||||
proc for proc in psutil.process_iter(['name'])
|
||||
if proc.info['name'] and 'matlab' in proc.info['name'].lower()
|
||||
]
|
||||
process_running = 1 if matlab_processes else 0
|
||||
MATLAB_PROCESS_RUNNING.set(process_running)
|
||||
print(f"🔍 MATLAB 进程状态: {'运行中' if process_running else '未运行'} (共 {len(matlab_processes)} 个进程)")
|
||||
|
||||
def check_matlab_script_status():
|
||||
"""检查特定 MATLAB 脚本是否在运行"""
|
||||
running_processes = []
|
||||
for proc in psutil.process_iter(['pid', 'name', 'cmdline']):
|
||||
try:
|
||||
process_name = proc.info['name']
|
||||
cmdline = " ".join(proc.info['cmdline']) if proc.info['cmdline'] else ""
|
||||
running_processes.append((proc.info['pid'], process_name, cmdline))
|
||||
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
|
||||
continue
|
||||
|
||||
# 更新 MATLAB 脚本运行状态
|
||||
#for script in SCRIPTS_TO_MONITOR :
|
||||
# is_running = any(script in cmdline for _, _, cmdline in running_processes)
|
||||
# MATLAB_SCRIPT_RUNNING.labels(script_name=script).set(1 if is_running else 0)
|
||||
# print(f"🔍 监控 {script}: {'✅ 运行中' if is_running else '❌ 未运行'}")
|
||||
|
||||
|
||||
cpu_usage = get_matlab_cpu_usage()
|
||||
if cpu_usage< 5.0:
|
||||
for script in SCRIPTS_TO_MONITOR :
|
||||
# is_running = False
|
||||
MATLAB_SCRIPT_RUNNING.labels(script_name=script).set( 0)
|
||||
is_running = False
|
||||
print(f"🔍 监控 {script}: {'✅ 运行中' if is_running else '❌ 未运行'}")
|
||||
else:
|
||||
for script in SCRIPTS_TO_MONITOR :
|
||||
MATLAB_SCRIPT_RUNNING.labels(script_name=script).set(1)
|
||||
is_running = True
|
||||
print(f"🔍 监控 {script}: {'✅ 运行中' if is_running else '❌ 未运行'}")
|
||||
|
||||
def get_matlab_cpu_usage():
|
||||
"""获取 MATLAB 进程的 CPU 使用率"""
|
||||
total_cpu_usage = 0.0
|
||||
for proc in psutil.process_iter(['pid', 'name', 'cpu_percent']):
|
||||
try:
|
||||
if proc.info['name'] and 'matlab' in proc.info['name'].lower():
|
||||
total_cpu_usage += proc.cpu_percent(interval=1) # 获取 CPU 使用率
|
||||
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
|
||||
continue
|
||||
return total_cpu_usage
|
||||
|
||||
|
||||
# 定义 Prometheus 监控路由
|
||||
@app.route('/metrics')
|
||||
def metrics():
|
||||
# 更新所有 Prometheus 指标
|
||||
script_count = get_matlab_script_count()
|
||||
MATLAB_SCRIPT_COUNT.set(script_count)
|
||||
check_matlab_process_status()
|
||||
check_matlab_script_status()
|
||||
|
||||
# 打印日志
|
||||
print(f"当前 MATLAB 运行的脚本数量: {script_count}")
|
||||
|
||||
# 返回 Prometheus 指标
|
||||
return Response(generate_latest(), mimetype='text/plain')
|
||||
|
||||
# 启动 Flask 服务器
|
||||
if __name__ == '__main__':
|
||||
print("🚀 启动 MATLAB 监控 Exporter,监听端口 8001...")
|
||||
app.run(host='0.0.0.0', port=8001)
|
BIN
prometheus-3.2.0-rc.1.windows-amd64.zip
Normal file
BIN
prometheus-3.2.0-rc.1.windows-amd64.zip
Normal file
Binary file not shown.
7
requirement.txt
Normal file
7
requirement.txt
Normal file
@ -0,0 +1,7 @@
|
||||
psutil
|
||||
flask
|
||||
prometheus_client
|
||||
|
||||
cmd.exe
|
||||
"C:\Program Files\Python310\python.exe" "C:\Users\Administrator\Desktop\jiankong\monitorMatlabV1.py"
|
||||
"C:\Program Files\Python310\python.exe" "C:\Users\Administrator\Desktop\jiankong\monitorMatlab.py"
|
1
~$ometheus 和 Grafana监控部署.docx
Normal file
1
~$ometheus 和 Grafana监控部署.docx
Normal file
@ -0,0 +1 @@
|
||||
fyx90 |