Obtaining history and trends

The methods we've discussed so far mostly dealt with configuration. We may also query some historical data. For example, to grab item history data, we would need to know several things:

  • Item ID
  • The Type of information setting for that item

Both of these can be found out by opening the item properties in the configuration section—the ID will be in the URL, and the type of information will be in that drop-down menu. Why do we have to specify the type of information? Unfortunately, the Zabbix API doesn't look it up for us but tries to find the values only in a specific table. By default, the history_uint (integer values) table is queried. To get the values for the CPU load item on A test host, the JSON string would look like this:

$ json='{"jsonrpc":"2.0","method":"history.get","params":{"history":0,"itemids":"23668","limit":3},"auth":"df83119ab78bbeb2065049412309f9b4","id":1}'  
Remember to replace both auth and itemid for this query.

There're a couple extra parameters worth discussing here:

  • The history parameter tells the API which table to query. With 0, the history table is queried. With 1, the history_str table is queried. With 2, the history_log table is queried. With 3, history_int is queried (which was the default). With 4, the history_text table is queried. We must manually match this value to the setting in the item properties.
  • The limit parameter limits the number of entries returned. This is quite useful here, as an item could have lots and lots of values. By the way, limit is supported for all other methods as well—we can limit the number of entries when retrieving hosts, items, and all other entities.

Now, run the curl command:

{"jsonrpc":"2.0","result":[{"itemid":"23668","clock":"1430988898","value":"0.0000","ns":"215287328"},{"itemid":"23668","clock":"1430988928","value":"0.0000","ns":"221534597"},{"itemid":"23668","clock":"1430988958","value":"0.0000","ns":"229668635"}],"id":1} 

We got our three values, but the output is a bit hard to read. There're many ways to format JSON strings, but in the shell, the easiest would be using Perl or Python commands. Rerun the curl command and append to it | json_pp:

$ curl ... | json_pp  
You might also have json_xs, which will have better performance, but performance should be no concern at all for us at this time.

This will invoke the Perl JSON tool, where pp stands for pure Perl, and the output will be a bit more readable:

{ 
  "jsonrpc" : "2.0", 
  "id" : 1, 
  "result" : [ 
    { 
        "clock" : "1430988898", 
        "itemid" : "23668", 
        "value" : "0.0000", 
        "ns" : "215287328" 
    }, 
    { 
        "ns" : "221534597", 
        "value" : "0.0000", 
        "itemid" : "23668", 
        "clock" : "1430988928" 
    }, 
    { 
        "value" : "0.0000", 
        "ns" : "229668635", 
        "clock" : "1430988958", 
        "itemid" : "23668" 
    } 
  ] 
} 
Notice how the output isn't really sorted. Ordering doesn't mean anything with JSON data, so tools don't normally sort the output.

Alternatively, use python -mjsontool, which will invoke Python's JSON tool module. That's a bit more typing, though.

In the output from the history.get method, each value is accompanied with an item ID, Unix timestamp, and nanosecond information—the same as the history tables we looked at earlier. That's not very surprising, as the API output comes from those tables. If we convert these values into human-readable format as discussed before by running date -d@<UNIX timestamp>, we'll see that they aren't recent—actually, they're the oldest values. We can get the most recent values by adding the sortfield and sortorder parameters:

$ json='{"jsonrpc":"2.0","method":"history.get","params":{"history":0,"itemids":"23668","limit":3,"sortfield":"clock","sortorder":"DESC"},"auth":"df83119ab78bbeb2065049412309f9b4","id":1}' 

These will sort the output by the clock value in descending order and then grab the three most recent values—check the returned Unix timestamps to make sure of that. If there're multiple values with the same clock value, other fields won't be used for secondary sorting.

We can also retrieve trend data—a new feature in Zabbix 3.0:

$ json='{"jsonrpc":"2.0","method":"trend.get","params":{"itemids":"23668","limit":3},"auth":"df83119ab78bbeb2065049412309f9b4","id":1}'  
The Zabbix API doesn't allow submitting historical data—all item values have to go through the Zabbix server using the zabbix_sender utility, which we discussed in Chapter 10, Advanced Item Monitoring. There're rumors that the API might be moved to the server side, which might allow merging data-submitting in the main API.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.171.235