add table console task in discovery

Former-commit-id: b13571167c063188102a8473fb57fa63c18bece3
This commit is contained in:
daniel 2019-02-22 12:53:35 +01:00
parent e65d128b25
commit 3d2bfa95b5
2 changed files with 942 additions and 58 deletions

View File

@ -28,6 +28,8 @@
require_once __DIR__.'/Wizard.main.php'; require_once __DIR__.'/Wizard.main.php';
require_once $config['homedir'].'/include/functions_users.php'; require_once $config['homedir'].'/include/functions_users.php';
require_once $config['homedir'].'/include/functions_reports.php';
require_once $config['homedir'].'/include/functions_cron.php';
/** /**
* Defined as wizard to guide user to explore running tasks. * Defined as wizard to guide user to explore running tasks.
@ -77,13 +79,22 @@ class DiscoveryTaskList extends Wizard
// Load styles. // Load styles.
parent::run(); parent::run();
$delete = (bool) get_parameter('delete', false); $force_run = (bool) get_parameter('force_run');
if ($force_run === true) {
return $this->forceConsoleTask();
}
if ($delete) { $delete_console_task = (bool) get_parameter('delete_console_task');
if ($delete_console_task === true) {
return $this->deleteConsoleTask();
}
$delete = (bool) get_parameter('delete', false);
if ($delete === true) {
return $this->deleteTask(); return $this->deleteTask();
} }
return $this->showList(); return $this->showListConsoleTask().'</ br>'.$this->showList();
} }
@ -145,6 +156,76 @@ class DiscoveryTaskList extends Wizard
} }
/**
* Force console task.
*
* @return void
*/
public function forceConsoleTask()
{
global $config;
if (! check_acl($config['id_user'], 0, 'PM')) {
db_pandora_audit(
'ACL Violation',
'Trying to access recon task viewer'
);
include 'general/noaccess.php';
return;
}
$id_console_task = (int) get_parameter('id_console_task');
if ($id_console_task !== null) {
cron_task_run($id_console_task, true);
// Trick to avoid double execution.
header('Location: '.$this->url);
}
}
/**
* Delete a Console task.
*
* @return void
*/
public function deleteConsoleTask()
{
global $config;
if (! check_acl($config['id_user'], 0, 'PM')) {
db_pandora_audit(
'ACL Violation',
'Trying to access recon task viewer'
);
include 'general/noaccess.php';
return;
}
$id_console_task = (int) get_parameter('id_console_task');
if ($id_console_task !== null) {
$result = db_process_sql_delete(
'tuser_task_scheduled',
['id' => $id_console_task]
);
if ($result == 1) {
return [
'result' => 0,
'msg' => __('Console Task successfully deleted'),
'id' => false,
];
}
// Trick to avoid double execution.
header('Location: '.$this->url);
}
}
/** /**
* Show complete list of running tasks. * Show complete list of running tasks.
* *
@ -203,7 +284,30 @@ class DiscoveryTaskList extends Wizard
foreach ($servers as $serverItem) { foreach ($servers as $serverItem) {
$id_server = $serverItem['id_server']; $id_server = $serverItem['id_server'];
$server_name = servers_get_name($id_server); $server_name = servers_get_name($id_server);
$recon_tasks = db_get_all_rows_field_filter('trecon_task', 'id_recon_server', $id_server); $recon_tasks = db_get_all_rows_field_filter(
'trecon_task',
'id_recon_server',
$id_server
);
$user_groups = implode(',', array_keys(users_get_groups()));
$defined_tasks = db_get_all_rows_filter(
'tuser_task_scheduled',
'id_grupo IN ('.$user_groups.')'
);
if (isset($tasks_console) === true
&& is_array($tasks_console) === true
) {
foreach ($tasks_console as $key => $value) {
$value['parameters'] = unserialize(
$value['parameters']
);
$value['type'] = 'Cron';
array_push($recon_tasks, $value);
}
}
// Show network tasks for Recon Server. // Show network tasks for Recon Server.
if ($recon_tasks === false) { if ($recon_tasks === false) {
@ -265,7 +369,9 @@ class DiscoveryTaskList extends Wizard
$data[1] = '<b>'.$task['name'].'</b>'; $data[1] = '<b>'.$task['name'].'</b>';
$data[2] = human_time_description_raw($task['interval_sweep']); $data[2] = human_time_description_raw(
$task['interval_sweep']
);
if ($task['id_recon_script'] == 0) { if ($task['id_recon_script'] == 0) {
$data[3] = $task['subnet']; $data[3] = $task['subnet'];
@ -281,23 +387,51 @@ class DiscoveryTaskList extends Wizard
if ($task['id_recon_script'] == 0) { if ($task['id_recon_script'] == 0) {
// Network recon task. // Network recon task.
$data[5] = html_print_image('images/network.png', true, ['title' => __('Network recon task')]).'&nbsp;&nbsp;'; $data[5] = html_print_image(
$data[5] .= network_profiles_get_name($task['id_network_profile']); 'images/network.png',
true,
['title' => __('Network recon task')]
).'&nbsp;&nbsp;';
$data[5] .= network_profiles_get_name(
$task['id_network_profile']
);
} else { } else {
// APP recon task. // APP recon task.
$data[5] = html_print_image('images/plugin.png', true).'&nbsp;&nbsp;'; $data[5] = html_print_image(
$data[5] .= db_get_sql(sprintf('SELECT name FROM trecon_script WHERE id_recon_script = %d', $task['id_recon_script'])); 'images/plugin.png',
true
).'&nbsp;&nbsp;';
$data[5] .= db_get_sql(
sprintf(
'SELECT name FROM trecon_script WHERE id_recon_script = %d',
$task['id_recon_script']
)
);
} }
if ($task['status'] <= 0 || $task['status'] > 100) { if ($task['status'] <= 0 || $task['status'] > 100) {
$data[6] = '-'; $data[6] = '-';
} else { } else {
$data[6] = progress_bar($task['status'], 100, 20, __('Progress').':'.$task['status'].'%', 1); $data[6] = progress_bar(
$task['status'],
100,
20,
__('Progress').':'.$task['status'].'%',
1
);
} }
$data[7] = ui_print_timestamp($task['utimestamp'], true); $data[7] = ui_print_timestamp(
$task['utimestamp'],
true
);
if (check_acl($config['id_user'], $task['id_group'], 'PM')) { if (check_acl(
$config['id_user'],
$task['id_group'],
'PM'
)
) {
// Check if is a H&D, Cloud or Application. // Check if is a H&D, Cloud or Application.
$data[8] = '<a href="'.ui_get_full_url( $data[8] = '<a href="'.ui_get_full_url(
sprintf( sprintf(
@ -306,12 +440,12 @@ class DiscoveryTaskList extends Wizard
$task['id_rt'] $task['id_rt']
) )
).'">'.html_print_image( ).'">'.html_print_image(
'images/wrench_orange.png', 'images/config.png',
true true
).'</a>'; ).'</a>';
$data[8] .= '<a href="'.ui_get_full_url( $data[8] .= '<a href="'.ui_get_full_url(
'index.php?sec=gservers&sec2=godmode/servers/discovery&wiz=tasklist&delete=1&task='.$task['id_rt'] 'index.php?sec=gservers&sec2=godmode/servers/discovery&wiz=tasklist&delete=1&task='.$task['id_rt']
).'">'.html_print_image( ).'" onClick="if (!confirm(\' '.__('Are you sure?').'\')) return false;">'.html_print_image(
'images/cross.png', 'images/cross.png',
true true
).'</a>'; ).'</a>';
@ -325,6 +459,7 @@ class DiscoveryTaskList extends Wizard
if (empty($table->data)) { if (empty($table->data)) {
echo '<div class="nf">'.__('Server').' '.$server_name.' '.__('has no recon tasks assigned').'</div>'; echo '<div class="nf">'.__('Server').' '.$server_name.' '.__('has no recon tasks assigned').'</div>';
} else { } else {
echo '<h2>'.__('Server task').'</h2>';
html_print_table($table); html_print_table($table);
} }
@ -359,6 +494,385 @@ class DiscoveryTaskList extends Wizard
} }
/**
* Show complete list of running tasks.
*
* @return boolean Success or not.
*/
public function showListConsoleTask()
{
global $config;
check_login();
if (! check_acl($config['id_user'], 0, 'PM')) {
db_pandora_audit(
'ACL Violation',
'Trying to access recon task viewer'
);
include 'general/noaccess.php';
return false;
}
$read_perms = check_acl(
$config['id_user'],
0,
'RR'
);
$write_perms = check_acl(
$config['id_user'],
0,
'RW'
);
$manage_perms = check_acl(
$config['id_user'],
0,
'RM'
);
$manage_pandora = check_acl(
$config['id_user'],
0,
'PM'
);
$url = 'index.php?sec=gservers&sec2=godmode/servers/discovery&wiz=tasklist&';
$user_groups = implode(
',',
array_keys(users_get_groups())
);
$defined_tasks = db_get_all_rows_filter(
'tuser_task_scheduled',
'id_grupo IN ('.$user_groups.')'
);
if (!check_acl($config['id_user'], 0, 'PM')) {
$read_tasks = [];
foreach ($defined_tasks as $task) {
$function_name = db_get_value(
'function_name',
'tuser_task',
'id',
$task['id_user_task']
);
if (($function_name != 'cron_task_execute_custom_script')
&& ($function_name != 'cron_task_do_backup')
) {
$read_tasks[] = $task;
}
}
$defined_tasks = $read_tasks;
if (empty($defined_tasks)) {
$defined_tasks = false;
}
}
if ($defined_tasks !== false) {
echo '<h2>'.__('Console task').'</h2>';
$table = new stdClass();
$table->class = 'databox data';
$table->width = '100%';
$table->data = [];
$table->head = [];
$table->head[0] = '';
$table->head[1] = __('User');
$table->head[2] = __('Task');
$table->head[3] = __('Scheduled');
$table->head[4] = __('Next execution');
$table->head[5] = __('Last run');
$table->head[6] = __('Group');
$table->head[7] = __('Operations');
$table->align[7] = 'left';
foreach ($defined_tasks as $task) {
$data = [];
$function_name = db_get_value(
'function_name',
'tuser_task',
'id',
$task['id_user_task']
);
switch ($function_name) {
case 'cron_task_generate_report':
if ($write_perms || $manage_pandora) {
$data[0] = '<a href="'.$url;
$data[0] .= 'force_run=1&id_console_task='.$task['id'].'">';
$data[0] .= html_print_image(
'images/target.png',
true,
['title' => __('Force run')]
);
$data[0] .= '</a>';
} else {
$data[0] = '';
}
$data[1] = $task['id_usuario'];
$data[2] = db_get_value(
'name',
'tuser_task',
'id',
$task['id_user_task']
);
$args = unserialize($task['args']);
$report = reports_get_report($args[0]);
// Check ACL in reports_get_report return false.
if ($report === false) {
continue;
}
$email = $args[1];
$data[2] .= '<br>- '.__('Report').": <a href='index.php?sec=reporting&sec2=operation/reporting/reporting_viewer&id=".$args[0]."'>";
$data[2] .= $report['name'].'</a>';
$data[2] .= '<br>- '.__('Email').": <a href='mailto:".$email."'>";
$data[2] .= ui_print_truncate_text(
$email,
60,
false
).'</a>';
break;
case 'cron_task_generate_report_by_template':
if ($write_perms || $manage_pandora) {
$data[0] = '<a href="'.$url;
$data[0] .= 'force_run=1&id_console_task='.$task['id'].'">';
$data[0] .= html_print_image(
'images/target.png',
true,
['title' => __('Force run')]
);
$data[0] .= '</a>';
} else {
$data[0] = '';
}
$data[1] = $task['id_usuario'];
$data[2] = db_get_value(
'name',
'tuser_task',
'id',
$task['id_user_task']
);
$args = unserialize($task['args']);
$filter = [];
$filter['id_report'] = $args[0];
$template = db_get_row_filter(
'treport_template',
$filter,
false
);
// Check ACL in reports_get_report return false.
if ($template === false) {
continue;
}
$agents_id = $args[1];
$id_group = $args[2];
$report_per_agent = $args[0];
$report_name = $args[3];
$email = $args[4];
$data[2] .= '<br>- '.__('Template').": <a href='index.php?sec=reporting&sec2=operation/reporting/reporting_viewer";
$data[2] .= '&id='.$args[0]."'>".$template['name'].'</a>';
$data[2] .= '<br>- '.__('Agents').': '.$agents_id.'</a>';
$data[2] .= '<br>- '.__('Report per agent').': '.$report_per_agent.'</a>';
$data[2] .= '<br>- '.__('Report name').': '.$report_name.'</a>';
$data[2] .= '<br>- '.__('Email').": <a href='mailto:".$email."'>".$email.'</a>';
break;
case 'cron_task_execute_custom_script':
if ($manage_pandora) {
$data[0] = '<a href="'.$url;
$data[0] .= 'force_run=1&id_console_task='.$task['id'].'">';
$data[0] .= html_print_image(
'images/target.png',
true,
['title' => __('Force run')]
);
$data[0] .= '</a>';
} else {
$data[0] = '';
}
$data[1] = $task['id_usuario'];
$data[2] = db_get_value(
'name',
'tuser_task',
'id',
$task['id_user_task']
);
$args = unserialize($task['args']);
$data[2] .= '<br>- '.__('Custom script').': '.$args[0];
break;
case 'cron_task_save_report_to_disk':
if ($write_perms || $manage_pandora) {
$data[0] = '<a href="'.$url;
$data[0] .= 'force_run=1&id_console_task='.$task['id'].'">';
$data[0] .= html_print_image(
'images/target.png',
true,
['title' => __('Force run')]
);
$data[0] .= '</a>';
} else {
$data[0] = '';
}
$data[1] = $task['id_usuario'];
$data[2] = db_get_value(
'name',
'tuser_task',
'id',
$task['id_user_task']
);
$args = unserialize($task['args']);
$report = reports_get_report($args[0]);
// Check ACL in reports_get_report return false.
if ($report === false) {
continue;
}
$path = $args[1];
$data[2] .= '<br>- '.__('Report').": <a href='index.php?sec=reporting&sec2=operation/reporting/reporting_viewer";
$data[2] .= '&id='.$args[0]."'>".$report['name'].'</a>';
$data[2] .= '<br>- '.__('Path').': '.$path.'</a>';
break;
case 'cron_task_save_xml_report_to_disk':
if ($write_perms || $manage_pandora) {
$data[0] = '<a href="'.$url;
$data[0] .= 'force_run=1&id_console_task='.$task['id'].'">';
$data[0] .= html_print_image(
'images/target.png',
true,
['title' => __('Force run')]
);
$data[0] .= '</a>';
} else {
$data[0] = '';
}
$data[1] = $task['id_usuario'];
$data[2] = db_get_value('name', 'tuser_task', 'id', $task['id_user_task']);
$args = unserialize($task['args']);
$report = reports_get_report($args[0]);
// Check ACL in reports_get_report return false.
if ($report === false) {
continue;
}
$path = $args[1];
$data[2] .= '<br>- '.__('Report').": <a href='index.php?sec=reporting&sec2=operation/reporting/reporting_viewer";
$data[2] .= '&id='.$args[0]."'>".$report['name'].'</a>';
$data[2] .= '<br>- '.__('Path').': '.$path.'</a>';
break;
case 'cron_task_do_backup':
if ($manage_pandora) {
$data[0] = '<a href="'.$url;
$data[0] .= 'force_run=1&id_console_task='.$task['id'].'">';
$data[0] .= html_print_image(
'images/target.png',
true,
['title' => __('Force run')]
);
$data[0] .= '</a>';
} else {
$data[0] = '';
}
$data[1] = $task['id_usuario'];
$data[2] = db_get_value(
'name',
'tuser_task',
'id',
$task['id_user_task']
);
$args = unserialize($task['args']);
break;
default:
// Ignore.
break;
}
$data[3] = cron_get_scheduled_string($task['scheduled']);
$data[4] = date('Y/m/d H:i:s', $args['first_execution']);
$data[5] = empty($task['last_run']) ? __('Never') : date('Y/m/d H:i:s', $task['last_run']);
$data[6] = ui_print_group_icon($task['id_grupo'], true);
if ($function_name == 'cron_task_do_backup' || $function_name == 'cron_task_execute_custom_script') {
if ($manage_pandora) {
$data[7] = '<a href="'.$url;
$data[7] .= 'edit_task=1&id='.$task['id'].'">';
$data[7] .= html_print_image(
'images/config.png',
true,
['title' => __('Edit')]
);
$data[7] .= '</a>';
}
if ($manage_pandora) {
$data[7] .= '<a href="'.$url;
$data[7] .= 'delete_console_task=1&id_console_task='.$task['id'].'" onClick="if (!confirm(\' '.__('Are you sure?').'\')) return false;">';
$data[7] .= html_print_image(
'images/cross.png',
true,
['title' => __('Delete')]
);
$data[7] .= '</a>';
}
} else {
if ($write_perms || $manage_pandora) {
$data[7] = '<a href="'.$url;
$data[7] .= 'edit_task=1&id='.$task['id'].'">';
$data[7] .= html_print_image(
'images/config.png',
true,
['title' => __('Edit')]
);
$data[7] .= '</a>';
}
if ($manage_perms || $manage_pandora) {
$data[7] .= '<a href="'.$url;
$data[7] .= 'delete_console_task=1&id_console_task='.$task['id'].'" onClick="if (!confirm(\' '.__('Are you sure?').'\')) return false;">';
$data[7] .= html_print_image(
'images/cross.png',
true,
['title' => __('Delete')]
);
$data[7] .= '</a>';
}
}
array_push($table->data, $data);
}
html_print_table($table);
}
return true;
}
/** /**
* Return target url sub-string to edit target task. * Return target url sub-string to edit target task.
* *

View File

@ -15,17 +15,23 @@ global $config;
require_once $config['homedir'].'/include/functions_db.php'; require_once $config['homedir'].'/include/functions_db.php';
// Update the execution interval of the given module // Update the execution interval of the given module.
function cron_update_module_interval($module_id, $cron) function cron_update_module_interval($module_id, $cron)
{ {
// Check for a valid cron // Check for a valid cron.
if (!cron_check_syntax($cron)) { if (!cron_check_syntax($cron)) {
return; return;
} }
if ($cron == '* * * * *') { if ($cron == '* * * * *') {
$module_interval = db_get_value_filter('module_interval', 'tagente_modulo', ['id_agente_modulo' => $module_id]); $module_interval = db_get_value_filter(
return db_process_sql('UPDATE tagente_estado SET current_interval = '.$module_interval.' WHERE id_agente_modulo = '.(int) $module_id); 'module_interval',
'tagente_modulo',
['id_agente_modulo' => $module_id]
);
return db_process_sql(
'UPDATE tagente_estado SET current_interval = '.$module_interval.' WHERE id_agente_modulo = '.(int) $module_id
);
} else { } else {
return db_process_sql( return db_process_sql(
'UPDATE tagente_estado SET current_interval = '.cron_next_execution($cron, $module_interval, $module_id).' WHERE id_agente_modulo = '.(int) $module_id 'UPDATE tagente_estado SET current_interval = '.cron_next_execution($cron, $module_interval, $module_id).' WHERE id_agente_modulo = '.(int) $module_id
@ -38,7 +44,7 @@ function cron_update_module_interval($module_id, $cron)
// Get the number of seconds left to the next execution of the given cron entry. // Get the number of seconds left to the next execution of the given cron entry.
function cron_next_execution($cron, $module_interval, $module_id) function cron_next_execution($cron, $module_interval, $module_id)
{ {
// Get day of the week and month from cron config // Get day of the week and month from cron config.
$cron_array = explode(' ', $cron); $cron_array = explode(' ', $cron);
$minute = $cron_array[0]; $minute = $cron_array[0];
$hour = $cron_array[1]; $hour = $cron_array[1];
@ -46,21 +52,34 @@ function cron_next_execution($cron, $module_interval, $module_id)
$month = $cron_array[3]; $month = $cron_array[3];
$wday = $cron_array[4]; $wday = $cron_array[4];
// Get last execution time // Get last execution time.
$last_execution = db_get_value('utimestamp', 'tagente_estado', 'id_agente_modulo', $module_id); $last_execution = db_get_value(
'utimestamp',
'tagente_estado',
'id_agente_modulo',
$module_id
);
$cur_time = ($last_execution !== false) ? $last_execution : time(); $cur_time = ($last_execution !== false) ? $last_execution : time();
// Any day of the way // Any day of the way.
if ($wday == '*') { if ($wday == '*') {
$nex_time = cron_next_execution_date($cron, $cur_time, $module_interval); $nex_time = cron_next_execution_date(
$cron,
$cur_time,
$module_interval
);
return ($nex_time - $cur_time); return ($nex_time - $cur_time);
} }
// A specific day of the week // A specific day of the week.
$count = 0; $count = 0;
$nex_time = $cur_time; $nex_time = $cur_time;
do { do {
$nex_time = cron_next_execution_date($cron, $nex_time, $module_interval); $nex_time = cron_next_execution_date(
$cron,
$nex_time,
$module_interval
);
$nex_time_wd = $nex_time; $nex_time_wd = $nex_time;
$array_nex = explode(' ', date('m w', $nex_time_wd)); $array_nex = explode(' ', date('m w', $nex_time_wd));
@ -68,12 +87,12 @@ function cron_next_execution($cron, $module_interval, $module_id)
$nex_wday = $array_nex[1]; $nex_wday = $array_nex[1];
do { do {
// Check the day of the week // Check the day of the week.
if ($nex_wday == $wday) { if ($nex_wday == $wday) {
return ($nex_time_wd - $cur_time); return ($nex_time_wd - $cur_time);
} }
// Move to the next day of the month // Move to the next day of the month.
$nex_time_wd += SECONDS_1DAY; $nex_time_wd += SECONDS_1DAY;
$array_nex_w = explode(' ', date('m w', $nex_time_wd)); $array_nex_w = explode(' ', date('m w', $nex_time_wd));
@ -84,7 +103,7 @@ function cron_next_execution($cron, $module_interval, $module_id)
$count++; $count++;
} while ($count < SECONDS_1MINUTE); } while ($count < SECONDS_1MINUTE);
// Something went wrong, default to 5 minutes // Something went wrong, default to 5 minutes.
return SECONDS_5MINUTES; return SECONDS_5MINUTES;
} }
@ -92,11 +111,11 @@ function cron_next_execution($cron, $module_interval, $module_id)
// Get the next execution date for the given cron entry in seconds since epoch. // Get the next execution date for the given cron entry in seconds since epoch.
function cron_next_execution_date($cron, $cur_time=false, $module_interval=300) function cron_next_execution_date($cron, $cur_time=false, $module_interval=300)
{ {
// Get cron configuration // Get cron configuration.
$cron_array = explode(' ', $cron); $cron_array = explode(' ', $cron);
// REMARKS: Months start from 1 in php (different to server) // REMARKS: Months start from 1 in php (different to server)
// Get current time // Get current time.
if ($cur_time === false) { if ($cur_time === false) {
$cur_time = time(); $cur_time = time();
} }
@ -107,7 +126,7 @@ function cron_next_execution_date($cron, $cur_time=false, $module_interval=300)
return $nex_time; return $nex_time;
} }
// Update minutes // Update minutes.
$min_s = cron_get_interval($cron_array[0]); $min_s = cron_get_interval($cron_array[0]);
$nex_time_array[0] = ($min_s['down'] == '*') ? 0 : $min_s['down']; $nex_time_array[0] = ($min_s['down'] == '*') ? 0 : $min_s['down'];
@ -118,22 +137,22 @@ function cron_next_execution_date($cron, $cur_time=false, $module_interval=300)
} }
} }
// Check if next hour is in cron // Check if next hour is in cron.
$nex_time_array[1]++; $nex_time_array[1]++;
$nex_time = cron_valid_date($nex_time_array); $nex_time = cron_valid_date($nex_time_array);
if ($nex_time === false) { if ($nex_time === false) {
// Update the month day if overflow // Update the month day if overflow.
$nex_time_array[1] = 0; $nex_time_array[1] = 0;
$nex_time_array[2]++; $nex_time_array[2]++;
$nex_time = cron_valid_date($nex_time_array); $nex_time = cron_valid_date($nex_time_array);
if ($nex_time === false) { if ($nex_time === false) {
// Update the month if overflow // Update the month if overflow.
$nex_time_array[2] = 1; $nex_time_array[2] = 1;
$nex_time_array[3]++; $nex_time_array[3]++;
$nex_time = cron_valid_date($nex_time_array); $nex_time = cron_valid_date($nex_time_array);
if ($nex_time === false) { if ($nex_time === false) {
// Update the year if overflow // Update the year if overflow.
$nex_time_array[3] = 1; $nex_time_array[3] = 1;
$nex_time_array[4]++; $nex_time_array[4]++;
$nex_time = cron_valid_date($nex_time_array); $nex_time = cron_valid_date($nex_time_array);
@ -141,16 +160,16 @@ function cron_next_execution_date($cron, $cur_time=false, $module_interval=300)
} }
} }
// Check the hour // Check the hour.
if (cron_is_in_cron($cron_array, $nex_time_array) && $nex_time) { if (cron_is_in_cron($cron_array, $nex_time_array) && $nex_time) {
return $nex_time; return $nex_time;
} }
// Update the hour if fails // Update the hour if fails.
$hour_s = cron_get_interval($cron_array[1]); $hour_s = cron_get_interval($cron_array[1]);
$nex_time_array[1] = ($hour_s['down'] == '*') ? 0 : $hour_s['down']; $nex_time_array[1] = ($hour_s['down'] == '*') ? 0 : $hour_s['down'];
// When an overflow is passed check the hour update again // When an overflow is passed check the hour update again.
$nex_time = cron_valid_date($nex_time_array); $nex_time = cron_valid_date($nex_time_array);
if ($nex_time >= $cur_time) { if ($nex_time >= $cur_time) {
if (cron_is_in_cron($cron_array, $nex_time_array) && $nex_time) { if (cron_is_in_cron($cron_array, $nex_time_array) && $nex_time) {
@ -158,32 +177,32 @@ function cron_next_execution_date($cron, $cur_time=false, $module_interval=300)
} }
} }
// Check if next day is in cron // Check if next day is in cron.
$nex_time_array[2]++; $nex_time_array[2]++;
$nex_time = cron_valid_date($nex_time_array); $nex_time = cron_valid_date($nex_time_array);
if ($nex_time === false) { if ($nex_time === false) {
// Update the month if overflow // Update the month if overflow.
$nex_time_array[2] = 1; $nex_time_array[2] = 1;
$nex_time_array[3]++; $nex_time_array[3]++;
$nex_time = cron_valid_date($nex_time_array); $nex_time = cron_valid_date($nex_time_array);
if ($nex_time === false) { if ($nex_time === false) {
// Update the year if overflow // Update the year if overflow.
$nex_time_array[3] = 1; $nex_time_array[3] = 1;
$nex_time_array[4]++; $nex_time_array[4]++;
$nex_time = cron_valid_date($nex_time_array); $nex_time = cron_valid_date($nex_time_array);
} }
} }
// Check the day // Check the day.
if (cron_is_in_cron($cron_array, $nex_time_array) && $nex_time) { if (cron_is_in_cron($cron_array, $nex_time_array) && $nex_time) {
return $nex_time; return $nex_time;
} }
// Update the day if fails // Update the day if fails.
$mday_s = cron_get_interval($cron_array[2]); $mday_s = cron_get_interval($cron_array[2]);
$nex_time_array[2] = ($mday_s['down'] == '*') ? 1 : $mday_s['down']; $nex_time_array[2] = ($mday_s['down'] == '*') ? 1 : $mday_s['down'];
// When an overflow is passed check the hour update in the next execution // When an overflow is passed check the hour update in the next execution.
$nex_time = cron_valid_date($nex_time_array); $nex_time = cron_valid_date($nex_time_array);
if ($nex_time >= $cur_time) { if ($nex_time >= $cur_time) {
if (cron_is_in_cron($cron_array, $nex_time_array) && $nex_time) { if (cron_is_in_cron($cron_array, $nex_time_array) && $nex_time) {
@ -191,26 +210,26 @@ function cron_next_execution_date($cron, $cur_time=false, $module_interval=300)
} }
} }
// Check if next month is in cron // Check if next month is in cron.
$nex_time_array[3]++; $nex_time_array[3]++;
$nex_time = cron_valid_date($nex_time_array); $nex_time = cron_valid_date($nex_time_array);
if ($nex_time === false) { if ($nex_time === false) {
// Update the year if overflow // Update the year if overflow.
$nex_time_array[3] = 1; $nex_time_array[3] = 1;
$nex_time_array[4]++; $nex_time_array[4]++;
$nex_time = cron_valid_date($nex_time_array); $nex_time = cron_valid_date($nex_time_array);
} }
// Check the month // Check the month.
if (cron_is_in_cron($cron_array, $nex_time_array) && $nex_time) { if (cron_is_in_cron($cron_array, $nex_time_array) && $nex_time) {
return $nex_time; return $nex_time;
} }
// Update the month if fails // Update the month if fails.
$mon_s = cron_get_interval($cron_array[3]); $mon_s = cron_get_interval($cron_array[3]);
$nex_time_array[3] = ($mon_s['down'] == '*') ? 1 : $mon_s['down']; $nex_time_array[3] = ($mon_s['down'] == '*') ? 1 : $mon_s['down'];
// When an overflow is passed check the hour update in the next execution // When an overflow is passed check the hour update in the next execution.
$nex_time = cron_valid_date($nex_time_array); $nex_time = cron_valid_date($nex_time_array);
if ($nex_time >= $cur_time) { if ($nex_time >= $cur_time) {
if (cron_is_in_cron($cron_array, $nex_time_array) && $nex_time) { if (cron_is_in_cron($cron_array, $nex_time_array) && $nex_time) {
@ -218,7 +237,7 @@ function cron_next_execution_date($cron, $cur_time=false, $module_interval=300)
} }
} }
// Update the year // Update the year.
$nex_time_array[4]++; $nex_time_array[4]++;
$nex_time = cron_valid_date($nex_time_array); $nex_time = cron_valid_date($nex_time_array);
@ -226,10 +245,10 @@ function cron_next_execution_date($cron, $cur_time=false, $module_interval=300)
} }
// Get an array with the cron interval // Get an array with the cron interval.
function cron_get_interval($element) function cron_get_interval($element)
{ {
// Not a range // Not a range.
if (!preg_match('/(\d+)\-(\d+)/', $element, $capture)) { if (!preg_match('/(\d+)\-(\d+)/', $element, $capture)) {
return [ return [
'down' => $element, 'down' => $element,
@ -250,12 +269,12 @@ function cron_is_in_cron($elems_cron, $elems_curr_time)
$elem_cron = array_shift($elems_cron); $elem_cron = array_shift($elems_cron);
$elem_curr_time = array_shift($elems_curr_time); $elem_curr_time = array_shift($elems_curr_time);
// If there is no elements means that is in cron // If there is no elements means that is in cron.
if ($elem_cron === null || $elem_curr_time === null) { if ($elem_cron === null || $elem_curr_time === null) {
return true; return true;
} }
// Go to last element if current is a wild card // Go to last element if current is a wild card.
if ($elem_cron != '*') { if ($elem_cron != '*') {
$elem_s = cron_get_interval($elem_cron); $elem_s = cron_get_interval($elem_cron);
// Check if there is no a range // Check if there is no a range
@ -263,7 +282,7 @@ function cron_is_in_cron($elems_cron, $elems_curr_time)
return false; return false;
} }
// Check if there is on the range // Check if there is on the range.
if ($elem_s['up'] !== false) { if ($elem_s['up'] !== false) {
if ($elem_s['down'] < $elem_s['up']) { if ($elem_s['down'] < $elem_s['up']) {
if ($elem_curr_time < $elem_s['down'] || $elem_curr_time > $elem_s['up']) { if ($elem_curr_time < $elem_s['down'] || $elem_curr_time > $elem_s['up']) {
@ -283,14 +302,365 @@ function cron_is_in_cron($elems_cron, $elems_curr_time)
function cron_valid_date($da) function cron_valid_date($da)
{ {
$st = sprintf('%04d:%02d:%02d %02d:%02d:00', $da[4], $da[3], $da[2], $da[1], $da[0]); $st = sprintf(
'%04d:%02d:%02d %02d:%02d:00',
$da[4],
$da[3],
$da[2],
$da[1],
$da[0]
);
$time = strtotime($st); $time = strtotime($st);
return $time; return $time;
} }
// Check if cron is properly constructed // Check if cron is properly constructed.
function cron_check_syntax($cron) function cron_check_syntax($cron)
{ {
return preg_match('/^[\d|\*].* .*[\d|\*].* .*[\d|\*].* .*[\d|\*].* .*[\d|\*]$/', $cron); return preg_match(
'/^[\d|\*].* .*[\d|\*].* .*[\d|\*].* .*[\d|\*].* .*[\d|\*]$/',
$cron
);
}
function cron_list_table()
{
global $config;
$read_perms = check_acl($config['id_user'], 0, 'RR');
$write_perms = check_acl($config['id_user'], 0, 'RW');
$manage_perms = check_acl($config['id_user'], 0, 'RM');
$manage_pandora = check_acl($config['id_user'], 0, 'PM');
$url = 'index.php?extension_in_menu=gservers&sec=extensions&sec2=enterprise/extensions/cron&';
$user_groups = implode(
',',
array_keys(users_get_groups())
);
$defined_tasks = db_get_all_rows_filter(
'tuser_task_scheduled',
'id_grupo IN ('.$user_groups.')'
);
if (!check_acl($config['id_user'], 0, 'PM')) {
$read_tasks = [];
foreach ($defined_tasks as $task) {
$function_name = db_get_value(
'function_name',
'tuser_task',
'id',
$task['id_user_task']
);
if (($function_name != 'cron_task_execute_custom_script')
&& ($function_name != 'cron_task_do_backup')
) {
$read_tasks[] = $task;
}
}
$defined_tasks = $read_tasks;
if (empty($defined_tasks)) {
$defined_tasks = false;
}
}
if ($defined_tasks !== false) {
echo '<h2>'.__('Scheduled jobs').'</h2>';
$table = new stdClass();
$table->class = 'databox data';
$table->width = '100%';
$table->data = [];
$table->head = [];
$table->head[0] = '';
$table->head[1] = __('User');
$table->head[2] = __('Task');
$table->head[3] = __('Scheduled');
$table->head[4] = __('Next execution');
$table->head[5] = __('Last run');
$table->head[6] = __('Group');
$table->head[7] = __('Actions');
$table->align[7] = 'left';
foreach ($defined_tasks as $task) {
$data = [];
$function_name = db_get_value(
'function_name',
'tuser_task',
'id',
$task['id_user_task']
);
switch ($function_name) {
case 'cron_task_generate_report':
if ($write_perms || $manage_pandora) {
$data[0] = '<a href="'.$url;
$data[0] .= 'force_run=1&id_user_task='.$task['id'].'">';
$data[0] .= html_print_image(
'images/target.png',
true,
['title' => __('Force run')]
);
$data[0] .= '</a>';
} else {
$data[0] = '';
}
$data[1] = $task['id_usuario'];
$data[2] = db_get_value(
'name',
'tuser_task',
'id',
$task['id_user_task']
);
$args = unserialize($task['args']);
$report = reports_get_report($args[0]);
// Check ACL in reports_get_report return false.
if ($report === false) {
continue;
}
$email = $args[1];
$data[2] .= '<br>- '.__('Report').": <a href='index.php?sec=reporting&sec2=operation/reporting/reporting_viewer&id=".$args[0]."'>";
$data[2] .= $report['name'].'</a>';
$data[2] .= '<br>- '.__('Email').": <a href='mailto:".$email."'>";
$data[2] .= ui_print_truncate_text($email, 60, false).'</a>';
break;
case 'cron_task_generate_report_by_template':
if ($write_perms || $manage_pandora) {
$data[0] = '<a href="'.$url;
$data[0] .= 'force_run=1&id_user_task='.$task['id'].'">';
$data[0] .= html_print_image(
'images/target.png',
true,
['title' => __('Force run')]
);
$data[0] .= '</a>';
} else {
$data[0] = '';
}
$data[1] = $task['id_usuario'];
$data[2] = db_get_value(
'name',
'tuser_task',
'id',
$task['id_user_task']
);
$args = unserialize($task['args']);
$filter = [];
$filter['id_report'] = $args[0];
$template = db_get_row_filter(
'treport_template',
$filter,
false
);
// Check ACL in reports_get_report return false.
if ($template === false) {
continue;
}
$agents_id = $args[1];
$id_group = $args[2];
$report_per_agent = $args[0];
$report_name = $args[3];
$email = $args[4];
$data[2] .= '<br>- '.__('Template').": <a href='index.php?sec=reporting&sec2=operation/reporting/reporting_viewer";
$data[2] .= '&id='.$args[0]."'>".$template['name'].'</a>';
$data[2] .= '<br>- '.__('Agents').': '.$agents_id.'</a>';
$data[2] .= '<br>- '.__('Report per agent').': '.$report_per_agent.'</a>';
$data[2] .= '<br>- '.__('Report name').': '.$report_name.'</a>';
$data[2] .= '<br>- '.__('Email').": <a href='mailto:".$email."'>".$email.'</a>';
break;
case 'cron_task_execute_custom_script':
if ($manage_pandora) {
$data[0] = '<a href="'.$url;
$data[0] .= 'force_run=1&id_user_task='.$task['id'].'">';
$data[0] .= html_print_image(
'images/target.png',
true,
['title' => __('Force run')]
);
$data[0] .= '</a>';
} else {
$data[0] = '';
}
$data[1] = $task['id_usuario'];
$data[2] = db_get_value(
'name',
'tuser_task',
'id',
$task['id_user_task']
);
$args = unserialize($task['args']);
$data[2] .= '<br>- '.__('Custom script').': '.$args[0];
break;
case 'cron_task_save_report_to_disk':
if ($write_perms || $manage_pandora) {
$data[0] = '<a href="'.$url;
$data[0] .= 'force_run=1&id_user_task='.$task['id'].'">';
$data[0] .= html_print_image(
'images/target.png',
true,
['title' => __('Force run')]
);
$data[0] .= '</a>';
} else {
$data[0] = '';
}
$data[1] = $task['id_usuario'];
$data[2] = db_get_value(
'name',
'tuser_task',
'id',
$task['id_user_task']
);
$args = unserialize($task['args']);
$report = reports_get_report($args[0]);
// Check ACL in reports_get_report return false.
if ($report === false) {
continue;
}
$path = $args[1];
$data[2] .= '<br>- '.__('Report').": <a href='index.php?sec=reporting&sec2=operation/reporting/reporting_viewer";
$data[2] .= '&id='.$args[0]."'>".$report['name'].'</a>';
$data[2] .= '<br>- '.__('Path').': '.$path.'</a>';
break;
case 'cron_task_save_xml_report_to_disk':
if ($write_perms || $manage_pandora) {
$data[0] = '<a href="'.$url;
$data[0] .= 'force_run=1&id_user_task='.$task['id'].'">';
$data[0] .= html_print_image(
'images/target.png',
true,
['title' => __('Force run')]
);
$data[0] .= '</a>';
} else {
$data[0] = '';
}
$data[1] = $task['id_usuario'];
$data[2] = db_get_value('name', 'tuser_task', 'id', $task['id_user_task']);
$args = unserialize($task['args']);
$report = reports_get_report($args[0]);
// Check ACL in reports_get_report return false.
if ($report === false) {
continue;
}
$path = $args[1];
$data[2] .= '<br>- '.__('Report').": <a href='index.php?sec=reporting&sec2=operation/reporting/reporting_viewer";
$data[2] .= '&id='.$args[0]."'>".$report['name'].'</a>';
$data[2] .= '<br>- '.__('Path').': '.$path.'</a>';
break;
case 'cron_task_do_backup':
if ($manage_pandora) {
$data[0] = '<a href="'.$url;
$data[0] .= 'force_run=1&id_user_task='.$task['id'].'">';
$data[0] .= html_print_image(
'images/target.png',
true,
['title' => __('Force run')]
);
$data[0] .= '</a>';
} else {
$data[0] = '';
}
$data[1] = $task['id_usuario'];
$data[2] = db_get_value(
'name',
'tuser_task',
'id',
$task['id_user_task']
);
$args = unserialize($task['args']);
break;
default:
// Ignore.
break;
}
$data[3] = cron_get_scheduled_string($task['scheduled']);
$data[4] = date('Y/m/d H:i:s', $args['first_execution']);
$data[5] = empty($task['last_run']) ? __('Never') : date('Y/m/d H:i:s', $task['last_run']);
$data[6] = ui_print_group_icon($task['id_grupo'], true);
if ($function_name == 'cron_task_do_backup' || $function_name == 'cron_task_execute_custom_script') {
if ($manage_pandora) {
$data[7] = '<a href="'.$url;
$data[7] .= 'edit_task=1&id='.$task['id'].'">';
$data[7] .= html_print_image(
'images/config.png',
true,
['title' => __('Edit')]
);
$data[7] .= '</a>';
}
if ($manage_pandora) {
$data[7] .= '<a href="'.$url;
$data[7] .= 'delete_task=1&id_user_task='.$task['id'].'">';
$data[7] .= html_print_image(
'images/cross.png',
true,
['title' => __('Delete')]
);
$data[7] .= '</a>';
}
} else {
if ($write_perms || $manage_pandora) {
$data[7] = '<a href="'.$url;
$data[7] .= 'edit_task=1&id='.$task['id'].'">';
$data[7] .= html_print_image(
'images/config.png',
true,
['title' => __('Edit')]
);
$data[7] .= '</a>';
}
if ($manage_perms || $manage_pandora) {
$data[7] .= '<a href="'.$url;
$data[7] .= 'delete_task=1&id_user_task='.$task['id'].'">';
$data[7] .= html_print_image(
'images/cross.png',
true,
['title' => __('Delete')]
);
$data[7] .= '</a>';
}
}
array_push($table->data, $data);
}
html_print_table($table);
}
} }