zabbix监控windows DHCP server

一、准备在HDCP服务器上创建两个脚本,check_dhcpscop.ps1 和 check_dhcpscop_value.ps1 

》1、创建powershell"check_dhcpscop.ps1"

#获取DHCP作用域,作用域的name,id,state,PercentageInUse,InUse,Free 值
$scops = Get-DhcpServerv4Scope |select @{name="{#SCOPNAME}";expression={$_.Name.replace(' ','')}},@{name="ScopeId";expression={$_.ScopeID.IPAddressToString}},@{name="State";expression={$_.State}}
$scopstate = Get-DhcpServerv4ScopeStatistics |Select @{name="ScopeId";expression={$_.ScopeID.IPAddressToString}},@{name="PercentageInUse";expression={$_.PercentageInUse}},@{name="FREE";expression={$_.Free}},@{name="Use";expression={$_.InUse}}

$ScopsInfor = @()
foreach($i in $scops) {
  foreach($j in $scopstate) {  
	if ($i.ScopeId -eq $J.ScopeId) {	  
          $i | Add-Member -MemberType NoteProperty -Name 'PercentageInUse' -Value $j.PercentageInUse  -Force
          $i | Add-Member -MemberType NoteProperty -Name 'Free' -Value $j.FREE -Force
	      $i | Add-Member -MemberType NoteProperty -Name 'Use' -Value $j.Use -Force

          $ScopsInfor = $ScopsInfor + $i	
	}
  }  
}

$ScopsInfor = $ScopsInfor|ConvertTo-Json

@"
{"data":$ScopsInfor}
"@

$ScopsInfor | Out-File -FilePath C:\zabbix\script\dhcpinfor\dhcp.txt

》2、创建powershell“get_dhcpinfor.ps1”

#监控项调用此脚本,会传进两个参数
Param($scopname, $propname)
$infor = ''

#读取第一个脚本获取到的信息
$content = Get-Content "C:\zabbix\script\dhcpinfor\dhcp.txt"

foreach ($line in $content){
	$str = $line
	if ($str.contains('{#SCOPNAME}')){
		$line =$line.replace('{#SCOPNAME}','ScopName')		
	}
	$infor = $infor+ $line
}

$dhcpinfor = ConvertFrom-Json $infor
foreach ($i in $dhcpinfor){

	if($i.ScopName -eq $scopname){

		if ($propname -eq 'Free'){
			return $i.Free
		}
		if ($propname -eq 'State'){
			return $i.State
		}
		if ($propname -eq 'PercentageInUse'){
			return $i.PercentageInUse
		}
		if ($propname -eq 'Use'){
			return $i.Use
		}
	}
}

3》两个脚本都放在dhcp服务器的 c:\zabbix\script\

二、创建zabbix 模板,新记事本,复制内容,后缀为.yaml    hdcp.yamn,

模板下载:https://files.cnblogs.com/files/blogs/700793/dhcp.zip?t=1709887209&download=true

zabbix_export:
  version: '6.0'
  date: '2023-02-02T03:19:35Z'
  groups:
    -
      uuid: 846977d1dfed4968bc5f8bdb363285bc
      name: 'Templates/Operating systems'
  templates:
    -
      uuid: 76e0de58829f458285537935d85819d3
      template: 'TBI Windows DHCP Server by Zabbix agent'
      name: 'TBI Windows DHCP Server by Zabbix agent'
      description: |
        Official Windows template. Requires agent of Zabbix 4.4 and newer.
        
        
        You can discuss this template or leave feedback on our forum https://www.zabbix.com/forum/zabbix-suggestions-and-feedback/387224-discussion-thread-for-official-zabbix-template-for-windows
        
        Template tooling version used: 0.41
      groups:
        -
          name: 'Templates/Operating systems'
      items:
        -
          uuid: 4732f2defd2e49d7ab9dd75b212d0de3
          name: 'Zabbix agent ping'
          type: ZABBIX_ACTIVE
          key: agent.ping
          history: 7d
          description: 'The agent always returns 1 for this item. It could be used in combination with nodata() for availability check.'
          valuemap:
            name: 'Zabbix agent ping status'
          tags:
            -
              tag: component
              value: system
          triggers:
            -
              uuid: f1fc1bad92034af8a14aeaf56a125c84
              expression: 'nodata(/TBI Windows DHCP Server by Zabbix agent/agent.ping,{$AGENT.NODATA_TIMEOUT})=1'
              name: 'Zabbix agent is not available'
              event_name: 'Zabbix agent is not available (or nodata for {$AGENT.NODATA_TIMEOUT})'
              priority: AVERAGE
              description: 'For active agents, nodata() with agent.ping is used with {$AGENT.NODATA_TIMEOUT} as time threshold.'
              manual_close: 'YES'
              tags:
                -
                  tag: scope
                  value: availability
        -
          uuid: 9de081e14a0745a58d69ebaa4860d1c3
          name: Uptime
          type: ZABBIX_ACTIVE
          key: system.uptime
          delay: 30s
          history: 2w
          trends: 0d
          units: uptime
          description: 'System uptime in ''N days, hh:mm:ss'' format.'
          tags:
            -
              tag: component
              value: system
          triggers:
            -
              uuid: 05f93c476eef49658a165a06510150e2
              expression: 'last(/TBI Windows DHCP Server by Zabbix agent/system.uptime)<10m'
              name: 'Host has been restarted'
              event_name: 'Host has been restarted (uptime < 10m)'
              priority: WARNING
              description: 'The device uptime is less than 10 minutes.'
              manual_close: 'YES'
              tags:
                -
                  tag: scope
                  value: notice
      discovery_rules:
        -
          uuid: 12bfdc6715774ead881f595600cfcfed
          name: 'DHCP Scope Discovery'
          key: check_dhcpscop
          delay: 5m
          filter:
            conditions:
              -
                macro: '{#SCOPNAME}'
                formulaid: A
          item_prototypes:
            -
              uuid: e0d67fe51ce842c9a4a67cd625ce28f5
              name: 'DHCP Scop Free_{#SCOPNAME}'
              key: 'get_dhcpinfor[{#SCOPNAME},Free]'
              trends: '0'
              status: DISABLED
              discover: NO_DISCOVER
              value_type: TEXT
            -
              uuid: 04633f19dbe44a45b729785ce5d88955
              name: 'DHCP Scop PerCentageInUse_{#SCOPNAME}'
              key: 'get_dhcpinfor[{#SCOPNAME},PercentageInUse]'
              delay: 5m
              value_type: FLOAT
              preprocessing:
                -
                  type: MULTIPLIER
                  parameters:
                    - '1'
              trigger_prototypes:
                -
                  uuid: d3337b91cf8a45f7a47790e8da3a03d2
                  expression: 'avg(/TBI Windows DHCP Server by Zabbix agent/get_dhcpinfor[{#SCOPNAME},PercentageInUse],3m)>=95'
                  recovery_mode: RECOVERY_EXPRESSION
                  recovery_expression: 'avg(/TBI Windows DHCP Server by Zabbix agent/get_dhcpinfor[{#SCOPNAME},PercentageInUse],3m)<90'
                  name: '{#SCOPNAME} Scope  usage above 95%'
                  priority: AVERAGE
            -
              uuid: f0dc48a0680a4ff9be34941ff94e8dcb
              name: 'DHCP Scop State_{#SCOPNAME}'
              key: 'get_dhcpinfor[{#SCOPNAME},State]'
              delay: 5m
              trends: '0'
              value_type: TEXT
              trigger_prototypes:
                -
                  uuid: a9266d377a53464082adf219da51b121
                  expression: 'last(/TBI Windows DHCP Server by Zabbix agent/get_dhcpinfor[{#SCOPNAME},State])="Inactive"'
                  recovery_mode: RECOVERY_EXPRESSION
                  recovery_expression: 'last(/TBI Windows DHCP Server by Zabbix agent/get_dhcpinfor[{#SCOPNAME},State])="Active"'
                  name: '{#SCOPNAME} Scope Static Inactice'
                  priority: AVERAGE
            -
              uuid: b7b5de331d0748f58f0b24fe968dd165
              name: 'DHCP Scop Use_{#SCOPNAME}'
              key: 'get_dhcpinfor[{#SCOPNAME},Use]'
              trends: '0'
              status: DISABLED
              discover: NO_DISCOVER
              value_type: TEXT
          graph_prototypes:
            -
              uuid: 1e336fc86f53426fa4e840335869d57a
              name: 'Dhcp Scop PerCentageInUse {#SCOPNAME}'
              percent_left: '100'
              graph_items:
                -
                  color: 1A7C11
                  calc_fnc: ALL
                  item:
                    host: 'TBI Windows DHCP Server by Zabbix agent'
                    key: 'get_dhcpinfor[{#SCOPNAME},PercentageInUse]'
      tags:
        -
          tag: class
          value: os
        -
          tag: target
          value: windows
      macros:
        -
          macro: '{$AGENT.NODATA_TIMEOUT}'
          value: 30m
          description: 'No data timeout for active agents. Consider to keep it relatively high.'
        -
          macro: '{$CPU.INTERRUPT.CRIT.MAX}'
          value: '50'
          description: 'The critical threshold of the % Interrupt Time counter.'
        -
          macro: '{$CPU.PRIV.CRIT.MAX}'
          value: '30'
          description: 'The threshold of the % Privileged Time counter.'
        -
          macro: '{$CPU.QUEUE.CRIT.MAX}'
          value: '3'
          description: 'The threshold of the Processor Queue Length counter.'
        -
          macro: '{$CPU.UTIL.CRIT}'
          value: '90'
          description: 'The critical threshold of the CPU utilization in %.'
        -
          macro: '{$IF.ERRORS.WARN}'
          value: '2'
        -
          macro: '{$IF.UTIL.MAX}'
          value: '90'
        -
          macro: '{$IFCONTROL}'
          value: '1'
        -
          macro: '{$MEM.PAGE_SEC.CRIT.MAX}'
          value: '1000'
          description: 'The warning threshold of the Memory Pages/sec counter.'
        -
          macro: '{$MEM.PAGE_TABLE_CRIT.MIN}'
          value: '5000'
          description: 'The warning threshold of the Free System Page Table Entries counter.'
        -
          macro: '{$MEMORY.UTIL.MAX}'
          value: '90'
          description: 'The warning threshold of the Memory util item.'
        -
          macro: '{$NET.IF.IFALIAS.MATCHES}'
          value: '.*'
          description: 'This macro is used in Network interface discovery. Can be overridden on the host or linked template level.'
        -
          macro: '{$NET.IF.IFALIAS.NOT_MATCHES}'
          value: CHANGE_THIS
          description: 'This macro is used in Network interface discovery. Can be overridden on the host or linked template level.'
        -
          macro: '{$NET.IF.IFDESCR.MATCHES}'
          value: '.*'
          description: 'This macro is used in Network interface discovery. Can be overridden on the host or linked template level.'
        -
          macro: '{$NET.IF.IFDESCR.NOT_MATCHES}'
          value: CHANGE_THIS
          description: 'This macro is used in Network interface discovery. Can be overridden on the host or linked template level.'
        -
          macro: '{$NET.IF.IFNAME.MATCHES}'
          value: '.*'
          description: 'This macro is used in Network interface discovery. Can be overridden on the host or linked template level.'
        -
          macro: '{$NET.IF.IFNAME.NOT_MATCHES}'
          value: Miniport|Virtual|Teredo|Kernel|Loopback|Bluetooth|HTTPS|6to4|QoS|Layer
          description: 'This macro is used in Network interface discovery. Can be overridden on the host or linked template level.'
        -
          macro: '{$SERVICE.NAME.MATCHES}'
          value: '^.*$'
          description: 'This macro is used in Service discovery. Can be overridden on the host or linked template level.'
        -
          macro: '{$SERVICE.NAME.NOT_MATCHES}'
          value: '^(?:RemoteRegistry|MMCSS|gupdate|SysmonLog|clr_optimization_v.+|sppsvc|gpsvc|Pml Driver HPZ12|Net Driver HPZ12|MapsBroker|IntelAudioService|Intel\(R\) TPM Provisioning Service|dbupdate|DoSvc|CDPUserSvc_.+|WpnUserService_.+|OneSyncSvc_.+|WbioSrvc|BITS|tiledatamodelsvc|GISvc|ShellHWDetection|TrustedInstaller|TabletInputService|CDPSvc|wuauserv)$'
          description: 'This macro is used in Service discovery. Can be overridden on the host or linked template level.'
        -
          macro: '{$SERVICE.STARTUPNAME.MATCHES}'
          value: '^(?:automatic|automatic delayed)$'
          description: 'This macro is used in Service discovery. Can be overridden on the host or linked template level.'
        -
          macro: '{$SERVICE.STARTUPNAME.NOT_MATCHES}'
          value: '^(?:manual|disabled)$'
          description: 'This macro is used in Service discovery. Can be overridden on the host or linked template level.'
        -
          macro: '{$SWAP.PFREE.MIN.WARN}'
          value: '20'
          description: 'The warning threshold of the minimum free swap.'
        -
          macro: '{$SYSTEM.FUZZYTIME.MAX}'
          value: '60'
          description: 'The threshold for difference of system time in seconds.'
        -
          macro: '{$VFS.DEV.DEVNAME.MATCHES}'
          value: '.*'
          description: 'This macro is used in physical disks discovery. Can be overridden on the host or linked template level.'
        -
          macro: '{$VFS.DEV.DEVNAME.NOT_MATCHES}'
          value: _Total
          description: 'This macro is used in physical disks discovery. Can be overridden on the host or linked template level.'
        -
          macro: '{$VFS.DEV.READ.AWAIT.WARN}'
          value: '0.02'
          description: 'Disk read average response time (in s) before the trigger would fire.'
        -
          macro: '{$VFS.DEV.UTIL.MAX.WARN}'
          value: '95'
          description: 'The warning threshold of disk time utilization in percent.'
        -
          macro: '{$VFS.DEV.WRITE.AWAIT.WARN}'
          value: '0.02'
          description: 'Disk write average response time (in s) before the trigger would fire.'
        -
          macro: '{$VFS.FS.FREE.MIN.CRIT}'
          value: 5G
          description: 'The critical threshold of the filesystem utilization.'
        -
          macro: '{$VFS.FS.FREE.MIN.WARN}'
          value: 10G
          description: 'The warning threshold of the filesystem utilization.'
        -
          macro: '{$VFS.FS.FSDRIVETYPE.MATCHES}'
          value: fixed
          description: 'This macro is used in filesystems discovery. Can be overridden on the host or linked template level.'
        -
          macro: '{$VFS.FS.FSDRIVETYPE.NOT_MATCHES}'
          value: ^\s$
          description: 'This macro is used in filesystems discovery. Can be overridden on the host or linked template level.'
        -
          macro: '{$VFS.FS.FSNAME.MATCHES}'
          value: '.*'
          description: 'This macro is used in filesystems discovery. Can be overridden on the host or linked template level.'
        -
          macro: '{$VFS.FS.FSNAME.NOT_MATCHES}'
          value: '^(?:/dev|/sys|/run|/proc|.+/shm$)'
          description: 'This macro is used in filesystems discovery. Can be overridden on the host or linked template level.'
        -
          macro: '{$VFS.FS.FSTYPE.MATCHES}'
          value: '.*'
          description: 'This macro is used in filesystems discovery. Can be overridden on the host or linked template level.'
        -
          macro: '{$VFS.FS.FSTYPE.NOT_MATCHES}'
          value: ^\s$
          description: 'This macro is used in filesystems discovery. Can be overridden on the host or linked template level.'
        -
          macro: '{$VFS.FS.PUSED.MAX.CRIT}'
          value: '90'
          description: 'The critical threshold of the filesystem utilization in percent.'
        -
          macro: '{$VFS.FS.PUSED.MAX.WARN}'
          value: '80'
          description: 'The warning threshold of the filesystem utilization in percent.'
      dashboards:
        -
          uuid: a6148375a09e403193d2457a09a6d880
          name: 'Network interfaces'
          pages:
            -
              widgets:
                -
                  type: GRAPH_PROTOTYPE
                  width: '24'
                  height: '5'
                  fields:
                    -
                      type: INTEGER
                      name: source_type
                      value: '2'
                    -
                      type: INTEGER
                      name: columns
                      value: '1'
                    -
                      type: INTEGER
                      name: rows
                      value: '1'
        -
          uuid: 15f071590aff491db3e5af07ddcbf4ec
          name: 'System performance'
          pages:
            -
              widgets:
                -
                  type: GRAPH_CLASSIC
                  width: '12'
                  height: '5'
                  fields:
                    -
                      type: INTEGER
                      name: source_type
                      value: '0'
                -
                  type: GRAPH_CLASSIC
                  x: '12'
                  width: '12'
                  height: '5'
                  fields:
                    -
                      type: INTEGER
                      name: source_type
                      value: '1'
                -
                  type: GRAPH_CLASSIC
                  'y': '5'
                  width: '12'
                  height: '5'
                  fields:
                    -
                      type: INTEGER
                      name: source_type
                      value: '0'
                -
                  type: GRAPH_CLASSIC
                  x: '12'
                  'y': '5'
                  width: '12'
                  height: '5'
                  fields:
                    -
                      type: INTEGER
                      name: source_type
                      value: '0'
                -
                  type: GRAPH_PROTOTYPE
                  'y': '10'
                  width: '24'
                  height: '5'
                  fields:
                    -
                      type: INTEGER
                      name: source_type
                      value: '2'
                    -
                      type: INTEGER
                      name: columns
                      value: '1'
                    -
                      type: INTEGER
                      name: rows
                      value: '1'
                -
                  type: GRAPH_PROTOTYPE
                  'y': '15'
                  width: '24'
                  height: '5'
                  fields:
                    -
                      type: INTEGER
                      name: source_type
                      value: '2'
                    -
                      type: INTEGER
                      name: columns
                      value: '1'
                    -
                      type: INTEGER
                      name: rows
                      value: '1'
                -
                  type: GRAPH_PROTOTYPE
                  'y': '20'
                  width: '24'
                  height: '5'
                  fields:
                    -
                      type: INTEGER
                      name: source_type
                      value: '2'
                    -
                      type: INTEGER
                      name: columns
                      value: '1'
                    -
                      type: INTEGER
                      name: rows
                      value: '1'
                -
                  type: GRAPH_PROTOTYPE
                  'y': '25'
                  width: '24'
                  height: '5'
                  fields:
                    -
                      type: INTEGER
                      name: source_type
                      value: '2'
                    -
                      type: INTEGER
                      name: columns
                      value: '1'
                    -
                      type: INTEGER
                      name: rows
                      value: '1'
      valuemaps:
        -
          uuid: 8e51851734de459b897654b55e097ed9
          name: 'Win32_NetworkAdapter::AdapterTypeId'
          mappings:
            -
              value: '0'
              newvalue: 'Ethernet 802.3'
            -
              value: '1'
              newvalue: 'Token Ring 802.5'
            -
              value: '2'
              newvalue: 'Fiber Distributed Data Interface (FDDI)'
            -
              value: '3'
              newvalue: 'Wide Area Network (WAN)'
            -
              value: '4'
              newvalue: LocalTalk
            -
              value: '5'
              newvalue: 'Ethernet using DIX header format'
            -
              value: '6'
              newvalue: ARCNET
            -
              value: '7'
              newvalue: 'ARCNET (878.2)'
            -
              value: '8'
              newvalue: ATM
            -
              value: '9'
              newvalue: Wireless
            -
              value: '10'
              newvalue: 'Infrared Wireless'
            -
              value: '11'
              newvalue: Bpc
            -
              value: '12'
              newvalue: CoWan
            -
              value: '13'
              newvalue: '1394'
        -
          uuid: d1945b12b60241b59e1576f119fd4085
          name: 'Win32_NetworkAdapter::NetConnectionStatus'
          mappings:
            -
              value: '0'
              newvalue: Disconnected
            -
              value: '1'
              newvalue: Connecting
            -
              value: '2'
              newvalue: Connected
            -
              value: '3'
              newvalue: Disconnecting
            -
              value: '4'
              newvalue: 'Hardware Not Present'
            -
              value: '5'
              newvalue: 'Hardware Disabled'
            -
              value: '6'
              newvalue: 'Hardware Malfunction'
            -
              value: '7'
              newvalue: 'Media Disconnected'
            -
              value: '8'
              newvalue: Authenticating
            -
              value: '9'
              newvalue: 'Authentication Succeeded'
            -
              value: '10'
              newvalue: 'Authentication Failed'
            -
              value: '11'
              newvalue: 'Invalid Address'
            -
              value: '12'
              newvalue: 'Credentials Required'
        -
          uuid: 000cb16205a5446589dcbfaf400dc6ad
          name: 'Windows service state'
          mappings:
            -
              value: '0'
              newvalue: Running
            -
              value: '1'
              newvalue: Paused
            -
              value: '2'
              newvalue: 'Start pending'
            -
              value: '3'
              newvalue: 'Pause pending'
            -
              value: '4'
              newvalue: 'Continue pending'
            -
              value: '5'
              newvalue: 'Stop pending'
            -
              value: '6'
              newvalue: Stopped
            -
              value: '7'
              newvalue: Unknown
            -
              value: '255'
              newvalue: 'No such service'
        -
          uuid: 247752b40d1642369bb9f70ace9be325
          name: zabbix.host.available
          mappings:
            -
              value: '0'
              newvalue: 'not available'
            -
              value: '1'
              newvalue: available
            -
              value: '2'
              newvalue: unknown
        -
          uuid: beee6a48ce11416788566615ef129743
          name: 'Zabbix agent ping status'
          mappings:
            -
              value: '1'
              newvalue: Up

三、将dhcp.yaml模板导入到zabbix中

 三 、在DHCP服务器上的C盘的zabbix文件zabbix_agentd.conf 中自定义用户参数,在尾行添加这两行

UserParameter=check_dhcpscop,powershell c:\zabbix\script\check_dhcpscop.ps1
UserParameter=get_dhcpinfor[*],powershell c:\zabbix\script\get_dhcpinfor.ps1 $1 $2

注意:

zabbix的用户参数限制使用特殊字符和空格,假设你的作用域名字是Vlan 10(带有空格),则会出现传参错误。
这里在check_dhcpscop.ps1里做了修改,会将空格去除。

四、以管理员身份运行一下两个脚本check_dhcpscop.ps1 和 get_dhcpinfor.ps1,看是否有报错。

五、重启一下zabbix-agent服务

六、到zabbix平台可以看到我们的DHCP信息就上来了

 参考:

1:https://blog.csdn.net/qq_42906357/article/details/128846407

posted @ 2024-03-08 12:08  聆听城市喧哗  阅读(380)  评论(0编辑  收藏  举报