Windows-Server-2008

Zabbix Agent 無法使用遠端腳本發現服務

  • November 20, 2017

我正在嘗試創建依賴於 PowerShell 腳本的模板,該腳本列舉遠端 Window 伺服器上的服務。然後,它應該為預設為禁用的每個服務創建一個項目,我可以去那裡啟用我想要監控的項目。我部署了以下腳本、配置更改和模板,但是沒有填充任何項目。

################################################################################
# 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>

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 "}"

引用自:https://serverfault.com/questions/884340