This repository was archived by the owner on Feb 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexport.php
More file actions
184 lines (153 loc) · 5.67 KB
/
export.php
File metadata and controls
184 lines (153 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2012-2013 Super-Visions BVBA |
| |
| 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; either version 2 |
| of the License, or (at your option) any later version. |
| |
| 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. |
+-------------------------------------------------------------------------+
*/
function exportcsv_poller_output($rrd_update_array) {
// create temporary file
global $exportcsv_file;
if(!isset($exportcsv_file)){
$exportcsv_file = tempnam(sys_get_temp_dir(), 'exportcsv-');
chmod($exportcsv_file, 0644);
}
$data_info_sql = "SELECT
dl.snmp_query_id,
dl.snmp_index,
dl.host_id,
ht.name AS host_template,
h.description,
dt.name AS data_template,
dtd.rrd_step AS step
FROM data_local dl
JOIN `host` h
ON dl.host_id=h.id
JOIN host_template ht
ON h.host_template_id=ht.id
JOIN data_template dt
ON dl.data_template_id = dt.id
JOIN data_template_data dtd
ON dl.id=dtd.local_data_id
WHERE dl.id = %d;";
$suggested_values_sql = "SELECT
sqgrs.text
FROM snmp_query_graph_rrd_sv sqgrs
JOIN data_input_data did
ON (did.value = sqgrs.snmp_query_graph_id)
JOIN data_input_fields dif
ON (dif.id = did.data_input_field_id AND dif.type_code = 'output_type')
JOIN data_template_data dtd
ON (dtd.id = did.data_template_data_id)
WHERE
field_name = 'exportcsv_title'
AND dtd.local_data_id = %d
ORDER BY sqgrs.sequence ASC;";
// open csv file
$fp = fopen($exportcsv_file, 'a');
foreach ($rrd_update_array as $rrd_array) {
// get data info
$data_info = db_fetch_row(sprintf($data_info_sql, $rrd_array['local_data_id']));
// create title
$title = $data_info["data_template"];
if($data_info['snmp_query_id'] != 0){
// search for substitution based on suggested value
foreach(db_fetch_assoc(sprintf($suggested_values_sql, $rrd_array['local_data_id'])) as $suggested_value){
$title = substitute_snmp_query_data($suggested_value['text'], $data_info['host_id'], $data_info['snmp_query_id'], $data_info['snmp_index']);
if(!substr_count($title, "|query")) break;
}
// fallback to "data_template - snmp_index"
if( substr_count($title, "|query") || $title == $data_info["data_template"] ) $title = $data_info["data_template"].' - '.$data_info['snmp_index'];
}
// add items to csv
foreach ($rrd_array['times'] as $curtime => $aval) {
foreach($aval as $key => $val) {
fputcsv($fp, array(
$curtime,
$data_info['step'],
$data_info['description'],
$title,
$key,
$val,
), ';' );
}
}
}
// close file
fclose($fp);
return $rrd_update_array;
}
function exportcsv_poller_bottom() {
global $exportcsv_file;
if(!isset($exportcsv_file)) return;
#$poller_interval = (int) read_config_option('exportcsv_poller_interval');
#$poller_runs = (int) read_config_option('exportcsv_poller_runs');
$now = strftime("%Y%m%d%H%M",time());
$export_config_sql = "SELECT * FROM plugin_exportcsv_config WHERE enabled = 'on';";
$export_config = db_fetch_assoc($export_config_sql);
foreach($export_config as $export){
cacti_log('Starting export '.$export['name'].' using method '.$export['method'], false, 'EXPORTCSV');
$success = false;
switch($export['method']){
case 'cp':
// move file to new destination
$success = copy($exportcsv_file, $export['path'].DIRECTORY_SEPARATOR.$export['prefix'].$now.'.csv');
break;
case 'php-scp':
if (!extension_loaded('ssh2')){
cacti_log('ERROR: required ssh2 extention is not loaded.', false, 'EXPORTCSV');
continue;
}
// connect
$session = ssh2_connect($export['host'], $export['port']);
if(!$session){
cacti_log(sprintf('ERROR: Could not make ssh connection to host %s at port %d.', $export['host'], $export['port']), false, 'EXPORTCSV');
break;
}
// authenticate
if(!ssh2_auth_agent($session, $export['user'])){
cacti_log(sprintf('ERROR: Could not authenticate as user %s.', $export['user']), false, 'EXPORTCSV');
break;
}
// send file
$success = ssh2_scp($session, $exportcsv_file, $export['path'].DIRECTORY_SEPARATOR.$export['prefix'].$now.'.csv');
break;
case 'cmd-scp':
// prepare options
$options = '';
if($export['port'] != 22) $options .= sprintf('-P %d ', $export['port']);
// prepare full scp command
$command = sprintf(EXPORTCSV_SCP_COMMAND,
$options.$exportcsv_file,
escapeshellarg($export['user']),
escapeshellarg($export['host']),
escapeshellarg($export['path'].DIRECTORY_SEPARATOR.$export['prefix'].$now.'.csv')
);
// execute command
$success = system($command) !== false;
break;
case 'php-sftp':
// TODO
break;
}
// TODO: debug output on succes/failure
if(!$success){
cacti_log('ERROR: Problem while exporting '.$export['name'], false, 'EXPORTCSV');
}else{
cacti_log('Export '.$export['name'].' completed.', false, 'EXPORTCSV');
}
}
if(!@unlink($exportcsv_file)){
cacti_log('ERROR: Could not remove temporary file '.$exportcsv_file, false, 'EXPORTCSV');
}
}
?>