Ad Widget

Collapse

​How to retrieve text data (not png image) through api_jsonrpc.php ?

Collapse
This topic has been answered.
X
X
 
  • Time
  • Show
Clear All
new posts
  • beginer2Zabbix
    Junior Member
    • Dec 2023
    • 8

    #1

    ​How to retrieve text data (not png image) through api_jsonrpc.php ?

    Hi Admin,

    I need help.

    I have a Zabbix server checking a computer running state. I can get the CPU data from browser like this by this URL
    http://my-zabbix-server/chart.php?fr...amp;amp;to=now.....

    (PS: below graph is not from my applicaiton, but has same layout)

    ​​

    My question is ​how to retrieve text data (in above png image) through api_jsonrpc.php ?
    I need the the max value for business purpose.
    ​​​


    I use Chrome to generate Powershell command to call Zabbix api_jsonrpc.php
    ​​

    Chrome command like this:

    PHP Code:
    Invoke-WebRequest -Uri "http://ipaddress/chart.php?from=now-3d&to=now&itemids%5B0%5D=188888&......" -Headers @{
        
    "User-Agent"=Mozilla/5.0....."
        "
    Referer"="http://ipaddress/history.php?action=showgraph&itemids[]=1888888"
        
    ...
    }
    ​ 
    I use Invoke-RestMethod instead

    PHP Code:

    $from 
    = (Get-Date).AddDays(-3).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ")
    $to = (Get-Date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ")

    $jsonBodyData 
    = @{
        
    jsonrpc "2.0"
        
    method "item.get"
        
    params = @{
            
    output "extend"
            
    itemids $itemId
            time_from 
    $from
            time_till 
    $from
            sortfield 
    "clock"
            
    sortorder "ASC"
        
    }
        
    auth $authToken
        id 
    2
    } | ConvertTo-Json
        
    $returnData 
    Invoke-RestMethod -Uri $zabbixApiUrl -Method Post -Body $returnData -ContentType "application/json"​ 


    But $returnData's result contains one record only, has prevvalue, and lastvalue, but no max value.
    May I know how to get the max value, may thanks.
    Last edited by beginer2Zabbix; 15-12-2023, 08:39.
  • Answer selected by beginer2Zabbix at 20-12-2023, 14:36.
    PeterZielony
    Senior Member
    • Nov 2022
    • 146

    Is it something you will have to run regularly? Or one off?
    You could take it out from db itself based on itemid

    if its regular requirement i would set calulated item for each period.

    One calculated item daily with max value for "yesterday's" max, and then based on that daily more items calculated max for 2, 3, 7 days etc based on initial daily.

    So zabbix backend dont have to calculate big data sets for each item but only for one daily and rest is based on few numbers from max each day. But you will have data with daily refresh for yesterday's data - more accurate and less impact on db/frontend with 1 day delay unless they need have last 24h one too - as a seperate calculated "last24h" along with yesterday's


    Cpu, ram usage are items with a lot of metrics values... for 200 pcs and occasionally you can run into timeout situations when frontend is busy or even cripple web interface


    ​​​​you could then use api to get values but to be honest i rather take from db itself - it will be presented in table right away but drawback is to have list of itemids .. but with api you still need them either way. I would say database directly for big dataset and for so many items- but that's how I would do it
    Last edited by PeterZielony; 16-12-2023, 16:41.

    Comment

    • beginer2Zabbix
      Junior Member
      • Dec 2023
      • 8

      #2
      hi
      I got progress about it.

      I updated code like this:

      PHP Code:

      $from 
      = (((Get-Date).AddDays(-3).ToUniversalTime().ticks - (Get-Date "1970-01-01").ToUniversalTime().ticks)/10000000).toString().SubString(010)
      $to = (((Get-Date).ToUniversalTime().ticks - (Get-Date "1970-01-01").ToUniversalTime().ticks)/10000000).toString().SubString(010)

      $jsonBodyData = @{
          
      jsonrpc "2.0"
          
      method "history.get"
          
      params = @{
              
      output "extend"
              
      history 0
              itemids 
      $itemId
              time_from 
      $from
              time_till 
      $to
              sortfield 
      "clock"
              
      sortorder "DESC"
              
      limit 100
          
      }
          
      auth $authToken
          id 
      2
      } | ConvertTo-Json
          
      $returnData 
      Invoke-RestMethod -Uri $zabbixApiUrl -Method Post -Body $returnData -ContentType "application/json"​​ 

      then , I received data like this:

      PHP Code:

      result
      : [
      ...
          {
              
      itemid"88887777"
              
      clock1702621309
              value
      34.4563
              ns
      775488818
           
      }
      ...

      but still no max data in resoponse.

      Comment

      • cyber
        Senior Member
        Zabbix Certified SpecialistZabbix Certified Professional
        • Dec 2006
        • 4807

        #3
        Why do you expect it to return max value? Just because it is on the picture together with others? Doesn't work that way..

        Where in here do you see anything about returning max value?
        The method allows to retrieve history data according to the given parameters.
        It returns you a selection of values, based on your criteria. If you want max or min or avg... use those returned values and calculate it... There are sorting functions in PHP..

        Comment

        • beginer2Zabbix
          Junior Member
          • Dec 2023
          • 8

          #4
          I updated code "trend.get"


          PHP Code:
          $from = (((Get-Date).AddDays(-3).ToUniversalTime().ticks - (Get-Date "1970-01-01").ToUniversalTime().ticks)/10000000).toString().SubString(010)
          $to = (((Get-Date).ToUniversalTime().ticks - (Get-Date "1970-01-01").ToUniversalTime().ticks)/10000000).toString().SubString(010)

          $jsonBodyData = @{
              
          jsonrpc "2.0"
              
          method "trend.get"
              
          params = @{
                  
          output "extend"
                  
          history 0
                  itemids 
          $itemId
                  time_from 
          $from
                  time_till 
          $to
                  sortfield 
          "value_max"
                  
          limit 100
              
          }
              
          auth $authToken
              id 
          2
          } | ConvertTo-Json
              
          $returnData 
          Invoke-RestMethod -Uri $zabbixApiUrl -Method Post -Body $returnData -ContentType "application/json"​​  

          $sortedData 
          $returnData.result Sort-Object -Property value_max -Descending
          Write
          -Host $sortedData[0].value_max​ 
          I can get the MAX VALUE!!!

          Then I tried change time duration,
          (Get-Date).AddDays(-1)....... can work as expect
          (Get-Date).AddHours(-24)....... can work as expect
          (Get-Date).AddHours(-12)....... can work as expect

          (Get-Date).AddHours(-9)....... no data in result of API response, but I can get data from webpage. Does anyone advice me how to fetch data within 10 hours?

          Comment

          • cyber
            Senior Member
            Zabbix Certified SpecialistZabbix Certified Professional
            • Dec 2006
            • 4807

            #5
            If any other "hour" works, then also 9 or 10 should work...

            Trends are ok, but you should not rely on them for last hour as trend is a record of min/max/avg value for 1h period.. so it may not be accurate for current hour, which may affect whole result of you select for multiple hours...

            Comment

            • beginer2Zabbix
              Junior Member
              • Dec 2023
              • 8

              #6
              Originally posted by cyber
              Why do you expect it to return max value? Just because it is on the picture together with others? Doesn't work that way..

              Where in here do you see anything about returning max value?

              It returns you a selection of values, based on your criteria. If you want max or min or avg... use those returned values and calculate it... There are sorting functions in PHP..
              Hi cyber

              thank you for your suggestion
              .
              The requirement is I need collect 200+ host's max cpu/memory for a specific period( 24Hours / 3Days / 1 week / 2 weeks ... ) , client need the max value only so far.
              Client insist I should achieve the same result, my powershell script retrieved data(the value of max) should as same as web page.

              Would you please help confirm how the Zabbix generate this graph?

              history.get? (filter response result as you mentioned above?)
              trend.get? (it works as expect, if the target period longer than 24H)
              or use Calculated items ?


              Comment

              • PeterZielony
                Senior Member
                • Nov 2022
                • 146

                #7
                Is it something you will have to run regularly? Or one off?
                You could take it out from db itself based on itemid

                if its regular requirement i would set calulated item for each period.

                One calculated item daily with max value for "yesterday's" max, and then based on that daily more items calculated max for 2, 3, 7 days etc based on initial daily.

                So zabbix backend dont have to calculate big data sets for each item but only for one daily and rest is based on few numbers from max each day. But you will have data with daily refresh for yesterday's data - more accurate and less impact on db/frontend with 1 day delay unless they need have last 24h one too - as a seperate calculated "last24h" along with yesterday's


                Cpu, ram usage are items with a lot of metrics values... for 200 pcs and occasionally you can run into timeout situations when frontend is busy or even cripple web interface


                ​​​​you could then use api to get values but to be honest i rather take from db itself - it will be presented in table right away but drawback is to have list of itemids .. but with api you still need them either way. I would say database directly for big dataset and for so many items- but that's how I would do it
                Last edited by PeterZielony; 16-12-2023, 16:41.

                Hiring in the UK? Drop a message

                Comment

                • beginer2Zabbix
                  Junior Member
                  • Dec 2023
                  • 8

                  #8
                  Originally posted by PeterZielony
                  Is it something you will have to run regularly? Or one off?
                  You could take it out from db itself based on itemid

                  if its regular requirement i would set calulated item for each period.

                  One calculated item daily with max value for "yesterday's" max, and then based on that daily more items calculated max for 2, 3, 7 days etc based on initial daily.

                  So zabbix backend dont have to calculate big data sets for each item but only for one daily and rest is based on few numbers from max each day. But you will have data with daily refresh for yesterday's data - more accurate and less impact on db/frontend with 1 day delay unless they need have last 24h one too - as a seperate calculated "last24h" along with yesterday's


                  Cpu, ram usage are items with a lot of metrics values... for 200 pcs and occasionally you can run into timeout situations when frontend is busy or even cripple web interface


                  ​​​​you could then use api to get values but to be honest i rather take from db itself - it will be presented in table right away but drawback is to have list of itemids .. but with api you still need them either way. I would say database directly for big dataset and for so many items- but that's how I would do it
                  Thank you for your guide.

                  This a maintenance work, client need max cpu/mem repors about 3 to 5 times a week. I have no permission to access database, no Java env, no Python env, thanks God I have Powershell 5.0(without any 3rd party lab) and a Zabbix guest account. look like I should retrieve all data(base on itemid) then filter the max value in my code.

                  Comment


                  • PeterZielony
                    PeterZielony commented
                    Editing a comment
                    Strange.. you have do all that work for client and they won't give you required access?


                    You could bring arg about locking frontend using api for big datasets - this isn't just "give me one value" but heavy calculation per host - you have thousands of values for cpu itself and mem (and you need them all.. without limit = 100 ) - multiply this by 200 hosts - api isn't designed to run reports that heavy.

                    Either way - you should have at least few additional items containing max per item and per time scale- then your powershell would work better (and faster without locking frontend)... but with read-only access you can't modify templates?

                    And like cyber said - do not rely on trends for this.. data won't be accurately presented
                    Last edited by PeterZielony; 17-12-2023, 21:11.
                • beginer2Zabbix
                  Junior Member
                  • Dec 2023
                  • 8

                  #9
                  Hi Guys

                  I did this work by history way. The workload on the server side is indeed very high when querying, but it is not to an unacceptable. Thank you

                  Comment

                  Working...