WIP NetworkMap class

Former-commit-id: ae987e5f6f3ec0997746093d20f7dbd5758c9ff1
This commit is contained in:
fbsanchez 2019-03-12 12:05:54 +01:00
parent c6fa365b90
commit d7983bec4e
4 changed files with 237 additions and 114 deletions

View File

@ -29,6 +29,7 @@
require_once $config['homedir'].'/include/graphs/functions_d3.php';
$progress_task_discovery = (bool) get_parameter('progress_task_discovery', 0);
$showmap = (bool) get_parameter('showmap', 0);
if ($progress_task_discovery) {
$id_task = get_parameter('id', 0);
@ -72,3 +73,13 @@ if ($progress_task_discovery) {
return;
}
if ($showmap) {
include_once $config['homedir'].'/include/class/NetworkMap.class.php';
$id_task = get_parameter('id', 0);
$map = new NetworkMap(
['id_task' => $id_task]
);
$map->printMap();
}

View File

@ -170,29 +170,41 @@ class NetworkMap
];
if (is_array($options)) {
// Previously nodes_and_relations.
if (isset($options['graph'])) {
$this->graph = $options['graph'];
}
// String dotmap.
if (isset($options['dot_graph'])) {
$this->dotGraph = $options['dot_graph'];
}
// Array of nodes, agents, virtual, etc.
if (isset($options['nodes'])) {
$this->nodes = $options['nodes'];
}
// Array of relations.
if (isset($options['relations'])) {
$this->relations = $options['relations'];
}
// User interface type. Simple or advanced.
if (isset($options['mode'])) {
$this->mode = $options['mode'];
}
// Map options, check default values above.
// This is only used while generating new maps using
// (generateDotGraph).
if (is_array($options['map_options'])) {
foreach ($options['map_options'] as $k => $v) {
$this->mapOptions[$k] = $v;
}
}
// Load from Discovery task.
// Load from tmap.
if ($options['id_map']) {
$this->idMap = $options['id_map'];
// Update nodes and relations.
@ -204,6 +216,7 @@ class NetworkMap
$this->createMap();
}
} else {
// Generate from group, task or network.
if ($options['id_group']) {
$this->idGroup = $options['id_group'];
}
@ -238,42 +251,13 @@ class NetworkMap
*/
public function createMap()
{
// If exists, load from DB.
if ($this->idMap) {
$this->loadMap();
return;
}
if ($this->network) {
$this->nodes = networkmap_get_new_nodes_from_ip_mask(
$this->network
);
}
if ($this->idTask) {
// Retrieve data from target task.
$this->loadMap();
}
}
/**
* Loads a map from a target map ID.
*
* @return void.
*/
public function loadMap()
{
if ($this->idMap) {
$this->map = db_get_row('tmap', 'id', $this->idMap);
// Retrieve or update nodes and relations.
$this->getNodes();
$this->getRelations();
// Nodes and relations.
$this->graph = networkmap_process_networkmap($this->idMap);
} else {
// Simulated map.
$this->idMap = uniqid();
// No tmap definition. Paint data.
@ -300,6 +284,26 @@ class NetworkMap
];
$this->graph = $this->generateNetworkMap();
}
/**
* Loads a map from a target map ID.
*
* @return void.
*/
public function loadMap()
{
if ($this->idMap) {
$this->map = db_get_row('tmap', 'id', $this->idMap);
// Retrieve or update nodes and relations.
$this->getNodes();
$this->getRelations();
// Nodes and relations.
$this->graph = networkmap_process_networkmap($this->idMap);
}
}
@ -330,6 +334,46 @@ class NetworkMap
}
/**
* Generate a graphviz string structure to be used later.
*
* @return void
*/
public function generateDotGraph()
{
if (!isset($this->dotGraph)) {
// Generate dot file.
$this->dotGraph = networkmap_generate_dot(
get_product_name(),
$this->idGroup,
$this->mapOptions['simple'],
$this->mapOptions['font_size'],
$this->mapOptions['layout'],
$this->mapOptions['nooverlap'],
$this->mapOptions['zoom'],
$this->mapOptions['ranksep'],
$this->mapOptions['center'],
$this->mapOptions['regen'],
$this->mapOptions['pure'],
$this->mapOptions['id'],
$this->mapOptions['show_snmp_modules'],
$this->mapOptions['cut_names'],
$this->mapOptions['relative'],
$this->mapOptions['text_filter'],
$this->network,
$this->mapOptions['dont_show_subgroups'],
// Strict user (strict_user).
false,
// Canvas size (size_canvas).
null,
$this->mapOptions['old_mode'],
$this->mapOptions['map_filter']
);
}
}
/**
* Generates a nodes - relationships array using graphviz dot
* schema.
@ -338,6 +382,10 @@ class NetworkMap
*/
public function generateNetworkMap()
{
if (!isset($this->dotGraph)) {
$this->generateDotGraph();
}
/*
* Let graphviz place the nodes.
*/
@ -370,35 +418,6 @@ class NetworkMap
break;
}
$nodes_and_relations = [];
// Generate dot file.
$graph = networkmap_generate_dot(
get_product_name(),
$this->idGroup,
$this->mapOptions['simple'],
$this->mapOptions['font_size'],
$this->mapOptions['layout'],
$this->mapOptions['nooverlap'],
$this->mapOptions['zoom'],
$this->mapOptions['ranksep'],
$this->mapOptions['center'],
$this->mapOptions['regen'],
$this->mapOptions['pure'],
$this->mapOptions['id'],
$this->mapOptions['show_snmp_modules'],
$this->mapOptions['cut_names'],
$this->mapOptions['relative'],
$this->mapOptions['text_filter'],
$this->network,
$this->mapOptions['dont_show_subgroups'],
// Strict user (strict_user).
false,
// Canvas size (size_canvas).
null,
$this->mapOptions['old_mode'],
$this->mapOptions['map_filter']
);
switch (PHP_OS) {
case 'WIN32':
case 'WINNT':
@ -421,7 +440,7 @@ class NetworkMap
$filename_dot .= '_'.$this->idMap.'.dot';
file_put_contents($filename_dot, $graph);
file_put_contents($filename_dot, $this->dotGraph);
switch (PHP_OS) {
case 'WIN32':
@ -449,13 +468,11 @@ class NetworkMap
$this->idMap,
$filename_plain,
$relation_nodes,
$graph
$this->dotGraph
);
unlink($filename_plain);
$id = $this->idMap;
/*
* Graphviz section ends here.
*/
@ -485,7 +502,7 @@ class NetworkMap
$node_center = [];
foreach ($nodes as $key => $node) {
$nodes_and_relations['nodes'][$index]['id'] = $node['id'];
$nodes_and_relations['nodes'][$index]['id_map'] = $id;
$nodes_and_relations['nodes'][$index]['id_map'] = $this->idMap;
$children_count = 0;
foreach ($relation_nodes as $relation) {
@ -532,7 +549,7 @@ class NetworkMap
$nodes_and_relations['relations'] = [];
$index = 0;
foreach ($relation_nodes as $relation) {
$nodes_and_relations['relations'][$index]['id_map'] = $id;
$nodes_and_relations['relations'][$index]['id_map'] = $this->idMap;
if (($relation['parent_type'] == 'agent') || ($relation['parent_type'] == '')) {
$nodes_and_relations['relations'][$index]['id_parent'] = $relation['id_parent'];
@ -565,34 +582,33 @@ class NetworkMap
$index++;
}
if ($this->idMap > 0) {
if ($this->idMap > 0 && (!isset($this->map['__simulated']))) {
enterprise_hook(
'save_generate_nodes',
[
$id,
$this->idMap,
$nodes_and_relations,
]
);
$pandorafms_node = $nodes_and_relations['nodes'][0];
$center = [
'x' => $node_center['x'],
'y' => $node_center['y'],
];
$networkmap['center_x'] = $center['x'];
$networkmap['center_y'] = $center['y'];
$this->map['center_x'] = $center['x'];
$this->map['center_y'] = $center['y'];
db_process_sql_update(
'tmap',
[
'center_x' => $networkmap['center_x'],
'center_y' => $networkmap['center_y'],
'center_x' => $this->map['center_x'],
'center_y' => $this->map['center_y'],
],
['id' => $id]
['id' => $this->idMap]
);
} else {
$this->map['center_x'] = $center['x'];
$this->map['center_y'] = $center['y'];
$this->map['center_x'] = $node_center['x'];
$this->map['center_y'] = $node_center['y'];
}
return $nodes_and_relations;
@ -633,11 +649,14 @@ class NetworkMap
{
$networkmap = $this->map;
$simulate = false;
if (!isset($networkmap['__simulated'])) {
$networkmap['filter'] = json_decode(
$networkmap['filter'],
true
);
} else {
$simulate = true;
}
// Hardcoded.
@ -719,7 +738,8 @@ class NetworkMap
$item = networkmap_db_node_to_js_node(
$node,
$count,
$count_item_holding_area
$count_item_holding_area,
$simulate
);
if ($item['deleted']) {
continue;
@ -739,7 +759,11 @@ class NetworkMap
// interfaces.
networkmap_clean_relations_for_js($relations);
$links_js = networkmap_links_to_js_links($relations, $nodes_graph);
$links_js = networkmap_links_to_js_links(
$relations,
$nodes_graph,
$simulate
);
$array_aux = [];
foreach ($links_js as $link_js) {
@ -1404,8 +1428,7 @@ class NetworkMap
$output .= $this->loadController();
$output .= $this->loadAdvancedInterface();
} else {
// Simple mode, no tmap entries.
$this->idMap = '0';
// Simulated, no tmap entries.
$output .= $this->loadMapSkel();
$output .= $this->loadMapData();
$output .= $this->loadController();

View File

@ -471,8 +471,22 @@ function get_networkmaps($id)
}
function networkmap_db_node_to_js_node($node, &$count, &$count_item_holding_area)
{
/**
* Translates node (nodes_and_relations) into JS node.
*
* @param array $node Node.
* @param integer $count Count.
* @param integer $count_item_holding_area Count_item_holding_area.
* @param boolean $simulated Simulated.
*
* @return array JS nodes.
*/
function networkmap_db_node_to_js_node(
$node,
&$count,
&$count_item_holding_area,
$simulated=false
) {
global $config;
$networkmap = db_get_row('tmap', 'id', $node['id_map']);
@ -492,7 +506,7 @@ function networkmap_db_node_to_js_node($node, &$count, &$count_item_holding_area
$item = [];
$item['id'] = $count;
if (enterprise_installed()) {
if (enterprise_installed() && $simulated === false) {
enterprise_include_once('include/functions_pandora_networkmap.php');
$item['id_db'] = $node['id_in_db'];
} else {
@ -670,29 +684,53 @@ function networkmap_clean_relations_for_js(&$relations)
}
function networkmap_links_to_js_links($relations, $nodes_graph)
{
/**
* Transform networkmap relations into js links.
*
* @param array $relations Relations.
* @param array $nodes_graph Nodes_graph.
* @param boolean $simulated Simulated.
*
* @return array JS relations.
*/
function networkmap_links_to_js_links(
$relations,
$nodes_graph,
$simulated=false
) {
$return = [];
if (enterprise_installed()) {
if (enterprise_installed() && $simulated === false) {
enterprise_include_once('include/functions_pandora_networkmap.php');
}
$count = 0;
foreach ($relations as $key => $relation) {
if (($relation['parent_type'] == 1) && ($relation['child_type'] == 1)) {
$id_target_agent = agents_get_agent_id_by_module_id($relation['id_parent_source_data']);
$id_source_agent = agents_get_agent_id_by_module_id($relation['id_child_source_data']);
$id_target_agent = agents_get_agent_id_by_module_id(
$relation['id_parent_source_data']
);
$id_source_agent = agents_get_agent_id_by_module_id(
$relation['id_child_source_data']
);
$id_target_module = $relation['id_parent_source_data'];
$id_source_module = $relation['id_child_source_data'];
} else if (($relation['parent_type'] == 1) && ($relation['child_type'] == 0)) {
$id_target_agent = agents_get_agent_id_by_module_id($relation['id_parent_source_data']);
} else if (($relation['parent_type'] == 1)
&& ($relation['child_type'] == 0)
) {
$id_target_agent = agents_get_agent_id_by_module_id(
$relation['id_parent_source_data']
);
$id_target_module = $relation['id_parent_source_data'];
$id_source_agent = $relation['id_child_source_data'];
} else if (($relation['parent_type'] == 0) && ($relation['child_type'] == 1)) {
} else if (($relation['parent_type'] == 0)
&& ($relation['child_type'] == 1)
) {
$id_target_agent = $relation['id_parent_source_data'];
$id_source_module = $relation['id_child_source_data'];
$id_source_agent = agents_get_agent_id_by_module_id($relation['id_child_source_data']);
$id_source_agent = agents_get_agent_id_by_module_id(
$relation['id_child_source_data']
);
} else {
$id_target_agent = $relation['id_parent_source_data'];
$id_source_agent = $relation['id_child_source_data'];
@ -701,7 +739,7 @@ function networkmap_links_to_js_links($relations, $nodes_graph)
$item = [];
$item['id'] = $count;
$count++;
if (enterprise_installed()) {
if (enterprise_installed() && $simulated === false) {
$item['id_db'] = get_relation_id($relation);
} else {
$item['id_db'] = $key;
@ -720,7 +758,7 @@ function networkmap_links_to_js_links($relations, $nodes_graph)
$item['source'] = -1;
$item['deleted'] = $relation['deleted'];
if (enterprise_installed()) {
if (enterprise_installed() && $simulated === false) {
$target_and_source = [];
$target_and_source = get_id_target_and_source_in_db($relation);
$item['target_id_db'] = (int) $target_and_source['target'];
@ -779,8 +817,12 @@ function networkmap_links_to_js_links($relations, $nodes_graph)
$item['link_color'] = '#FAD403';
}
$agent = agents_get_agent_id_by_module_id($relation['id_parent_source_data']);
$agent2 = agents_get_agent_id_by_module_id($relation['id_child_source_data']);
$agent = agents_get_agent_id_by_module_id(
$relation['id_parent_source_data']
);
$agent2 = agents_get_agent_id_by_module_id(
$relation['id_child_source_data']
);
foreach ($nodes_graph as $key2 => $node) {
if (isset($node['id_agent'])) {
if ($node['id_agent'] == $agent) {
@ -805,7 +847,9 @@ function networkmap_links_to_js_links($relations, $nodes_graph)
$item['link_color'] = '#FAD403';
}
$agent2 = agents_get_agent_id_by_module_id($relation['id_child_source_data']);
$agent2 = agents_get_agent_id_by_module_id(
$relation['id_child_source_data']
);
foreach ($nodes_graph as $key2 => $node) {
if (isset($node['id_agent'])) {
if ($node['id_agent'] == $relation['id_parent_source_data']) {
@ -830,7 +874,9 @@ function networkmap_links_to_js_links($relations, $nodes_graph)
$item['link_color'] = '#FAD403';
}
$agent = agents_get_agent_id_by_module_id($relation['id_parent_source_data']);
$agent = agents_get_agent_id_by_module_id(
$relation['id_parent_source_data']
);
foreach ($nodes_graph as $key2 => $node) {
if (isset($node['id_agent'])) {
if ($node['id_agent'] == $agent) {
@ -848,7 +894,9 @@ function networkmap_links_to_js_links($relations, $nodes_graph)
}
}
}
} else if (($relation['parent_type'] == 3) && ($relation['child_type'] == 3)) {
} else if (($relation['parent_type'] == 3)
&& ($relation['child_type'] == 3)
) {
foreach ($nodes_graph as $key2 => $node) {
if ($relation['id_parent'] == $node['id_db']) {
$agent = $node['id_db'];
@ -860,7 +908,9 @@ function networkmap_links_to_js_links($relations, $nodes_graph)
$agent2 = $node['id_db'];
}
}
} else if (($relation['parent_type'] == 3) || ($relation['child_type'] == 3)) {
} else if (($relation['parent_type'] == 3)
|| ($relation['child_type'] == 3)
) {
if ($relation['parent_type'] == 3) {
foreach ($nodes_graph as $key2 => $node) {
if ($relation['id_parent'] == $node['id_db']) {
@ -898,7 +948,10 @@ function networkmap_links_to_js_links($relations, $nodes_graph)
}
}
if ((($item['target'] == -1) || ($item['source'] == -1)) && $relation['parent_type'] == 1 && $relation['child_type'] == 1) {
if ((($item['target'] == -1) || ($item['source'] == -1))
&& $relation['parent_type'] == 1
&& $relation['child_type'] == 1
) {
continue;
}
@ -1112,7 +1165,10 @@ function networkmap_loadfile(
) {
global $config;
$height_map = 200;
if ((int) $id > 0) {
$height_map = db_get_value('height', 'tmap', 'id', $id);
}
$networkmap_nodes = [];

View File

@ -31,3 +31,36 @@ function progress_task_list(id, name, url) {
}
});
}
function show_map(id, name, url) {
var params = [];
params.push("page=include/ajax/task_list.ajax");
params.push("showmap=1");
params.push("id=" + id);
$("#progress_task")
.empty()
.hide()
.append("<p>Loading map</p>")
.dialog({
title: "Task: " + name,
resizable: true,
draggable: true,
modal: false,
width: 1280,
height: 700
})
.show();
jQuery.ajax({
data: params.join("&"),
type: "POST",
url: (action = url),
dataType: "html",
success: function(data) {
$("#progress_task")
.empty()
.append(data);
}
});
}