zabbix_export:
  version: '7.2'
  template_groups:
    - uuid: 2d7fe132d91747ffa509a1690176b0e6
      name: Local/Modules
  templates:
    - uuid: d8710e98468648dca73526c9e114f5ae
      template: '- D-Link sysObjectID-specific hardware monitoring by SNMP'
      name: '- D-Link sysObjectID-specific hardware monitoring by SNMP'
      description: |
        Hardware monitoring (CPU and memory usage) for D-Link devices which have model-specific SNMP OIDs for that purpose (in some cases the whole vendor-specific OID tree is under the OID reported in SNMPv2-MIB::sysObjectID.0).
        
        Generic MIBs used:
        SNMPv2-MIB (for getting sysObjectID.0)
        
        Model-specific MIBs used:
        1.3.6.1.4.1.171.10.76.31.1 DGS-1210-20ME-AX
        1.3.6.1.4.1.171.10.166.* DGS-1210-*/FL
      groups:
        - name: Local/Modules
      discovery_rules:
        - uuid: 538d9c8a51b54c20a1d402ae42e30865
          name: 'Hardware monitoring discovery'
          type: SNMP_AGENT
          snmp_oid: 'get[1.3.6.1.2.1.1.2.0]'
          key: system.hw.hwmon.discovery
          delay: '{$HWMON.DISCOVERY.INTERVAL}'
          description: 'Discover hardware monitoring items based on the system object ID (SNMPv2-MIB::sysObjectID.0).'
          item_prototypes:
            - uuid: de6ec885d22f4e93bd137179dcbc2d83
              name: '{#ENT_NAME}: CPU utilization'
              type: SNMP_AGENT
              snmp_oid: '{#CPU.OID}'
              key: 'system.cpu.util[{#CPU.ARG}]'
              delay: '{$HWMON.INTERVAL}'
              units: '%'
              description: 'CPU utilization percentage.'
              tags:
                - tag: component
                  value: cpu
              trigger_prototypes:
                - uuid: b3a99ff09a24483dbf99c9802f703b31
                  expression: 'min(/- D-Link sysObjectID-specific hardware monitoring by SNMP/system.cpu.util[{#CPU.ARG}],5m)>{$CPU.UTIL.CRIT:"{#ENT_NAME}"}'
                  name: '{#ENT_NAME}: High CPU utilization'
                  event_name: 'High CPU utilization on {HOST.NAME} ({#ENT_NAME}: over {$CPU.UTIL.CRIT:"{#ENT_NAME}"}% for 5m)'
                  opdata: 'Current utilization: {ITEM.LASTVALUE1}'
                  priority: WARNING
                  description: 'The CPU utilization is too high. The system might be slow to respond.'
                  tags:
                    - tag: scope
                      value: performance
            - uuid: 5beb65e9d3814ddda0de98f6f40bb21e
              name: '{#ENT_NAME}: Memory utilization'
              type: SNMP_AGENT
              snmp_oid: '{#MEM.OID}'
              key: 'vm.memory.util[{#MEM.ARG}]'
              delay: '{$HWMON.INTERVAL}'
              units: '%'
              description: 'Memory utilization percentage.'
              tags:
                - tag: component
                  value: memory
              trigger_prototypes:
                - uuid: 5e473bbfa1c8465fb337584fa5505c90
                  expression: 'min(/- D-Link sysObjectID-specific hardware monitoring by SNMP/vm.memory.util[{#MEM.ARG}],5m)>{$MEMORY.UTIL.MAX:"{#ENT_NAME}"}'
                  name: '{#ENT_NAME}: High memory utilization'
                  event_name: 'High memory utilization on {HOST.NAME} ({#ENT_NAME}: >{$MEMORY.UTIL.MAX:"{#ENT_NAME}"}% for 5m)'
                  opdata: 'Current utilization: {ITEM.LASTVALUE1}'
                  priority: WARNING
                  description: 'The system is running out of free memory.'
                  tags:
                    - tag: scope
                      value: capacity
                    - tag: scope
                      value: performance
          graph_prototypes:
            - uuid: c32261f70223453e886f74cd524e62ee
              name: '{#ENT_NAME}: CPU utilization'
              ymin_type_1: FIXED
              ymax_type_1: FIXED
              graph_items:
                - drawtype: GRADIENT_LINE
                  color: 199C0D
                  item:
                    host: '- D-Link sysObjectID-specific hardware monitoring by SNMP'
                    key: 'system.cpu.util[{#CPU.ARG}]'
            - uuid: 8cb33fac89ab4cf5a88c86dc01515f54
              name: '{#ENT_NAME}: Memory utilization'
              ymin_type_1: FIXED
              ymax_type_1: FIXED
              graph_items:
                - drawtype: GRADIENT_LINE
                  color: 199C0D
                  item:
                    host: '- D-Link sysObjectID-specific hardware monitoring by SNMP'
                    key: 'vm.memory.util[{#MEM.ARG}]'
          preprocessing:
            - type: REGEX
              parameters:
                - '^\.?(.*)$'
                - \1
            - type: STR_REPLACE
              parameters:
                - 'SNMPv2-SMI::enterprises.'
                - 1.3.6.1.4.1.
            - type: REGEX
              parameters:
                - '^.*$'
                - '<use_5min_averages={{$HWMON.DLINK.USE_5MIN_AVERAGES}.btoa()}>\0'
            - type: JAVASCRIPT
              parameters:
                - |
                  // Default values for the script parameters.
                  var params = {
                  	"use_5min_averages": false,
                  }
                  
                  // Decode parameters which are prepended to the actual value.
                  // Inserting user macro references directly into the JavaScript code does not
                  // work reliably when those macros are actually undefined (ZBX-26298),
                  // therefore the parameter values are prepended using “Regular expression”
                  // preprocessing items (with one item per macro, because ZBX-26298 affects
                  // the macro expansion in those items too).
                  params = (function(params) {
                  	var regex = /^<([^=]*)=([^>]*)>/;
                  	for (var match; (match = value.match(regex)); ) {
                  		var param_value = match[2];
                  		if (!param_value.startsWith('{')) {
                  			var param_key = match[1];
                  			var decoded = new TextDecoder().decode(atob(param_value));
                  			var old_type = typeof params[param_key];
                  			if (old_type == "boolean") 
                  				params[match[1]] = decoded && (decoded != "0");
                  			else
                  				params[match[1]] = decoded;
                  		}
                  		value = value.substring(match[0].length);
                  	}
                  	return params;
                  })(params);
                  
                  var device_infos = [
                  	{
                  		"devices": {
                  			"1.3.6.1.4.1.171.10.76.31.1":	"DGS-1210-20/ME Ax",
                  		},
                  		"cpu_1min": { "oid": "1.3.6.1.4.1.171.10.76.31.1.100.1.2", "name": "agentCPUutilizationIn1min" },
                  		"cpu_5min": { "oid": "1.3.6.1.4.1.171.10.76.31.1.100.1.3", "name": "agentCPUutilizationIn5min" },
                  		"mem_1min": { "oid": "1.3.6.1.4.1.171.10.76.31.1.100.2.2", "name": "agentMEMutilizationIn1min" },
                  		"mem_5min": { "oid": "1.3.6.1.4.1.171.10.76.31.1.100.2.3", "name": "agentMEMutilizationIn5min" },
                  	},
                  	{
                  		"devices": {
                  			"1.3.6.1.4.1.171.10.166.1":		"DGS-1210-10P/FL",
                  			"1.3.6.1.4.1.171.10.166.1.1":	"DGS-1210-10P/FL Rx",
                  			"1.3.6.1.4.1.171.10.166.2":		"DGS-1210-10MP/FL",
                  			"1.3.6.1.4.1.171.10.166.2.1":	"DGS-1210-10MP/FL Rx",
                  			"1.3.6.1.4.1.171.10.166.3":		"DGS-1210-10/FL",
                  			"1.3.6.1.4.1.171.10.166.3.1":	"DGS-1210-10/FL Rx",
                  			"1.3.6.1.4.1.171.10.166.4":		"DGS-1210-20/FL",
                  			"1.3.6.1.4.1.171.10.166.4.1":	"DGS-1210-20/FL Rx",
                  			"1.3.6.1.4.1.171.10.166.5":		"DGS-1210-26/FL",
                  			"1.3.6.1.4.1.171.10.166.5.1":	"DGS-1210-26/FL Rx",
                  			"1.3.6.1.4.1.171.10.166.6":		"DGS-1210-28/FL",
                  			"1.3.6.1.4.1.171.10.166.6.1":	"DGS-1210-28/FL Rx",
                  			"1.3.6.1.4.1.171.10.166.7":		"DGS-1210-28P/FL",
                  			"1.3.6.1.4.1.171.10.166.7.1":	"DGS-1210-28P/FL Rx",
                  			"1.3.6.1.4.1.171.10.166.8":		"DGS-1210-28MP/FL",
                  			"1.3.6.1.4.1.171.10.166.8.1":	"DGS-1210-28MP/FL Rx",
                  			"1.3.6.1.4.1.171.10.166.9":		"DGS-1210-52/FL",
                  			"1.3.6.1.4.1.171.10.166.9.1":	"DGS-1210-52/FL Rx",
                  			"1.3.6.1.4.1.171.10.166.10":	"DGS-1210-52MP/FL",
                  			"1.3.6.1.4.1.171.10.166.10.1":	"DGS-1210-52MP/FL Rx",
                  		},
                  		"cpu_1min": { "oid": "1.3.6.1.4.1.171.11.166.1000.1.30.2", "name": "cpuLast1MinUsage" },
                  		"cpu_5min": { "oid": "1.3.6.1.4.1.171.11.166.1000.1.30.3", "name": "cpuLast5MinUsage" },
                  		"mem_1min": { "oid": "1.3.6.1.4.1.171.11.166.1000.1.31.2", "name": "ramLast1MinUsage" },
                  		"mem_5min": { "oid": "1.3.6.1.4.1.171.11.166.1000.1.31.3", "name": "ramLast5MinUsage" },
                  	},
                  ];
                  
                  function lld_macro_name(name) {
                  	return "{#" + name.toUpperCase() + "}";
                  }
                  
                  var device_info = (function(sys_oid) {
                  	for (var i = 0, n = device_infos.length; i < n; ++i) {
                  		var info = device_infos[i];
                  		var dev_name = info.devices[sys_oid];
                  		if (dev_name) {
                  			var result = {};
                  			result["{#ENT_NAME}"] = dev_name;
                  			var add_oid = function(param_base) {
                  				var param_name = param_base + (params.use_5min_averages ? "_5min" : "_1min");
                  				var param_info = info[param_name];
                  				if (param_info) {
                  					result[lld_macro_name(param_base + ".arg")] = param_info.name + ".0";
                  					result[lld_macro_name(param_base + ".oid")] = param_info.oid + ".0";
                  				}
                  			};
                  			add_oid("cpu");
                  			add_oid("mem");
                  			return result;
                  		}
                  	}
                  	return null;
                  })(value);
                  
                  if (!device_info) {
                  	throw "Unsupported sysObjectID.0: " + value;
                  }
                  
                  return JSON.stringify([device_info]);
      tags:
        - tag: target
          value: dlink
      macros:
        - macro: '{$CPU.UTIL.CRIT}'
          value: '90'
          description: 'Problem threshold for the "High CPU utilization" trigger in %.'
        - macro: '{$CPU.UTIL.CRIT.RECOVERY}'
          value: '80'
          description: 'Recovery threshold for the "High CPU utilization" trigger in %.'
        - macro: '{$HWMON.DISCOVERY.INTERVAL}'
          value: 12h
          description: 'Update interval for hardware monitoring discovery.'
        - macro: '{$HWMON.DLINK.USE_5MIN_AVERAGES}'
          value: '0'
          description: 'Set to 1 if you want to use CPU and memory utilization values averaged over 5 minute intervals instead of 1 minute intervals.'
        - macro: '{$HWMON.INTERVAL}'
          value: 0;m/1s0
          description: 'Update interval for hardware monitoring SNMP items.'
        - macro: '{$MEMORY.UTIL.MAX}'
          value: '90'
          description: 'Problem threshold for the "High memory utilization" trigger in %.'
        - macro: '{$MEMORY.UTIL.MAX.RECOVERY}'
          value: '80'
          description: 'Recovery threshold for the "High memory utilization" trigger in %.'
