PowerShell host inventory script for Zabbix
Starting from Zabbix 2.0, there is an host inventory tab for every host you add to Zabbix.
By default, only the OS field get’s populated by System Information on a Windows host that’s being monitored using the Zabbix agent.
However, you can use PowerShell using WMI queries to fill the Host Inventory tab of your Windows hosts with more useful data.
The PowerShell script below can be used to populate the following fields in the Host Inventory tab:
- Name
- OS (Full Details)
- Serial Number A
- HW Architecture
- Vendor
You can of course, add more functions to the script to populate other fields.
#requires -version 3.0 #Get-HostInventory.ps1 <# **************************************************************** * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED * * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF * * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, * * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. * **************************************************************** #> <# .Synopsis Query hardware information from the server using WMI queries to use in external application (for example, Zabbix) .Description This script uses functions to query certain information from the Operating System and removes any formatting, so that only the values will be displayed DETAILS This script uses Get-WmiObject, to make it compattible with PowerShell v2 machines. Every command is written as a function to be used as user parameters for Zabbix to query. The userparameter in your Zabbix agent configuration file (zabbix_agentd.conf) should be something like this: UserParameter=HostInventory_[*],PowerShell.exe -nologo -command "& { . C:ZabbixGet-HostInventory.ps1; Get-$1 }" Make sure the path to the script matches the location you have store the script. (I've used the C:Zabbix folder in this example) .Notes Version 2.0 Last Updated July 31, 2014 Marco Janse https://www.ictstuff.info #> function Get-ComputerName { Get-WmiObject Win32_OperatingSystem | Select-Object -ExpandProperty PSComputername } function Get-OsFullDetails { Get-WmiObject Win32_OperatingSystem | Select-Object -ExpandProperty Caption } function Get-HWArchitecture { Get-WmiObject Win32_OperatingSystem | Select-Object -ExpandProperty OSArchitecture } function Get-Vendor { Get-WmiObject Win32_BIOS | Select-Object -ExpandProperty Manufacturer } function Get-SerialNumberA { Get-WmiObject Win32_BIOS | Select-Object -ExpandProperty SerialNumber } #end of script
Note: I have used Get-WmiObject in favour of the newer and better Get-CimInstance, because we still have servers running PowerShell version 2.0. Feel free however to change this if all your servers are running PowerShell v3 or higher.
Save the scrip as Get-HostInventory.ps1 and save it on the Windows server(s) where you want to use this. I store them in C:Zabbix, along with the agent.
Next, we need to add a userparameter to the Zabbix agent configuration file in order for Zabbix to use these PowerShell functions. This config file is usually named zabbix_agentd.conf and in my case it’s located in C:Zabbix.
Assuming your Zabbix folder is C:Zabbix and the script is stored in the same directory with the name Get-HostInventory.ps1, the userparameter line should look like this:
UserParameter=HostInventory_[*],PowerShell.exe -nologo -command "& { . C:\ZabbixGet-HostInventory.ps1; Get-$1 }"
Restart the Zabbix agent for the change to take effect.
Make sure that the execution policy of PowerShell is set to RemoteSigned, otherwise, the script won’t be able to run.
To set the execution policy in PowerShell, open PowerShell in an elevated prompt and type the following oneliner:
Set-ExecutionPolicy RemoteSigned
To test the script using Zabbix server, you can use the Zabbix_get command. For example:
zabbix_get -s <IPaddress or fqdn> -p 10050 -k HostInventory_[ComputerName]
Where <IPaddress or fqdn> should be replaced by your server IP or fqdn.
If everything is working, we can start adding the querying the objects in Zabbix.
First thing you should do is set the Host Inventory to automatic on your host. By default, host inventory is disabled on a per-host base. I prefer to do a mass update of all Windows Host and set this to automatic. Here’s how:
- Login to your Zabbix FrontEnd environment
- Select all hosts you want to change and select Mass Update
- Switch to the Inventory tab and check the Inventory mode checkbox. Use the dropdown to set it to Automatic.
- Keep all other settings as they are and choose Update.
Here’s a graphical example:
Now we can start creating the items in Zabbix and link them to hostinventory lines, using the Zabbix frontend.
I prefer to use a Template for Hostinventory, so I can add all hosts that use the script and manage the items centrally. Feel free to create Host items if you like. Here’s how to create the items using a new template:
- Login to your Zabbix FrontEnd environment
- Go to Configuration-Templates and choose Create Template.
- Give your template a name. I’ve named it Template OS Windows Host Inventory
- Fill in the following details:
- Name: For example: HostInventory – ComputerName
- Type: Zabbix Agent
- Key: In this case, it should map to the ComputerName item as follows: HostInventory_[ComputerName]
- Type of information: Text
- Update Interval: I used 1 day as an interval, you don’t need to query these values too frequently. Value for 1 day is 86400.
- History storage period (in days): I’ve set this to 7 days.
- Application: I’ve created a new application call Host Inventory for easy sorting.
- Populates host inventory field: map this to the right field in the Host Inventory tab, in this case Name
- Save your Item and repeat for the other items in the script, which are:
- HostInventory_[HWArchitecture]
- HostInventory_[SerialNumberA]
- HostInventory_[Vendor]
- HostInventory_[OSFullDetails]
- Finally, link your template to the hosts you have configured with the hostinventory script. Items should start getting populated within the day.
Note: You could set the Update interval very short at first to test this script and start filling the host inventory fields quickly. Once they’re filled, I recommend to set the update interval of these kind of items to once a day.
Here’s a screenprint of a Host inventory item as an graphical example:
I hope you’ll find this post useful and can use this in your own Zabbix environment 🙂
La2Walker
You’ll need an API user with write permissions for the hosts. If you run the script periodically from cron, you won’t have to think about the inv. mode ever again
EGAIS
You’ll need an API user with write permissions for the hosts. If you run the script periodically from cron, you won’t have to think about the inv. mode ever again
DeklarantAlco
I do already have automatic installation and configuration of Zabbix monitoring, the inventory is the next step to make it perfect.
John Maxwell Gomes
Hello Macro,
I have followed your instruction. Could not get data in the inventory field.
Power shell script location C:\zabbix\Get-HostInventory.ps1
The eeor shows in the field is
. : The term ‘C:ZabbixGet-HostInventory.ps1’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.At line:1
Though i have changed the line in zabbix.agent.conf like the following
UserParameter=HostInventory_[*],PowerShell.exe -nologo -command “& { . C:zabbixGet-HostInventory.ps1; Get-$1 }”
the path starts with ‘z’
admin
Hi John,
I’ve notices there’s a typo in the Userparameter line:
Old line:
UserParameter=HostInventory_[*],PowerShell.exe -nologo -command "& { . C:ZabbixGet-HostInventory.ps1; Get-$1 }"
New line:
UserParameter=HostInventory_[*],PowerShell.exe -nologo -command "& { . C:\Zabbix\Get-HostInventory.ps1; Get-$1 }"
There’s a backslash missing in the script.
You should be able to check the script by opening a Command prompt and typing the following for example.
PowerShell.exe -nologo -command "& { . C:\Zabbix\Get-HostInventory.ps1; Get-ComputerName }"
Of course, both the Get-HostInfo script and the Zabbix agent files should be in C:\Zabbix, or you have to change accordingly
Let me know if this helps.
Regards,
Marco
John Maxwell Gomes
It works now.
Thank you.