Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions poller_graphs.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,14 @@ function hmib_gt_graph(int $host_id, int $graph_template_id): void {
[$host_id, $graph_template_id]);

if (!$exists) {
// Honor the Graph Template 'Test Data Source' setting: if enabled and the
// data source does not return valid data for this host, skip the graph.
if (!test_data_sources($graph_template_id, $host_id)) {
Comment thread
TheWitness marked this conversation as resolved.
Resolved
debug("Skipping Graph Template '$name' for Host: $host_id - Test Data Source returned no valid data");

return;
}

print "NOTE: Adding Graph: '$name' for Host: " . $host_id;

$command = "$php_bin -q $base/cli/add_graphs.php" .
Expand Down Expand Up @@ -450,6 +458,14 @@ function add_summary_graphs(int $host_id, int $host_template): void {
[$host_id, $gt['graph_template_id']]);

if (!$exists) {
// Honor the Graph Template 'Test Data Source' setting: if enabled and the
// data source does not return valid data for this host, skip the graph.
if (!test_data_sources($gt['graph_template_id'], $host_id)) {
debug('Skipping Graph Template ' . $gt['graph_template_id'] . " for Host: $host_id - Test Data Source returned no valid data");

continue;
}

print "NOTE: Adding item: '" . $gt['graph_template_id'] . "' for Host: " . $host_id;

$command = "$php_bin -q $base/cli/add_graphs.php" .
Expand Down Expand Up @@ -564,6 +580,14 @@ function hmib_dq_graphs(int $host_id, int $query_id, int $graph_template_id, int
[$host_id, $query_id, $graph_template_id, $index]);

if (!$exists) {
// Honor the Graph Template 'Test Data Source' setting: if enabled and the
// data source does not return valid data for this host/index, skip the graph.
if (!test_data_sources($graph_template_id, $host_id, $query_id, $index)) {
debug("Skipping Data Query Graph for Host: $host_id, index: $index - Test Data Source returned no valid data");

continue;
}

$command = "$php_bin -q $base/cli/add_graphs.php" .
' --graph-template-id=' . $graph_template_id .
' --graph-type=ds' .
Expand Down
87 changes: 76 additions & 11 deletions tests/Unit/PollerGraphsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@
* The script chdir()s, includes cli_check.php and exits at the top level, so
* it cannot be included. The functions under test are cut out of the source
* and defined here, with the helpers they call replaced by recorders.
*
* The Test Data Source gate (test_data_sources()) mirrors Cacti's own
* create_save_graph(): a Graph Template with 'Test Data Source' enabled must
* not be graphed on a device whose data source returns no valid data.
*/

if (!function_exists('debug')) {
function debug($message) {
$GLOBALS['__debug_out'][] = (string) $message;
}
}

Expand All @@ -32,9 +37,20 @@ function cacti_escapeshellarg($string) {
}
}

if (!function_exists('hmib_gt_graph')) {
function hmib_gt_graph($host_id, $graph_template_id) {
if (!function_exists('exec_with_timeout')) {
function exec_with_timeout($command, &$output, &$return_code, $timeout) {
$GLOBALS['__exec_count']++;
$output = array('Graph Added');
$return_code = 0;
}
}

if (!function_exists('test_data_sources')) {
function test_data_sources($graph_template_id, $host_id, $snmp_query_id = 0, $snmp_index = '', $values = array()) {
$GLOBALS['__graph_calls'][] = array('gt', $host_id, $graph_template_id);
$GLOBALS['__tds_args'][] = array($graph_template_id, $host_id, $snmp_query_id, $snmp_index);

return $GLOBALS['__tds_return'];
}
}

Expand All @@ -44,20 +60,20 @@ function add_host_dq_graphs($host_id, $dq, $field = '', $regex = '', $include =
}
}

if (!function_exists('hmib_dq_graphs')) {
function hmib_dq_graphs($host_id, $query_id, $graph_template_id, $query_type_id) {
$GLOBALS['__graph_calls'][] = array('dqg', $host_id, $query_id);
}
}

beforeAll(function () {
hmib_test_define_function('poller_graphs.php', 'add_host_based_graphs');
hmib_test_define_function('poller_graphs.php', 'add_summary_graphs');
hmib_test_define_function('poller_graphs.php', 'hmib_gt_graph');
hmib_test_define_function('poller_graphs.php', 'hmib_dq_graphs');
});

beforeEach(function () {
hmib_test_reset_db_mocks();
$GLOBALS['__graph_calls'] = array();
$GLOBALS['__tds_args'] = array();
$GLOBALS['__debug_out'] = array();
$GLOBALS['__tds_return'] = true;
$GLOBALS['__exec_count'] = 0;

// The summary device code starts a PHP binary; `true` accepts the arguments and does nothing.
$GLOBALS['__test_config'] = array('path_php_binary' => 'true');
Expand All @@ -77,23 +93,23 @@ function hmib_test_templates($users, $procs) {

add_host_based_graphs();

expect($GLOBALS['__graph_calls'])->toBe(array(array('gt', '9', '11'), array('gt', '9', '12')));
expect($GLOBALS['__graph_calls'])->toBe(array(array('gt', 9, 11), array('gt', 9, 12)));
});

it('does not add the processes graph when only the users template exists', function () {
hmib_test_templates('11', false);

add_host_based_graphs();

expect($GLOBALS['__graph_calls'])->toBe(array(array('gt', '9', '11')));
expect($GLOBALS['__graph_calls'])->toBe(array(array('gt', 9, 11)));
});

it('adds the processes graph when only the processes template exists', function () {
hmib_test_templates(false, '12');

add_host_based_graphs();

expect($GLOBALS['__graph_calls'])->toBe(array(array('gt', '9', '12')));
expect($GLOBALS['__graph_calls'])->toBe(array(array('gt', 9, 12)));
});

it('adds the disk and processor data query graphs when those queries exist', function () {
Expand All @@ -106,6 +122,55 @@ function hmib_test_templates($users, $procs) {
expect($GLOBALS['__graph_calls'])->toBe(array(array('dq', '9', '22'), array('dq', '9', '21')));
});

it('skips a device graph template when its Test Data Source check fails', function () {
$GLOBALS['__tds_return'] = false;

hmib_gt_graph(9, 11);

expect($GLOBALS['__tds_args'])->toBe(array(array(11, 9, 0, '')));
expect($GLOBALS['__exec_count'])->toBe(0);
expect(implode(' ', $GLOBALS['__debug_out']))->toContain('Skipping Graph Template');
});

it('creates a device graph template when its Test Data Source check passes', function () {
$GLOBALS['__tds_return'] = true;

hmib_gt_graph(9, 11);

expect($GLOBALS['__tds_args'])->toBe(array(array(11, 9, 0, '')));
expect($GLOBALS['__exec_count'])->toBe(1);
});

it('skips a data query graph when its Test Data Source check fails', function () {
hmib_test_mock_db('db_fetch_assoc_prepared', 'host_snmp_cache', array(
array('field_value' => 'disk one', 'snmp_index' => '7'),
));

$GLOBALS['__tds_return'] = false;

hmib_dq_graphs(9, 3, 11, 5, 'hrStorageDescr');

expect($GLOBALS['__tds_args'])->toBe(array(array(11, 9, 3, '7')));
expect(implode(' ', $GLOBALS['__debug_out']))->toContain('Skipping Data Query Graph');
});

it('skips a summary template graph when its Test Data Source check fails', function () {
$GLOBALS['__tds_return'] = false;

// No data-query graphs; one directly-assigned host_graph template.
hmib_test_mock_db('db_fetch_assoc_prepared', function ($sql, $params) {
return strpos($sql, 'FROM host_graph') !== false;
}, array(
array('graph_template_id' => 11),
));

add_summary_graphs(42, 5);

expect($GLOBALS['__tds_args'])->toBe(array(array(11, 42, 0, '')));
expect($GLOBALS['__exec_count'])->toBe(0);
expect(implode(' ', $GLOBALS['__debug_out']))->toContain('Skipping Graph Template');
});

it('builds the summary graphs for the device it just created', function () {
$queries = array();

Expand Down
Loading