Ad Widget

Collapse

Help understanding Script Items

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • benderisgreat
    Junior Member
    • Dec 2020
    • 6

    #1

    Help understanding Script Items

    I have been looking over the documentation for script items [ https://www.zabbix.com/documentation...emtypes/script ] and trying to implement an item for a while in different ways and feel I don't understand this at all.

    In the documentation, parameters are made, but don't seem to be utilized IN the scripts (in this example, host isn't in the provided example script:

    Code:
    Use the {HOST.CONN} macro as parameter value and get a response with expanded macro:[LIST][*]Create an item with type “Script”.[*]Create a parameter:[/LIST]
    [I]Name:[/I] host
    [I]Value:[/I] {HOST.CONN}[LIST][*]In the [I]Script[/I] field, enter the following code:[/LIST]
    var request = new CurlHttpRequest(); return request.Post("https://postman-echo.com/post", JSON.parse(value));
    But the explanation of Parameters is:
    Specify the variables to be passed to the script as the attribute and value pairs.

    In any case, my use case is I have a discovery rule, it generates three macros, and I would like to run a calculation on one of those macros for this items value.


    Code:
    Name: Value Data
    Type: Script
    Key: some.unique.name
    Prameters: { mygreatvalue : {#mygreatmacro} }
    Script: return mygreatvalue <---- ? where I am lost
    Generates this error: Cannot execute script: ReferenceError: identifier 'mygreatvalue' undefined


    Zabbix Version 5.4beta1
  • rockandstone
    Junior Member
    • Mar 2021
    • 24

    #2
    Hello, I am sorry about my English.
    I am trying to understand this kind of item aswell...
    I had your problem and I fixed in a way, but I think it is a bad trick and I hope there could be another polished way:

    Code:
    Parameters: { mygreatvalue : {#mygreatmacro} }
    [B]Script: return value;[/B]
    And then you add a preprocessing JSONPath | $.mygreatvalue in the corresponding tab.

    Have you fixed the problem yet?

    Comment

    • ZabbixBoy
      Junior Member
      • May 2021
      • 1

      #3
      Hi.

      As you see on the picture parameters are passed to a script as JSON string. You can parse it to an object and use for own puprose as below example shows:
      Code:
      var params = JSON.parse(value);
      return '{"jsonrpc":"2.0","result":[{"itemid":"41832","lastvalue":' + params.boid + '}],"id":1}';
      If you are passing one parameter you can get its value using "value" variable in a script, but the "value" variable is still JSON string.

      Comment


      • rockandstone
        rockandstone commented
        Editing a comment
        Thank you. Now I understand why it didn't work properly... I had to do things in another way but I can try your solution aswell
    Working...