Ad Widget

Collapse

HTTP Agent - macro {DATE}, change dots to dash

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Krevedka
    Junior Member
    • Jul 2019
    • 17

    #1

    HTTP Agent - macro {DATE}, change dots to dash


    Hello! Please tell me, is it possible to insert a date, current day variable in the zabbix in the HTTP agent in the URL, in the format "yyyy’ - ‘MM’ - ‘dd’? {DATE} is yyyy.mm.dd. The link does not work with dots. I'm trying to redo the macro points in the date on dash. It does
    How it doesn’t work for me: I made variables in the template. {$CURRDATE} = {{DATE}.regsub(\.,-)}
    ​​​​​{$CURRDATE2} = {{DATE}.regsub("\.","-")}
    ​​​​​​{$CURRDATE2} = {{DATE}.regsub("\.",-)}


    Items do not work in all cases, URL does not work, error 500
  • Semiadmin
    Senior Member
    • Oct 2014
    • 1625

    #2
    Hi, Krevedka.
    If your Zabbix version is 4.2 and above you may try such trick: create an LLD rule for the current date and an HTTP agent check as an item prototype.
    Dependent LLD rule would start at the beginning of each day by some dummy master item (for example, icmpping[localhost]) and get in JS preprocessing 2 LLD macros: {#DATE} (for example) with the current date in the required format (via JS date functions) and {#CONST} (for example) with some unchanging value. In item prototype use {#DATE} in the URL and {#CONST} in the key for continuity of history. In the name, you may use any macro.

    Comment

    • Krevedka
      Junior Member
      • Jul 2019
      • 17

      #3
      Originally posted by Semiadmin
      Hi, Krevedka.
      If your Zabbix version is 4.2 and above you may try such trick: create an LLD rule for the current date and an HTTP agent check as an item prototype.
      Dependent LLD rule would start at the beginning of each day by some dummy master item (for example, icmpping[localhost]) and get in JS preprocessing 2 LLD macros: {#DATE} (for example) with the current date in the required format (via JS date functions) and {#CONST} (for example) with some unchanging value. In item prototype use {#DATE} in the URL and {#CONST} in the key for continuity of history. In the name, you may use any macro.
      It looks like a working way, but I'm completely new to zabbix, could you tell us in more detail how to do this?

      Comment

      • Semiadmin
        Senior Member
        • Oct 2014
        • 1625

        #4
        I think it's easier to write than to detail explain... This is a simple template example.

        Code:
        <?xml version="1.0" encoding="UTF-8"?>
        
        -<zabbix_export>
        
        <version>4.4</version>
        
        <date>2020-04-06T19:24:45Z</date>
        
        
        -<groups>
        
        
        -<group>
        
        <name>test</name>
        
        </group>
        
        </groups>
        
        
        -<templates>
        
        
        -<template>
        
        <template>the current date in URL</template>
        
        <name>the current date in URL</name>
        
        
        -<groups>
        
        
        -<group>
        
        <name>test</name>
        
        </group>
        
        </groups>
        
        
        -<applications>
        
        
        -<application>
        
        <name>test</name>
        
        </application>
        
        </applications>
        
        
        -<items>
        
        
        -<item>
        
        <name>dummy item for LLD rule</name>
        
        <type>SIMPLE</type>
        
        <key>icmpping[localhost]</key>
        
        <delay>0;h0m1</delay>
        
        <history>0</history>
        
        <trends>0</trends>
        
        <value_type>TEXT</value_type>
        
        
        -<applications>
        
        
        -<application>
        
        <name>test</name>
        
        </application>
        
        </applications>
        
        </item>
        
        </items>
        
        
        -<discovery_rules>
        
        
        -<discovery_rule>
        
        <name>date discovery</name>
        
        <type>DEPENDENT</type>
        
        <key>date.discovery</key>
        
        <delay>0</delay>
        
        <lifetime>0</lifetime>
        
        
        -<item_prototypes>
        
        
        -<item_prototype>
        
        <name>{#CONST}</name>
        
        <type>HTTP_AGENT</type>
        
        <key>test[{#CONST}]</key>
        
        <trends>0</trends>
        
        <value_type>TEXT</value_type>
        
        
        -<applications>
        
        
        -<application>
        
        <name>test</name>
        
        </application>
        
        </applications>
        
        <url>http://{#DATE}some_url.com</url>
        
        </item_prototype>
        
        </item_prototypes>
        
        
        -<master_item>
        
        <key>icmpping[localhost]</key>
        
        </master_item>
        
        
        -<preprocessing>
        
        
        -<step>
        
        <type>JAVASCRIPT</type>
        
        <params>var date = new Date(); return date.toString().replace(/(.*)\s.*/,"[{\"{#DATE}\": \"$1\",\"{#CONST}\":\"SOME_TEXT\"}]"); </params>
        
        </step>
        
        </preprocessing>
        
        </discovery_rule>
        
        </discovery_rules>
        
        </template>
        
        </templates>
        
        </zabbix_export>

        Comment

        • Krevedka
          Junior Member
          • Jul 2019
          • 17

          #5
          Thx!! It`s alive!

          Comment

          • kaba4ek
            Junior Member
            • Mar 2021
            • 2

            #6

            Hi! I want to modify you template, but do something wrong. I need to past a current day in http-agent request. Im use API to get a current SLA for my service:
            Request Body:
            {
            "jsonrpc": "2.0",
            "method": "service.getsla",
            "params": {
            "serviceids": "26",
            "intervals": [
            {
            "from": 1616493527,
            "to": 1616581878
            }
            ]
            },
            "auth": "6dd63be3f42a0aaa78dab3d06fa0675f",
            "id": 1
            }


            Maybe you can help, how can i past in "from" and "to" current day?

            Comment

            • Juanito
              Junior Member
              • Mar 2021
              • 4

              #7
              Thanks Semiadmin for sharing this template! I had the same date format issue as Krevedka - it's working perfectly.

              I have a challenging extra step: my URL containing the well formatted {#DATE} needs also to contain another parameter {#PARAM} coming from another http discovery. I came to the conclusion it's not doable, unless Semiadmin you have another trick?

              Comment

              Working...