0

Good afternoon. I can't creating a "Discovery rules" in Zabbix. Items not created.

I'm creating a "Discovery rules" in Zabbix for server hard drives. Next, I will watch their smart. I use "smartmontools". Created a "Discovery rules":

Type: Zabbix agent
Key: system.run["smartctl --scan-open"]

I get:

/dev/sda -d ata # /dev/sda, ATA device
/dev/sdb -d sat # /dev/sdb [SAT], ATA device
/dev/sdc -d sat # /dev/sdc [SAT], ATA device

I convert the result to the form (through preprocessing in JavaScript):

[{"data":[{"{#DISK}":"sda"},{"{#DISK}":"sdb"},]}]

Next, I create a "Item prototypes":

Name: DISK {#DISK}
Type: Zabbix agent
Key: system.run["smartctl -A /dev/{$DISK}"]

But for some reason, the "Item prototypes" does not appear on the server. Help me please. What am I doing wrong?

Tonik
  • 3
  • 2

2 Answers2

0

Problem solved. It should have been done like this:

   {"data":[{"{#DISK}":"sda"},{"{#DISK}":"sdb"}]}       // Right
   [{"data":[{"{#DISK}":"sda"},{"{#DISK}":"sdb"},]}]    // wrong

Here is the JavaScript:

const sd = value.match(/sd(.)/g);
const col = sd.length;
const arr = [];
const mcleftbegin = '{"{#DISK}":"';
const mcleft = ',{"{#DISK}":"';
const mcright = '"}';
for (i = 0; i <= col; i++) {
    if (i % 2 == 0) {
        if (i == 0) {
            arr.push(mcleftbegin + sd[i] + mcright); 
        }
        else {
            arr.push(mcleft + sd[i] + mcright);
        }
    }
}
arr.pop();
var jsonl = '{"data":[';
var jsonr = ']}';
const coll = (col / 2) - 1;
for (y = 0; y <= coll; y++) {
    if (y == coll) {
        jsonl = jsonl + arr[y] + jsonr;
    }
    else {
        jsonl = jsonl + arr[y];
    }
}
return jsonl;
Tonik
  • 3
  • 2
0

Your json is invalid, that's probably why it doesn't work. See Can you use a trailing comma in a JSON object?

Also: system.run is deprecated, do not use it unless for tests.

Check the "info" column in the LLD page for error messages. You should see something like:

cannot parse as a valid JSON object: invalid JSON object value starting character at: ']}]'

Update on the code in the selfanswer: you can use JSON.stringify() and simplify a lot:

const sd = value.match(/sd(.)/g);
var data = [];
for (i = 0; i <= sd.length; i++) {
    data.push({"{#DISK}": sd[i]}); 
}
return JSON.stringify(data);
Iron Bishop
  • 1,749
  • 1
  • 7
  • 16