Since Icinga-API is finally capable of supporting developers with
data we are going to blog a little to make coders out there feel
comfortable with this wonderful piece of work.

Here you can see the current progress of the API and related interfaces:

  • API overall: 50%
  • IDO interface: 50%
  • file interface: 25%

If you want to fetch data from the IDO you have to take care about the
following requirements:

  • PHP5
  • web-server module for PHP PDOs

Enough talking… let’s see some code!

1.) configuration

Configuration is simply done by using an associative array.

$idoConfig = array (
	‘type’ 		=> ‘’,
	‘host’ 		=> ‘’,
	‘database’ 	=> ‘’,
	‘user’ 		=> ‘’,
	‘password’ 	=> ‘’,
	‘persistent’ 	=> ,
	‘table_prefix’ 	=> ‘’,
);

Example:

$idoConfig = array (
	‘type’ 		=> ‘mysql’,
	‘host’ 		=> ‘localhost’,
	‘database’ 	=> ‘ido’,
	‘user’ 		=> ‘idouser’,
	‘password’ 	=> ‘idopassword’,
	‘persistent’ 	=> true,
	‘table_prefix’ 	=> ‘icinga_’,
);

2.) fetching data: hostnames and corresponding states
Create an instance of class IcingaApi:

$api = IcingaApi::getConnection(IcingaApi::CONNECTION_IDO, $idoConfig);

Create your search:

$apiRes = $api->createSearch()
->setSearchTarget(IcingaApi::TARGET_HOST)
->setResultColumns(array(’HOST_NAME’, ‘HOST_CURRENT_STATE’))
->fetch();

By using setSearchFilter() you can define filters to narrow down the result set:

$apiRes = $api->createSearch()
->setSearchTarget(IcingaApi::TARGET_HOST)
->setResultColumns(array(’HOST_NAME’, ‘HOST_CURRENT_STATE’))
->setSearchFilter(HOST_NAME, ‘Switch%’, IcingaApi::MATCH_LIKE)
->fetch();

3.) processing result

foreach($apiRes as $apiHandle){
	echo ‘Host ‘.$apiHandle->host_name.’ has state ‘.$apiHandle->host_current_state.’
’; }

Output without filter:
Host localhost has state 0
Host MySql has state 0
Host router-01 has state 0
Host windows100 has state 0
Host Apache_01 has state 0

Output with filters:
Host switch70 has the current state 0
Host switch71 has the current state 0
Host switch72 has the current state 0
Host switch73 has the current state 0
Host switch74 has the current state 0
Host switch75 has the current state 0
Host switch76 has the current state 0
Host switch77 has the current state 0

4.) complete code without use of filters:

 ‘mysql’,
	‘host’ 		=> ‘localhost’,
	‘database’ 	=> ‘ido’,
	‘user’ 		=> ‘idouser’,
	‘password’ 	=> ‘idopass’,
	‘persistent’ 	=> true,
	‘table_prefix’ 	=> ‘icinga_’,
);

// Include required files
require_once($apiFile);

// Instance the class
$api = IcingaApi::getConnection(IcingaApi::CONNECTION_IDO, $idoConfig);

// Create search
$apiRes = $api->createSearch()
->setSearchTarget(IcingaApi::TARGET_HOST)
->setResultColumns(array(’HOST_NAME’, ‘HOST_CURRENT_STATE’))
->fetch();

// Create output
foreach($apiRes as $apiHandle){
	echo ‘Host ‘.$apiHandle->host_name.’ has the current state ‘.$apiHandle->host_current_state.’
’; } ?>

That’s all folks but there’s more to follow!

Please have a look at the git repository for further information:
https://git.icinga.com

Have fun!