Skip to content

Instantly share code, notes, and snippets.

@vkushnir
Last active October 7, 2022 06:38
Show Gist options
  • Save vkushnir/f573e9fe5a9a5a48456936556075b2ca to your computer and use it in GitHub Desktop.
Save vkushnir/f573e9fe5a9a5a48456936556075b2ca to your computer and use it in GitHub Desktop.
Zabbix JavaScript Preprocessing

Zabbix JavaScript Preprocessing

json_trim1.js

Trim all values for key "{#LLD}"

json_trim2.js

Trim all values

json_uniq1.js

Remove all dublicates form json with same values for key "{#SERIAL}"

list2json1.js

Input: "www.google.com::443, :1.1.1.1:443,localhost:127.0.0.1:" Output:

[
  {"{#NAME}": "www.google.com:443", "{#HOST}": "www.google.com", "{#IP}": "", "{#PORT}": "443"},
  {"{#NAME}": "1.1.1.1:443", "{#HOST}": "", "{#IP}": "1.1.1.1", "{#PORT}": "443"},
  {"{#NAME}": "localhost:127.0.0.1", "{#HOST}": "localhost", "{#IP}": "127.0.0.1", "{#PORT}": ""}
]

list2json2.js

Input: "Сервер Google:www.google.com:443, Сервер CloudFlare:1.1.1.1:443, Локалхост:127.0.0.1:`" Output:

[
  {"{#ID}": "www.google.com:443", "{#NAME}": "Сервер Google", "{#HOST}": "www.google.com", "{#PORT}": "443"},
  {"{#ID}": "1.1.1.1:443", "{#NAME}": "Сервер CloudFlare", "{#HOST}": "1.1.1.1", "{#PORT}": "443"},
  {"{#ID}": "127.0.0.1", "{#NAME}": "Локалхост", "{#HOST}": "127.0.0.1", "{#PORT}": ""}
]

list2json3.js

Input: "/var/log;/etc/zabbix;/opt" Output:

[
  {"{#PATH}": "/var/log"},
  {"{#PATH}": "/etc/zabbix"},
  {"{#PATH}": "/opt"}
]

snmp_adminstring2json1.js

Converts all json {#SNMPINDEX} for example "5.84.69.83.84.50.4.105.99.109.112" to {"{#CTRLINDEX}": "TEST2"}, {"{#CTRLNAME}": "icmp"}.

snmp_hex2json1.js

Converts all json {#NAME} for example D0 9C D0 BE D0 B4 D1 83 D0 BB D1 8C 20 D1 81 D0 B1 D0 BE D1 80 D0 B0 20 D1 82 D0 BE D0 BD D0 B5 D1 80 D0 B0 20 48 50 20 43 45 39 38 30 41 00 to "Модуль сбора тонера HP CE980A"

str2unixtime1.js

Input: "01/12/77" Output: 221864400

str2unixtime2.js

Input: "01/12/1977 07:30:14" Output: 221891414

return JSON.stringify(JSON.parse(value).map(function(val) {
val['{#LLD}'] = val['{#LLD}'].trim();
return val
}))
return JSON.stringify(JSON.parse(value).map(function(val) {
Object.keys(val).map(function(key) {
val[key] = val[key].trim()
});
return val;
}))
return JSON.stringify(JSON.parse(value).filter(function(obj, index, self) {
return index === self.map(function(obj) {
return obj['{#SERIAL}']
}).indexOf(obj['{#SERIAL}'])
}).map(function(obj) {
obj['{#SERIAL}'] = obj['{#SERIAL}'].trim();
return obj
}))
return JSON.stringify("{$LLD.MACRO}".split(',').map(function(dict) {
const d = dict.split(':');
return {
"{#NAME}": d.filter(function(val) {return val.trim() !== "";}).join(':'),
"{#HOST}": d[0].trim(),
"{#IP}": d[1].trim(),
"{#PORT}": d[2].trim()
};
}));
return JSON.stringify("{$LLD.MACRO}".split(',').map(function(dict) {
const d = dict.split(':');
return {
"{#ID}": d.slice(1).filter(function(val) {return val.trim() !== ""; }).join(':'),
"{#NAME}": d[0].trim(),
"{#HOST}": d[1].trim(),
"{#PORT}": d[2].trim()
};
}));
return JSON.stringify(JSON.parse("{$LLD.MACRO}").split(';').map(function(path) {
return {'{#PATH}': path};
}
));
function split_index(obj) {
const numbers = obj['{#SNMPINDEX}'].split('.').map(Number);
const idx_len = numbers.slice(0, 1)[0];
const nm_len = numbers.slice(idx_len + 1, idx_len + 2)[0];
obj['{#CTRLINDEX}'] = String.fromCharCode.apply(null, numbers.slice(1, idx_len + 1));
obj['{#CTRLNAME}'] = String.fromCharCode.apply(null, numbers.slice(idx_len + 2, idx_len + nm_len + 2));
if (obj['{#CTRLDESCR}'] === "")
obj['{#CTRLDESCR}'] = obj['{#CTRLINDEX}'].concat('.', obj['{#CTRLNAME}'])
return obj
}
return JSON.stringify(JSON.parse(value).map(split_index))
var data = JSON.parse(value)
function f(obj){
if (obj['{#NAME}'].trim().endsWith(" 00")) {
obj['{#NAME}'] = decodeURIComponent(obj['{#NAME}'].trim().slice(0,-2).replace(/\s+/g, '').replace(/[0-9a-fA-F]{2}/g, '%$&'))
}
return obj
}
return JSON.stringify(data.map(f))
onst mdy = value.split('/');
const date = new Date(+mdy[2], mdy[0] - 1, +mdy[1]);
return date.getTime() / 1000 | 0;
const dt = value.split(' ');
const mdy = dt[0].split('/');
const hms = dt[1].split(':');
const date = new Date(+mdy[2], mdy[0] - 1, +mdy[1], +hms[0], +hms[1], +hms[2]);
return date.getTime() / 1000 | 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment