Add Inspection API to db connection

refs #9641
This commit is contained in:
Matthias Jentsch 2015-07-16 15:29:45 +02:00
parent 3cd6d2e490
commit f4054d575b
2 changed files with 48 additions and 10 deletions

View File

@ -129,16 +129,13 @@ class DbResourceForm extends Form
*/
public static function isValidResource(Form $form)
{
try {
$resource = ResourceFactory::createResource(new ConfigObject($form->getValues()));
$resource->getConnection()->getConnection();
} catch (Exception $e) {
$form->addError(
$form->translate('Connectivity validation failed, connection to the given resource not possible.')
);
return false;
$result = ResourceFactory::createResource(new ConfigObject($form->getValues()))->inspect();
if ($result->hasError()) {
$form->addError(sprintf($form->translate('Connectivity validation failed: %s'), $result->getError()));
}
return true;
// TODO: display diagnostics in $result->toArray() to the user
return ! $result->hasError();
}
}

View File

@ -3,6 +3,9 @@
namespace Icinga\Data\Db;
use Exception;
use Icinga\Data\Inspectable;
use Icinga\Data\Inspection;
use PDO;
use Iterator;
use Zend_Db;
@ -23,7 +26,7 @@ use Icinga\Exception\ProgrammingError;
/**
* Encapsulate database connections and query creation
*/
class DbConnection implements Selectable, Extensible, Updatable, Reducible
class DbConnection implements Selectable, Extensible, Updatable, Reducible, Inspectable
{
/**
* Connection config
@ -435,4 +438,42 @@ class DbConnection implements Selectable, Extensible, Updatable, Reducible
return $column . ' ' . $sign . ' ' . $this->dbAdapter->quote($value);
}
}
public function inspect()
{
$insp = new Inspection('Db Connection');
try {
$this->getDbAdapter()->getConnection();
$config = $this->dbAdapter->getConfig();
$insp->write(sprintf(
'Connection to %s as %s on %s:%s successful',
$config['dbname'],
$config['username'],
$config['host'],
$config['port']
));
switch ($this->dbType) {
case 'mysql':
$rows = $this->dbAdapter->query(
'SHOW VARIABLES WHERE variable_name ' .
'IN (\'version\', \'protocol_version\', \'version_compile_os\');'
)->fetchAll();
$sqlinsp = new Inspection('MySQL');
foreach ($rows as $row) {
$sqlinsp->write($row->variable_name . ': ' . $row->value);
}
$insp->write($sqlinsp);
break;
case 'pgsql':
$row = $this->dbAdapter->query('SELECT version();')->fetchAll();
$sqlinsp = new Inspection('PostgreSQL');
$sqlinsp->write($row[0]->version);
$insp->write($sqlinsp);
break;
}
} catch (Exception $e) {
return $insp->error(sprintf('Connection failed %s', $e->getMessage()));
}
return $insp;
}
}