Categories

Archives

Using Icinga-API for fun and profit – IDO basics – part 1

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’ 		=><Type of database>,
	‘host’ 		=><Database hostname>,
	‘database’ 	=><Databasename>,
	‘user’ 		=><Username>,
	‘password’ 	=><password>,
	‘persistent’ 	=> <true | false>,
	‘table_prefix’ 	=><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.<br />;
}

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:

<?
// Path to icinga api file
$apiFile = ‘icinga-api/IcingaApi.php’;
 
// Database connection
$idoConfig = array (
	‘type’ 		=>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.<br />;
}
?>

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

Please have a look at the git repository for further information:
https://git.icinga.org/index?p=icinga-api.git;a=summary

Have fun!

Share

5 comments to Using Icinga-API for fun and profit – IDO basics – part 1

  • Will there be a soap interface for this api? This would be GREAT and AWESOME!!!

    • Spike

      @Thomas:
      Could you please expand on why you’d like a soap interface, how you plan to use it, and why that would be better over say a RESTful webservice or xml-rpc? thanks

  • Hi all,

    Any chance for an update on this? How far along are you? What’s the status of the REST interface? Really looking forward to that one.

    Keep up the good work!

  • Bernd Erk

    Hi Tim,

    thank you for the feedback. At the moment there is no active work on the REST Interface. We are working on the details in the web-frontend and API support for Oracle backend.

    Checkout alpha.icinga.org to see the actual development.

    REST is our timeline after first stable release.

    Regards

    Bernd

  • Can somebody please tell me where that configuration code is supposed to go? I’ve looked all over the place but can’t find anything like it.

    Thanks

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">