2015-04-24 14:26:44 +02:00
< ? php
namespace Icinga\Module\Director ;
use Icinga\Data\Db\DbConnection ;
2015-08-29 01:05:23 +02:00
use Icinga\Module\Director\Objects\DirectorDeploymentLog ;
2015-08-28 18:02:44 +02:00
use Zend_Db_Expr ;
2015-04-24 14:26:44 +02:00
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-08-28 18:02:44 +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-07-27 22:39:29 +02:00
public function fetchImportStatistics ()
{
$query = " SELECT 'imported_properties' AS stat_name, COUNT(*) AS stat_value "
. " FROM import_run i "
. " JOIN imported_rowset_row rs ON i.rowset_checksum = rs.rowset_checksum "
. " JOIN imported_row_property rp ON rp.row_checksum = rs.row_checksum "
. " UNION ALL "
. " SELECT 'imported_rows' AS stat_name, COUNT(*) AS stat_value "
. " FROM import_run i "
. " JOIN imported_rowset_row rs ON i.rowset_checksum = rs.rowset_checksum "
. " UNION ALL "
. " SELECT 'unique_rows' AS stat_name, COUNT(*) AS stat_value "
. " FROM imported_row "
. " UNION ALL "
. " SELECT 'unique_properties' AS stat_name, COUNT(*) AS stat_value "
. " FROM imported_property "
;
return $this -> db () -> fetchPairs ( $query );
}
2015-07-26 15:36:32 +02:00
public function getImportrunRowsetChecksum ( $id )
{
$db = $this -> db ();
$query = $db -> select ()
-> from ( 'import_run' , 'rowset_checksum' )
-> where ( 'id = ?' , $id );
return $db -> fetchOne ( $query );
}
2015-08-02 13:29:20 +02:00
protected function fetchTemplateRelations ( $type )
2015-07-29 17:19:45 +02:00
{
$db = $this -> db ();
$query = $db -> select () -> from (
2015-07-30 08:19:34 +02:00
array ( 'p' => 'icinga_' . $type ),
2015-07-29 17:19:45 +02:00
array (
2015-07-30 08:19:34 +02:00
'name' => 'o.object_name' ,
'parent' => 'p.object_name'
2015-07-29 17:19:45 +02:00
)
) -> join (
2015-07-30 08:19:34 +02:00
array ( 'i' => 'icinga_' . $type . '_inheritance' ),
'p.id = i.parent_' . $type . '_id' ,
2015-07-29 17:19:45 +02:00
array ()
) -> join (
2015-07-30 08:19:34 +02:00
array ( 'o' => 'icinga_' . $type ),
'o.id = i.' . $type . '_id' ,
2015-07-29 17:19:45 +02:00
array ()
2015-07-30 08:19:34 +02:00
) -> where ( " o.object_type = 'template' " )
-> order ( 'p.object_name' )
-> order ( 'o.object_name' );
2015-07-29 17:19:45 +02:00
2015-08-02 13:29:20 +02:00
return $db -> fetchAll ( $query );
}
public function fetchTemplateTree ( $type )
{
$relations = $this -> fetchTemplateRelations ( $type );
2015-07-29 17:19:45 +02:00
$children = array ();
2015-07-30 08:19:34 +02:00
$objects = array ();
2015-07-29 17:19:45 +02:00
foreach ( $relations as $rel ) {
2015-07-30 08:19:34 +02:00
foreach ( array ( 'name' , 'parent' ) as $col ) {
if ( ! array_key_exists ( $rel -> $col , $objects )) {
$objects [ $rel -> $col ] = ( object ) array (
2015-07-29 17:19:45 +02:00
'name' => $rel -> $col ,
'children' => array ()
);
}
}
}
2015-07-30 08:19:34 +02:00
2015-07-29 17:19:45 +02:00
foreach ( $relations as $rel ) {
2015-07-30 08:19:34 +02:00
$objects [ $rel -> parent ] -> children [ $rel -> name ] = $objects [ $rel -> name ];
$children [ $rel -> name ] = $rel -> parent ;
2015-07-29 17:19:45 +02:00
}
2015-07-30 08:19:34 +02:00
foreach ( $children as $name => $object ) {
unset ( $objects [ $name ]);
2015-07-29 17:19:45 +02:00
}
2015-07-30 08:19:34 +02:00
return $objects ;
2015-07-29 17:19:45 +02:00
}
2015-07-22 23:33:24 +02:00
public function fetchLatestImportedRows ( $source , $columns = null )
{
$db = $this -> db ();
$lastRun = $db -> select () -> from ( 'import_run' , array ( 'rowset_checksum' ));
if ( is_int ( $source ) || ctype_digit ( $source )) {
$lastRun -> where ( 'source_id = ?' , $source );
} else {
$lastRun -> where ( 'source_name = ?' , $source );
}
$lastRun -> order ( 'start_time DESC' ) -> limit ( 1 );
$checksum = $db -> fetchOne ( $lastRun );
return $this -> fetchImportedRowsetRows ( $checksum , $columns );
}
2015-08-28 23:46:40 +02:00
public function getLatestImportedChecksum ( $source )
{
if ( $this -> getDbType () === 'pgsql' ) {
$col = " LOWER(ENCODE(rowset_checksum, 'hex')) " ;
} else {
$col = '(LOWER(HEX(import_run.rowset_checksum)))' ;
}
$db = $this -> db ();
$lastRun = $db -> select () -> from ( 'import_run' , array ( $col ));
if ( is_int ( $source ) || ctype_digit ( $source )) {
$lastRun -> where ( 'source_id = ?' , ( int ) $source );
} else {
$lastRun -> where ( 'source_name = ?' , $source );
}
$lastRun -> order ( 'start_time DESC' ) -> limit ( 1 );
return $db -> fetchOne ( $lastRun );
}
2015-07-22 23:33:24 +02:00
public function listImportedRowsetColumnNames ( $checksum )
{
$db = $this -> db ();
$query = $db -> select () -> distinct () -> from (
array ( 'p' => 'imported_property' ),
'property_name'
) -> join (
array ( 'rp' => 'imported_row_property' ),
'rp.property_checksum = p.checksum' ,
array ()
) -> join (
array ( 'rsr' => 'imported_rowset_row' ),
'rsr.row_checksum = rp.row_checksum' ,
array ()
) -> where ( 'rsr.rowset_checksum = ?' , $checksum );
return $db -> fetchCol ( $query );
}
2015-07-26 15:40:53 +02:00
public function createImportedRowsetRowsQuery ( $checksum , $columns = null )
2015-07-22 23:33:24 +02:00
{
$db = $this -> db ();
$query = $db -> select () -> from (
array ( 'r' => 'imported_row' ),
array ()
) -> join (
array ( 'rsr' => 'imported_rowset_row' ),
'rsr.row_checksum = r.checksum' ,
array ()
) -> where ( 'rsr.rowset_checksum = ?' , $checksum );
$propertyQuery = $db -> select () -> from (
array ( 'rp' => 'imported_row_property' ),
array (
'property_value' => 'p.property_value' ,
2015-08-28 23:52:02 +02:00
'format' => 'p.format' ,
2015-07-22 23:33:24 +02:00
'row_checksum' => 'rp.row_checksum'
)
) -> join (
array ( 'p' => 'imported_property' ),
'rp.property_checksum = p.checksum' ,
array ()
);
$fetchColumns = array ( 'object_name' => 'r.object_name' );
if ( $columns === null ) {
$columns = $this -> listImportedRowsetColumnNames ( $checksum );
}
foreach ( $columns as $c ) {
$fetchColumns [ $c ] = sprintf ( 'p_%s.property_value' , $c );
2015-08-28 23:52:02 +02:00
$fetchColumns [ $c . '__f' ] = sprintf ( 'p_%s.format' , $c );
2015-07-22 23:33:24 +02:00
$p = clone ( $propertyQuery );
$query -> joinLeft (
array ( sprintf ( 'p_' . $c ) => $p -> where ( 'p.property_name = ?' , $c )),
sprintf ( 'p_%s.row_checksum = r.checksum' , $c ),
array ()
);
}
$query -> columns ( $fetchColumns );
2015-07-26 15:40:53 +02:00
return $query ;
}
public function fetchImportedRowsetRows ( $checksum , $columns = null )
{
return $this -> db () -> fetchAll (
$this -> createImportedRowsetRowsQuery ( $checksum , $columns )
);
2015-07-22 23:33:24 +02:00
}
2015-06-01 16:30:24 +02:00
public function enumCommands ()
{
2015-07-29 18:07:21 +02:00
return $this -> enumIcingaObjects ( 'command' );
2015-06-01 16:30:24 +02:00
}
2015-07-29 18:07:21 +02:00
public function enumCheckcommands ()
2015-04-24 14:26:44 +02:00
{
2015-07-29 18:07:21 +02:00
$filters = array (
'object_type IN (?)' => array ( 'object' , 'external_object' ),
'methods_execute IN (?)' => array ( 'PluginCheck' , 'IcingaCheck' ),
);
return $this -> enumIcingaObjects ( 'command' , $filters );
2015-06-08 14:37:02 +02:00
}
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
}
2015-07-29 18:07:21 +02:00
public function enumZones ()
2015-04-24 14:26:44 +02:00
{
2015-07-29 18:07:21 +02:00
return $this -> enumIcingaObjects ( 'zone' );
2015-04-24 14:26:44 +02:00
}
2015-06-02 17:29:07 +02:00
2015-07-29 18:07:21 +02:00
public function enumZoneTemplates ()
2015-07-28 15:19:37 +02:00
{
2015-07-29 18:07:21 +02:00
return $this -> enumIcingaTemplates ( 'zone' );
2015-07-28 15:19:37 +02:00
}
2015-07-29 18:07:21 +02:00
public function enumHosts ()
2015-07-24 09:51:31 +02:00
{
2015-07-29 18:07:21 +02:00
return $this -> enumIcingaObjects ( 'host' );
}
2015-07-24 09:51:31 +02:00
2015-07-29 18:07:21 +02:00
public function enumHostTemplates ()
{
return $this -> enumIcingaTemplates ( 'host' );
2015-07-24 09:51:31 +02:00
}
2015-06-02 17:29:07 +02:00
public function enumHostgroups ()
{
2015-07-29 18:07:21 +02:00
return $this -> enumIcingaObjects ( 'hostgroup' );
2015-06-02 17:29:07 +02:00
}
2015-06-03 14:59:29 +02:00
public function enumServices ()
{
2015-07-29 18:07:21 +02:00
return $this -> enumIcingaObjects ( 'service' );
2015-06-03 14:59:29 +02:00
}
2015-07-28 15:19:37 +02:00
public function enumServiceTemplates ()
{
2015-07-29 18:07:21 +02:00
return $this -> enumIcingaTemplates ( 'service' );
2015-07-28 15:19:37 +02:00
}
2015-06-03 14:59:29 +02:00
public function enumServicegroups ()
{
2015-07-29 18:07:21 +02:00
return $this -> enumIcingaObjects ( 'servicegroup' );
2015-06-03 14:59:29 +02:00
}
2015-06-08 14:37:02 +02:00
2015-06-12 13:16:41 +02:00
public function enumUsers ()
{
2015-07-29 18:07:21 +02:00
return $this -> enumIcingaObjects ( 'user' );
}
public function enumUserTemplates ()
{
return $this -> enumIcingaTemplates ( 'user' );
}
public function enumUsergroups ()
{
return $this -> enumIcingaObjects ( 'usergroup' );
}
public function enumSyncRule ()
{
return $this -> enum ( 'sync_rule' , array ( 'id' , 'rule_name' ));
2015-06-12 13:16:41 +02:00
}
2015-07-24 09:51:31 +02:00
public function enumImportSource ()
{
2015-07-30 08:33:35 +02:00
return $this -> enum ( 'import_source' , array ( 'id' , 'source_name' ));
2015-07-24 09:51:31 +02:00
}
2015-07-28 11:49:30 +02:00
public function enumDatalist ()
{
2015-07-29 18:07:21 +02:00
return $this -> enum ( 'director_datalist' , array ( 'id' , 'list_name' ));
2015-07-28 11:49:30 +02:00
}
2015-07-28 15:19:37 +02:00
public function enumDatafields ()
{
2015-07-29 18:07:21 +02:00
return $this -> enum ( 'director_datafield' , array (
2015-07-28 15:19:37 +02:00
'id' ,
2015-07-29 18:07:21 +02:00
" caption || ' (' || varname || ')' " ,
));
2015-07-28 15:19:37 +02:00
}
2015-07-29 18:07:21 +02:00
public function enum ( $table , $columns = null , $filters = array ())
2015-06-12 13:16:41 +02:00
{
2015-07-29 18:07:21 +02:00
if ( $columns === null ) {
$columns = array ( 'id' , 'object_name' );
}
$select = $this -> db () -> select () -> from ( $table , $columns ) -> order ( $columns [ 1 ]);
foreach ( $filters as $key => $val ) {
$select -> where ( $key , $val );
}
2015-06-12 13:16:41 +02:00
return $this -> db () -> fetchPairs ( $select );
}
2015-07-29 18:07:21 +02:00
public function enumIcingaObjects ( $type , $filters = array ())
2015-06-08 14:37:02 +02:00
{
2015-07-29 18:07:21 +02:00
$filters = array ( 'object_type = ?' => 'object' ) + $filters ;
return $this -> enum ( 'icinga_' . $type , null , $filters );
2015-06-08 14:37:02 +02:00
}
2015-07-29 18:07:21 +02:00
public function enumIcingaTemplates ( $type , $filters = array ())
2015-06-08 14:37:02 +02:00
{
2015-07-29 18:07:21 +02:00
$filters = array ( 'object_type = ?' => 'template' ) + $filters ;
return $this -> enum ( 'icinga_' . $type , null , $filters );
2015-06-08 14:37:02 +02:00
}
2015-08-29 01:05:23 +02:00
public function getUncollectedDeployments ()
{
$db = $this -> db ();
$query = $db -> select ()
-> from ( 'director_deployment_log' )
-> where ( 'stage_name IS NOT NULL' )
-> where ( 'stage_collected IS NULL' )
-> order ( 'stage_name' );
return DirectorDeploymentLog :: loadAll ( $this , $query , 'stage_name' );
}
2015-04-24 14:26:44 +02:00
}