'Alert Association Rule Migration', 'padding' => TRUE, 'header-border' => TRUE));
$checkers = cache_alert_rules();
$assocs = cache_alert_assoc();
foreach($assocs as $assoc)
{
$checkers[$assoc['alert_test_id']]['assocs'][] = $assoc;
}
//echo '
Alert Rules
';
echo 'This page will assist you to migrate from legacy association format to the new association format.';
//print_vars($checkers);
echo generate_box_close();
foreach($checkers as $alert)
{
if($vars['migrate'] == $alert['alert_test_id'] || (!$alert['alert_assoc'] && $vars['migrate'] == 'all'))
{
print_message("Migrating ".$alert['alert_name']);
$ruleset = migrate_assoc_rules($alert);
dbUpdate(array('alert_assoc' => json_encode($ruleset)), 'alert_tests', '`alert_test_id` = ?', array($alert['alert_test_id']));
dbDelete('alert_assoc', '`alert_test_id` = ?', array($alert['alert_test_id']));
$alert['alert_assoc'] = json_encode($ruleset);
update_alert_table($alert, FALSE);
}
}
$checkers = cache_alert_rules();
$assocs = cache_alert_assoc();
foreach($assocs as $assoc)
{
$checkers[$assoc['alert_test_id']]['assocs'][] = $assoc;
}
foreach($checkers as $alert)
{
//if($alert['alert_test_id'] != 11) { continue; }
echo generate_box_open(array('title' => $alert['alert_name'], 'padding' => TRUE, 'header-border' => TRUE));
if(!$alert['alert_assoc']) {
$ruleset = migrate_assoc_rules($alert);
$query = parse_qb_ruleset($alert['entity_type'], $ruleset, TRUE);
$data = dbFetchRows($query);
$error = dbError();
$field = $config['entities'][$alert['entity_type']]['table_fields']['id'];
$existing_entities = get_alert_entities($alert['alert_test_id']);
$entities = array();
foreach($data as $datum)
{
$entities[$datum[$field]] = array('entity_id' => $datum[$field], 'device_id' => $datum['device_id']);
}
$legacy = get_alert_entities_from_assocs($alert);
//r($legacy);
//r($entities);
//r($existing_entities);
$add = array_diff_key($entities, $existing_entities);
$remove = array_diff_key($existing_entities, $entities);
//print_vars(array_diff_key($legacy, $entities));
//echo count($add) .' to add, '.count($remove).' to remove.
';
$changed = count($add) + count($remote);
$result = count($data);
$existing = count(get_alert_entities($alert['alert_test_id']));
echo '';
echo '
';
echo '
';
if(is_array($alert['assocs']))
{
echo '
Original Association Rules
';
echo('
');
// Loop the associations which link this alert to this device
foreach ($alert['assocs'] as $assoc_id => $assoc)
{
echo('');
echo('');
if (is_array($assoc['device_attribs']))
{
$text_block = array();
foreach ($assoc['device_attribs'] as $attribute)
{
$text_block[] = escape_html($attribute['attrib'].' '.$attribute['condition'].' '.$attribute['value']);
}
echo(''.implode(' ', $text_block).' ');
} else {
echo '* ';
}
echo(' | ');
echo('');
if (is_array($assoc['entity_attribs']))
{
$text_block = array();
foreach ($assoc['entity_attribs'] as $attribute)
{
$text_block[] = escape_html($attribute['attrib'].' '.$attribute['condition'].' '.$attribute['value']);
}
echo(''.implode(' ', $text_block).' ');
} else {
echo '* ';
}
echo(' | ');
echo('
');
}
// End loop of associations
echo '
';
} else {
print_message("No Legacy Associations!");
}
echo '
';
echo '
New Association Ruleset
';
echo render_qb_rules($alert['entity_type'], $ruleset);
echo '
';
echo '
';
$c_exist = count($existing_entities);
$c_legacy = count($legacy);
$c_new = count($entities);
//echo 'Database | '.count($existing_entities).' | Legacy Rules | '.count($legacy).' | New Ruleset | '.count($entities).' |
---|
';
if ($changed != 0) {
print_error("Migration results don't match for group ".$alert['alert_name'].". Please report this to the Observium developers. (DB: ". $c_exist ." | Old: ".$c_legacy." | New: ".$c_new.")");
echo '';
//print_r($alert['assocs']);
echo '
';
echo '' . safe_json_encode($ruleset, JSON_PRETTY_PRINT) . '
';
r($query);
//r($add);
//r($remove);
} else {
print_message("Migration results match! (DB: ". $c_exist ." | Old: ".$c_legacy." | New: ".$c_new.")", 'success');
echo 'Migrate Alert';
}
//echo "results: " . $result . "
";
//echo "existing:" . $existing . "
";
//echo "error:" . $error;
//echo "";
} else {
echo 'Alert '.$alert['alert_name']. ' already has new-style association rules
';
}
echo generate_box_close();
}
// EOF