2015-04-24 14:26:44 +02:00
< ? php
namespace Icinga\Module\Director ;
use Icinga\Data\Db\DbConnection ;
class Db extends DbConnection
{
protected $modules = array ();
2015-06-08 14:37:02 +02:00
protected static $zoneCache ;
protected static $commandCache ;
2015-04-24 14:26:44 +02:00
protected function db ()
{
return $this -> getDbAdapter ();
}
2015-06-18 11:01:45 +02:00
public function fetchActivityLogEntryById ( $id )
2015-06-01 14:33:07 +02:00
{
$sql = 'SELECT * FROM director_activity_log WHERE id = ' . ( int ) $id ;
return $this -> db () -> fetchRow ( $sql );
}
2015-06-18 11:01:45 +02:00
public function fetchActivityLogEntry ( $checksum )
2015-06-23 16:45:25 +02:00
{
2015-06-23 16:12:26 +02:00
if ( $this -> getDbType () === 'pgsql' ) {
2015-06-23 16:45:25 +02:00
$checksum = new \Zend_Db_Expr ( " \\ x " . bin2hex ( $checksum ));
2015-06-23 16:12:26 +02:00
}
2015-06-18 11:01:45 +02:00
$sql = 'SELECT * FROM director_activity_log WHERE checksum = ?' ;
2015-06-23 16:45:25 +02:00
$ret = $this -> db () -> fetchRow ( $sql , $checksum );
if ( is_resource ( $ret -> checksum )) {
$ret -> checksum = stream_get_contents ( $ret -> checksum );
}
if ( is_resource ( $ret -> parent_checksum )) {
$ret -> checksum = stream_get_contents ( $ret -> parent_checksum );
}
2015-06-18 11:01:45 +02:00
2015-06-23 16:45:25 +02:00
return $ret ;
2015-06-18 11:01:45 +02:00
}
2015-04-24 15:57:01 +02:00
public function getLastActivityChecksum ()
{
2015-06-23 15:24:05 +02:00
if ( $this -> getDbType () === 'pgsql' ) {
2015-06-23 13:28:14 +02:00
$select = " SELECT checksum FROM (SELECT * FROM (SELECT 1 AS pos, LOWER(ENCODE(checksum, 'hex')) AS checksum FROM director_activity_log ORDER BY change_time DESC LIMIT 1) a UNION SELECT 2 AS pos, '' AS checksum) u ORDER BY pos LIMIT 1 " ;
2015-06-23 15:24:05 +02:00
} else {
$select = " SELECT checksum FROM (SELECT * FROM (SELECT 1 AS pos, LOWER(HEX(checksum)) AS checksum FROM director_activity_log ORDER BY change_time DESC LIMIT 1) a UNION SELECT 2 AS pos, '' AS checksum) u ORDER BY pos LIMIT 1 " ;
2015-06-23 13:28:14 +02:00
}
2015-04-24 15:57:01 +02:00
return $this -> db () -> fetchOne ( $select );
}
2015-04-24 14:26:44 +02:00
public function enumCheckcommands ()
{
$select = $this -> db () -> select () -> from ( 'icinga_command' , array (
'id' ,
'object_name' ,
)) -> where ( 'object_type IN (?)' , array ( 'object' , 'external_object' ))
-> where ( 'methods_execute IN (?)' , array ( 'PluginCheck' , 'IcingaCheck' ))
-> order ( 'object_name ASC' );
return $this -> db () -> fetchPairs ( $select );
}
2015-06-01 16:30:24 +02:00
public function enumCommands ()
{
2015-06-08 14:37:02 +02:00
if ( self :: $commandCache === null ) {
$select = $this -> db () -> select () -> from ( 'icinga_command' , array (
'id' ,
'object_name' ,
))
-> order ( 'object_name ASC' );
self :: $commandCache = $this -> db () -> fetchPairs ( $select );
}
return self :: $commandCache ;
2015-06-01 16:30:24 +02:00
}
2015-04-24 14:26:44 +02:00
public function enumZones ()
{
2015-06-08 14:37:02 +02:00
if ( self :: $zoneCache === null ) {
$select = $this -> db () -> select () -> from ( 'icinga_zone' , array (
'id' ,
'object_name' ,
2015-06-23 12:56:59 +02:00
)) -> where ( 'object_type = ?' , 'object' ) -> order ( 'object_name ASC' );
2015-06-08 14:37:02 +02:00
self :: $zoneCache = $this -> db () -> fetchPairs ( $select );
}
return self :: $zoneCache ;
}
public function getZoneName ( $id )
{
$objects = $this -> enumZones ();
return $objects [ $id ];
}
public function getCommandName ( $id )
{
$objects = $this -> enumCommands ();
return $objects [ $id ];
2015-04-24 14:26:44 +02:00
}
public function enumHosts ()
{
$select = $this -> db () -> select () -> from ( 'icinga_host' , array (
'id' ,
'object_name' ,
2015-06-23 12:56:59 +02:00
)) -> where ( 'object_type = ?' , 'object' ) -> order ( 'object_name ASC' );
2015-04-24 14:26:44 +02:00
return $this -> db () -> fetchPairs ( $select );
}
2015-06-02 17:29:07 +02:00
public function enumHostgroups ()
{
$select = $this -> db () -> select () -> from ( 'icinga_hostgroup' , array (
'id' ,
'object_name' ,
2015-06-23 12:56:59 +02:00
)) -> where ( 'object_type = ?' , 'object' ) -> order ( 'object_name ASC' );
2015-06-02 17:29:07 +02:00
return $this -> db () -> fetchPairs ( $select );
}
2015-06-03 14:59:29 +02:00
public function enumServices ()
{
$select = $this -> db () -> select () -> from ( 'icinga_service' , array (
'id' ,
'object_name' ,
2015-06-23 12:56:59 +02:00
)) -> where ( 'object_type = ?' , 'object' ) -> order ( 'object_name ASC' );
2015-06-03 14:59:29 +02:00
return $this -> db () -> fetchPairs ( $select );
}
public function enumServicegroups ()
{
$select = $this -> db () -> select () -> from ( 'icinga_servicegroup' , array (
'id' ,
'object_name' ,
2015-06-23 12:56:59 +02:00
)) -> where ( 'object_type = ?' , 'object' ) -> order ( 'object_name ASC' );
2015-06-03 14:59:29 +02:00
return $this -> db () -> fetchPairs ( $select );
}
2015-06-08 14:37:02 +02:00
2015-06-12 13:16:41 +02:00
public function enumUsers ()
{
$select = $this -> db () -> select () -> from ( 'icinga_user' , array (
'id' ,
'object_name' ,
2015-06-23 12:56:59 +02:00
)) -> where ( 'object_type = ?' , 'object' ) -> order ( 'object_name ASC' );
2015-06-12 13:16:41 +02:00
return $this -> db () -> fetchPairs ( $select );
}
public function enumUsergroups ()
{
$select = $this -> db () -> select () -> from ( 'icinga_usergroup' , array (
'id' ,
'object_name' ,
2015-06-23 12:56:59 +02:00
)) -> where ( 'object_type = ?' , 'object' ) -> order ( 'object_name ASC' );
2015-06-12 13:16:41 +02:00
return $this -> db () -> fetchPairs ( $select );
}
2015-06-08 14:37:02 +02:00
public function clearZoneCache ()
{
// TODO: wipe cache on update/insert/delete
self :: $zoneCache = null ;
}
public function clearCommandCache ()
{
// TODO: wipe cache on update/insert/delete
self :: $commandCache = null ;
}
2015-04-24 14:26:44 +02:00
}