zabbix_export:
  version: '6.0'
  template_groups:
    - uuid: 7df96b18c230490a9a0a9e1e2e3f4g5h
      name: Templates/Applications
  templates:
    - uuid: 8ef07c29d3414a1bba1baf2f3f4g5h6j
      template: 'Hikvision NVR ISAPI by HTTP'
      name: 'Hikvision NVR ISAPI by HTTP'
      description: '通过海康威视 ISAPI 协议监控 NVR 的硬盘状态及摄像头在线状态。'
      groups:
        - name: Templates/Applications
      items:
        # 1. 硬盘主监控项
        - uuid: a1b2c3d4e5f6412381230123456789ab
          name: 'Hikvision: Get HDD Raw Data'
          type: HTTP_AGENT
          key: hikvision.hdd.raw
          delay: 5m
          history: '0'
          trends: '0'
          value_type: TEXT
          url: 'http://{$NVR_IP}/ISAPI/Storage/hdd'
          authtype: DIGEST
          username: '{$NVR_USER}'
          password: '{$NVR_PASS}'
          description: '获取 NVR 的所有硬盘原始 XML 数据。'
          
        # 2. 摄像头在线状态主监控项
        - uuid: b2c3d4e5f6a1412381230123456789cd
          name: 'Hikvision: Get Camera Status Raw Data'
          type: HTTP_AGENT
          key: hikvision.recording.raw
          delay: 5m
          history: '0'
          trends: '0'
          value_type: TEXT
          url: 'http://{$NVR_IP}/ISAPI/ContentMgmt/InputProxy/channels/status'
          authtype: DIGEST
          username: '{$NVR_USER}'
          password: '{$NVR_PASS}'
          description: '获取 NVR 下挂所有摄像头的在线/离线原始 XML 数据。'

      discovery_rules:
        # 1. 硬盘自动发现
        - uuid: c3d4e5f6a1b2412381230123456789ef
          name: 'HDD Discovery'
          type: DEPENDENT
          key: hikvision.hdd.discovery
          delay: '0'
          master_item:
            key: hikvision.hdd.raw
          preprocessing:
            - type: XML_TO_JSON
              parameters:
                - ''
            - type: JAVASCRIPT
              parameters:
                - |
                  var data = JSON.parse(value);
                  var hddList = data.hddList.hdd;
                  var result = [];
                  if (!Array.isArray(hddList)) { hddList = [hddList]; }
                  for (var i = 0; i < hddList.length; i++) {
                      result.push({
                          "{#HDD_ID}": hddList[i].id.toString(),
                          "{#HDD_NAME}": hddList[i].hddName.toString()
                      });
                  }
                  return JSON.stringify(result);
          item_prototypes:
            - uuid: d4e5f6a1b2c3412381230123456789aa
              name: 'HDD {#HDD_NAME} ({#HDD_ID}): Status'
              type: DEPENDENT
              key: 'hikvision.hdd.status[{#HDD_ID}]'
              delay: '0'
              master_item:
                key: hikvision.hdd.raw
              value_type: CHAR
              trends: '0'
              history: 7d
              preprocessing:
                - type: REGEX
                  parameters:
                    - '<id>{#HDD_ID}</id>[\s\S]*?<status>([a-zA-Z]+)</status>'
                    - '\1'
          trigger_prototypes:
            - uuid: e5f6a1b2c3d4412381230123456789bb
              name: 'HDD {#HDD_NAME} ({#HDD_ID}) 状态异常: {ITEM.VALUE}'
              expression: 'last(/Hikvision NVR ISAPI by HTTP/hikvision.hdd.status[{#HDD_ID}])<>"ok" and last(/Hikvision NVR ISAPI by HTTP/hikvision.hdd.status[{#HDD_ID}])<>"idle"'
              priority: HIGH
              description: '当硬盘状态既不是 ok（正在读写）也不是 idle（休眠/备用）时触发告警。'

        # 2. 摄像头在线状态自动发现
        - uuid: f6a1b2c3d4e5412381230123456789cc
          name: 'Camera Online Discovery'
          type: DEPENDENT
          key: hikvision.recording.discovery
          delay: '0'
          master_item:
            key: hikvision.recording.raw
          preprocessing:
            - type: XML_TO_JSON
              parameters:
                - ''
            - type: JAVASCRIPT
              parameters:
                - |
                  var data = JSON.parse(value);
                  var list = data.InputProxyChannelStatusList.InputProxyChannelStatus;
                  var result = [];
                  if (!Array.isArray(list)) { list = [list]; }
                  for (var i = 0; i < list.length; i++) {
                      result.push({ "{#CH_ID}": list[i].id.toString() });
                  }
                  return JSON.stringify(result);
          item_prototypes:
            - uuid: a1b2c3d4e5f6412381230123456789dd
              name: 'Channel {#CH_ID} Camera Online Status'
              type: DEPENDENT
              key: 'hikvision.recording.status[{#CH_ID}]'
              delay: '0'
              master_item:
                key: hikvision.recording.raw
              value_type: CHAR
              trends: '0'
              history: 7d
              preprocessing:
                - type: REGEX
                  parameters:
                    - '<id>{#CH_ID}</id>[\s\S]*?<online>([a-z]+)</online>'
                    - '\1'
          trigger_prototypes:
            - uuid: b2c3d4e5f6a1412381230123456789ee
              name: '通道 {#CH_ID} 摄像头断开掉线!'
              expression: 'last(/Hikvision NVR ISAPI by HTTP/hikvision.recording.status[{#CH_ID}])="false"'
              priority: HIGH
              description: '当前摄像头通道在线状态为 false 时触发掉线告警。'

      macros:
        - macro: '{$NVR_IP}'
          description: '海康威视 NVR 的局域网 IP 地址'
        - macro: '{$NVR_USER}'
          value: admin
          description: 'ISAPI 认证用户名'
        - macro: '{$NVR_PASS}'
          description: 'ISAPI 认证密码'