Using API libraries

While we looked at a low-level API example, you aren't likely to use shell scripts to work with the Zabbix API. The shell isn't that well suited for working with JSON data even with extra tools, so another programming or scripting language might be a better choice. For many of those languages, we wouldn't have to implement full raw JSON handling, as there're libraries available. At the time of writing this, a list of available libraries is maintained at http://zabbix.org/wiki/Docs/api/libraries. Alternatively, just go to http://zabbix.org and look for the Zabbix API libraries link.

All of these libraries are community supplied. There're no quality guarantees, and any bugs should be reported to the library maintainers, not to Zabbix.

For example, a Perl library called Zabbix::Tiny aims to be a very simple abstraction layer for the Zabbix API, solving the authentication and request ID issues, and other repetitive tasks when working with the API. It can be easily installed from the Comprehensive Perl Archive Network (CPAN):

# cpan Zabbix::Tiny

To create a new user, we would save the following in a file:

use strict; 
use warnings; 
use Zabbix::Tiny; 
my $zabbix = Zabbix::Tiny->new( 
    server => http://localhost/zabbix/api_jsonrpc.php, 
    password => 'zabbix', 
    user => 'Admin', 
); 
$zabbix->do( 
    'user.create', 
    alias    => 'new_user', 
    passwd => 'secure_password', 
    usrgrps => [ '13' ], 
    name => 'New', 
    surname => 'User', 
    type => 3, 
); 

This would create a new user. While most parameters are self-explanatory, the type parameter tells the API whether this is a user, admin, or super admin. A value of 3 denotes the super admin user type. The group ID is hardcoded to 13—that's something to customize. If the file we saved this in was called zabbix_tiny-add_user.pl, we would call it like this:

$ perl  zabbix_tiny-add_user.pl  

While this might seem longer than our raw JSON string, it also deals with logging in, and it's easier to write than raw JSON. For more information on this particular Zabbix API library, refer to http://zabbix.org/wiki/Docs/howto/Perl_Zabbix::Tiny_API.

There're a lot of different Zabbix API libraries for various languages—Python alone has seven different libraries at the time of writing this. It can be a bit of a challenge to choose the best one.

If programming around a library isn't your thing, there's also a Python-based project to create command-line tools for API operations, called Zabbix Gnomes. It can be found at https://github.com/q1x/zabbix-gnomes.

..................Content has been hidden....................

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