Merge branch 'maint/7.0'
[ninja.git] / modules / reports / views / summary / csv.php
blob16a09799bfb87f0282efa4804c38652c6b3f4fc8
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
2 $csv_content = array(
3 array(
4 $this->options->get_value('summary_type'),
5 'From: '.$options->get_date('start_time'),
6 'To: '.$options->get_date('end_time'),
7 'Duration: '.($options['end_time'] - $options['start_time'])
9 );
11 if(Summary_options::RECENT_ALERTS == $options['summary_type']) {
12 // headers
13 $csv_content[] = array(
14 'TIME',
15 'ALERT TYPE',
16 'HOST',
17 'SERVICE',
18 'STATE TYPE',
19 'INFORMATION'
22 // content
23 foreach($result as $log_entry) {
24 $output = $log_entry['output'];
25 if ($options['include_long_output'] && $log_entry['long_output'] !== NULL) {
26 $output .= "\n" . str_replace(PHP_EOL, "\n", $log_entry['long_output']);
28 $csv_content[] = array(
29 date($date_format, $log_entry['timestamp']),
30 Reports_Model::event_type_to_string($log_entry['event_type']),
31 isset($log_entry['host_name'])?$log_entry['host_name']:'',
32 isset($log_entry['service_description'])? $log_entry['service_description'] : 'N/A',
33 $log_entry['hard'] ? _('Hard') : _('Soft'),
34 trim($output)
37 } elseif(Summary_options::TOP_ALERT_PRODUCERS == $options['summary_type']) {
38 // summary of services
39 // headers
40 $csv_content[] = array(
41 'HOST',
42 'SERVICE',
43 'ALERT TYPE',
44 'TOTAL ALERTS'
47 // content
48 foreach($result as $log_entry) {
49 $csv_content[] = array(
50 $log_entry['host_name'],
51 isset($log_entry['service_description']) ? $log_entry['service_description'] : null,
52 Reports_Model::event_type_to_string($log_entry['event_type'], 'service'),
53 $log_entry['total_alerts']
56 } else {
57 // custom settings, even more alert types to choose from;
58 // also explains the nested layout of $result
59 $header = array(
60 'TYPE',
61 'HOST',
62 'STATE',
63 'SOFT ALERTS',
64 'HARD ALERTS',
65 'TOTAL ALERTS'
67 switch($options['report_type']) {
68 case 'hostgroups':
69 $label = _('Hostgroup');
70 array_splice($header, 1, 1, 'HOSTGROUP');
71 break;
72 case 'hosts':
73 $label = _('Host');
74 break;
75 case 'services':
76 $label = _('Service');
77 array_splice($header, 2, 0, 'SERVICE');
78 break;
79 case 'servicegroups':
80 $label = _('Servicegroup');
81 array_splice($header, 1, 1, 'SERVICEGROUP');
82 break;
84 $csv_content[] = $header;
85 foreach ($result as $host_name => $ary) {
86 $service_name = null;
87 if($options['report_type'] == 'services') {
88 list($host_name, $service_name) = explode(';', $host_name);
90 foreach($ary['host'] as $state => $host) {
91 $row = array(
92 $label,
93 $host_name,
94 $host_state_names[$state],
95 $host[0], # soft
96 $host[1], # hard
97 $host[0] + $host[1] # total
99 if($service_name) {
100 array_splice($row, 2, 0, $service_name);
103 $csv_content[] = $row;
104 foreach($ary['service'] as $state => $service) {
105 $row = array(
106 $label,
107 $host_name,
108 $service_state_names[$state],
109 $service[0], # soft
110 $service[1], # hard
111 $service[0] + $service[1] # total
113 if($service_name) {
114 array_splice($row, 2, 0, $service_name);
117 $csv_content[] = $row;
121 $out = fopen('php://output', 'w');
122 foreach ($csv_content as $line) {
123 fputcsv($out, $line);
125 fclose($out);