Ad Widget

Collapse

Date() Trigger

Collapse
This topic has been answered.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Polo
    Junior Member
    • Aug 2023
    • 5

    #1

    Date() Trigger

    Hello everyone,

    I need help with a trigger, and the Zabbix documentation hasn't been helpful.

    We are querying a value from an SQL database. After preprocessing, we receive the date in the following format: YYYYMMDD as a character string.

    Now, I want to create a trigger that activates when the date is older than two days.

    Can someone assist me with this?

    Best regards, Polo
  • Answer selected by Polo at 11-07-2024, 10:04.
    Markku
    Senior Member
    Zabbix Certified SpecialistZabbix Certified ProfessionalZabbix Certified Expert
    • Sep 2018
    • 1781

    Use Javascript preprocessing to convert the date value to a unix timestamp and then use now() function in the trigger expression.

    Markku

    Comment

    • Markku
      Senior Member
      Zabbix Certified SpecialistZabbix Certified ProfessionalZabbix Certified Expert
      • Sep 2018
      • 1781

      #2
      Use Javascript preprocessing to convert the date value to a unix timestamp and then use now() function in the trigger expression.

      Markku

      Comment

      • Polo
        Junior Member
        • Aug 2023
        • 5

        #3
        Hey Markku,

        thank you! That worked:

        Code:
        // Input date in YYYYMMDD format
        var dateStr = value;
        
        // Extract year, month, and day
        var year = parseInt(dateStr.substring(0, 4), 10);
        var month = parseInt(dateStr.substring(4, 6), 10) - 1; // Months are 0-based in JavaScript
        var day = parseInt(dateStr.substring(6, 8), 10);
        
        // Create a Date object
        var date = new Date(year, month, day);
        
        // Return the Unix timestamp (seconds since 1970-01-01)
        return Math.floor(date.getTime() / 1000);
        Best regards, Polo

        Comment

        Working...