Ad Widget

Collapse

Fetching average values trough API

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BigBoatCap
    Junior Member
    • Aug 2018
    • 6

    #1

    Fetching average values trough API

    Hello everybody!

    My task is to get monthly average values for such items as CPU and RAM Utilization.
    Seems to be a simple quest using history.get method, but...

    Here is what i got:
    Zabbix 3.4.9

    Considering i can get itemids.


    JSON body:

    {
    "jsonrpc": "2.0",
    "method": "history.get",
    "params": {
    "output": "extend",
    "history": 0,
    "itemids": "45172",
    "sortfield": "clock",
    "sortorder": "DESC",
    "limit": 1
    },
    "auth": "xxxx147459452d03c3c57cbc3218xxxx",
    "id": 1,
    }

    I probably should use a trigger expression like that(monthly average):

    "server": "system.cpu.load.avg(1m)",

    Could you please advice me how to implement it?

    Newbie in ZabbixAPI, regards.
    Last edited by BigBoatCap; 08-08-2018, 15:54.
  • BigBoatCap
    Junior Member
    • Aug 2018
    • 6

    #2
    And here is what i found:
    Triggers are not my case, i shouldn't use them.

    Trends seem to be my solution and it's ok if i get hourly values for entire month and calc an avg between them later, but:

    using this JSON example request https://www.zabbix.com/documentation...ce/trend/get?s[]=trend&s[]=get unfortunately i get an error:

    {
    "jsonrpc": "2.0",
    "method": "trend.get",
    "params": {
    "output": [
    "itemid",
    "clock",
    "num",
    "value_min",
    "value_avg",
    "value_max",
    ],
    "itemids": [
    "10378"
    ],
    "limit": "1"
    },
    "auth": "xxxx147459452d03c3c57cbc3218xxxx",
    "id": 1
    }

    ##########
    RESPONSE:

    {
    "jsonrpc": "2.0",
    "error": {
    "code": -32700,
    "message": "Parse error",
    "data": "Invalid JSON. An error occurred on the server while parsing the JSON text."
    },
    "id": null
    }

    would appreciate any help...
    Last edited by BigBoatCap; 08-08-2018, 16:31.

    Comment

    • BigBoatCap
      Junior Member
      • Aug 2018
      • 6

      #3
      My bad, extra comma noticed in this example:

      "value_max", => "value_max"

      Anyways, is there a way to get a monthly period at once?

      Comment

      • BigBoatCap
        Junior Member
        • Aug 2018
        • 6

        #4
        I got it:

        Code:
        {
            "jsonrpc": "2.0",
            "method": "trend.get",
            "params": {
            "output": [
                    "itemid",
                    "clock",
                    "num",
                    "value_avg"
                ],
                "itemids": [
                    "45172"
                ],
                "time_from": "1530392400",
                "time_till": "1532973600",
                "limit": "1055",
                "countOutput": "True"
            },
        
            "auth": "xxxx7be6f7a467a1f29dc3533832xxxx",
            "id": 1
        }

        Last edited by BigBoatCap; 20-08-2018, 07:28.

        Comment

        • jusavard
          Member
          • Sep 2013
          • 48

          #5
          Hi,
          I'm having about the same needs you had. My boss want a mail generated report that show monthly the average/max cpu/mem/disk usage for each server. Can't blame him, we're in cloud environment and the more ressource we ask for for, the more we pay.

          So I tried what you wrote down here. I don't know if you were satisfy with what it gives but i'm not. I was expecting "ONE" result per itemid. For instance, for foobar server, for "mem_usage" itemid, give me the average usage between 1 month ago and now.

          Here is my curl/json request which is similare to yours :

          Code:
          curl -s -H "Content-Type: application/json-rpc" -X POST http://zabbix.myworkplace.com/zabbix/api_jsonrpc.php -d '{"jsonrpc":"2.0","method":"trend.get","id":1,"auth":"'"$AUTH_HASH"'","params" : {"output": ["clock","value_avg","num","itemid"],"itemids":["'"$MEM_USAGE_ITEMID"'"],"time_from": "'"$MONTHAGOTIMESTAMP"'","time_till": "'"$NOWTIMESTAMP"'", "limit": "1055"}}' | jq -s .
          As you might have guessed :
          AUTH_HASH is my Authentification hash
          MEM_USAGE_ITEMID is the itemid ot the item "Used memory" for a designated host.
          MONTHAGOTIMESTAMP is the unixtimestamp for a month ago from nom
          NOWTIMESTAMP is the unix timestamp for now

          What I got was multiple results :

          Code:
          [
            [
              {
                "jsonrpc": "2.0",
                "result": [
                  {
                    "clock": "1547416800",
                    "value_avg": "8446834210",
                    "num": "60",
                    "itemid": "29922"
                  },
                  {
                    "clock": "1547420400",
                    "value_avg": "8401490534",
                    "num": "60",
                    "itemid": "29922"
                  },
                  {
                    "clock": "1547424000",
                    "value_avg": "8336784520",
                    "num": "60",
                    "itemid": "29922"
                  },
                  {
                    "clock": "1547427600",
                    "value_avg": "8481665570",
                    "num": "60",
                    "itemid": "29922"
                  },
                  {
                    "clock": "1547431200",
                    "value_avg": "8471615829",
                    "num": "60",
                    "itemid": "29922"
                  },
          If i'm not mistaking these are multiple averages from the trends table. Thoses are every hourly average for the itemid for the specified time.
          I guess I can simply get all those hourly averages and make a montly average of these and do the same for the max but I can't believe there is no way to that easier and faster. I mean those averages are pretty much the same there is where you chose a timeline in a graph.
          How does the graphs get thoses data (average/min/max) on monthly/yearly/custom period so fast ? Is is there any way to get thoses data via the API ?

          Comment

          • BigBoatCap
            Junior Member
            • Aug 2018
            • 6

            #6
            Hi,

            these are multiple averages from the trends table.
            yes, these are and, yes, you do can
            simply get all those hourly averages and make a montly average of these
            and it's simple indeed and i bet it wouldn't be slower to calculate(as probably the web script does the same). I've got what i need & unfortunately don't know the way to do it "simpler".
            Cheers.
            Last edited by BigBoatCap; 14-02-2019, 11:02.

            Comment

            Working...