Ad Widget

Collapse

Calculated variable in web scenario

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • apolyakov
    Junior Member
    • Aug 2015
    • 1

    #1

    Calculated variable in web scenario

    Dear all

    I have a RESTful service which needs to be posted with some JSON. Say the JSON is
    {
    "eodDate":"2015-08-04",
    ..
    }

    I would like to monitor that using web scenario and send relevant request. Problem isthat eodDate is calculated. It is a previous business date, also it need to be in yyyy-mm-dd format.
    So I setup web scenario and successfulkly created request with embeddded variable for eodDate, so far so good. But now I need to pass actual value and here I stuck.

    I can either try to make use of Zabbix macro but {DATE} is not suitable for that since it is in different format and also I can't see how to use date arithmetic (since I need yesterday).

    On the other hand I could extract eodDate from some item (SQL) but I don't see any way to reference item.value in web scenario variables.

    Third way would be to create another web service which just supplies this eodDate and then put calling and parsing output of this web service as a first step of the scenario (using {eod_date} = regex: parsing)

    Could you the dear experts confirm nothing apart from third way is possible? I don't really want to deploy a new service, I would prefer to get eodDate by looking at some item or calculating it by Zabbix

    Thank you
  • jamesNJ
    Senior Member
    • Jun 2015
    • 103

    #2
    The docs seem to imply that user macros like {$MARCO} are supported in the variable field.

    So then another idea would be to look and see where zabbix saves user macros, and then update that database record on a daily basis.

    So for example create a user macro called {$YESTERDAY} with some recognized value (like 9999-88-77) and then look through the database tables to see if you can find where that value is stored. When you do find it, change it to something better like 2015-08-05 and then check the zabbix front-end if the macro value was updated. If so you have a solution.

    You could easily update this value with cron, but I bet if you are using postgresql there might be a way to set a procedure or trigger that updates that value daily.

    Comment

    • jamesNJ
      Senior Member
      • Jun 2015
      • 103

      #3
      I was able to verify part of this scheme. I use mysql, but the tables and sql should easily translate to other databases.

      The see macros are stored in the table "globalmacro". It is a very simple table with 3 columns, globalmacroid, macro, value. I did as suggested above creating the new macro in the web interface and easily found the test value. I was then able to easily modify that value and verify it was visible in the web interface. In my case the new marco was globalmacroid=3, so it was updated easily with:

      update globalmacro set value="2015-08-05" where globalmacroid=3;

      You could easily construct a cron job script to figure out the value you need in there, and update it daily right at midnight, etc.

      So the next part will be up to you ... can you test that user macros are useful in this way in web scenarios? Put the two together and I think you have a solution.
      Last edited by jamesNJ; 21-08-2015, 06:50.

      Comment

      • jderjabo
        Junior Member
        • Jan 2020
        • 1

        #4
        Thanks jamesNJ. Your answer very helps.
        Hire my instruction:

        # mysql -uUSER -p -h localhost
        Enter password: xxxxxxxx
        MariaDB [(none)]> show databases;
        +--------------------+
        | Database |
        +--------------------+
        | information_schema |
        | zabbix |
        +--------------------+

        MariaDB [(none)]> use zabbix;

        OR
        # mysql -uUSER -p'PASSWORD' -h localhost DBNAME


        MariaDB [zabbix]> show tables;
        MariaDB [zabbix]> select * from globalmacro;
        +---------------+-------------------+-----------------------------------------------------------------------+-------------+
        | globalmacroid | macro | value | description |
        +---------------+-------------------+-----------------------------------------------------------------------+-------------+
        | 2 | {$SNMP_COMMUNITY} | public | |
        | 5 | {$TODAY1} | 2020-99-99 | yyyy-mm-dd |
        | 6 | {$TODAY2} | 20209999 | yyyymmdd |
        | 7 | {$TODAY3} | 2020/99/99 | yyyy/mm/dd |
        +---------------+-------------------+-----------------------------------------------------------------------+-------------+

        MariaDB [zabbix]> SELECT CURDATE()+0;
        +-------------+
        | CURDATE()+0 |
        +-------------+
        | 20200129 |
        +-------------+

        MariaDB [zabbix]> update globalmacro set value=CURDATE() where globalmacroid=5;
        MariaDB [zabbix]> update globalmacro set value=CURDATE()+0 where globalmacroid=6;
        MariaDB [zabbix]> select * from globalmacro;
        +---------------+-------------------+-----------------------------------------------------------------------+-------------+
        | globalmacroid | macro | value | description |
        +---------------+-------------------+-----------------------------------------------------------------------+-------------+
        | 2 | {$SNMP_COMMUNITY} | public | |
        | 5 | {$TODAY1} | 2020-01-29 | yyyy-mm-dd |
        | 6 | {$TODAY2} | 20200129 | yyyymmdd |
        | 7 | {$TODAY3} | 2020/99/99 | yyyy/mm/dd |
        +---------------+-------------------+-----------------------------------------------------------------------+-------------+


        #save to file_name.sh

        mysql -uUSER -p'password123' -h localhost zabbix <<EOFMYSQL
        update globalmacro set value=CURDATE() where globalmacroid=5;
        update globalmacro set value=CURDATE()+0 where globalmacroid=6;
        update globalmacro set value=date_format(CURDATE(), '%Y/%m/%d') where globalmacroid=7;
        EOFMYSQL
        dt=`date +"%Y/%m/%d"`
        printf "$dt"


        OR

        mysql -uUSER -p'password123' -h localhost zabbix <<EOFMYSQL
        update globalmacro set value=date_format(ADDTIME(NOW(), '02:00:00'), '%Y-%m-%d') where globalmacroid=5;
        update globalmacro set value=date_format(ADDTIME(NOW(), '02:00:00'), '%Y%m%d') where globalmacroid=6;
        update globalmacro set value=date_format(ADDTIME(NOW(), '02:00:00'), '%Y/%m/%d') where globalmacroid=7;
        EOFMYSQL
        dt=`date +"%Y/%m/%d %T" --date "+120 min"`
        printf "$dt"


        Create zabbix item "external check" with key
        file_name.sh[{HOST.NAME}]
        Last edited by jderjabo; 30-01-2020, 09:52.

        Comment

        Working...