[ZBXNEXT-1424] Zabbix API for Value Mappings Created: 2012 Sep 13  Updated: 2015 Oct 21  Resolved: 2015 Oct 21

Status: Closed
Project: ZABBIX FEATURE REQUESTS
Component/s: API (A)
Affects Version/s: 2.0.2
Fix Version/s: 3.0.0alpha4

Type: New Feature Request Priority: Major
Reporter: Kester Allen Assignee: Unassigned
Resolution: Fixed Votes: 25
Labels: missingapi, patch, valuemapping
Environment:

Ubuntu 12.04


Attachments: File zabbixAPIValueMapping2.2.tar.gz     File zabbixAPIValueMapping2.4.tar.gz    
Issue Links:
Duplicate
is duplicated by ZBXNEXT-2787 API Value Mapping Closed
is duplicated by ZBXNEXT-2789 API Value Mapping 2.4 Closed

 Description   

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



 Comments   
Comment by Kester Allen [ 2012 Sep 21 ]

------

Comment by Alexei Vladishev [ 2012 Oct 09 ]

I agree, it must be supported by Zabbix out of the box.

Comment by richlv [ 2013 Jun 06 ]

ZBXNEXT-1679 asks for valuemap xml import/export

Comment by Justin McNutt [ 2013 Jun 27 ]

XML import/export would be nice as well, but that also requires user interaction via the GUI (unless there's an API method like xml.import, which would be amazing). Admittedly, this would go a long way, but would not resolve all cases where API access is needed.

API access to the value mappings is not just about configuration. In fact, XML import is likely to be a better way to accomplish configuration. However, If I query Event history for an Item that uses a value mapping, there's no good way using the API to query that mapping for proper interpretation. The extra bit of labelling that gives context to the raw data is hidden.

I would say that the request for API access to the value mappings is much more about reading what's there and proper interpretation of the Event data than anything else. Of course, once there's a "valuemap.*" set of methods, adding .create, .update, and .delete methods is probably not too much of a stretch. But the .get method is probably the most crucial.

In my humble opinion...

Comment by Chuck Dand [ 2013 Jun 28 ]

I'd say that depends on massively on why your using the API in the first place. If implementing a configuration management of Zabbix using something like Puppet via the API, then the ability to create and update Value Mappings is essential, XML import just won't cut it in that circumstance.

Comment by richlv [ 2013 Jun 28 ]

offtopic, but xml import/export via the api is possible already : https://www.zabbix.com/documentation/2.0/manual/appendix/api/configuration

Comment by Justin McNutt [ 2013 Jul 05 ]

The configuration.import and configuration.export methods do not allow for import/export of valuemaps. If it did, this would be a crude, but effective substitute for a set of valuemap.X methods.

Comment by richlv [ 2013 Jul 08 ]

oh, sure - just noted that having xml import/export would indirectly expose partial functionality over the api, too

Comment by Dimitri Bellini [ 2014 Sep 17 ]

I think is also useful to have an API to query a Value Mapping for a specific Item.
Thanks

Comment by Jens Berthold [ 2015 Mar 26 ]

This request is open for 2 1/2 years - is there any plan to implement this soon?
Sorry, but what use is an API if I cannot even modify all aspects of standard items?

Comment by richlv [ 2015 Mar 26 ]

unfortunately, it is not on the roadmap yet. it likely will wait for ZBXNEXT-2519

Comment by richlv [ 2015 Apr 20 ]

ZBXNEXT-2787 has a patch for 2.2

Comment by Damien Vargas [ 2015 Apr 20 ]

ZBXNEXT-2789 has a patch for 2.4

Comment by Damien Vargas [ 2015 Apr 20 ]

Frontend Class of API ValueMapping. Use README to install.

Comment by Ivo Kurzemnieks [ 2015 Sep 30 ]

(1)
Translation strings added:

  • At least one mapping should be given for value map "%1$s".
  • Duplicate "name" value "%1$s" for value map.
  • Duplicate mapping value "%1$s" for value map "%2$s".
  • Empty new value in value map "%1$s".
  • Empty value map ID.
  • Incorrect value map ID.
  • Mapping is missing parameters: %1$s for value map "%2$s".
  • No "%1$s" given for value map.
  • Only super admins can create value maps.
  • Only super admins can delete value maps.
  • Only super admins can update value maps.
  • Value map is missing parameters: %1$s
  • Value map name cannot be empty.

Translation strings removed:

  • Mapping value "%1$s" is not unique.
  • Value cannot be mapped to empty string.
  • Value map "%1$s" "%2$s".
  • Value map "%1$s".
  • Value map with valuemapid "%1$s" does not exist.
  • Value mapping must have at least one mapping.

Alexander Vladishev Updated list of strings. Please check.

Ivo Kurzemnieks CLOSED

Comment by Ivo Kurzemnieks [ 2015 Sep 30 ]

RESOLVED in svn://svn.zabbix.com/branches/dev/ZBXNEXT-1424

Comment by Alexander Vladishev [ 2015 Oct 13 ]

(2) it must be written easier

if (self::$userData['type'] == USER_TYPE_SUPER_ADMIN) {
}
elseif ($options['editable'] === null && self::$userData['type'] == USER_TYPE_ZABBIX_ADMIN) {
}
elseif ($options['editable'] !== null || self::$userData['type'] != USER_TYPE_SUPER_ADMIN) {
    return [];
}

Maybe so:

if ($options['editable'] !== null && self::$userData['type'] != USER_TYPE_SUPER_ADMIN) {
    return [];
}

Ivo Kurzemnieks Indeed so. RESOLVED in r56239

Alexander Vladishev CLOSED

Comment by Alexander Vladishev [ 2015 Oct 14 ]

(3) Take a look at my minor changes in r56131, r56132, r56141, r56142.

Ivo Kurzemnieks CLOSED

Comment by Alexander Vladishev [ 2015 Oct 14 ]

(4) check for the duplicated names in validateCreate() is not effective

Alexander Vladishev RESOLVED in r56143

Ivo Kurzemnieks Thanks!
CLOSED

Comment by Alexander Vladishev [ 2015 Oct 14 ]

(5) SQL request must be removed from valuemap.create().

Alexander Vladishev RESOLVED in r56145

Ivo Kurzemnieks Thanks!
CLOSED

Comment by Alexander Vladishev [ 2015 Oct 14 ]

(6) check for the duplicated names in validateUpdate() is not effective

Alexander Vladishev RESOLVED in r56156

Ivo Kurzemnieks Thanks!
CLOSED

Comment by Alexander Vladishev [ 2015 Oct 14 ]

(7) selectMappings option must support "count"

Ivo Kurzemnieks RESOLVED in r56236

Alexander Vladishev Custom SQL request should be created in this case:

SELECT valuemapid,COUNT(*) AS cnt FROM mappings GROUP BY valuemapid

REOPENED

Ivo Kurzemnieks RESOLVED in r56264

Alexander Vladishev Good! But take a look at my changes in r56269.

Ivo Kurzemnieks Thanks!
CLOSED

Comment by Alexander Vladishev [ 2015 Oct 14 ]

(8) Too many SQL requests with selectMappings option.

SELECT t.mappingid,t.valuemapid FROM mappings t WHERE t.valuemapid IN ('24','25','26','27')
SELECT m.* FROM mappings m WHERE m.mappingid BETWEEN '180' AND '187'

Only one request will be enough to get mappings.

SELECT m.* FROM mappings m WHERE m.valuemapid IN ('24','25','26','27')

Ivo Kurzemnieks RESOLVED in r56235

Alexander Vladishev Using of array_key_exists() in the filter options is useless. 'valuemapids' is always present in $options array.

'filter' => array_key_exists('valuemapids', $options)
    ? ...
    : ...

REOPENED

Ivo Kurzemnieks RESOLVED in r56264

Alexander Vladishev CLOSED

Comment by Alexander Vladishev [ 2015 Oct 14 ]

(9) valuemap.get() returns an empty array instead of number of the valuemaps for non super-admins

  • Request
    {
        "countOutput" : true,
        "editable" : true
    }
    
  • Response
    Logged as superadmin:
    {
        "jsonrpc": "2.0",
        "result": "27",
        "id": 7
    }
    
    Logged as admin or regular user:
    {
        "jsonrpc": "2.0",
        "result": [],
        "id": 9
    }
    

Ivo Kurzemnieks For me this happened only for regular users. RESOLVED in r56239

Alexander Vladishev Still reproducible. REOPENED

Ivo Kurzemnieks RESOLVED in r56268

Alexander Vladishev CLOSED

Comment by Alexander Vladishev [ 2015 Oct 14 ]

(10) Possible "Undefined index" in valuemap.update()

  • Request:
    {
        {
            "valuemapid" => 24,
            "name" => "test"
        },
        {
            "valuemapid" => 25
        }
    }
    
Undefined index: name [CFrontendApiWrapper->update() → CApiWrapper->__call() → CFrontendApiWrapper->callMethod() → CApiWrapper->callMethod() → CFrontendApiWrapper->callClientMethod() → CLocalApiClient->callMethod() → call_user_func_array() → CValueMap->update() in include/classes/api/services/CValueMap.php:196]

Alexander Vladishev RESOLVED in r56184

Ivo Kurzemnieks Possibly fixed and then broken again. Found it again and fixed.
RESOLVED in r56226

Alexander Vladishev Thanks! Take a look at my changes in r56256.

Ivo Kurzemnieks CLOSED

Comment by Alexander Vladishev [ 2015 Oct 15 ]

(11) valuemap.update() can be work faster

Alexander Vladishev RESOLVED in r56184

Ivo Kurzemnieks Thanks!
CLOSED

Comment by Alexander Vladishev [ 2015 Oct 15 ]

(12) valuemap.delete() can be work faster

Alexander Vladishev RESOLVED in r56186

Ivo Kurzemnieks Thanks!
CLOSED

Comment by Alexander Vladishev [ 2015 Oct 15 ]

(13) caching in function getMappedValue() is not effective

getMappedValue('100', 17);

SELECT  vm.valuemapid FROM valuemaps vm WHERE vm.valuemapid='17'
SELECT  t.mappingid,t.valuemapid FROM mappings t WHERE t.valuemapid='17'
SELECT m.mappingid,m.value,m.newvalue FROM mappings m WHERE m.mappingid BETWEEN '89' AND '165'          <= here we can cache all mappings

getMappedValue('101', 17);

SELECT  vm.valuemapid FROM valuemaps vm WHERE vm.valuemapid='17'
SELECT  t.mappingid,t.valuemapid FROM mappings t WHERE t.valuemapid='17'
SELECT m.mappingid,m.value,m.newvalue FROM mappings m WHERE m.mappingid BETWEEN '89' AND '165'

Alexander Vladishev RESOLVED in r56190.

Ivo Kurzemnieks Thanks!
CLOSED

Comment by Alexander Vladishev [ 2015 Oct 16 ]

(14) Confirmation messages must be located in the view

frontends/php/adm.valuemapping.php:124:133

if ($valuemap_count['cnt'] == 0) {
    $data['confirmMessage'] = _('Delete selected value mapping?');
}
else {
    $data['confirmMessage'] = _n(
        'Delete selected value mapping? It is used for %d item!',
        'Delete selected value mapping? It is used for %d items!',
        $valuemap_count['cnt']
    );
}

Ivo Kurzemnieks RESOLVED in r56240

Alexander Vladishev CLOSED

Comment by Alexander Vladishev [ 2015 Oct 19 ]

(15) nonexistent fields are used in the API calls

frontends/php/adm.valuemapping.php:107,136 'selectMappings' => ['mappingid', 'value', 'newvalue']

Alexander Vladishev RESOLVED in r56253

Ivo Kurzemnieks Thanks!
CLOSED

Comment by Alexander Vladishev [ 2015 Oct 19 ]

(16) Direct SQL requests must be replaced by API calls.

frontends/php/adm.valuemapping.php:120

Alexander Vladishev RESOLVED in r56255

Ivo Kurzemnieks Thanks!
CLOSED

Comment by Alexander Vladishev [ 2015 Oct 19 ]

(17) Templates must be written by using our html classes

Alexander Vladishev RESOLVED in r56260.

Ivo Kurzemnieks Minor coding style fix in r56266, please review.

Alexander Vladishev CLOSED

Comment by Alexander Vladishev [ 2015 Oct 19 ]

(18) Added [Clone] button for value mappings. It will be more consistent with other forms.

Alexander Vladishev RESOLVED in r56261.

Ivo Kurzemnieks Thanks! jQuery code simplified in r56267, please review.

Alexander Vladishev Thanks! CLOSED

Comment by Alexander Vladishev [ 2015 Oct 19 ]

(19) Unused form name "valuemap_form"; unused class "row_mappings"

Alexander Vladishev RESOLVED in r56262.

Ivo Kurzemnieks

Undefined variable: tab [adm.valuemapping.php:152 → CView->render() → include() in include\views\administration.general.valuemapping.edit.php:109]

RESOLVED in r56265

Alexander Vladishev Many thanks! CLOSED

Comment by Alexander Vladishev [ 2015 Oct 19 ]

(20) "sortfield" argument must support "name". Specification is updated accordingly.

Ivo Kurzemnieks RESOLVED in r56263

Alexander Vladishev CLOSED

Comment by Ivo Kurzemnieks [ 2015 Oct 20 ]

Implemented in pre-3.0.0alpha4 (trunk) r56276

Comment by Ivo Kurzemnieks [ 2015 Oct 20 ]

(21) API documentation updated:

Alexander Vladishev Very good! But, "mappings" property is not described on > Value map object

REOPENED

Ivo Kurzemnieks RESOLVED

Alexander Vladishev Many thanks! CLOSED

Comment by Alexander Vladishev [ 2015 Oct 20 ]

(22) User documentation:

Alexander Vladishev

Martins Valkovskis Reviewed. looks good.

Martins Valkovskis

Alexander Vladishev Looks good. CLOSED

Generated at Tue Oct 16 15:36:18 EEST 2018 using JIRA 7.3.9#73021-sha1:26adfd3d76803e1a04219c5cd05b58dbafff0dbf.