67 lines
1.9 KiB
C++
67 lines
1.9 KiB
C++
// 看门狗 Monitor 入口
|
||
//
|
||
// 用法:vdi-kiosk-monitor -- <主进程命令行>
|
||
// 例: vdi-kiosk-monitor -- /home/armbian/vdi-terminal/bin/kiosk-shell
|
||
//
|
||
// 对应会议纪要要求:
|
||
// "必须部署独立的 Monitor(看门狗)程序监控主进程状态,
|
||
// 主进程假死或崩溃时由 Monitor 执行 kill 操作并重启"
|
||
|
||
#include <cstdio>
|
||
#include <string>
|
||
|
||
#include "monitor.h"
|
||
|
||
int main(int argc, char** argv) {
|
||
if (argc < 2) {
|
||
std::fprintf(stderr, "用法: vdi-kiosk-monitor [--verbose] -- <主进程命令行>\n");
|
||
return 2;
|
||
}
|
||
|
||
int idx = 1;
|
||
vdi::MonitorConfig config;
|
||
config.verbose = true;
|
||
|
||
while (idx < argc && std::string(argv[idx]).rfind("--", 0) == 0) {
|
||
const std::string opt = argv[idx];
|
||
if (opt == "--") {
|
||
++idx;
|
||
break;
|
||
}
|
||
if (opt == "--quiet") {
|
||
config.verbose = false;
|
||
} else if (opt == "--verbose") {
|
||
config.verbose = true;
|
||
} else if (opt == "--heartbeat-timeout" && idx + 1 < argc) {
|
||
config.heartbeatTimeoutMs = std::atoi(argv[++idx]);
|
||
} else if (opt == "--check-interval" && idx + 1 < argc) {
|
||
config.checkIntervalMs = std::atoi(argv[++idx]);
|
||
} else if (opt == "--max-restart" && idx + 1 < argc) {
|
||
config.maxRestartAttempts = std::atoi(argv[++idx]);
|
||
} else if (opt == "--heartbeat-file" && idx + 1 < argc) {
|
||
config.heartbeatFile = argv[++idx];
|
||
} else {
|
||
// 未知选项视为主进程命令行的起点
|
||
break;
|
||
}
|
||
++idx;
|
||
}
|
||
|
||
if (idx >= argc) {
|
||
std::fprintf(stderr, "缺少主进程命令行\n");
|
||
return 2;
|
||
}
|
||
|
||
// 拼出主进程命令行
|
||
std::string command;
|
||
for (int i = idx; i < argc; ++i) {
|
||
if (i > idx) {
|
||
command += " ";
|
||
}
|
||
command += argv[i];
|
||
}
|
||
|
||
vdi::Monitor monitor(config);
|
||
return monitor.run(command);
|
||
}
|