feat: добавить установку агента для Windows Server 2012+ (PowerShell скрипт, Scheduled Task, авт. установка Python)

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
mirivlad 2026-04-14 04:31:20 +08:00
parent 808b7c7d1e
commit c2c9934ff8
4 changed files with 175 additions and 16 deletions

View File

@ -229,8 +229,9 @@ $app->get('/api/status', function (Request $request, Response $response, $args)
->withHeader('Content-Type', 'application/json');
});
// Agent installation script route (public, no auth middleware, no csrf)
// Agent installation script routes (public, no auth middleware, no csrf)
$app->get('/agent/install.sh', [$agentController, 'generateInstallScript']);
$app->get('/agent/install.ps1', [$agentController, 'generateWindowsInstallScript']);
// Run app
$app->run();

View File

@ -421,4 +421,132 @@ systemctl status server-monitor-agent
$response->getBody()->write(json_encode($data));
return $response->withHeader('Content-Type', 'application/json');
}
public function generateWindowsInstallScript(Request $request, Response $response, $args)
{
$queryParams = $request->getQueryParams();
$token = $queryParams['token'] ?? null;
$server_id = $queryParams['server_id'] ?? null;
if (!empty($server_id) && empty($token)) {
$stmt = $this->pdo->prepare("SELECT encrypted_token FROM agent_tokens WHERE server_id = :server_id LIMIT 1");
$stmt->execute([':server_id' => $server_id]);
$result = $stmt->fetch();
if ($result && !empty($result['encrypted_token'])) {
$token = EncryptionHelper::decrypt($result['encrypted_token']);
}
}
if (empty($token)) {
$response->getBody()->write('Token is required');
return $response->withStatus(400);
}
$apiUrl = 'https://mon.mirv.top/api/v1/metrics';
$agentPyUrl = 'https://mon.mirv.top/agent/agent.py';
// PowerShell скрипт установки
$lines = [];
$lines[] = '# Скрипт установки агента мониторинга для Windows Server 2012+';
$lines[] = '# Запустите от имени Администратора (PowerShell)';
$lines[] = '';
$lines[] = '$ErrorActionPreference = "Stop"';
$lines[] = '$Token = "' . addslashes($token) . '"';
$lines[] = '$ApiUrl = "' . addslashes($apiUrl) . '"';
$lines[] = '$InstallDir = "C:\\Program Files\\MonAgent"';
$lines[] = '';
$lines[] = 'Write-Host "=== Установка агента мониторинга ===" -ForegroundColor Cyan';
$lines[] = '';
$lines[] = '# Проверяем права администратора';
$lines[] = '$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)';
$lines[] = 'if (-not $isAdmin) {';
$lines[] = ' Write-Host "ОШИБКА: Запустите PowerShell от имени Администратора!" -ForegroundColor Red';
$lines[] = ' exit 1';
$lines[] = '}';
$lines[] = '';
$lines[] = '# Включаем TLS 1.2';
$lines[] = '[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12';
$lines[] = '';
$lines[] = '# Проверяем Python';
$lines[] = 'Write-Host "Проверка Python..." -ForegroundColor Yellow';
$lines[] = '$pythonCmd = Get-Command python -ErrorAction SilentlyContinue';
$lines[] = 'if (-not $pythonCmd) {';
$lines[] = ' Write-Host "Установка Python 3.12..." -ForegroundColor Yellow';
$lines[] = ' $installer = "$env:TEMP\\python-installer.exe"';
$lines[] = ' $pythonUrl = "https://www.python.org/ftp/python/3.12.4/python-3.12.4-amd64.exe"';
$lines[] = ' Write-Host "Скачивание установщика Python..." -ForegroundColor Yellow';
$lines[] = ' Invoke-WebRequest -Uri $pythonUrl -OutFile $installer -UseBasicParsing';
$lines[] = ' Write-Host "Установка Python (тихая установка)..." -ForegroundColor Yellow';
$lines[] = ' Start-Process -FilePath $installer -ArgumentList "/quiet", "InstallAllUsers=1", "PrependPath=1", "Include_pip=1" -Wait -NoNewWindow';
$lines[] = ' Remove-Item $installer -Force -ErrorAction SilentlyContinue';
$lines[] = ' $pythonCmd = Get-Command python -ErrorAction SilentlyContinue';
$lines[] = ' if (-not $pythonCmd) {';
$lines[] = ' Write-Host "ОШИБКА: Python не установлен. Установите вручную с https://python.org" -ForegroundColor Red';
$lines[] = ' exit 1';
$lines[] = ' }';
$lines[] = ' Write-Host "Python успешно установлен!" -ForegroundColor Green';
$lines[] = '}';
$lines[] = '';
$lines[] = '# Устанавливаем psutil';
$lines[] = 'Write-Host "Установка psutil..." -ForegroundColor Yellow';
$lines[] = 'python -m pip install psutil --quiet';
$lines[] = 'if ($LASTEXITCODE -ne 0) {';
$lines[] = ' Write-Host "ОШИБКА: Не удалось установить psutil" -ForegroundColor Red';
$lines[] = ' exit 1';
$lines[] = '}';
$lines[] = '';
$lines[] = '# Создаём директорию';
$lines[] = 'Write-Host "Создание директории $InstallDir..." -ForegroundColor Yellow';
$lines[] = 'if (-not (Test-Path $InstallDir)) {';
$lines[] = ' New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null';
$lines[] = '}';
$lines[] = 'Set-Location $InstallDir';
$lines[] = '';
$lines[] = '# Создаём конфигурационный файл';
$lines[] = 'Write-Host "Создание конфигурации..." -ForegroundColor Yellow';
$lines[] = '$config = @{';
$lines[] = ' token = $Token';
$lines[] = ' api_url = $ApiUrl';
$lines[] = ' interval_seconds = 60';
$lines[] = '} | ConvertTo-Json';
$lines[] = '$config | Out-File -FilePath "$InstallDir\\config.json" -Encoding UTF8 -Force';
$lines[] = '';
$lines[] = '# Скачиваем agent.py';
$lines[] = 'Write-Host "Скачивание agent.py..." -ForegroundColor Yellow';
$lines[] = 'Invoke-WebRequest -Uri "' . addslashes($agentPyUrl) . '" -OutFile "$InstallDir\\agent.py" -UseBasicParsing';
$lines[] = '';
$lines[] = '# Создаём Scheduled Task для автозапуска';
$lines[] = 'Write-Host "Регистрация службы..." -ForegroundColor Yellow';
$lines[] = '$serviceTaskName = "MonAgent"';
$lines[] = 'Unregister-ScheduledTask -TaskName $serviceTaskName -Confirm:$false -ErrorAction SilentlyContinue';
$lines[] = '$trigger = New-ScheduledTaskTrigger -AtStartup';
$lines[] = '$action = New-ScheduledTaskAction -Execute "python" -Argument "$InstallDir\\agent.py" -WorkingDirectory $InstallDir';
$lines[] = '$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -RestartCount 3 -RestartInterval (New-TimeSpan -Minutes 1)';
$lines[] = '$principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest';
$lines[] = 'Register-ScheduledTask -TaskName $serviceTaskName -Trigger $trigger -Action $action -Settings $settings -Principal $principal -Force | Out-Null';
$lines[] = '';
$lines[] = '# Запускаем задачу';
$lines[] = 'Start-ScheduledTask -TaskName $serviceTaskName';
$lines[] = '';
$lines[] = 'Write-Host ""';
$lines[] = 'Write-Host "=== Агент мониторинга установлен! ===" -ForegroundColor Green';
$lines[] = 'Write-Host "Директория: $InstallDir" -ForegroundColor White';
$lines[] = 'Write-Host "Служба: MonAgent (Scheduled Task)" -ForegroundColor White';
$lines[] = 'Write-Host "Лог: $InstallDir\\agent.log" -ForegroundColor White';
$lines[] = 'Write-Host ""';
$lines[] = 'Write-Host "Для управления:" -ForegroundColor Cyan';
$lines[] = 'Write-Host " Проверить статус: Get-ScheduledTask -TaskName MonAgent" -ForegroundColor Gray';
$lines[] = 'Write-Host " Остановить: Disable-ScheduledTask -TaskName MonAgent" -ForegroundColor Gray';
$lines[] = 'Write-Host " Запустить: Start-ScheduledTask -TaskName MonAgent" -ForegroundColor Gray';
$lines[] = 'Write-Host " Удалить: Unregister-ScheduledTask -TaskName MonAgent -Confirm:$false" -ForegroundColor Gray';
$script = implode("\n", $lines) . "\n";
return $response
->withHeader('Content-Type', 'text/plain; charset=UTF-8')
->withHeader('Content-Disposition', 'attachment; filename="install.ps1"')
->withBody(\Nyholm\Psr7\Utils::streamFor($script));
}
}

View File

@ -22,21 +22,46 @@
</button>
</div>
</div>
<p class="mb-0 mt-3">Скачайте скрипт установки агента мониторинга:</p>
<a href="/agent/install.sh?token={{ token }}" class="btn btn-primary mt-2">
<i class="fas fa-download"></i> Скачать install.sh
<p class="mb-0 mt-3">Скачайте скрипт установки агента:</p>
<div class="d-flex gap-2 mt-2">
<a href="/agent/install.sh?token={{ token }}" class="btn btn-primary">
<i class="fab fa-linux"></i> Агент для Linux
</a>
<a href="/agent/install.ps1?token={{ token }}" class="btn btn-info">
<i class="fab fa-windows"></i> Агент для Windows
</a>
</div>
</div>
<div class="mt-4">
<h5>Инструкция по установке агента:</h5>
<ol>
<li>Скачайте скрипт установки с помощью кнопки выше</li>
<li>Загрузите его на сервер, который вы хотите мониторить</li>
<li>Выполните команду: <code>chmod +x install.sh && ./install.sh</code></li>
<li>Агент начнет отправлять метрики на сервер мониторинга</li>
<h5>Инструкция по установке:</h5>
<div class="card mb-3">
<div class="card-header">
<i class="fab fa-linux"></i> Linux
</div>
<div class="card-body">
<ol class="mb-0">
<li>Скачайте скрипт <code>install.sh</code></li>
<li>Загрузите на сервер</li>
<li>Выполните: <code>chmod +x install.sh && ./install.sh</code></li>
</ol>
</div>
</div>
<div class="card">
<div class="card-header">
<i class="fab fa-windows"></i> Windows Server 2012+
</div>
<div class="card-body">
<ol class="mb-0">
<li>Скачайте скрипт <code>install.ps1</code></li>
<li>Запустите PowerShell <strong>от имени Администратора</strong></li>
<li>Выполните: <code>Set-ExecutionPolicy Bypass -Scope Process -Force; .\install.ps1</code></li>
</ol>
</div>
</div>
</div>
<div class="d-grid gap-2 d-md-flex justify-content-md-end mt-4">
<a href="/servers" class="btn btn-primary me-md-2">

View File

@ -54,12 +54,17 @@
<div class="mt-4">
<h5>Управление агентом мониторинга:</h5>
<div class="row">
<div class="col-md-6 mb-2">
<div class="col-md-4 mb-2">
<a href="/agent/install.sh?server_id={{ server.id }}" class="btn btn-outline-primary w-100">
<i class="fas fa-download"></i> Скачать install.sh
<i class="fab fa-linux"></i> Агент для Linux
</a>
</div>
<div class="col-md-6 mb-2">
<div class="col-md-4 mb-2">
<a href="/agent/install.ps1?server_id={{ server.id }}" class="btn btn-outline-info w-100">
<i class="fab fa-windows"></i> Агент для Windows
</a>
</div>
<div class="col-md-4 mb-2">
<a href="/servers/{{ server.id }}/regenerate-token" class="btn btn-outline-warning w-100" onclick="return confirm('Вы уверены, что хотите сгенерировать новый токен? Это сделает недействительным старый скрипт установки.');">
<i class="fas fa-sync-alt"></i> Сбросить токен
</a>