zabbix apijsonrpc.php,A PHP library for the Zabbix™ JSON-RPC API.

PhpZabbixApi

68747470733a2f2f7472617669732d63692e6f72672f636f6e6669726d2f5068705a61626269784170692e7376673f6272616e63683d6d6173746572

About

PhpZabbixApi is an open-source PHP class library to communicate with the Zabbix™ JSON-RPC API.

Because PhpZabbixApi is generated directly from the origin Zabbix™ PHP front-end source code / files, each real Zabbix™ JSON-RPC API method is implemented (hard-coded) directly as an own PHP method. This means PhpZabbixApi is IDE-friendly, because you've a PHP method for each API method, and there are no PHP magic functions or alike.

License

PhpZabbixApi is licensed under the MIT license.

Getting the thing

You can get PhpZabbixApi in 3 different ways:

building it yourself

download a pre-built library release

using PHP composer / Packagist

Make sure the version of the library matches the Zabbix™ PHP front-end / API version.

Building

If you want to build your own library, have a look at the configuration file inc/config.inc.php.

You might want to point PATH_ZABBIX to your Zabbix™ installation directory.

If you setup everything correctly, you should be able to create the library by executing:

php build/build.php

There are also pre-built libraries available in the src/ directory, if you don't want to build it yourself.

Download

Download a release and extract the pre-built PHP library from the src/ directory.

Make sure you've downloaded the following files and stored them in the same directory:

ZabbixApi.php

AbstractZabbixApi.php

For example:

my_application

├── index.php

└── lib

├── AbstractZabbixApi.php

└── ZabbixApi.php

Composer

If you're using PHP composer, you can load the library directly via:

composer require confirm-it-solutions/php-zabbix-api:

All tagged versions can be installed, for example:

composer require 'confirm-it-solutions/php-zabbix-api:^2.2'

composer require 'confirm-it-solutions/php-zabbix-api:^2.4'

If you're looking for more "bleeding-edge" versions (e.g. for testing), then you could also use branches:

composer require 'confirm-it-solutions/php-zabbix-api:[email protected]'

composer require 'confirm-it-solutions/php-zabbix-api:[email protected]'

Using the thing

Naming concept

To translate a Zabbix™ API call into a PHP method call, you can simply

remove the dot

capitalize the first letter of the action

Example:

Zabbix™ API PHP API

----------- -------

graph.get graphGet()

host.massUpdate hostMassUpdate()

dcheck.isWritable dcheckIsWritable()

Customizing the API class

By default there are only 2 classes defined:

AbstractZabbixApi

└── ZabbixApi

If you want to customize or extend the library, you might want to do that in the ZabbixApi class.

Out of the box, ZabbixApi is an empty class inherited from AbstractZabbixApi.

By customizing only ZabbixApi, you're able to update AbstractZabbixApi (the build) at any time, without merging your customizations manually.

Basic usage

To use the PhpZabbixApi you just have to load ZabbixApi.php, create a new ZabbixApi instance, and you're ready to go:

// load ZabbixApi

require_once __DIR__.'/vendor/autoload.php';

use ZabbixApi\ZabbixApi;

use ZabbixApi\Exception;

try {

// connect to Zabbix API

$api = new ZabbixApi('http://zabbix.confirm.ch/api_jsonrpc.php', 'zabbix_user', 'zabbix_password');

/* ... do your stuff here ... */

} catch (Exception $e) {

// Exception in ZabbixApi catched

echo $e->getMessage();

}

?>

The API can also work with HTTP Basic Authroization, you just have to call the constructor with additional parameters:

// connect to Zabbix API with HTTP basic auth

$api = new ZabbixApi('http://zabbix.confirm.ch/api_jsonrpc.php', 'zabbix_user', 'zabbix_password', 'http_user', 'http_password');

Examples

Simple request

Here's a simple request to fetch all defined graphs via graph.get API method:

// get all graphs

$graphs = $api->graphGet();

// print all graph IDs

foreach ($graphs as $graph) {

echo $graph->graphid."\n";

}

Request with parameters

Most of the time you want to define some specific parameters.

Here's an example to fetch all CPU graphs via graph.get API method:

// get all graphs named "CPU"

$cpuGraphs = $api->graphGet(array(

'output' => 'extend',

'search' => array('name' => 'CPU')

));

// print graph ID with graph name

foreach ($cpuGraphs as $graph) {

printf("id:%d name:%s\n", $graph->graphid, $graph->name);

}

Define default parameters

Sometimes you want to define default parameters, which will be included in each API request.

You can do that by defining the parameters in an array via setDefaultParams():

// use extended output for all further requests

$api->setDefaultParams(array(

'output' => 'extend'

));

// get all graphs named "CPU"

$cpuGraphs = $api->graphGet(array(

'search' => array('name' => 'CPU')

));

// print graph ID with graph name

foreach ($cpuGraphs as $graph) {

printf("id:%d name:%s\n", $graph->graphid, $graph->name);

}

Get associative / un-indexed array

By default all API responses will be returned in an indexed array.

So if you then looking for a specific named graph, you've to loop through the indexed array and compare the name attribute of each element. This can be a bit of a pain, and because of that, there's a simple way to to get an associative instead of an indexed array. You just have to define the 2nd parameter of the API method, which is the name of attribute you'd like to use as an array key.

Here's an example to fetch all graphs in an associative array, with the graph's name as array key:

// get all graphs in an associative array (key=name)

$graphs = $api->graphGet(array(), 'name');

// print graph ID with graph name

if (array_key_exists('CPU Load Zabbix Server', $graphs)) {

echo 'CPU Load graph exists';

} else {

echo 'Could not find CPU Load graph';

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值