#8285 Add CSV export on agents modules view

This commit is contained in:
miguel angel rasteu 2023-07-13 13:12:43 +02:00
parent be8ccde589
commit 3c345ed1f9
3 changed files with 214 additions and 1 deletions

View File

@ -267,6 +267,23 @@ function agents_modules_load_js()
// } // }
// }); // });
} }
/* <![CDATA[ */
function export_csv() {
let group_id = $('#group_id option:selected').val();
let module_group_id = $('#modulegroup option:selected').val();
let agent_id = $('#id_agents2 option:selected').map((_, e) => e.value).get();
let module_id = $('#module option:selected').map((_, e) => e.value).get();
let filters_array = {group_id: group_id, module_group_id:module_group_id, agent_id:agent_id, module_id:module_id}
let jsonFilters = JSON.stringify(filters_array)
let filters = window.btoa(jsonFilters)
var f = document.forms.filter_form;
blockResubmit($(this));
f.action = "extensions/agents_modules_csv.php?get_agents_module_csv=1&filters="+filters;
$("#filter_form").submit();
}
/* ]]> */
</script> </script>
<?php <?php
} }
@ -653,7 +670,7 @@ function mainAgentsModules()
} }
if ($config['pure'] != 1) { if ($config['pure'] != 1) {
$show_filters = '<form method="post" action="index.php?sec=view&sec2=extensions/agents_modules" class="w100p">'; $show_filters = '<form id="filter_form" method="post" action="index.php?sec=view&sec2=extensions/agents_modules" class="w100p">';
$show_filters .= '<table class="filter-table-adv w100p no-border" cellpadding="4" cellspacing="4">'; $show_filters .= '<table class="filter-table-adv w100p no-border" cellpadding="4" cellspacing="4">';
$show_filters .= '<tr>'; $show_filters .= '<tr>';
$show_filters .= '<td width="33%">'.$filter_type.'</td>'; $show_filters .= '<td width="33%">'.$filter_type.'</td>';
@ -679,6 +696,13 @@ function mainAgentsModules()
'onclick' => 'select_selected()', 'onclick' => 'select_selected()',
], ],
true true
).html_print_button(
__('Export to CSV'),
'srcbutton',
false,
'export_csv()',
['class' => 'secondary mini'],
true,
), ),
], ],
true true

View File

@ -0,0 +1,117 @@
<?php
/**
* Export data.
*
* @category Tools
* @package Pandora FMS
* @subpackage Operation
* @version 1.0.0
* @license See below
*
* ______ ___ _______ _______ ________
* | __ \.-----.--.--.--| |.-----.----.-----. | ___| | | __|
* | __/| _ | | _ || _ | _| _ | | ___| |__ |
* |___| |___._|__|__|_____||_____|__| |___._| |___| |__|_|__|_______|
*
* ============================================================================
* Copyright (c) 2005-2023 Pandora FMS
* Please see https://pandorafms.com/community/ for full contribution list
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation for version 2.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* ============================================================================
*/
/*
require_once $config['homedir'].'/include/config.php';
require_once $config['homedir'].'/include/functions_reporting.php';
require_once $config['homedir'].'/include/functions_modules.php';
*/
require_once '../include/config.php';
require_once '../include/functions_agents.php';
require_once '../include/functions_reporting.php';
require_once '../include/functions_modules.php';
require_once '../include/functions_users.php';
global $config;
check_login();
// ACL Check.
if (! check_acl($config['id_user'], 0, 'AR')) {
db_pandora_audit(
AUDIT_LOG_ACL_VIOLATION,
'Trying to access Agent view (Grouped)'
);
include 'general/noaccess.php';
exit;
}
$get_agents_module_csv = get_parameter('get_agents_module_csv', 0);
if ($get_agents_module_csv === '1') {
// ***************************************************
// Header output
// ***************************************************
$config['ignore_callback'] = true;
while (@ob_end_clean()) {
}
$filename = 'agents_module_view_'.date('Ymd').'-'.date('His');
// Set cookie for download control.
setDownloadCookieToken();
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="'.$filename.'.csv"');
// ***************************************************
// Data processing
// ***************************************************
echo pack('C*', 0xEF, 0xBB, 0xBF);
$json_filters = get_parameter('filters', '');
$filters = json_decode(
base64_decode(
get_parameter('filters', '')
),
true
);
$results = export_agents_module_csv($filters);
$divider = $config['csv_divider'];
$dataend = PHP_EOL;
$header_fields = [
__('Agent'),
__('Module'),
__('Data'),
];
$out_csv = '';
foreach ($header_fields as $key => $value) {
$out_csv .= $value.$divider;
}
$out_csv .= "\n";
foreach ($results as $result) {
foreach ($result as $key => $value) {
$out_csv .= io_safe_output($value).$divider;
}
$out_csv .= "\n";
}
echo io_safe_output($out_csv);
exit;
}

View File

@ -4682,3 +4682,75 @@ function get_agent_module_childs(
} }
} }
} }
/**
* Function for export a csv file from Agents/Module view
*
* @param array $filters Data from agents/module filter.
*
* @return array Returns the data that will be saved in the csv file
*/
function export_agents_module_csv($filters)
{
$query_filter = '';
foreach ($filters as $key => $filter) {
switch ($key) {
case 'group_id':
if ($filter != 0) {
$query_filter .= ' AND ta.id_grupo = '.$filter.' ';
}
break;
case 'module_group_id':
if ($filter != 0) {
$query_filter .= ' AND tam.id_module_group = '.$filter.' ';
;
}
break;
case 'agent_id':
if (count($filter) > 0) {
$agent_filter = '('.implode(', ', $filter).')';
$query_filter .= ' AND ta.id_agente IN '.$agent_filter.' ';
}
break;
case 'module_id':
if (count($filter) > 0) {
if (is_numeric($filter[0]) === false) {
foreach ($filter as $key => $module) {
$filter[$key] = io_safe_input($module);
}
$module_filter = '(\''.implode("', '", $filter).'\')';
$query_filter .= ' AND tam.nombre IN '.$module_filter.' ';
} else {
$module_filter = '('.implode(', ', $filter).')';
$query_filter .= ' AND tam.id_tipo_modulo IN '.$module_filter.' ';
}
}
break;
default:
// Nothing to do
break;
}
}
// Query fields result.
$query = sprintf(
'SELECT ta.alias as agent, tam.nombre as module, tae.datos as data
FROM tagente_modulo as tam
INNER JOIN tagente as ta ON tam.id_agente = ta.id_agente
INNER JOIN tagente_estado as tae ON tam.id_agente_modulo = tae.id_agente_modulo
WHERE ta.disabled = 0
%s
',
$query_filter
);
$result = db_get_all_rows_sql($query);
return $result;
}