} } // Fetch all existing device IDs and update the maximum ID. foreach (dbFetchRows("SELECT `device_id` FROM `devices` ORDER BY `device_id` ASC") as $device) { $devices[] = $device['device_id']; $max_id = $device['device_id']; } //print_vars($entities); // Cleanup common entity tables with links to devices that on longer exist // Loop for found stale entity entries //print_vars($entities_count); foreach ($entities as $table => $entries) { $entity_types = array_keys($entries); // Just limit for exist types foreach ($devices as $device_id) { $device_entities = get_device_entities($device_id, $entity_types); foreach ($device_entities as $entity_type => $entity_ids) { $entity_count = count(array_intersect($entities[$table][$entity_type], $entity_ids)); if (!$entity_count) { continue; } $entities[$table][$entity_type] = array_diff($entities[$table][$entity_type], $entity_ids); $entities_count -= $entity_count; if (count($entities[$table][$entity_type]) === 0) { unset($entities[$table][$entity_type]); break; } } if (count($entities[$table]) === 0) { unset($entities[$table]); break;
function delete_device($id, $delete_rrd = FALSE) { global $config; $ret = PHP_EOL; $device = device_by_id_cache($id); $host = $device['hostname']; if ($host == '') { $ret .= "Error finding host in the database."; } else { $ports = dbFetchRows("SELECT * FROM `ports` WHERE `device_id` = ?", array($id)); if (!empty($ports)) { $ret .= ' * Deleted interfaces: '; foreach ($ports as $int_data) { $int_if = $int_data['ifDescr']; $int_id = $int_data['port_id']; delete_port($int_id, $delete_rrd); $deleted_ports[] = "id={$int_id} ({$int_if})"; } $ret .= implode(', ', $deleted_ports) . PHP_EOL; } // Remove entities from common tables $deleted_entities = array(); foreach (get_device_entities($id) as $entity_type => $entity_ids) { foreach ($config['entity_tables'] as $table) { $where = '`entity_type` = ?' . generate_query_values($entity_ids, 'entity_id'); $table_status = dbDelete($table, $where, array($entity_type)); if ($table_status) { $deleted_entities[$entity_type] = 1; } } } if (count($deleted_entities)) { $ret .= ' * Deleted common entity entries linked to device: '; $ret .= implode(', ', array_keys($deleted_entities)) . PHP_EOL; } $deleted_tables = array(); $ret .= ' * Deleted device entries from tables: '; foreach ($config['device_tables'] as $table) { $where = '`device_id` = ?'; $table_status = dbDelete($table, $where, array($id)); if ($table_status) { $deleted_tables[] = $table; } } $ret .= implode(', ', $deleted_tables) . PHP_EOL; if ($delete_rrd) { $device_rrd = rtrim(get_rrd_path($device, ''), '/'); if (is_file($device_rrd . '/status.rrd')) { external_exec("rm -rf " . escapeshellarg($device_rrd)); $ret .= ' * Deleted device RRDs dir: ' . $device_rrd . PHP_EOL; } } $ret .= " * Deleted device: {$host}"; } return $ret; }
/** * * Get all attributes for all entities from device * * @param string $entity_type * @param mixed $entity_id * @return array */ function get_device_entities_attribs($device_id, $entity_types = NULL) { $attribs = array(); foreach (get_device_entities($device_id, $entity_types) as $entity_type => $entities) { $where = generate_query_values($entities, 'entity_id'); foreach (dbFetchRows("SELECT * FROM `entity_attribs` WHERE `entity_type` = ?" . $where, array($entity_type)) as $entry) { $attribs[$entry['entity_type']][$entry['entity_id']][$entry['attrib_type']] = $entry['attrib_value']; } } $GLOBALS['cache']['entity_attribs'] = $attribs; return $GLOBALS['cache']['entity_attribs']; }