Windows自动安装Zabbix_agent脚本
需要用管理员身份执行以下脚本,即可实现自动下载Zabbix_agent+自动解压缩+自动修改配置文件+自动注册+自动定时重启;
$zabbixPath = "C:\Program Files"
$URL = 'https://cdn.zabbix.com/zabbix/binaries/stable/4.4/4.4.10/zabbix_agent-4.4.10-windows-amd64-openssl.zip'
$FILE = $env:temp + '\zabbix_agent-4.4.10-windows-amd64-openssl.zip'
$zabbixPathInstall = "$zabbixPath\zabbix_agent"
$zabbix_agent_FALSE = (Test-Path $zabbixPathInstall)
$zabbixConf = "$zabbixPathInstall\conf\zabbix_agentd.conf"
$zabbixService = "C:\'Program Files'\zabbix_agent\bin\zabbix_agentd.exe -c C:\'Program Files'\zabbix_agent\conf\zabbix_agentd.conf -i"
$zabbixRestart = "$zabbixPathInstall\restartzabbixagent.bat"
$rebootService = ("@echo off", 'net stop "Zabbix Agent"', 'net start "Zabbix Agent"')
#在C盘"C:\Program Files"目录下创建zabbix_agent文件夹
$zabbix_agent_FALSE = (Test-Path $zabbixPathInstall)
if ($zabbix_agent_FALSE -eq "True") {
remove-Item -Recurse -Force $zabbixPathInstall
}
New-Item -Path $zabbixPath -Name zabbix_agent -ItemType directory
#从官网下载zabbix_agent文件
((New-Object System.Net.WebClient).DownloadFile($URL, $FILE))
#解压文件到刚创建的目录下,服务器地址请更换成自己的
Expand-Archive -Path $FILE -DestinationPath $zabbixPathInstall
#修改Zabbix_Agent Conf文件
Function sed($Filename, $Oldvalue, $Newvalue) {
if (Test-Path $Filename) {
$content = get-content $Filename
clear-content $Filename
foreach ($line in $content) {
$liner = $line.Replace($Oldvalue, $Newvalue)
Add-content $Filename -Value $liner
}
}
}
sed $zabbixConf "Server=127.0.0.1" "Server=zabbix-proxy.***.com"
sed $zabbixConf "ServerActive=127.0.0.1" "ServerActive=zabbix-proxy.***.com:10051"
#注册Zabbix Agent服务并启动,删除下载的原文件
(Invoke-Expression $zabbixService) 2> $null
net start "Zabbix Agent"
Remove-Item $FILE
#配置计划任务-每天定时自动重启agent
foreach ($restartzabbixagent in $rebootService) {
echo $restartzabbixagent >> $zabbixRestart
}
SCHTASKS /Create /sc daily /tn "restartzabbixdaily" /st 01:00:00 /tr $zabbixRestart