icingaweb2-module-director/library/Director/Db.php

398 lines
11 KiB
PHP
Raw Normal View History

<?php
namespace Icinga\Module\Director;
use Icinga\Data\Db\DbConnection;
use Icinga\Module\Director\Objects\DirectorDeploymentLog;
2015-08-28 18:02:44 +02:00
use Zend_Db_Expr;
class Db extends DbConnection
{
protected $modules = array();
protected static $zoneCache;
protected static $commandCache;
protected function db()
{
return $this->getDbAdapter();
}
2015-06-18 11:01:45 +02:00
public function fetchActivityLogEntryById($id)
{
$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
}
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
}
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);
}
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)
{
$db = $this->db();
$query = $db->select()->from(
2015-07-30 08:19:34 +02:00
array('p' => 'icinga_' . $type),
array(
2015-07-30 08:19:34 +02:00
'name' => 'o.object_name',
'parent' => 'p.object_name'
)
)->join(
2015-07-30 08:19:34 +02:00
array('i' => 'icinga_' . $type . '_inheritance'),
'p.id = i.parent_' . $type . '_id',
array()
)->join(
2015-07-30 08:19:34 +02:00
array('o' => 'icinga_' . $type),
'o.id = i.' . $type . '_id',
array()
2015-07-30 08:19:34 +02:00
)->where("o.object_type = 'template'")
->order('p.object_name')
->order('o.object_name');
2015-08-02 13:29:20 +02:00
return $db->fetchAll($query);
}
public function fetchTemplateTree($type)
{
$relations = $this->fetchTemplateRelations($type);
$children = array();
2015-07-30 08:19:34 +02:00
$objects = array();
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(
'name' => $rel->$col,
'children' => array()
);
}
}
}
2015-07-30 08:19:34 +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-30 08:19:34 +02:00
foreach ($children as $name => $object) {
unset($objects[$name]);
}
2015-07-30 08:19:34 +02:00
return $objects;
}
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);
}
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);
}
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);
}
public function createImportedRowsetRowsQuery($checksum, $columns = null)
{
$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',
'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);
$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);
return $query;
}
public function fetchImportedRowsetRows($checksum, $columns = null)
{
return $this->db()->fetchAll(
$this->createImportedRowsetRowsQuery($checksum, $columns)
);
}
public function enumCommands()
{
2015-07-29 18:07:21 +02:00
return $this->enumIcingaObjects('command');
}
2015-07-29 18:07:21 +02:00
public function enumCheckcommands()
{
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);
}
public function getZoneName($id)
{
$objects = $this->enumZones();
return $objects[$id];
}
public function getCommandName($id)
{
$objects = $this->enumCommands();
return $objects[$id];
}
2015-07-29 18:07:21 +02:00
public function enumZones()
{
2015-07-29 18:07:21 +02:00
return $this->enumIcingaObjects('zone');
}
2015-06-02 17:29:07 +02:00
2015-07-29 18:07:21 +02:00
public function enumZoneTemplates()
{
2015-07-29 18:07:21 +02:00
return $this->enumIcingaTemplates('zone');
}
2015-07-29 18:07:21 +02:00
public function enumHosts()
{
2015-07-29 18:07:21 +02:00
return $this->enumIcingaObjects('host');
}
2015-07-29 18:07:21 +02:00
public function enumHostTemplates()
{
return $this->enumIcingaTemplates('host');
}
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
}
public function enumServices()
{
2015-07-29 18:07:21 +02:00
return $this->enumIcingaObjects('service');
}
public function enumServiceTemplates()
{
2015-07-29 18:07:21 +02:00
return $this->enumIcingaTemplates('service');
}
public function enumServicegroups()
{
2015-07-29 18:07:21 +02:00
return $this->enumIcingaObjects('servicegroup');
}
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
}
public function enumImportSource()
{
2015-07-30 08:33:35 +02:00
return $this->enum('import_source', array('id', 'source_name'));
}
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
}
public function enumDatafields()
{
2015-07-29 18:07:21 +02:00
return $this->enum('director_datafield', array(
'id',
2015-07-29 18:07:21 +02:00
"caption || ' (' || varname || ')'",
));
}
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-07-29 18:07:21 +02:00
$filters = array('object_type = ?' => 'object') + $filters;
return $this->enum('icinga_' . $type, null, $filters);
}
2015-07-29 18:07:21 +02:00
public function enumIcingaTemplates($type, $filters = array())
{
2015-07-29 18:07:21 +02:00
$filters = array('object_type = ?' => 'template') + $filters;
return $this->enum('icinga_' . $type, null, $filters);
}
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');
}
}