Deploy Zabbix agent on a large number of Windows-based servers using Powershell

Hello!
Recently I faced the task — to quickly install a large number of Zabbix agent on Windows servers.
I decided that it can and should be solved with scripts. As a "language" I chose my favorite (and not just me!) Powershell.
Small manual You will find under the cut!

Powershell v2.0+ offers a very convenient means to remotely start (Powershell Remoting) and I would gladly would have used them but in my case there was one problem — on the "road" could come across servers with Windows Server 2003 (no required updates), so I decided to use psexec.exe

I needed a highly automated solution, so I wrote a script in which I load a list of DNS server names from a file and get the XML file to import in Zabbix.

First what we need to do is put on a shared resource folder (\\server\share\DeployZabbix), which will:
1) the agent File for x86 systems (zabbix_agentd86.exe)
2) the agent File for x64-system (zabbix_agentd64.exe)
3) the configuration File zabbix_agentd.conf (which you need to specify at least the address/DNS name of the Zabbix server)
4) 2 of the batch file, Install86.bat or Install64.bat with the following contents:
Install86.bat:
the
"C:\Program Files\Zabbix\zabbix_agentd86.exe" --config "C:\Program Files\Zabbix\zabbix_agentd.conf" --install
net start "Zabbix Agent"


Install64.bat:
the
"C:\Program Files\Zabbix\zabbix_agentd64.exe" --config "C:\Program Files\Zabbix\zabbix_agentd.conf" --install
net start "Zabbix Agent"


Second what we need is the folder in which there will be a script to deploy agents and it works:
1) Actually, the script DeployZabbix.ps1 (about it below)
2) Three .txt file (Source1.txt, Source2.txt, Source3.txt) — is "pieces."xml file, which will get the output. remember to replace the group "Default" to the desired in the text!
Source1.txt
<?xml version="1.0" encoding="UTF-8"?>
<zabbix_export>
<version>2.0</version>
<date>2012-11-21T09:18:17Z</date>
<groups>
<group>
<name>Default</name>
</group>
</groups>
<hosts>



Source2.txt
<proxy/>
<status>0</status>
<ipmi_authtype>-1</ipmi_authtype>
<ipmi_privilege>2</ipmi_privilege>
<ipmi_username/>
<ipmi_password/>
<templates>
<template>
<name>Template OS Windows</name>
</template>
</templates>
<groups>
<group>
<name>Default</name>
</group>
</groups>
<interfaces>
<interface>
<default>1</default>
<type>1</type>
<useip>0</useip > 
<ip/>



Source3.txt
<port>10050</port>
<interface_ref > if1 < /interface_ref > 
</interface>
</interfaces>
<applications/>
<items/>
<discovery_rules/>
<macros/>
<inventory/>
</host>


3) Actually, the psexec.exe (it can take here)
4) the File computers.csv, where FQDN is the server names are each on a new line.

third point, the most important thing is the script itself. When writing a script you should add a test of bit depth. Also I prefer to log actions in a text file, then, in which case, you can see what went wrong.

the
Remove-Item .\out.txt #remove old files
Remove-Item .\out.xml #remove old files
copy .\Source1.txt .\out.txt #copy the first part xml to a new file
#import list of computers
Import-CSV ".\computers.csv" -header("ComputerName") | ForEach {
$ComputerName = $_.ComputerName #a little easier, I feel so comfortable :)
New-Item "\\$ComputerName\c$\Program Files\Zabbix" -Type Directory
$path = "\\$ComputerName\c$\Program Files\Zabbix"
Copy-Item \\server\share\DeployZabbix\* $path
$bit = Get-WmiObject Win32_Processor -computername $ComputerName | where {$_.DeviceID -eq "CPU0"} | Select AddressWidth
if ($bit -like '*64*') {
.\psexec.exe \\$ComputerName "C:\\Program Files\Zabbix\Install64.bat" -h
}
else {
.\psexec.exe \\$ComputerName "C:\\Program Files\Zabbix\Install86.bat" -h
}
Add-Content .\out.txt ('<host>')
Add-Content .\out.txt ('<host>' + $_.ComputerName + '</host>')
Add-Content .\out.txt ('<name>' + $_.ComputerName + '</name>')
gc Source2.txt | Out-File .\out.txt -Append -Encoding default

gc Source3.txt | Out-File .\out.txt -Append -Encoding default
}
Add-Content .\out.txt ('</hosts>')
Add-Content .\out.txt ('</zabbix_export>')
Rename-Item .\out.txt "out.xml"



You can add more lines to the script, removed unnecessary files, but since they weigh less than 1Mb, I did not bother.

Then, the resulting .the xml file (which will lie in the directory with the script) to import a list of hosts in Zabbix.

That's all. I hope this tutorial will be at least useful to someone.
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

Briefly on how to make your Qt geoservice plugin

Database replication PostgreSQL-based SymmetricDS

Developing for Sailfish OS: notifications for example apps for taking notes