From 427c56280e27f3c54db150c5f1d48c0f150362e9 Mon Sep 17 00:00:00 2001 From: Henrik Smederod Date: Wed, 7 May 2014 14:36:20 +0200 Subject: [PATCH] alert_history_csv: Enable printing of long output MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Take include long output option into consideration when creating a csv from alert history. Submitting a passive check result with Okänd status ","Räksmörgåsen är mumsig works (= all output is in the same cell) when opening the csv in both Excel 2013 and Libreoffice Libreoffice also handles the newlines properly (=one cell with several lines in it), but Excel does not (and can not, it seems from some googling). fgetcsv is able to read back the csv properly, so at least one "script-friendly" method exists for parsing the file. Newlines in csv feel quite strange to me, but since f{put,get}csv supports it and Libreoffice supports it, I guess we're good to go. This implements #8416 Change-Id: If95bc04917c2d254a9e25cb618a39c4460a28995 Signed-off-by: Henrik Smederod --- modules/reports/views/summary/csv.php | 48 +++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/modules/reports/views/summary/csv.php b/modules/reports/views/summary/csv.php index 3d29897cc..9450ea33c 100644 --- a/modules/reports/views/summary/csv.php +++ b/modules/reports/views/summary/csv.php @@ -1,51 +1,57 @@ options->get_value('summary_type'), - 'From: '.$options->get_date('start_time'), - 'To: '.$options->get_date('end_time'), - 'Duration: '.($options['end_time'] - $options['start_time']) -)).'"'); +$csv_content = array( + array( + $this->options->get_value('summary_type'), + 'From: '.$options->get_date('start_time'), + 'To: '.$options->get_date('end_time'), + 'Duration: '.($options['end_time'] - $options['start_time']) + ) +); if(Summary_options::RECENT_ALERTS == $options['summary_type']) { // headers - $csv_content[] = '"'.implode('", "', array( + $csv_content[] = array( 'TIME', 'ALERT TYPE', 'HOST', 'SERVICE', 'STATE TYPE', 'INFORMATION' - )).'"'; + ); // content foreach($result as $log_entry) { - $csv_content[] = '"'.implode('", "', array( + $output = $log_entry['output']; + if ($options['include_long_output'] && $log_entry['long_output'] !== NULL) { + $output .= "\n" . str_replace(PHP_EOL, "\n", $log_entry['long_output']); + } + $csv_content[] = array( date($date_format, $log_entry['timestamp']), Reports_Model::event_type_to_string($log_entry['event_type']), isset($log_entry['host_name'])?$log_entry['host_name']:'', isset($log_entry['service_description'])? $log_entry['service_description'] : 'N/A', $log_entry['hard'] ? _('Hard') : _('Soft'), - $log_entry['output'] - )).'"'; + $output + ); } } elseif(Summary_options::TOP_ALERT_PRODUCERS == $options['summary_type']) { // summary of services // headers - $csv_content[] = '"'.implode('", "', array( + $csv_content[] = array( 'HOST', 'SERVICE', 'ALERT TYPE', 'TOTAL ALERTS' - )).'"'; + ); // content foreach($result as $log_entry) { - $csv_content[] = '"'.implode('", "', array( + $csv_content[] = array( $log_entry['host_name'], isset($log_entry['service_description']) ? $log_entry['service_description'] : null, Reports_Model::event_type_to_string($log_entry['event_type'], 'service'), $log_entry['total_alerts'] - )).'"'; + ); } } else { // custom settings, even more alert types to choose from; @@ -75,7 +81,7 @@ if(Summary_options::RECENT_ALERTS == $options['summary_type']) { array_splice($header, 1, 1, 'SERVICEGROUP'); break; } - $csv_content[] = '"'.implode('", "', $header).'"'; + $csv_content[] = $header; foreach ($result as $host_name => $ary) { $service_name = null; if($options['report_type'] == 'services') { @@ -94,7 +100,7 @@ if(Summary_options::RECENT_ALERTS == $options['summary_type']) { array_splice($row, 2, 0, $service_name); } } - $csv_content[] = '"'.implode('", "', $row).'"'; + $csv_content[] = $row; foreach($ary['service'] as $state => $service) { $row = array( $label, @@ -108,8 +114,12 @@ if(Summary_options::RECENT_ALERTS == $options['summary_type']) { array_splice($row, 2, 0, $service_name); } } - $csv_content[] = '"'.implode('", "', $row).'"'; + $csv_content[] = $row; } } -echo implode($csv_content, "\n")."\n"; +$out = fopen('php://output', 'w'); +foreach ($csv_content as $line) { + fputcsv($out, $line); +} +fclose($out); -- 2.11.4.GIT