自动化安装zabbix agent在windows2012
来源:互联网 发布:手机上怎样申请淘宝号 编辑:程序博客网 时间:2024/06/08 17:28
通过winrm自动化安装zabbix agent.
1. 设置winrm
2. 下载zabbix安装包到http server下
3. 通过winrm接口执行powershell script,或通过ansible, 先下载脚本,再执行脚本
ansible 10.0.202.219 -m win_command -a "powershell.exe invoke-webrequest -uri http://10.0.202.210:18083/repo/windows/zabbix/zabbix_install.ps1 -outfile C:\zabbix_install.ps1"
ansible 10.0.202.219 -m win_command -a "powershell.exe invoke-webrequest -uri http://10.0.202.210:18083/repo/windows/zabbix/zabbix-win.zip -outfile C:\zabbix\zabbix-win.zip"
ansible 10.0.202.219 -m win_shell -a "C:\zabbix_install.ps1"
=============================================================
zabbix install powershell script:
[root@cop zabbix]# cat zabbix_install.ps1
param($win_repo_ip,$zabbixserverip)
mkdir C:\zabbix
invoke-webrequest -uri http://$win_repo_ip:18083/repo/windows/zabbix/zabbix-win.zip -outfile C:\zabbix\zabbix-win.zip
$file = "C:\zabbix\zabbix-win.zip"
$shell = New-Object -ComObject shell.application
$zip = $shell.NameSpace($file)
$dest = $shell.NameSpace((Split-Path $file -Parent))
$dest.CopyHere($zip.Items())
Copy-Item C:\zabbix\zabbix-win\bin\win64\*.exe C:\zabbix\zabbix-win
Copy-Item C:\zabbix\zabbix-win\conf\zabbix_agentd.win.conf C:\zabbix\zabbix-win
Rename-Item C:\zabbix\zabbix-win\zabbix_agentd.win.conf zabbix_agentd.conf
Remove-Item C:\zabbix\zabbix-win.zip
$conf= Get-Content C:\zabbix\zabbix-win\zabbix_agentd.conf -Encoding UTF8
$conf= $conf.Replace("LogFile=c:\zabbix_agentd.log", "LogFile=c:\zabbix\zabbix_agentd.log")
$conf= $conf.Replace("Server=127.0.0.1", "Server=$zabbixserverip")
$conf= $conf.Replace("ServerActive=127.0.0.1", "ServerActive=$zabbixserverip:10051")
$conf= $conf.Replace("Hostname=Windows host", "Hostname=${Env:COMPUTERNAME}")
#$conf= $conf.Replace("# HostnameItem=system.hostname", "HostnameItem=system.hostname")
$conf= $conf.Replace("# HostMetadata=", "HostMetadata=")
[IO.File]::WriteAllLines("C:\zabbix\zabbix-win\zabbix_agentd.conf", $conf)
cd C:\zabbix\zabbix-win
.\zabbix_agentd.exe -i -c C:\zabbix\zabbix-win\zabbix_agentd.conf
.\zabbix_agentd.exe -s -c zabbix_agentd.conf
if (-not(Get-NetFirewallRule | where Name -eq "Zabbix Agent"))
{
New-NetFirewallRule `
-Name "Zabbix Agent" `
-DisplayName "Zabbix Agent" `
-Description "for Zabbix Agent" `
-Group "Zabbix" `
-Enabled True `
-Profile Any `
-Direction Inbound `
-Action Allow `
-EdgeTraversalPolicy Block `
-LooseSourceMapping $False `
-LocalOnlyMapping $False `
-OverrideBlockRules $False `
-Program Any `
-LocalAddress Any `
-RemoteAddress Any `
-Protocol TCP `
-LocalPort 10050 `
-RemotePort Any `
-LocalUser Any `
-RemoteUser Any
}
else
{
echo "Windows PowerShell Remoting port TCP 10050 was alredy opend. Show Rule"
Get-NetFirewallPortFilter -Protocol TCP | where Localport -eq 10050
}
=====================================
ansible 10.0.202.219 -m win_command -a "powershell.exe invoke-webrequest -uri http://10.0.202.210:18083/repo/windows/zabbix/zabbix_install.ps1 -outfile C:\zabbix_install.ps1"
ansible 10.0.202.219 -m win_shell -a "C:\zabbix_install.ps1"