9983-Changes inventory reports

This commit is contained in:
Pablo Aragon 2023-02-02 10:05:24 +01:00
parent c4659f0c58
commit 1d8112ae74
5 changed files with 351 additions and 74 deletions

View File

@ -923,6 +923,12 @@ switch ($action) {
$selected_agent_group_filter = $es['agent_group_filter'];
$selected_module_group = $es['module_group'];
$search_module_name = $es['search_module_name'];
$tags = $es['tags'];
$alias = $es['alias'];
$description_switch = $es['description_switch'];
$last_status_change = $es['last_status_change'];
break;
case 'inventory':
@ -3880,38 +3886,93 @@ $class = 'databox filters';
<?php
$rows_select = [];
$rows_select[0] = __('Not assigned');
if ($is_metaconsole === false) {
$rows = db_get_all_rows_sql(
'SELECT * FROM tmodule_group ORDER BY name'
);
$rows = io_safe_output($rows);
if (empty($rows) === false) {
foreach ($rows as $module_group) {
$rows_select[$module_group['id_mg']] = $module_group['name'];
}
}
} else {
$rows_select = modules_get_modulegroups();
}
$rows_select = modules_get_modulegroups();
html_print_select($rows_select, 'modulegroup', $modulegroup, '', __($is_none), -1, true, false, true, '', false, 'width: 120px;');
html_print_select(
$rows_select,
'module_group',
'module_group[]',
$selected_module_group,
'',
__('All'),
-1,
false,
'',
'',
false,
true,
true,
'',
false,
'width: 120px;'
'width: 200px;'
);
?>
</td>
</tr>
<tr id="row_search_module_name" class="datos">
<td class="bolder">
<?php echo __('Search module name'); ?>
</td>
<td >
<?php
html_print_input_text('search_module_name', $search_module_name, '', 40, 100);
?>
</td>
</tr>
<tr id="row_tags" class="datos">
<td class="bolder">
<?php
echo __('Tags');
?>
</td>
<td>
<?php
$rows_select = [];
$rows_select = tags_get_user_tags();
html_print_select(
$rows_select,
'tags[]',
$tags,
'',
'',
'',
false,
true,
true,
'',
false,
'width: 200px;'
);
?>
</td>
</tr>
<tr id="row_alias" class="datos">
<td class="bolder">
<?php
echo __('Alias');
?>
</td>
<td><?php html_print_checkbox_switch('alias', 1, $alias); ?></td>
</tr>
<tr id="row_description_switch" class="datos">
<td class="bolder">
<?php
echo __('Description');
?>
</td>
<td><?php html_print_checkbox_switch('description_switch', 1, $description_switch); ?></td>
</tr>
<tr id="row_last_status_change" class="datos">
<td class="bolder">
<?php
echo __('Last status change');
?>
</td>
<td><?php html_print_checkbox_switch('last_status_change', 1, $last_status_change); ?></td>
</tr>
</tbody>
</table>
@ -6376,6 +6437,11 @@ function chooseType() {
$("#row_agent_group_filter").hide();
$("#row_module_group_filter").hide();
$("#row_module_group_filter").hide();
$("#row_alias").hide();
$("#row_search_module_name").hide();
$("#row_description_switch").hide();
$("#row_last_status_change").hide();
$("#row_tags").hide();
$("#row_os").hide();
$("#row_custom_field_filter").hide();
$("#row_custom_field").hide();
@ -7002,7 +7068,6 @@ function chooseType() {
$("#row_agents_inventory_display_options").show();
$("#row_agent_server_filter").show();
$("#row_agent_group_filter").show();
$("#row_group").show();
$("#row_os").show();
$("#row_custom_field").show();
$("#row_custom_field_filter").show();
@ -7028,10 +7093,14 @@ function chooseType() {
break;
case 'modules_inventory':
$("#row_group").show();
$("#row_agent_server_filter").show();
$("#row_agent_group_filter").show();
$("#row_module_group_filter").show();
$("#row_alias").show();
$("#row_search_module_name").show();
$("#row_description_switch").show();
$("#row_last_status_change").show();
$("#row_tags").show();
break;

View File

@ -2333,6 +2333,11 @@ switch ($action) {
$es['agent_server_filter'] = get_parameter('agent_server_filter');
$es['module_group'] = get_parameter('module_group');
$es['agent_group_filter'] = get_parameter('agent_group_filter');
$es['search_module_name'] = get_parameter('search_module_name');
$es['tags'] = get_parameter('tags');
$es['alias'] = get_parameter('alias', '');
$es['description_switch'] = get_parameter('description_switch', '');
$es['last_status_change'] = get_parameter('last_status_change', '');
$values['external_source'] = json_encode($es);
break;
@ -3121,6 +3126,11 @@ switch ($action) {
$es['agent_server_filter'] = get_parameter('agent_server_filter');
$es['module_group'] = get_parameter('module_group');
$es['agent_group_filter'] = get_parameter('agent_group_filter');
$es['search_module_name'] = get_parameter('search_module_name');
$es['tags'] = get_parameter('tags');
$es['alias'] = get_parameter('alias', '');
$es['description_switch'] = get_parameter('description_switch', '');
$es['last_status_change'] = get_parameter('last_status_change', '');
$values['external_source'] = json_encode($es);
break;

View File

@ -2619,9 +2619,10 @@ function reporting_agents_inventory($report, $content)
$custom_field_sql = '';
$search_sql = '';
$sql_order_by = 'ORDER BY tagente.id_agente ASC';
if (empty(array_filter($es_agent_custom_fields)) === false) {
$custom_field_sql = 'INNER JOIN tagent_custom_data tacd ON tacd.id_agent = tagente.id_agente';
$custom_field_sql = 'LEFT JOIN tagent_custom_data tacd ON tacd.id_agent = tagente.id_agente';
if ($es_agent_custom_fields[0] != 0) {
$custom_field_sql .= ' AND tacd.id_field IN ('.implode(',', $es_agent_custom_fields).')';
}
@ -2655,6 +2656,12 @@ function reporting_agents_inventory($report, $content)
$user_groups_to_sql = implode(',', array_keys(users_get_groups()));
if (array_search('alias', $es_agents_inventory_display_options) !== false) {
$sql_order_by = 'ORDER BY tagente.alias ASC';
} else if (array_search('direccion', $es_agents_inventory_display_options) !== false) {
$sql_order_by = 'ORDER BY tagente.direccion ASC';
}
$sql = sprintf(
'SELECT DISTINCT(tagente.id_agente) AS id_agente,
tagente.id_os,
@ -2670,12 +2677,12 @@ function reporting_agents_inventory($report, $content)
LEFT JOIN tagente_modulo tam
ON tam.id_agente = tagente.id_agente
%s
WHERE (tagente.id_grupo IN (%s) OR tasg.id_group IN (%s))
%s',
WHERE (tagente.id_grupo IN (%s) OR tasg.id_group IN (%s))%s%s',
$custom_field_sql,
$user_groups_to_sql,
$user_groups_to_sql,
$search_sql
$search_sql,
$sql_order_by
);
if (is_metaconsole()) {
@ -2807,11 +2814,16 @@ function reporting_modules_inventory($report, $content)
$search_sql = '';
if (empty($es_agent_group_filter) === false) {
$search_sql .= ' AND (ta.id_grupo = '.$es_agent_group_filter.' OR tasg.id_group = '.$es_agent_group_filter.')';
$search_sql .= '(ta.id_grupo = '.$es_agent_group_filter.' OR tasg.id_group = '.$es_agent_group_filter.')';
}
if (empty($module_group) === false && $module_group > -1) {
$search_sql .= ' AND tam.id_module_group = '.$module_group;
$modules = implode(',', $module_group);
if ($search_sql !== '') {
$search_sql .= ' AND ';
}
$search_sql .= 'tam.id_module_group IN ('.$modules.')';
}
$user_groups_to_sql = '';
@ -2821,25 +2833,80 @@ function reporting_modules_inventory($report, $content)
$user_groups = $user_groupsAR;
$user_groups_to_sql = implode(',', array_keys($user_groups));
$sql_alias = '';
$sql_order_by = 'ORDER BY tam.nombre ASC';
if ((int) $external_source['alias'] === 1) {
$sql_alias = " ta.alias,\n ";
$sql_order_by = 'ORDER BY ta.alias ASC, tam.nombre ASC';
}
$sql_description = '';
if ((int) $external_source['description_switch'] === 1) {
$sql_description = "\n tam.descripcion,";
}
$sql_last_status = '';
$sql_last_status_join = '';
if ((int) $external_source['last_status_change'] === 1) {
$sql_last_status = ",\n tae.last_status_change";
$sql_last_status_join = "\n LEFT JOIN tagente_estado tae";
$sql_last_status_join .= "\n ON tae.id_agente_modulo = tam.id_agente_modulo";
}
if (empty($external_source['search_module_name']) === false) {
if ($search_sql !== '') {
$search_sql .= ' AND ';
}
$search_sql .= "tam.nombre LIKE '%".$external_source['search_module_name']."%'";
}
$tags_condition = tags_get_user_tags();
$tags_imploded = implode(',', array_keys($tags_condition));
if (users_is_admin() === false) {
if ($search_sql !== '') {
$search_sql .= ' AND ';
}
$search_sql .= 'ttm.id_tag IN ('.$tags_imploded.')';
}
if (empty($external_source['tags']) === false) {
$external_tags_imploded = implode(',', $external_source['tags']);
if ($search_sql !== '') {
$search_sql .= ' AND ';
}
$search_sql .= 'ttm.id_tag IN ('.$external_tags_imploded.')';
}
$sql_where = '';
if (empty($search_sql) === false) {
$sql_where .= 'WHERE '.$search_sql;
}
$sql = sprintf(
'SELECT tam.id_agente_modulo,
tam.nombre,
tam.descripcion,
'SELECT DISTINCT(tam.id_agente_modulo),
%s tam.id_agente_modulo,
tam.nombre,%s
tam.id_module_group,
ttm.id_tag,
ta.id_grupo AS group_id,
tasg.id_group AS sec_group_id
tasg.id_group AS sec_group_id%s
FROM tagente_modulo tam
INNER JOIN tagente ta
ON tam.id_agente = ta.id_agente
LEFT JOIN tagent_secondary_group tasg
ON ta.id_agente = tasg.id_agent
LEFT JOIN ttag_module ttm
ON ttm.id_agente_modulo = tam.id_agente_modulo
WHERE (ta.id_grupo IN (%s) OR tasg.id_group IN (%s)) %s',
$user_groups_to_sql,
$user_groups_to_sql,
$search_sql
ON ttm.id_agente_modulo = tam.id_agente_modulo%s
%s
%s',
$sql_alias,
$sql_description,
$sql_last_status,
$sql_last_status_join,
$sql_where,
$sql_order_by
);
if (is_metaconsole()) {
@ -2865,6 +2932,23 @@ function reporting_modules_inventory($report, $content)
$agents = db_get_all_rows_sql($sql);
foreach ($agents as $agent_key => $agent_value) {
$sql2 = 'SELECT tm.id_tag
FROM ttag_module tm
WHERE tm.id_agente_modulo = '.$agent_value['id_agente_modulo'];
$tags_rows = db_get_all_rows_sql($sql2);
$tags_ids = [];
foreach ($tags_rows as $tag_row) {
array_push($tags_ids, $tag_row['id_tag']);
}
$column_value = implode(',', $tags_ids);
$agents[$agent_key]['tags'] = $column_value;
$agents[$agent_key]['server_id'] = $server_id;
}
$return_data[$server_id] = $agents;
if (is_metaconsole()) {
@ -2884,33 +2968,44 @@ function reporting_modules_inventory($report, $content)
$module_row_data = [];
$i = 0;
foreach ($return['data'] as $row) {
if (is_array($module_row_data[$row['id_agente_modulo']]) === false) {
$module_row_data[$row['id_agente_modulo']]['nombre'] = $row['nombre'];
$module_row_data[$row['id_agente_modulo']]['descripcion'] = $row['descripcion'];
$module_row_data[$row['id_agente_modulo']]['id_module_group'] = $row['id_module_group'];
$module_row_data[$row['id_agente_modulo']]['id_tag'] = [];
$module_row_data[$row['id_agente_modulo']]['group_id'] = [];
$module_row_data[$row['id_agente_modulo']]['sec_group_id'] = [];
if (array_key_exists('alias', $row) === true) {
$module_row_data[$i]['alias'] = $row['alias'];
}
if (in_array($row['id_tag'], $module_row_data[$row['id_agente_modulo']]['id_tag']) === false
&& $row['id_tag'] !== null
) {
$module_row_data[$row['id_agente_modulo']]['id_tag'][] = $row['id_tag'];
if (array_key_exists('nombre', $row) === true) {
$module_row_data[$i]['nombre'] = $row['nombre'];
}
if (in_array($row['group_id'], $module_row_data[$row['id_agente_modulo']]['group_id']) === false
&& $row['group_id'] !== null
) {
$module_row_data[$row['id_agente_modulo']]['group_id'][] = $row['group_id'];
if (array_key_exists('descripcion', $row) === true) {
$module_row_data[$i]['descripcion'] = $row['descripcion'];
}
if (in_array($row['sec_group_id'], $module_row_data[$row['id_agente_modulo']]['sec_group_id']) === false
&& $row['sec_group_id'] !== null
) {
$module_row_data[$row['id_agente_modulo']]['sec_group_id'][] = $row['sec_group_id'];
$module_row_data[$i]['id_module_group'] = $row['id_module_group'];
$module_row_data[$i]['id_tag'] = [];
$module_row_data[$i]['group_id'] = [];
$module_row_data[$i]['sec_group_id'] = [];
if (array_key_exists('tags', $row) === true) {
$module_row_data[$i]['id_tag'][] = $row['tags'];
}
if (array_key_exists('id_group', $row) === true) {
$module_row_data[$i]['group_id'][] = $row['group_id'];
}
if (array_key_exists('sec_group_id', $row) === true) {
$module_row_data[$i]['sec_group_id'][] = $row['sec_group_id'];
}
if (array_key_exists('last_status_change', $row) === true) {
$human_last_status_change = human_time_comparation($row['last_status_change']);
$module_row_data[$i]['last_status_change'] = $human_last_status_change;
}
$i++;
}
$return['data'] = $module_row_data;

View File

@ -1551,6 +1551,57 @@ function reporting_html_agents_inventory($table, $item, $pdf=0)
$table1->head = [];
// Sort array columns.
$tmp_sort_array = [];
foreach ($item['data'] as $data_key => $data_value) {
if (array_key_exists('alias', $data_value) === true) {
$tmp_sort_array['alias'] = $data_value['alias'];
}
if (array_key_exists('direccion', $data_value) === true) {
$tmp_sort_array['direccion'] = $data_value['direccion'];
}
if (array_key_exists('id_os', $data_value) === true) {
$tmp_sort_array['id_os'] = $data_value['id_os'];
}
if (array_key_exists('agent_version', $data_value) === true) {
$tmp_sort_array['agent_version'] = $data_value['agent_version'];
}
if (array_key_exists('id_grupo', $data_value) === true) {
$tmp_sort_array['id_grupo'] = $data_value['id_grupo'];
}
if (array_key_exists('comentarios', $data_value) === true) {
$tmp_sort_array['comentarios'] = $data_value['comentarios'];
}
if (array_key_exists('url_address', $data_value) === true) {
$tmp_sort_array['url_address'] = $data_value['url_address'];
}
if (array_key_exists('remote', $data_value) === true) {
$tmp_sort_array['remote'] = $data_value['remote'];
}
if (array_key_exists('secondary_groups', $data_value) === true) {
$tmp_sort_array['secondary_groups'] = $data_value['secondary_groups'];
}
if (array_key_exists('custom_fields', $data_value) === true) {
$tmp_sort_array['custom_fields'] = $data_value['custom_fields'];
}
if (array_key_exists('estado', $data_value) === true) {
$tmp_sort_array['estado'] = $data_value['estado'];
}
unset($item['data'][$data_key]);
$item['data'][$data_key] = $tmp_sort_array;
}
foreach ($item['data'][0] as $field_key => $field_value) {
switch ($field_key) {
case 'alias':
@ -1632,7 +1683,7 @@ function reporting_html_agents_inventory($table, $item, $pdf=0)
} else if ($data_field_key === 'estado') {
$column_value = ($pdf === 0) ? ui_print_module_status((int) $data_field_value, true) : modules_get_modules_status((int) $data_field_value);
} else if ($data_field_key === 'id_grupo') {
$column_value = ui_print_group_icon((int) $data_field_value, true, 'groups_small', '', $show_link);
$column_value = groups_get_name((int) $data_field_value);
} else if ($data_field_key === 'custom_fields') {
$custom_fields_value = [];
@ -1648,7 +1699,7 @@ function reporting_html_agents_inventory($table, $item, $pdf=0)
if (is_array($data_field_value)) {
foreach ($data_field_value as $value) {
$custom_fields_value[] = ui_print_group_icon((int) $value['id_group'], true, 'groups_small', '', $show_link);
$custom_fields_value[] = groups_get_name((int) $value['id_group']);
}
}
@ -1705,19 +1756,50 @@ function reporting_html_modules_inventory($table, $item, $pdf=0)
$table1->style[0] = 'text-align: left;vertical-align: top;min-width: 100px;';
$table1->style[1] = 'text-align: left;vertical-align: top;min-width: 100px;';
$table1->style[2] = 'text-align: left;vertical-align: top; min-width: 100px';
$table1->style[2] = 'text-align: left;vertical-align: top;min-width: 100px;';
$table1->style[3] = 'text-align: left;vertical-align: top;min-width: 100px;';
$table1->style[4] = 'text-align: left;vertical-align: top;min-width: 100px;';
$table1->style[5] = 'text-align: left;vertical-align: top; min-width: 100px';
$table1->style[5] = 'text-align: left;vertical-align: top;min-width: 100px;';
$table1->style[6] = 'text-align: left;vertical-align: top;min-width: 100px;';
$table1->style[7] = 'text-align: left;vertical-align: top;min-width: 100px;';
$table1->head = [];
$first_index = array_key_first($item['data']);
$table1->head[] = __('Name');
$table1->head[] = __('Description');
$table1->head[] = __('Module group');
$table1->head[] = __('Tags');
$table1->head[] = __('Agent group');
$table1->head[] = __('Agent secondary groups');
foreach ($item['data'][$first_index] as $field_key => $field_value) {
switch ($field_key) {
case 'alias':
$table1->head[] = __('Alias');
break;
case 'nombre':
$table1->head[] = __('Name');
break;
case 'descripcion':
$table1->head[] = __('Description');
break;
case 'id_module_group':
$table1->head[] = __('Module group');
break;
case 'id_tag':
$table1->head[] = __('Tags');
break;
case 'group_id':
$table1->head[] = __('Agent group');
break;
case 'sec_group_id':
$table1->head[] = __('Agent secondary groups');
break;
case 'last_status_change':
$table1->head[] = __('Last status change');
}
}
$table1->headstyle[0] = 'text-align: left';
$table1->headstyle[1] = 'text-align: left';
@ -1725,15 +1807,20 @@ function reporting_html_modules_inventory($table, $item, $pdf=0)
$table1->headstyle[3] = 'text-align: left';
$table1->headstyle[4] = 'text-align: left';
$table1->headstyle[5] = 'text-align: left';
$table1->headstyle[6] = 'text-align: left';
$table1->headstyle[7] = 'text-align: left';
$table1->data = [];
foreach ($item['data'] as $module_id => $module_data) {
unset($module_data['server_id']);
$row = [];
$first_item = array_pop(array_reverse($module_data));
foreach ($module_data as $data_field_key => $data_field_value) {
if ($data_field_key === 'nombre') {
if ($data_field_key === 'alias') {
$column_value = $data_field_value;
} else if ($data_field_key === 'nombre') {
$column_value = $data_field_value;
} else if ($data_field_key === 'descripcion') {
$column_value = $data_field_value;
@ -1746,13 +1833,27 @@ function reporting_html_modules_inventory($table, $item, $pdf=0)
$column_value = $module_group_name;
} else if ($data_field_key === 'id_tag') {
$tags_names = array_map(
function ($tag_id) {
return db_get_value('name', 'ttag', 'id_tag', $tag_id);
},
$data_field_value
);
$column_value = implode('<br>', $tags_names);
if (empty($data_field_value[0]) === false) {
$sql = 'SELECT name
FROM ttag
WHERE id_tag IN ('.$data_field_value[0].')';
$tags_rows = db_get_all_rows_sql($sql);
$tags_names = [];
foreach ($tags_rows as $tag_row) {
array_push($tags_names, $tag_row['name']);
}
$column_value = implode('<br>', $tags_names);
} else {
$tags_names = array_map(
function ($tag_id) {
return db_get_value('name', 'ttag', 'id_tag', $tag_id);
},
$data_field_value
);
$column_value = implode('<br>', $tags_names);
}
} else if ($data_field_key === 'group_id') {
$column_value = groups_get_name($data_field_value[0]);
} else if ($data_field_key === 'sec_group_id') {
@ -1764,6 +1865,8 @@ function reporting_html_modules_inventory($table, $item, $pdf=0)
);
$column_value = implode('<br>', $sec_groups_names);
} else if ($data_field_key === 'last_status_change') {
$column_value = $data_field_value;
}
$row[] = $column_value;

View File

@ -1048,7 +1048,7 @@ function tags_has_user_acl_tags($id_user=false)
* @param string Access flag where check what tags have the user
* @param bool returns 0 if the user has all the tags
*
* @return string SQL condition for tagente_module
* @return array Returns the user's Tags
*/
function tags_get_user_tags($id_user=false, $access='AR', $return_tag_any=false)
{