增加磁盘和网卡硬件信息显示
This commit is contained in:
157
agent/agent.sh
Executable file
157
agent/agent.sh
Executable file
@@ -0,0 +1,157 @@
|
||||
#!/bin/bash
|
||||
# 启动/停止/重启脚本
|
||||
|
||||
# ===================== 配置区 =====================
|
||||
# 程序路径
|
||||
AGENT_PATH="/bin/monitor-agent"
|
||||
# 日志文件路径
|
||||
LOG_FILE="/root/monitor/agent.log"
|
||||
# PID文件路径(记录进程ID)
|
||||
PID_FILE="/root/monitor/agent.pid"
|
||||
# 启动参数(根据实际需求调整)
|
||||
START_ARGS=""
|
||||
# ==================== 配置区结束 ====================
|
||||
|
||||
# 检查程序文件是否存在
|
||||
check_agent_exists() {
|
||||
if [ ! -f "${AGENT_PATH}" ]; then
|
||||
echo "错误:程序文件 ${AGENT_PATH} 不存在!"
|
||||
exit 1
|
||||
fi
|
||||
if [ ! -x "${AGENT_PATH}" ]; then
|
||||
echo "错误:程序文件 ${AGENT_PATH} 没有执行权限,正在尝试添加..."
|
||||
chmod +x "${AGENT_PATH}"
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "错误:添加执行权限失败,请手动执行 chmod +x ${AGENT_PATH}"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# 检查进程是否运行
|
||||
check_running() {
|
||||
if [ -f "${PID_FILE}" ]; then
|
||||
PID=$(cat "${PID_FILE}")
|
||||
if ps -p "${PID}" > /dev/null 2>&1; then
|
||||
return 0 # 运行中
|
||||
else
|
||||
rm -f "${PID_FILE}" # PID文件存在但进程已死,清理PID文件
|
||||
fi
|
||||
fi
|
||||
return 1 # 未运行
|
||||
}
|
||||
|
||||
# 启动程序
|
||||
start_agent() {
|
||||
if check_running; then
|
||||
echo "✅ monitor-agent 已在运行(PID: $(cat ${PID_FILE}))"
|
||||
return 0
|
||||
fi
|
||||
|
||||
echo "🚀 正在启动 monitor-agent..."
|
||||
# 创建日志目录(如果不存在)
|
||||
mkdir -p "$(dirname ${LOG_FILE})"
|
||||
# 后台启动程序,重定向日志,记录PID
|
||||
nohup "${AGENT_PATH}" ${START_ARGS} > "${LOG_FILE}" 2>&1 &
|
||||
AGENT_PID=$!
|
||||
echo "${AGENT_PID}" > "${PID_FILE}"
|
||||
|
||||
# 等待2秒检查是否启动成功
|
||||
sleep 2
|
||||
if check_running; then
|
||||
echo "✅ monitor-agent 启动成功(PID: ${AGENT_PID})"
|
||||
echo "日志文件:${LOG_FILE}"
|
||||
else
|
||||
echo "❌ monitor-agent 启动失败!请查看日志:${LOG_FILE}"
|
||||
rm -f "${PID_FILE}"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
# 停止程序
|
||||
stop_agent() {
|
||||
if ! check_running; then
|
||||
echo "ℹ️ monitor-agent 未运行"
|
||||
return 0
|
||||
fi
|
||||
|
||||
PID=$(cat "${PID_FILE}")
|
||||
echo "🛑 正在停止 monitor-agent(PID: ${PID})..."
|
||||
# 优雅停止(先尝试TERM信号,失败则强制KILL)
|
||||
kill "${PID}" > /dev/null 2>&1
|
||||
sleep 3
|
||||
|
||||
if ps -p "${PID}" > /dev/null 2>&1; then
|
||||
echo "⚠️ 优雅停止失败,强制杀死进程..."
|
||||
kill -9 "${PID}" > /dev/null 2>&1
|
||||
sleep 1
|
||||
fi
|
||||
|
||||
# 清理PID文件
|
||||
rm -f "${PID_FILE}"
|
||||
echo "✅ monitor-agent 已停止"
|
||||
}
|
||||
|
||||
# 查看状态
|
||||
status_agent() {
|
||||
if check_running; then
|
||||
echo "✅ monitor-agent 运行中(PID: $(cat ${PID_FILE}))"
|
||||
else
|
||||
echo "ℹ️ monitor-agent 未运行"
|
||||
fi
|
||||
}
|
||||
|
||||
# 重启程序
|
||||
restart_agent() {
|
||||
echo "🔄 正在重启 monitor-agent..."
|
||||
stop_agent
|
||||
sleep 2
|
||||
start_agent
|
||||
}
|
||||
|
||||
# 帮助信息
|
||||
show_help() {
|
||||
echo "使用方法:$0 [start|stop|restart|status|help]"
|
||||
echo " start - 启动 monitor-agent"
|
||||
echo " stop - 停止 monitor-agent"
|
||||
echo " restart - 重启 monitor-agent"
|
||||
echo " status - 查看 monitor-agent 运行状态"
|
||||
echo " help - 显示帮助信息"
|
||||
}
|
||||
|
||||
# 主逻辑
|
||||
main() {
|
||||
# 检查是否为root用户(可选,根据需求调整)
|
||||
if [ "$(id -u)" -ne 0 ]; then
|
||||
echo "警告:建议使用root用户运行此脚本(当前用户:$(whoami))"
|
||||
# exit 1 # 如果强制要求root,取消注释
|
||||
fi
|
||||
|
||||
check_agent_exists
|
||||
|
||||
case "$1" in
|
||||
start)
|
||||
start_agent
|
||||
;;
|
||||
stop)
|
||||
stop_agent
|
||||
;;
|
||||
restart)
|
||||
restart_agent
|
||||
;;
|
||||
status)
|
||||
status_agent
|
||||
;;
|
||||
help|--help|-h)
|
||||
show_help
|
||||
;;
|
||||
*)
|
||||
echo "错误:无效参数 '$1'"
|
||||
show_help
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
# 执行主逻辑
|
||||
main "$@"
|
||||
Binary file not shown.
38
agent/monitor-agent.service
Normal file
38
agent/monitor-agent.service
Normal file
@@ -0,0 +1,38 @@
|
||||
[Unit]
|
||||
# 服务描述
|
||||
Description=Monitor Agent Service
|
||||
# 依赖:网络启动后再启动该服务
|
||||
After=network.target network-online.target
|
||||
# 可选:依赖其他服务(如mysql、redis等),按需添加
|
||||
# Requires=mysql.service
|
||||
|
||||
[Service]
|
||||
# 服务类型:forking 对应脚本的后台启动(nohup)
|
||||
Type=forking
|
||||
# 启动命令(指向我们写的启动脚本)
|
||||
ExecStart=/root/monitor/agent.sh start
|
||||
# 停止命令
|
||||
ExecStop=/root/monitor/agent.sh stop
|
||||
# 重启命令
|
||||
ExecReload=/root/monitor/agent.sh restart
|
||||
# PID文件路径(必须与脚本中PID_FILE一致)
|
||||
PIDFile=/root/monitor/agent.pid
|
||||
# 运行用户(建议root,与脚本执行用户一致)
|
||||
User=root
|
||||
Group=root
|
||||
# 故障重启策略:失败后自动重启,最多重启5次
|
||||
Restart=on-failure
|
||||
RestartSec=3s
|
||||
StartLimitInterval=30s
|
||||
StartLimitBurst=5
|
||||
# 日志重定向(可选,统一到systemd日志)
|
||||
StandardOutput=append:/root/monitor/agent.log
|
||||
StandardError=append:/root/monitor/agent.log
|
||||
# 进程退出时清理环境
|
||||
KillMode=process
|
||||
# 优雅停止超时时间
|
||||
TimeoutStopSec=10s
|
||||
|
||||
[Install]
|
||||
# 开机自启级别:多用户模式
|
||||
WantedBy=multi-user.target
|
||||
Reference in New Issue
Block a user