From 7e6e2ae5c60a3b70214599caa3d57e4a68e9f960 Mon Sep 17 00:00:00 2001 From: Robin Sonefors Date: Wed, 9 Apr 2014 09:54:00 +0200 Subject: [PATCH] livestatus: Stop relying on broken short-cut Earlier versions of the livestatus library had a short-cut, where you could give a string as a column name, in which case the library would assume you wanted less deeply nested arrays back - which as if by accident happened to correspond to what we need whenever we want to get a list of names. This version of the livestatus library's support for stat responses seemingly makes the shortcut appear to work, except the query building code doesn't support the shortcut, so that instead of restricting the columns in the query to what we'll be returning, we download ALL THE DATAS! This has the downside of large installations eating up tons of memory and crashing horribly whenever a stupidly written form is about to be rendered. This works around that "quirk" in a minimally invasive way, by no longer relying on that shortcut in all the places I've found it to be used. The PNP helper by design throws an exception, so it won't run in a browser, but it showed up in my grep. The execute_command code (bug #8617) should no longer be trivially triggerable, but was manually verified when the same change was made for 6.2. The tests passes. Signed-off-by: Robin Sonefors --- application/helpers/nagvisconfig.php | 5 ++++- application/helpers/pnp.php | 4 ++-- application/models/execute_command.php | 14 +++++++------- test/unit_test/tests/report_Test.php | 8 ++++++-- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/application/helpers/nagvisconfig.php b/application/helpers/nagvisconfig.php index 069be3be2..2ce02b9bd 100644 --- a/application/helpers/nagvisconfig.php +++ b/application/helpers/nagvisconfig.php @@ -23,11 +23,14 @@ class nagvisconfig_Core { $nagvis_config = $cfg->getConfig('nagvis'); $contactgroups = Livestatus::instance()->getContactGroups(array( - 'columns'=>'name', + 'columns'=>array('name'), 'filter'=>array( 'members'=>array('>='=>$auth->get_user()->username) ) )); + foreach ($contactgroups as $idx) { + $contactgroups[$idx] = $contactgroups[$idx]['name']; + } $groups_per_type = array( 'auth_groups' => $auth->get_user()->groups, diff --git a/application/helpers/pnp.php b/application/helpers/pnp.php index 31ad3ca9f..6ea7eb348 100644 --- a/application/helpers/pnp.php +++ b/application/helpers/pnp.php @@ -17,10 +17,10 @@ throw new Exception('deprecated'); $ls = Livestatus::instance(); $objects = null; if (empty($service)) { - $objects = $ls->getHosts(array('filter' => array('name' => $host), 'columns' => 'pnpgraph_present')); + $objects = $ls->getHosts(array('filter' => array('name' => $host), 'columns' => array('pnpgraph_present'))); } else { $service = urldecode($service); - $objects = $ls->getServices(array('filter' => array('host_name' => $host, 'description' => $service), 'columns' => 'pnpgraph_present')); + $objects = $ls->getServices(array('filter' => array('host_name' => $host, 'description' => $service), 'columns' => array('pnpgraph_present'))); } if (isset($objects) and isset($objects[0]) and $objects[0]['pnpgraph_present'] === 1) { return true; diff --git a/application/models/execute_command.php b/application/models/execute_command.php index 517b56816..42bc29d93 100644 --- a/application/models/execute_command.php +++ b/application/models/execute_command.php @@ -43,9 +43,6 @@ class Execute_Command_Model extends Model { $ary = array(); switch ($param_name) { - case 'host_name': - $ary = Livestatus::instance()->getHosts(array('columns' => 'name')); - break; case 'service': case 'service_description': $ary = Livestatus::instance()->getServices(array('columns' => array('host_name', 'description'))); @@ -54,19 +51,22 @@ class Execute_Command_Model extends Model foreach ($ary as $v) { $ret_ary[] = $v['host_name'].';'.$v['description']; } - $ary = $ret_ary; + return $ret_ary; } break; + case 'host_name': + $ary = Livestatus::instance()->getHosts(array('columns' => array('name'))); + break; case 'hostgroup_name': - $ary = Livestatus::instance()->getHostgroups(array('columns' => 'name')); + $ary = Livestatus::instance()->getHostgroups(array('columns' => array('name'))); break; case 'servicegroup_name': - $ary = Livestatus::instance()->getServicegroups(array('columns' => 'name')); + $ary = Livestatus::instance()->getServicegroups(array('columns' => array('name'))); break; } $res = array(); foreach ($ary as $val) { - $res[$val] = $val; + $res[$val['name']] = $val['name']; } return $res; diff --git a/test/unit_test/tests/report_Test.php b/test/unit_test/tests/report_Test.php index 6b0836325..b3b60a6dc 100644 --- a/test/unit_test/tests/report_Test.php +++ b/test/unit_test/tests/report_Test.php @@ -89,8 +89,12 @@ class report_Test extends PHPUnit_Framework_TestCase { if ($this->auth->authorized_for('service_view_all')) $msg .= ' with service_view_all'; - $out = Livestatus::instance()->getHosts(array('columns' => 'name')); - $opts['host_name'] = $out; + $out = Livestatus::instance()->getHosts(array('columns' => array('name'))); + $res = array(); + foreach ($out as $row) { + $res[] = $row['name']; + } + $opts['host_name'] = $res; $result = array(); for ($host_state = 1; $host_state <= 7; $host_state++) { $opts['host_states'] = $host_state; -- 2.11.4.GIT