Ad Widget

Collapse

Home made Veeam job Discovery template problem with Powershell and Json

Collapse
This topic has been answered.
X
X
 
  • Time
  • Show
Clear All
new posts
  • BenoitAnseaume
    Junior Member
    • Jun 2024
    • 5

    #1

    Home made Veeam job Discovery template problem with Powershell and Json

    Hello all,

    I am trying to make my first template for the community, but I am facing an issue with my zabbix server and JSON discovery.

    I want to discover all backup jobs on a Windows Veeam "backup and recovery" server but when my zabbix server receives the JSON data via Zabbix trapper, he always says :
    (the data is sent by a PowerShell code and I am using zabbix 6.0.30)

    Click image for larger version

Name:	image.png
Views:	862
Size:	7.0 KB
ID:	486070

    there is the full JSON content :

    Code:
    {
        "data":  [
                     {
                         "{#NOM}":  "dfs1"
                     },
                     {
                         "{#NOM}":  "Wasabi_dfs1"
                     },
                     {
                         "{#NOM}":  "files"
                     },
                     {
                         "{#NOM}":  "Wasabi_files"
                     },
                     {
                         "{#NOM}":  "Wasabi_fw1"
                     },
                     {
                         "{#NOM}":  "Wasabi_dc4"
                     },
                     {
                         "{#NOM}":  "files2"
                     },
                     {
                         "{#NOM}":  "dc4"
                     },
                     {
                         "{#NOM}":  "dc1"
                     },
                     {
                         "{#NOM}":  "Wasabi_laby"
                     },
                     {
                         "{#NOM}":  "fw1"
                     },
                     {
                         "{#NOM}":  "laby"
                     },
                     {
                         "{#NOM}":  "Wasabi_dc1"
                     }
                 ]
    }​
    discovery rule :

    Click image for larger version

Name:	image.png
Views:	578
Size:	19.8 KB
ID:	486071

    item prototype :

    Click image for larger version

Name:	image.png
Views:	583
Size:	25.7 KB
ID:	486072

    If anyone can help me ? I can give other information if needed.
  • Answer selected by BenoitAnseaume at 21-06-2024, 15:44.
    PeterZielony
    Senior Member
    • Nov 2022
    • 146

    i use this format without data in it as in the documentation here and never had problems


    Note that since Zabbix 4.2, the format of the JSON returned by low-level discovery rules has been changed. It is no longer expected that the JSON will contain the "data" object. Low-level discovery will now accept a normal JSON containing an array, in order to support new features such as the item value preprocessing and custom paths to low-level discovery macro values in a JSON document.

    Built-in discovery keys have been updated to return an array of LLD rows at the root of JSON document. Zabbix will automatically extract a macro and value if an array field uses the {#MACRO} syntax as a key. Any new native discovery checks will use the new syntax without the "data" elements. When processing a low-level discovery value first the root is located (array at $. or $.data).

    While the "data" element has been removed from all native items related to discovery, for backward compatibility Zabbix will still accept the JSON notation with a "data" element, though its use is discouraged. If the JSON contains an object with only one "data" array element, then it will automatically extract the content of the element using JSONPath $.data. Low-level discovery now accepts optional user-defined LLD macros with a custom path specified in JSONPath syntax.
    without data it is just easier.

    Working with double quotes in powershell can be tricky too especially when using sender as it has to pass string value already double quoted. Also found that removing empty spaces from json when passing to sender helps too.

    you can do trickery too. One time i had no time to play with formatting for sender so in powershell i replaced json output char " with ? And then in trapper item brought it back by preprocessing that replaces ? Back to "
    Last edited by PeterZielony; 21-06-2024, 13:26.

    Comment

    • PeterZielony
      Senior Member
      • Nov 2022
      • 146

      #2
      json seems wrong for discovery and should opens with square brackets. Your json should look like this:


      Code:
      [
      {
      "{#NOM}": "dfs1"
      },
      {
      "{#NOM}": "Wasabi_dfs1"
      },
      {
      "{#NOM}": "files"
      },
      {
      "{#NOM}": "Wasabi_files"
      },
      {
      "{#NOM}": "Wasabi_fw1"
      },
      {
      "{#NOM}": "Wasabi_dc4"
      },
      {
      "{#NOM}": "files2"
      },
      {
      "{#NOM}": "dc4"
      },
      {
      "{#NOM}": "dc1"
      },
      {
      "{#NOM}": "Wasabi_laby"
      },
      {
      "{#NOM}": "fw1"
      },
      {
      "{#NOM}": "laby"
      },
      {
      "{#NOM}": "Wasabi_dc1"
      }
      ]
      Also you need pre-processing in item prototype

      Hiring in the UK? Drop a message

      Comment

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

        #3
        The LLD documentation (https://www.zabbix.com/documentation...evel_discovery) says that the "data" syntax is still accepted.

        But, in the screenshot there are no quotes at all, thus the received JSON is invalid. I think you should work with your PowerShell script to ensure that the output data is appropriately quoted.

        Markku

        Comment

        • PeterZielony
          Senior Member
          • Nov 2022
          • 146

          #4
          i use this format without data in it as in the documentation here and never had problems


          Note that since Zabbix 4.2, the format of the JSON returned by low-level discovery rules has been changed. It is no longer expected that the JSON will contain the "data" object. Low-level discovery will now accept a normal JSON containing an array, in order to support new features such as the item value preprocessing and custom paths to low-level discovery macro values in a JSON document.

          Built-in discovery keys have been updated to return an array of LLD rows at the root of JSON document. Zabbix will automatically extract a macro and value if an array field uses the {#MACRO} syntax as a key. Any new native discovery checks will use the new syntax without the "data" elements. When processing a low-level discovery value first the root is located (array at $. or $.data).

          While the "data" element has been removed from all native items related to discovery, for backward compatibility Zabbix will still accept the JSON notation with a "data" element, though its use is discouraged. If the JSON contains an object with only one "data" array element, then it will automatically extract the content of the element using JSONPath $.data. Low-level discovery now accepts optional user-defined LLD macros with a custom path specified in JSONPath syntax.
          without data it is just easier.

          Working with double quotes in powershell can be tricky too especially when using sender as it has to pass string value already double quoted. Also found that removing empty spaces from json when passing to sender helps too.

          you can do trickery too. One time i had no time to play with formatting for sender so in powershell i replaced json output char " with ? And then in trapper item brought it back by preprocessing that replaces ? Back to "
          Last edited by PeterZielony; 21-06-2024, 13:26.

          Hiring in the UK? Drop a message

          Comment

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

            #5
            you can do trickery too. One time i had no time to play with formatting for sender so in powershell i replaced json output char " with ? And then in trapper item brought it back by preprocessing that replaces ? Back to "
            Now that's clever! In PowerShell I always have problems with either quoting or character sets

            Markku

            Comment

            • BenoitAnseaume
              Junior Member
              • Jun 2024
              • 5

              #6
              Bingo ! I have made some adjustments and it work's !
              Click image for larger version

Name:	image.png
Views:	562
Size:	22.3 KB
ID:	486143
              In my PowerShell code, I have replaced the " character into ? as you said, and I replace it back into zabbix preprocessing !

              I have also removed the "data" part in the beginning of my JSON.

              It works instantly !
              Click image for larger version

Name:	image.png
Views:	764
Size:	5.1 KB
ID:	486141
              Click image for larger version

Name:	image.png
Views:	571
Size:	11.7 KB
ID:	486142
              There is my PowerShell code :

              Code:
              # Variables
              $zabbixServer = "zabbix server"
              $zabbixHost = "Zabbix host"
              $zabbixSenderPath = "C:\zabbix_agent2\bin\zabbix_sender.exe"
              
              # Veeam module Import
              Import-Module Veeam.Backup.PowerShell
              
              # Connection to Veeam Server
              Connect-VBRServer -Server "localhost"
              
              # Get Veeam Jobs into a variable
              $backupJobs = Get-VBRJob
              
              # Prepare data for Zabbix Discovery
              $discoveryData = @()
              foreach ($job in $backupJobs) {
                  # Clean Zabbix Job Name
                  $cleanJobName = $job.Name -replace '[^a-zA-Z0-9_]', ''
                  $discoveryData += @{"{#NOM}"=$cleanJobName}
              }
              #Convert data into Json and replace " with ?
              $discoveryJson = $discoveryData | ConvertTo-Json -Compress
              $finaljson = $discoveryJson | ForEach-Object {$_ -replace '\"', '?'}
              #Send data to Zabbix server
              & $zabbixSenderPath -z $zabbixServer -s $zabbixHost -k job.json -o $finaljson
              
              # Prepare job results for Zabbix
              foreach ($job in $backupJobs) {
                  # Clean Zabbix Job Name
                  $cleanJobName = $job.Name -replace '[^a-zA-Z0-9_]', ''
                  $jobName = "job[" + $cleanJobName + "]"
                  $lastResult = $job.GetLastResult()
              
                  # #Send data to Zabbix server
                  & $zabbixSenderPath -z $zabbixServer -s $zabbixHost -k $jobName -o $lastResult
                  echo $jobName
              }
              
              # Disconnect from zabbix host
              Disconnect-VBRServer​

              Comment

              • OndrejBalaz
                Junior Member
                • Aug 2024
                • 1

                #7
                Hi, is your template, including the script, functional now? If yes, would you be able to share it publicly, perhaps on GitHub? Thanks.

                Comment

              Working...