function update_device_alert_table($device) { $dbc = array(); $alert_table = array(); $msg = "<h4>Building alerts for device " . $device['hostname'] . '</h4>'; $msg_class = ''; $conditions = cache_device_conditions($device); //foreach ($conditions['cond'] as $test_id => $test) //{ // #print_vars($test); // #echo('<span class="label label-info">Matched '.$test['alert_name'].'</span> '); //} $db_cs = dbFetchRows("SELECT * FROM `alert_table` WHERE `device_id` = ?", array($device['device_id'])); foreach ($db_cs as $db_c) { $dbc[$db_c['entity_type']][$db_c['entity_id']][$db_c['alert_test_id']] = $db_c; } $msg .= '<br /><h5>Checkers matching this device:</h5> '; foreach ($conditions['cond'] as $alert_test_id => $alert_test) { $msg .= '<span class="label label-info">' . $alert_test['alert_name'] . '</span> '; foreach ($alert_test['assoc'] as $assoc_id => $assoc) { // Check that the entity_type matches the one we're interested in. // echo("Matching $assoc_id (".$assoc['entity_type'].")"); list($entity_table, $entity_id_field, $entity_name_field) = entity_type_translate($assoc['entity_type']); $alert = $conditions['cond'][$assoc['alert_test_id']]; $entities = match_device_entities($device['device_id'], $assoc['attributes'], $assoc['entity_type']); foreach ($entities as $id => $entity) { $alert_table[$assoc['entity_type']][$entity[$entity_id_field]][$assoc['alert_test_id']][] = $assoc_id; } // echo(count($entities)." matched".PHP_EOL); $msg .= '<br />'; } } $msg .= '<h5>Matching entities:</h5>'; foreach ($alert_table as $entity_type => $entities) { foreach ($entities as $entity_id => $entity) { $entity_name = entity_name($entity_type, $entity_id); $msg .= '<span class="label label-ok">' . $entity_name . '</span> '; foreach ($entity as $alert_test_id => $b) { # echo(str_pad($entity_type, "20").str_pad($entity_id, "20").str_pad($alert_test_id, "20")); # echo(str_pad(implode($b,","), "20")); $msg .= '<span class="label label-info">' . $conditions['cond'][$alert_test_id]['alert_name'] . '</span><br >'; $msg_class = 'success'; if (isset($dbc[$entity_type][$entity_id][$alert_test_id])) { if ($dbc[$entity_type][$entity_id][$alert_test_id]['alert_assocs'] != implode($b, ",")) { $update_array = array('alert_assocs' => implode($b, ",")); } #echo("[".$dbc[$entity_type][$entity_id][$alert_test_id]['alert_assocs']."][".implode($b,",")."]"); if (is_array($update_array)) { dbUpdate($update_array, 'alert_table', '`alert_table_id` = ?', array($dbc[$entity_type][$entity_id][$alert_test_id]['alert_table_id'])); unset($update_array); } unset($dbc[$entity_type][$entity_id][$alert_test_id]); } else { $alert_table_id = dbInsert(array('device_id' => $device['device_id'], 'entity_type' => $entity_type, 'entity_id' => $entity_id, 'alert_test_id' => $alert_test_id, 'alert_assocs' => implode($b, ",")), 'alert_table'); dbInsert(array('alert_table_id' => $alert_table_id), 'alert_table-state'); } } } } $msg .= "Checking for stale entries: "; foreach ($dbc as $type => $entity) { foreach ($entity as $entity_id => $alert) { foreach ($alert as $alert_test_id => $data) { dbDelete('alert_table', "`alert_table_id` = ?", array($data['alert_table_id'])); dbDelete('alert_table-state', "`alert_table_id` = ?", array($data['alert_table_id'])); $msg .= "-"; } } } print_message($msg, $msg_class); }
function update_device_group_table($device) { $groups_table_db = array(); $group_table = array(); $msg = '<h4 style="">构建组成员资格 ' . generate_device_link($device) . ':</h4>'; $msg_class = ''; $msg_enable = FALSE; $groups = cache_device_groups($device); $g_t_dbs = dbFetchRows("SELECT * FROM `group_table` WHERE `device_id` = ?", array($device['device_id'])); foreach ($g_t_dbs as $g_t_db) { $groups_table_db[$g_t_db['entity_type']][$g_t_db['entity_id']][$g_t_db['group_id']] = $g_t_db; } $msg .= PHP_EOL; $msg .= ' <h4 style="">匹配这个设备组:</h4> '; foreach ($groups['group'] as $group_id => $group_test) { $msg .= '<span class="label label-info">' . $group_test['group_name'] . '</span> '; $msg_enable = TRUE; foreach ($group_test['assoc'] as $assoc_id => $assoc) { // Check that the entity_type matches the one we're interested in. // echo("匹配 $assoc_id (".$assoc['entity_type'].")"); list($entity_table, $entity_id_field, $entity_name_field) = entity_type_translate($assoc['entity_type']); $group = $groups['group'][$assoc['group_id']]; $entities = match_device_entities($device['device_id'], $assoc['entity_attribs'], $assoc['entity_type']); foreach ($entities as $id => $entity) { $group_table[$assoc['entity_type']][$entity[$entity_id_field]][$assoc['group_id']][] = $assoc_id; } // echo(count($entities)." matched".PHP_EOL); } } $msg .= PHP_EOL; $msg .= ' <h4>匹配实体:</h4> '; foreach ($group_table as $entity_type => $entities) { foreach ($entities as $entity_id => $entity) { $entity_name = entity_name($entity_type, $entity_id); $msg .= '<span class="label label-ok">' . $entity_name . '</span> '; $msg_enable = TRUE; foreach ($entity as $group_id => $b) { # echo(str_pad($entity_type, "20").str_pad($entity_id, "20").str_pad($group_id, "20")); # echo(str_pad(implode($b,","), "20")); $msg .= '<span class="label label-info">' . $groups['group'][$group_id]['group_name'] . '</span><br >'; $msg_class = 'success'; if (isset($groups_table_db[$entity_type][$entity_id][$group_id])) { if ($groups_table_db[$entity_type][$entity_id][$group_id]['group_assocs'] != implode($b, ",")) { $update_array = array('group_assocs' => implode($b, ",")); } #echo("[".$groups_table_db[$entity_type][$entity_id][$group_id]['group_assocs']."][".implode($b,",")."]"); if (is_array($update_array)) { dbUpdate($update_array, 'group_table', '`group_table_id` = ?', array($groups_table_db[$entity_type][$entity_id][$group_id]['group_table_id'])); unset($update_array); } unset($groups_table_db[$entity_type][$entity_id][$group_id]); } else { $group_table_id = dbInsert(array('device_id' => $device['device_id'], 'entity_type' => $entity_type, 'entity_id' => $entity_id, 'group_id' => $group_id, 'group_assocs' => implode($b, ",")), 'group_table'); } } } } $msg .= PHP_EOL; $msg .= " <h4>检测过时的内容:</h4> "; foreach ($groups_table_db as $type => $entity) { foreach ($entity as $entity_id => $alert) { foreach ($alert as $group_id => $data) { dbDelete('group_table', "`group_table_id` = ?", array($data['group_table_id'])); //dbDelete('group_table-state', "`group_table_id` = ?", array($data['group_table_id'])); $msg .= "-"; $msg_enable = TRUE; } } } if ($msg_enable) { print_message($msg, $msg_class); } }