Hi,
I had a unique requirement where I had to do deduplication based on substring of a whole message but after dedup want to preserver the full message.
For example if the alarm message formed is like "VARBIND1=<varbind-value1> VARBIND2=<varbind-value2> VARBIND3=<varbind-value3>" I want the full message to be saved in Zabbix item value history but want Dedup on part of the message "VARBIND1=<varbind-value1> VARBIND3=<varbind-value3>".
There is no out of the box solution for this in Zabbix, so I used JavaScript Item Preprocessing to achieve this. The following are the details of my implementation:
Created two Preprocessing steps, first step is to extract VARBIND values from a SNMP Trap received and form a alarm message, and 2nd step is to perform Dedup on part of the message:

Step#1: "Regular expression":
I had a unique requirement where I had to do deduplication based on substring of a whole message but after dedup want to preserver the full message.
For example if the alarm message formed is like "VARBIND1=<varbind-value1> VARBIND2=<varbind-value2> VARBIND3=<varbind-value3>" I want the full message to be saved in Zabbix item value history but want Dedup on part of the message "VARBIND1=<varbind-value1> VARBIND3=<varbind-value3>".
There is no out of the box solution for this in Zabbix, so I used JavaScript Item Preprocessing to achieve this. The following are the details of my implementation:
Created two Preprocessing steps, first step is to extract VARBIND values from a SNMP Trap received and form a alarm message, and 2nd step is to perform Dedup on part of the message:
Step#1: "Regular expression":
- Regular Expression: Timeticks.*\n.*\n.*value=STRING: ([^\n]+)\n.*value=STRING: ([^\n]+)\n.*value=Timeticks: ([^\n]+)
- Parameters: VARBIND1=\2 VARBIND2=\1 VARBIND3=\3
Code:
//API token key
var auth = '9fb3099486f8f574fe02664631b5e8975b2b3bee6e54c7179 d3ddaf91e2a5f86';
//API URL
var url = 'http://10.133.19.250/zabbix/api_jsonrpc.php';
function getApiData() {[INDENT]var req = new HttpRequest();
req.addHeader('Content-Type: application/json-rpc');
req.addHeader('Authorization: Bearer 9fb3099486f8f574fe02664631b5e8975b2b3bee6e54c7179d 3ddaf91e2a5f86');
var data = '{"jsonrpc": "2.0", "method": "item.get", "params": { "output": ["lastvalue", "lastclock"], "host": "linpubaa012.gl.test.com", "itemids":["55208"] }, "id": 1 }';
resp = req.post(url , data);
if (req.getStatus() != 200) {[/INDENT][INDENT=2]throw new Error('API request failed: ' + req.getStatus());[/INDENT][INDENT]} else {[/INDENT][INDENT=2]return JSON.parse(resp);[/INDENT][INDENT]}[/INDENT]
}
try {[INDENT]//fetch Item lastvalue and lastclock using Zabbix API[/INDENT][INDENT]var tmp = getApiData();
//compare lastvalue and current value
if (tmp != null && tmp.result !=null && tmp.result[0].lastvalue != "") {[/INDENT][INDENT=2]var t1 = JSON.stringify(tmp.result[0].lastvalue).replace(/\\/g,'');
t1 = t1.substring(1, t1.length-1);
//remove VARBIND2 value for Dedup
c1 = t1.replace(/VARBIND2.*VARBIND3/,'VARBIND3');
c2 = value.replace(/VARBIND2.*VARBIND3/,'VARBIND3');
//add 5 minute dedup interval
var e1 = new Date(0);
e1.setUTCSeconds(parseInt(tmp.result[0].lastclock));
e1.setMinutes(e1.getMinutes() + 5);
e2 = new Date();
//if the value not changed in last 5 minutes then discard the value
if(e2 < e1 && c1 == c2) {[/INDENT][INDENT=3]return null;[/INDENT][INDENT=2]}[/INDENT][INDENT]}
//return the new Item value
return value;[/INDENT]
} catch (error) {[INDENT]return error.message;[/INDENT]
}