0

I'm trying to create template that relies on PowerShell scripts which enumerates the services on a remote Window server. It should then create an item for each service that defaults to disabled where I can go and enable the items that I want monitored. I have the below scripts, config changes, and template deployed, however no items ever populate.

################################################################################
# get_services.ps1
#
################################################################################

$services = Get-Service | select name

Write-Host "{"
Write-Host " `"data`":[`n"
ForEach($s in $services) {
    $line= "{ `"{#SERVICE}`" : `"" + $s.Name + "`" }"
    Write-Host $line
}
Write-Host
Write-Host " ]"
Write-Host "}"

################################################################################
# get_servicestate.ps1
#
################################################################################

Param ([String] $name = 0)
$service = Get-Service -Name $name
Write-Host $service.Status

### Option: UserParameter
#   User-defined parameter to monitor. There can be several user-defined parameters.
#   Format: UserParameter=<key>,<shell command>
#
# Mandatory: no
# Default:
# UserParameter=
UserParameter=org.service.discovery,powershell -NoProfile -ExecutionPolicy Bypass -File "S:\Zabbix\templates\Zabbix_Services_monitoring\get_services.ps1"
UserParameter=org.service.state[*],powershell -NoProfile -ExecutionPolicy Bypass -File S:\Zabbix\templates\Zabbix_Services_monitoring\get_servicestate.ps1 "$1"

<?xml version="1.0" encoding="UTF-8"?>
<zabbix_export>
    <version>3.0</version>
    <date>2017-11-20T16:44:57Z</date>
    <groups>
        <group>
            <name>
                    Templates
                </name>
        </group>
    </groups>
    <templates>
        <template>
            <template>Template Microsoft Services</template>
            <name>Template Microsoft Services</name>
            <description/>
            <groups>
                <group>
                    <name>
                    Templates
                </name>
                </group>
            </groups>
            <applications>
                <application>
                    <name>Windows Services</name>
                </application>
            </applications>
            <items/>
            <discovery_rules>
                <discovery_rule>
                    <name>Windows Service</name>
                    <type>0</type>
                    <snmp_community/>
                    <snmp_oid/>
                    <key>org.service.discovery</key>
                    <delay>30</delay>
                    <status>1</status>
                    <allowed_hosts/>
                    <snmpv3_contextname/>
                    <snmpv3_securityname/>
                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
                    <snmpv3_authprotocol>0</snmpv3_authprotocol>
                    <snmpv3_authpassphrase/>
                    <snmpv3_privprotocol>0</snmpv3_privprotocol>
                    <snmpv3_privpassphrase/>
                    <delay_flex/>
                    <params/>
                    <ipmi_sensor/>
                    <authtype>0</authtype>
                    <username/>
                    <password/>
                    <publickey/>
                    <privatekey/>
                    <port/>
                    <filter>
                        <evaltype>0</evaltype>
                        <formula/>
                        <conditions/>
                    </filter>
                    <lifetime>30</lifetime>
                    <description/>
                    <item_prototypes>
                        <item_prototype>
                            <name>Windows Service {#SERVICE}</name>
                            <type>0</type>
                            <snmp_community/>
                            <multiplier>0</multiplier>
                            <snmp_oid/>
                            <key>org.service.state[{#SERVICE}]</key>
                            <delay>30</delay>
                            <history>90</history>
                            <trends>0</trends>
                            <status>0</status>
                            <value_type>4</value_type>
                            <allowed_hosts/>
                            <units/>
                            <delta>0</delta>
                            <snmpv3_contextname/>
                            <snmpv3_securityname/>
                            <snmpv3_securitylevel>0</snmpv3_securitylevel>
                            <snmpv3_authprotocol>0</snmpv3_authprotocol>
                            <snmpv3_authpassphrase/>
                            <snmpv3_privprotocol>0</snmpv3_privprotocol>
                            <snmpv3_privpassphrase/>
                            <formula>1</formula>
                            <delay_flex/>
                            <params/>
                            <ipmi_sensor/>
                            <data_type>0</data_type>
                            <authtype>0</authtype>
                            <username/>
                            <password/>
                            <publickey/>
                            <privatekey/>
                            <port/>
                            <description/>
                            <inventory_link>0</inventory_link>
                            <applications>
                                <application>
                                    <name>Windows Services</name>
                                </application>
                            </applications>
                            <valuemap/>
                            <logtimefmt/>
                            <application_prototypes/>
                        </item_prototype>
                    </item_prototypes>
                    <trigger_prototypes>
                        <trigger_prototype>
                            <expression>{Template Microsoft Services:org.service.state[{#SERVICE}].str(Stopped)}&lt;&gt;0</expression>
                            <name>Windows Service {#SERVICE} is down</name>
                            <url/>
                            <status>0</status>
                            <priority>4</priority>
                            <description/>
                            <type>0</type>
                            <dependencies/>
                        </trigger_prototype>
                    </trigger_prototypes>
                    <graph_prototypes/>
                    <host_prototypes/>
                </discovery_rule>
            </discovery_rules>
            <macros/>
            <templates/>
            <screens/>
        </template>
    </templates>
</zabbix_export>
0

get_services.ps1 does not generate valid JSON. Dictionary values need to be sepearted by a comma with the exception of the last item in the array. The corrected .ps1 file is below:

################################################################################
# get_services.ps1
#
################################################################################

$services = Get-Service | select name
$idx = 1

Write-Host "{"
Write-Host " `"data`":[`n"
ForEach($s in $services) {

    if ($idx -lt $services.count)
    {
        $line= "{ `"{#SERVICE}`" : `"" + $s.Name + "`" },"
        write-host $line
    }
    elseif ($idx -ge $services.count)
    {
        $line= "{ `"{#SERVICE}`" : `"" + $s.Name + "`" }"
        write-host $line
    }

    $idx++;

}
Write-Host
Write-Host " ]"
Write-Host "}"
2
  • Even with the valid JSON the services do not enumerate.
    – Kirkland
    Nov 20 '17 at 20:37
  • The template was disabled in addition to the JSON issue. It is now working.
    – Kirkland
    Nov 20 '17 at 20:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.