[#37] 修复dcgm exporter启动

This commit is contained in:
2025-11-07 17:29:06 +08:00
parent 1819fb9c46
commit dac180f12b
8 changed files with 193 additions and 12 deletions

View File

@@ -1 +1 @@
1.42.0
1.43.0

View File

@@ -14,6 +14,16 @@ log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
# 运行时开关(可通过环境变量覆盖)
# 1) 是否自动启动 nv-hostengine(容器内通常没有 systemd)
AUTO_START_DCGM="${AUTO_START_DCGM:-1}"
# 2) 是否默认禁用 Profiling 指标(避免在部分环境触发 DCGM Profiling 崩溃)
DCGM_EXPORTER_DISABLE_PROFILING="${DCGM_EXPORTER_DISABLE_PROFILING:-1}"
# 3) 自定义 collectors 文件;若为空且禁用 Profiling,则自动生成 no-prof 清单
DCGM_EXPORTER_COLLECTORS="${DCGM_EXPORTER_COLLECTORS:-}"
# 4) 监听地址
DCGM_EXPORTER_LISTEN="${DCGM_EXPORTER_LISTEN:-:9400}"
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
@@ -160,10 +170,21 @@ check_dcgm_service() {
elif pgrep -f nv-hostengine > /dev/null; then
log_success "nv-hostengine 进程已在运行"
else
log_warning "DCGM 服务未运行,需要手动启动"
log_info "启动 DCGM 服务的方法:"
log_info " 1. 使用 systemd: sudo systemctl start dcgm"
log_info " 2. 手动启动: nohup nv-hostengine > /var/log/nv-hostengine.log 2>&1 &"
log_warning "DCGM 服务未运行"
if [[ "${AUTO_START_DCGM}" == "1" ]]; then
log_info "尝试自动启动 nv-hostengine(容器内无 systemd 场景)..."
nohup nv-hostengine > /var/log/nv-hostengine.log 2>&1 &
sleep 2
if pgrep -f nv-hostengine >/dev/null; then
log_success "nv-hostengine 已启动"
else
log_error "nv-hostengine 启动失败,请手动检查 /var/log/nv-hostengine.log"
fi
else
log_info "启动 DCGM 服务的方法:"
log_info " 1. 使用 systemd: sudo systemctl start dcgm"
log_info " 2. 手动启动: nohup nv-hostengine > /var/log/nv-hostengine.log 2>&1 &"
fi
fi
# 测试 DCGM 连接
@@ -172,7 +193,7 @@ check_dcgm_service() {
if dcgmi discovery -l > /dev/null 2>&1; then
log_success "DCGM 连接测试成功"
else
log_warning "DCGM 连接测试失败,请检查服务状态"
log_warning "DCGM 连接测试失败,请检查服务状态(驱动/权限/设备可见性)"
fi
fi
}
@@ -269,6 +290,7 @@ start_dcgm_exporter() {
local binary_path="/usr/local/bin/dcgm-exporter"
local log_file="/var/log/dcgm-exporter.log"
local pid_file="/var/run/dcgm-exporter.pid"
local collectors_arg=""
# 检查服务是否已经在运行
if [[ -f "$pid_file" ]]; then
@@ -282,15 +304,48 @@ start_dcgm_exporter() {
fi
fi
# 计算 collectors 参数
if [[ -n "${DCGM_EXPORTER_COLLECTORS}" ]]; then
if [[ -f "${DCGM_EXPORTER_COLLECTORS}" ]]; then
collectors_arg=(--collectors "${DCGM_EXPORTER_COLLECTORS}")
log_info "使用自定义 collectors: ${DCGM_EXPORTER_COLLECTORS}"
else
log_warning "指定的 DCGM_EXPORTER_COLLECTORS 文件不存在: ${DCGM_EXPORTER_COLLECTORS}(将忽略)"
fi
elif [[ "${DCGM_EXPORTER_DISABLE_PROFILING}" == "1" ]]; then
local cfg_dir="/etc/dcgm-exporter"
local default_cfg="${cfg_dir}/default-counters.csv"
local no_prof_cfg="${cfg_dir}/no-prof.csv"
mkdir -p "${cfg_dir}"
if [[ -f "${default_cfg}" ]]; then
grep -v 'DCGM_FI_PROF_' "${default_cfg}" > "${no_prof_cfg}" || true
collectors_arg=(--collectors "${no_prof_cfg}")
log_info "已生成无 Profiling 的 collectors: ${no_prof_cfg}"
else
log_warning "未找到默认 collectors 文件: ${default_cfg}"
fi
fi
# 检查端口是否被占用
if netstat -tuln 2>/dev/null | grep -q ":9400 "; then
if netstat -tuln 2>/dev/null | grep -q ":${DCGM_EXPORTER_LISTEN#:} "; then
log_warning "端口 9400 已被占用,请检查是否有其他服务在运行"
return 1
fi
# 启动前再校验一次 DCGM 主机引擎
if ! (systemctl is-active --quiet dcgm 2>/dev/null || pgrep -f nv-hostengine >/dev/null); then
log_warning "nv-hostengine 未运行,尝试自动启动"
nohup nv-hostengine > /var/log/nv-hostengine.log 2>&1 &
sleep 2
fi
# 启动服务
log_info "正在启动 DCGM Exporter..."
nohup "$binary_path" --address=:9400 > "$log_file" 2>&1 &
if [[ ${#collectors_arg[@]} -gt 0 ]]; then
nohup "$binary_path" --address="${DCGM_EXPORTER_LISTEN}" "${collectors_arg[@]}" > "$log_file" 2>&1 &
else
nohup "$binary_path" --address="${DCGM_EXPORTER_LISTEN}" > "$log_file" 2>&1 &
fi
local pid=$!
# 保存 PID
@@ -310,6 +365,20 @@ start_dcgm_exporter() {
else
log_error "DCGM Exporter 服务启动失败"
rm -f "$pid_file"
# 失败回退:若未禁用 Profiling,也未指定 collectors,则尝试自动回退到 no-prof 再起一次
if [[ -z "${DCGM_EXPORTER_COLLECTORS}" && "${DCGM_EXPORTER_DISABLE_PROFILING}" != "1" ]]; then
log_warning "尝试以无 Profiling 清单回退启动"
local cfg_dir="/etc/dcgm-exporter"; local default_cfg="${cfg_dir}/default-counters.csv"; local no_prof_cfg="${cfg_dir}/no-prof.csv"
if [[ -f "${default_cfg}" ]]; then
grep -v 'DCGM_FI_PROF_' "${default_cfg}" > "${no_prof_cfg}" || true
nohup "$binary_path" --address="${DCGM_EXPORTER_LISTEN}" --collectors "${no_prof_cfg}" > "$log_file" 2>&1 &
sleep 2
if pgrep -f dcgm-exporter >/dev/null; then
log_success "DCGM Exporter 已用无 Profiling 清单启动"
return 0
fi
fi
fi
return 1
fi
}