function check_deploy_status($conn, $wizard) { $data = array(); $os = $wizard->get_step_data('deploy_os'); //Linux Deployment Status --> Apply Configuration if ($os == 'linux') { $sensor_id = get_sensor_id(); $error_apply = FALSE; try { list($agentless_list, $al_total) = Ossec_agentless::get_list($conn, $sensor_id, ' AND status = 1'); if ($al_total > 0) { Ossec_agentless::save_in_config($conn, $sensor_id, $agentless_list); } //Enabling agentless Ossec_control::execute_action($sensor_id, 'enable_al'); //Restarting ossec Ossec_control::execute_action($sensor_id, 'restart'); // Delete "/var/tmp/.reload_<sensor_id>" file in order to hide the "Apply Changes" button @unlink('/var/tmp/.reload_' . $sensor_id); } catch (Exception $e) { $error_apply = $e->getMessage(); } //If there was an error applying the configuration we show the error if ($error_apply !== FALSE) { $error_apply_msg = _('Error Applying Agentless Configuration'); set_scan_error_message($wizard, $error_apply_msg); Av_exception::write_log(Av_exception::USER_ERROR, $error_apply); $response['error'] = TRUE; return $response; } //If everything was right, the percent is 100% and the remaining is 0 $data['finish'] = TRUE; $data['percent'] = 100; $data['remaining'] = 0; //Setting the deployment status to 3 --> Finished $wizard->set_step_data('deploy_step', 3); } elseif ($os == 'windows') { $jobs = $wizard->get_step_data('deploy_jobs'); //If the array of jobs IDs is empty, we are finished if (!is_array($jobs) || count($jobs) == 0) { $data['finish'] = TRUE; $data['percent'] = 100; $data['remaining'] = 0; //Setting the deployment status to 3 --> Finished $wizard->set_step_data('deploy_step', 3); } else { $succes = 0; //Going through the jobs foreach ($jobs as $id => $job) { try { //Getting the status of the job $state = Welcome_wizard::current_jobs($job['job_id']); if ($state['job_status'] == 'task-succeeded') { //If it is success, we count it and we delete it from the jobs array if ($state['job_result'][0] === TRUE) { unset($jobs[$id]); $succes++; } elseif ($state['job_result'][0] === FALSE) { unset($jobs[$id]); Av_exception::write_log(Av_exception::USER_ERROR, $job['agent'] . ': ' . $state['job_result'][1]); } } elseif ($state['job_status'] == 'task-failed' || $state['job_status'] == 'task-revoked') { unset($jobs[$id]); $_msg = $job['agent'] . ': ' . _("Couldn't complete windows OSSEC agent deploy: ") . $state['job_status']; Av_exception::write_log(Av_exception::USER_ERROR, $_msg); } } catch (Exception $e) { //In case of critical error we delete from the array to avoid loops unset($jobs[$id]); Av_exception::write_log(Av_exception::USER_ERROR, $job['agent'] . ': ' . $e->getMessage()); } } //IF after checking the status, the array is empty, we are finished if (!is_array($jobs) || count($jobs) == 0) { $data['finish'] = TRUE; $data['percent'] = 100; $data['remaining'] = 0; //Setting the deployment status to 3 --> Finished $wizard->set_step_data('deploy_step', 3); } else { //Total number of host that were selected to be deployed $total = $wizard->get_step_data('deploy_total_ips'); $total = $total < 1 ? 1 : $total; //Number of host left to be deployed --> Pending jobs $current = count($jobs); //Percentage of the remaining hosts $pending = $total - $current; $percent = round(100 * ($pending / $total)); $data['finish'] = FALSE; $data['percent'] = $percent; $data['remaining'] = $current; } //Updating the number of host successfully deployed $deployed = $wizard->get_step_data('deploy_success'); $deployed += $succes; $wizard->set_step_data('deploy_success', $deployed); //Updating the array of jobs left $wizard->set_step_data('deploy_jobs', $jobs); } } //Saving wizard status $wizard->save_status(); $response['error'] = FALSE; $response['data'] = $data; return $response; }
function check_scan_progress($conn, $wizard) { //File to cache scan object $user = Session::get_session_user(); $scan_file = 'w_last_asset_object-' . md5($user); $data = array(); try { $obj = Av_scan::get_object_from_file($scan_file); //Get status $aux_status = $obj->get_status(); $status = $aux_status['status']['code']; if ($status == Av_scan::ST_SCAN_FINISHED) { //Scanning has finished properly $info = array(); $scan_report = $obj->download_scan_report(); //Delete scan $obj->delete_scan(); Cache_file::remove_file($scan_file); //Parsing scan report $nmap_parser = new Nmap_parser(); $scan_report = $nmap_parser->parse_json($scan_report, $obj->get_sensor()); // Add summary $scan_report['nmap_data']['elapsed'] = $aux_status['elapsed_time']; $info = Welcome_wizard::format_result_scan($conn, $scan_report); $wizard->set_step_data('scan_step', 3); $wizard->set_step_data('scan_info', $info); $data['finish'] = TRUE; } else { $percent = $aux_status['scanned_targets'] / $aux_status['number_of_targets'] * 100; $data['finish'] = FALSE; $data['percent'] = round($percent); $data['current'] = $aux_status['scanned_targets']; $data['total'] = $aux_status['number_of_targets']; if ($aux_status['remaining_time'] == -1) { $data['time'] = _('Calculating Remaining Time'); } else { $data['time'] = Welcome_wizard::format_time($aux_status['remaining_time']) . ' ' . _('remaining'); } } $response['error'] = FALSE; $response['data'] = $data; $wizard->save_status(); } catch (Exception $e) { $msg = _('Error! Asset scan cannot be completed. Please try again'); set_scan_error_message($wizard, $msg); $response['error'] = TRUE; } return $response; }
function check_scan_progress($conn, $wizard) { $data = array(); $obj = new Scan(); $status = $obj->get_status(); //Get status if ($status == 'Scan Finished') { $info = array(); $result = $obj->get_results(); $obj->delete_data(); $info = Welcome_wizard::format_result_scan($conn, $result); $wizard->set_step_data('scan_step', 3); $wizard->set_step_data('scan_info', $info); $data['finish'] = TRUE; } elseif ($status == 'Scanning Hosts') { $progress = $obj->get_progress(); $percent = $progress['hosts_scanned'] / $progress['total_hosts'] * 100; $data['finish'] = FALSE; $data['percent'] = round($percent); $data['current'] = $progress['hosts_scanned']; $data['total'] = $progress['total_hosts']; if ($progress['remaining'] == -1) { $data['time'] = _('Calculating Remaining Time'); } else { $data['time'] = Welcome_wizard::format_time($progress['remaining']) . ' ' . _('remaining'); } } else { $msg = _("Invalid NMAP status ({$status}). Expecting 'Scanning Hosts' or 'Scan Finished'"); set_scan_error_message($wizard, $msg); $response['error'] = TRUE; return $response; } $response['error'] = FALSE; $response['data'] = $data; $wizard->save_status(); return $response; }