我正在尝试创建依赖于 PowerShell 脚本的模板,该脚本枚举远程 Windows 服务器上的服务。然后它应该为每个服务创建一个默认为禁用的项目,我可以在其中启用我想要监控的项目。我部署了以下脚本、配置更改和模板,但是没有任何项目填充。
################################################################################
# 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)}<>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>
答案1
get_services.ps1 未生成有效的 JSON。字典值需要用逗号分隔,但数组中的最后一项除外。更正后的 .ps1 文件如下:
################################################################################
# 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 "}"