Monitoring service state in Zabbix by using Powershell script


In this article I will show you how to create a Powershell script that will discover certain services running on your Windows Servers. The script will then send service information (name and state) to my Zabbix monitoring server. You’ll see that the code is pretty simple to understand since it uses some basic Powershell commands.
I’ve used two user parameters keys to send data to Zabbix, one is used to populate host items while the other one sends the service state information:

#val parameter will be used to execute the script to either populate the items or retrieve values.
param([Int32]$val=0)


$services = get-service | Where-Object {$_.Name -like “IT*” } 
if ($val -eq “1”)
{
write-host “{“
write-host ” `”data`”:[“
write-host
}


foreach ($ser in $services)
{

    if ( $val -eq “1”)
    {

        $line = ” { `”{#SERVICENAME}`”:`”” + $ser.Name + “`” },”
        Write-Host $line
    }
    else
    {
        $computer = “$env:computername.$env:userdnsdomain”.ToLower()
        $line = “- getservice.state[`”” + $ser.Name + “`”] ” + $ser.Status
        write-host $line
    }
}


# Close the JSON message
if ($val -eq “1”)
{
write-host
write-host ” ]”
write-host “}”
write-host
}

Now modify the Zabbix configuration file (zabbix_agentd config) on each server and add the following lines:
UserParameter=getservice.name, powershell -NoProfile -ExecutionPolicy Bypass -file “C:\Program Files\Zabbix Agent\UserParameters\script\GetService.ps1” -val 1
UserParameter=getservice.status, powershell -NoProfile -ExecutionPolicy Bypass -file “C:\Program Files\Zabbix Agent\UserParameters\script\GetService.ps1” -val 0 | “C:\Program Files\Zabbix Agent\zabbix_sender.exe” -v -c “C:\Program Files\Zabbix Agent\zabbix_agentd.conf” -i –

Advertisements

2 thoughts on “Monitoring service state in Zabbix by using Powershell script

  1. Hi,

    I’m trying to implement this script in zabbix.
    However, i’ve noticed some strange codes:

    I would change the following:
    instead of only taking processes on drive d:\ I would exclude everything in c:\windows
    #Get the processes that run on the machine from a certain path
    $colItems = Get-Process | Where-Object {$_.Path -notlike “C:\windows\*” -and $_.path -ne $null}

    Next is how you type the JSON file, this is much easier: (haven’t checked what the minimum requirement is for convertTo-JSON

    if ( $keys -eq “1”){
    Write-host ($colitems | select-object @{Name=’#processname’; Expression={$_.Processname}}, @{Name=’#path’; Expression={$_.path}}, @{Name=’#id’; Expression={$_.id}} | ConvertTo-Json)

    Last

    can you shortly tell how to create the LLD in zabbix web interface, i’m having a hard time figuring it out.

    Like

    Reply
  2. Hello,
    You can try to use something like
    If process path not (get-process | Where-Object { $_.Name -like “procesname” }).Path do

    For the json from what i know you can use get-process | ConvertTo-Json and it will simply convert all values to Json.

    For the low level discovery you need to use the discovery rules in a template or host and use items/triggers probes. You need to specify a key that will be used by Zabbix server to execute the script and fetch in the values. The key is then added in the userparameter section of the config file on each of the client nodes.

    Like

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s