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

Add Additional API Flag For Trigger and TriggerPrototype Get Method

XMLWordPrintable

    • Icon: Change Request Change Request
    • Resolution: Unresolved
    • Icon: High High
    • None
    • 6.4.1rc2, 7.0.0alpha1
    • None
    • None

      My Zabbix instance  is completely customized and we produce our own custom templates. As such, I have some pretty extensive code that heavily uses the API for many different things. Since templates, items, triggers, etc are all managed through code I have some functionality that performs a daily resynchronization function to ensure that the code and Zabbix database are in sync. One of the things I want to ensure stays in sync is triggers and triggerprototypes, but this would require a lot of extra code on my part that could be prone to mistakes. For example, in my code, triggers are defined with expressions like:

      last(/Template Thing/some.item.key) >= {$SOME_USER_MACRO}

       

      When I grab this trigger's expression via the API without any API flags I get the functionid, like so:

      {31456}

      >= {$SOME_USER_MACRO}

       

      But my only option with API flags is 'expandExpression', which will resolve both the functionids and the macros and I end up with:

      last(/Template Thing/some.item.key) >= 1

       

      My resynchronization code does simple string comparisons of the object fields to determine if an update (i.e. trigger.update())needs to be sent, and as seen above, the trigger expression in my code will not equal the trigger expression in the database whether I use expandExpression or not.

       

      Long Story Short:

      It would be useful to have an additional API flag that only expands the functionids, but leaves built-in and usermacros unresolved: i.e. 'expandFunctions' to support additional API use cases. It looks like the API code is already nicely structured to support this in CMacrosResolver with a few extra lines of code, like below. This would be needed in both CTrigger.php and CTriggerPrototype.php. I'm currently patching my instance locally, but it would be good to have this built permanently into the code. Thanks much!

       

      iff --git a/ui/include/classes/api/services/CTrigger.php b/ui/include/classes/api/services/CTrigger.php
      index 9544289739..0d28b3cea1 100644
      --- a/ui/include/classes/api/services/CTrigger.php
      +++ b/ui/include/classes/api/services/CTrigger.php
      @@ -103,6 +103,7 @@ class CTrigger extends CTriggerGeneral {
                              'expandDescription'                             => null,
                              'expandComment'                                 => null,
                              'expandExpression'                              => null,
      +                       'expandFunctions'                               => null,
                              'output'                                                => API_OUTPUT_EXTEND,
                              'selectGroups'                                  => null,
                              'selectHostGroups'                              => null,
      @@ -481,7 +482,7 @@ class CTrigger extends CTriggerGeneral {
                      }
       
                      // expand expressions
      -               if ($options['expandExpression'] !== null && $result) {
      +               if (($options['expandExpression'] !== null || $options['expandFunctions'] !== null) && $result) {
                              $sources = [];
                              if (array_key_exists('expression', reset($result))) {
                                      $sources[] = 'expression';
      @@ -491,9 +492,16 @@ class CTrigger extends CTriggerGeneral {
                              }
       
                              if ($sources) {
      -                               $result = CMacrosResolverHelper::resolveTriggerExpressions($result,
      -                                       ['resolve_usermacros' => true, 'resolve_macros' => true, 'sources' => $sources]
      -                               );
      +                               if ($options['expandFunctions']) {
      +                                       $result = CMacrosResolverHelper::resolveTriggerExpressions($result,
      +                                               ['sources' => $sources]
      +                                       );
      +                               }
      +                               else {
      +                                       $result = CMacrosResolverHelper::resolveTriggerExpressions($result,
      +                                               ['resolve_usermacros' => true, 'resolve_macros' => true, 'sources' => $sources]
      +                                       );
      +                               }
                              }
                      } 

       

       

       

            Unassigned Unassigned
            GRyan337 Ryan Eberly
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: