Uploaded image for project: 'ZABBIX FEATURE REQUESTS'
  1. ZABBIX FEATURE REQUESTS
  2. ZBXNEXT-1424

Zabbix API for Value Mappings

XMLWordPrintable

      It would be very convenient to be add value mappings via the API. I currently have a script that does this via db insertions, but it is fragile with respect to the database schema changing.

      The script is below:

      #!/usr/bin/perl
      
      use warnings;
      use strict;
      
      my $usage = "$0 valueMapName number newvalue [number2 newvalue2 [...]]
      E.g.: 
          makevaluemap.pl 'Alarm Status' 1  ok 2  unknown 3  stale 4  problem
          makevaluemap.pl 'Aliveness' 0  dead 1 alive
      ";
      
      my $valueMapName = shift() || die "No new valuemap name";
      my @mapList = @ARGV;
      die "No mappings given. Usage: $usage\n" if scalar(@mapList) == 0;
      
      my $user = qx/whoami/;
      chomp($user);
      die "Must be run as the zabbix user" if $user ne 'zabbix';
      
      my $isEvenNumber = scalar(@mapList) % 2 == 0;
      die "Must give mapping->value pairs. Usage: $usage\n" if not $isEvenNumber;
      my %mappings = @mapList;
      
      my $newValueMapId = 1 + int(qx/psql -t -c 'select max(valuemapid) from valuemaps'/);
      my $newMappingId = 1 + int(qx/psql -t -c 'select max(mappingid) from mappings'/);
      
      eval {
          my $valueMapCmd = qq/psql -t -c "insert into valuemaps (valuemapid, name) values ('$newValueMapId', '$valueMapName');"/;
          print "$valueMapCmd\n";
          system $valueMapCmd;
          eval {
              for my $from (keys %mappings) {
                  my $to = $mappings{$from};
                  my $mappingCmd= qq/psql -t -c "insert into mappings (mappingid, valuemapid, value, newvalue) values ('$newMappingId', '$newValueMapId', '$from', '$to');"/;
                  print "$mappingCmd\n";
                  system $mappingCmd;
                  $newMappingId++;
              }
          };
          if ($@) {
              die "something went wrong inserting into mappings $@";
          }
      };
      if ($@) {
          die "something went wrong inserting into valuemaps $@";
      }
      

      Specification

            Unassigned Unassigned
            kester Kester Allen
            Votes:
            25 Vote for this issue
            Watchers:
            18 Start watching this issue

              Created:
              Updated:
              Resolved: