📜 ⬆️ ⬇️

Deploy Zabbix agents to a large number of Windows-based servers using Powershell

Good day!
Recently, I faced a task - to quickly install a large number of Zabbix agents on a Windows server.
I decided that this can and should be solved using scripts. I chose Powershell as my favorite language (and not just me!).
You will find a small manual under the cut!

Powershell v2.0 + offers very convenient means of remote launch (Powershell Remoting) and I would love to use them, but in my case there was one problem - a server with Windows Server 2003 (without the necessary updates) could be on the “way”, therefore I decided to use psexec.exe

I needed the most automated solution, so I wrote such a script in which I load the list of DNS server names from the file, and at the output I get an XML file for import into Zabbix.

The first thing we need to do is put a folder ( \\ server \ share \ DeployZabbix ) on the share , which will contain:
1) Agent file for x86-systems (zabbix_agentd86.exe)
2) Agent file for x64 systems (zabbix_agentd64.exe)
3) Configuration file zabbix_agentd.conf (in which you need to specify at least the address / DNS name of the Zabbix server)
4) 2 batch files, Install86.bat and Install64.bat with the following content:
Install86.bat:
"C:\Program Files\Zabbix\zabbix_agentd86.exe" --config "C:\Program Files\Zabbix\zabbix_agentd.conf" --install net start "Zabbix Agent" 

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


The second thing we need is a folder in which the script for deploying agents will be located, and everything necessary for its work:
1) Actually, the script itself DeployZabbix.ps1 (about it below)
2) Three .txt files (Source1.txt, Source2.txt, Source3.txt) are the “pieces” of the .xml file that will be output. Do not forget to replace the “Default” group with the one you need 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, psexec.exe itself (you can take it from here )
4) The computers.csv file, in which the FQDN names of the servers each come with a new line.

Well, the third , the most important, is the script itself. When writing a script, you should add a bit check. I also prefer logging actions to a text file, so that later, in which case, I can see what happened wrong.

 Remove-Item .\out.txt #   Remove-Item .\out.xml #   copy .\Source1.txt .\out.txt #   xml    #   Import-CSV ".\computers.csv" -header("ComputerName") | ForEach { $ComputerName = $_.ComputerName #- ,    :) 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 Add-Content .\out.txt ('<dns>' + $_.ComputerName + '</dns>') 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 also add lines so that the script removes unnecessary files, but since they weigh less than 1Mb, I did not bother.

Then, the resulting .xml-file (which will be located in the directory with the script) is imported into the list of hosts in Zabbix.

That's all. I hope this tutorial will be at least useful to someone.

Source: https://habr.com/ru/post/168185/


All Articles