Repository navigation
Expand file tree
/
Copy pathimport_log.php
More file actions
218 lines (194 loc) · 8.1 KB
/
Copy pathimport_log.php
File metadata and controls
218 lines (194 loc) · 8.1 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 The Cacti Group |
| |
| 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. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDTool-based Graphing Solution |
+-------------------------------------------------------------------------+
| This code is designed, written, and maintained by the Cacti Group. See |
| about.php and/or the AUTHORS file for specific developer information. |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/
require(__DIR__ . '/../../include/cli_check.php');
require(__DIR__ . '/includes/functions.php');
// process calling arguments
$parms = $_SERVER['argv'];
array_shift($parms);
$logfile = false;
$logid = false;
$reprocess = false;
$usecacti = false;
$table_mode = null;
$table_names = '';
$description = 'Imported using import_log.php';
$length = -1;
$delete_after = false;
if (cacti_sizeof($parms)) {
$shortopts = 'VvHh';
$longopts = [
'logfile:',
'logid:',
'reprocess:',
'usecacti',
'table-mode:',
'table-names:',
'description:',
'length:',
'delete-after',
'version',
'help'
];
$options = getopt($shortopts, $longopts);
foreach ($options as $arg => $value) {
// getopt() returns an array instead of a scalar when an option is repeated more than
// once on the command line - none of the options below are meant to be repeatable, so
// normalize to the last occurrence (a defensive fallback; a real invocation will only
// ever pass each of these once).
if (is_array($value)) {
$value = end($value);
}
switch($arg) {
case 'logfile':
$logfile = $value;
break;
case 'logid':
$logid = $value;
break;
case 'reprocess':
$reprocess = $value;
break;
case 'usecacti':
$usecacti = true;
break;
case 'table-mode':
if (in_array($value, array('cacti', 'reference', 'all'), true)) {
$table_mode = $value;
} else {
print "ERROR: Invalid --table-mode value '$value', must be cacti, reference, or all" . PHP_EOL . PHP_EOL;
display_help();
exit(1);
}
break;
case 'table-names':
$table_names = $value;
break;
case 'description':
$description = $value;
break;
case 'length':
$length = (int) $value;
break;
case 'delete-after':
$delete_after = true;
break;
case 'version':
case 'V':
case 'v':
display_version();
exit(0);
case 'help':
case 'H':
case 'h':
display_help();
exit(0);
default:
print "ERROR: Invalid Argument: ($arg)" . PHP_EOL . PHP_EOL;
display_help();
exit(1);
}
}
}
if ($logid !== false && !preg_match('/^[1-9][0-9]*$/', trim((string) $logid))) {
print __('ERROR: Invalid --logid value \'%s\', must be a positive integer', $logid, 'slowlog') . PHP_EOL . PHP_EOL;
display_help();
exit(1);
}
if ($logfile !== false) {
if ($table_mode === 'reference' && trim($table_names) === '') {
print 'ERROR: --table-mode=reference requires --table-names="..." for a --logfile import' . PHP_EOL . PHP_EOL;
display_help();
exit(1);
}
// --logid alongside --logfile means the caller (the web upload handler) already
// inserted the parent record so its status is visible immediately - reuse it
// instead of creating a second one.
import_logfile($logfile, $description, $length, $table_names, $usecacti, false, $table_mode, $logid !== false ? (int) $logid : null);
if ($delete_after) {
@unlink($logfile);
}
} elseif ($logid !== false) {
import_post_process((int) $logid, $table_names, $usecacti, $table_mode);
} elseif ($reprocess !== false) {
if (strtolower($reprocess) == 'all') {
slowlog_reprocess_all($table_names, $usecacti, $table_mode);
} elseif (preg_match('/^[1-9][0-9]*$/', trim((string) $reprocess))) {
slowlog_reprocess((int) $reprocess, $table_names, $usecacti, $table_mode);
} else {
print "ERROR: Invalid --reprocess value '$reprocess', must be a positive integer logid or 'all'" . PHP_EOL . PHP_EOL;
display_help();
exit(1);
}
}
/**
* display_version - displays version information
*
* Prints this script's name/Cacti version/copyright. Called from the
* CLI argument parser for the '--version'/'-V'/'-v' flag, and from
* display_help() to prefix the usage text.
*
* @return void
*/
function display_version(): void {
$version = get_cacti_cli_version();
print "Cacti Import Slowlog, Version $version, " . COPYRIGHT_YEARS . PHP_EOL;
}
/**
* Prints this script's version banner followed by its command-line
* usage/argument summary (import modes, table-mode options, reprocess,
* description, length truncation, and post-import cleanup). Called from
* the CLI argument parser for the '--help'/'-H'/'-h' flag, and whenever
* an invalid argument is supplied.
*
* @return void
*/
function display_help(): void {
display_version();
print PHP_EOL . 'usage: import_log.php [ --usecacti | --table-mode=cacti|reference|all ] --logid=N | --logfile=S | --reprocess=N|all' . PHP_EOL . PHP_EOL;
print 'Cacti utility for auditing the MySQL/MariaDB slow log file.' . PHP_EOL;
print 'Options:' . PHP_EOL;
print ' --usecacti - The logid when performing batch operations' . PHP_EOL;
print ' --logid=N - The logid when performing batch operations' . PHP_EOL;
print ' --logfile=S - The logfile assuming the current Cacti database' . PHP_EOL;
print ' --reprocess=N|all - Re-run method/table/timeout classification for an existing' . PHP_EOL;
print ' logid (or every logid), e.g. after new methods/tables are' . PHP_EOL;
print ' added. Does not need the original logfile.' . PHP_EOL;
print ' --table-mode=cacti|reference|all - How to distinguish Cacti tables from other' . PHP_EOL;
print ' tables. cacti: use this Cacti DB (same as --usecacti);' . PHP_EOL;
print ' reference: compare against the table list saved with the' . PHP_EOL;
print ' import; all: detect everything, no OTHER TABLES grouping' . PHP_EOL;
print ' (default).' . PHP_EOL;
print ' --table-names="t1 t2" - Space-separated reference table list for a --logfile' . PHP_EOL;
print ' import; required with --table-mode=reference. Saved with' . PHP_EOL;
print ' the import so --logid/--reprocess can reuse it later.' . PHP_EOL;
print ' --logid=N together with --logfile=S - Reuse an already-created parent record' . PHP_EOL;
print ' (e.g. one inserted up front by a caller) instead of' . PHP_EOL;
print ' creating a new one.' . PHP_EOL;
print ' --description=S - LogFile description to save with a --logfile import' . PHP_EOL;
print ' (default: "Imported using import_log.php").' . PHP_EOL;
print ' --length=N - Truncate each imported query to N characters, -1 for no limit' . PHP_EOL;
print ' (default: -1). Only used with --logfile.' . PHP_EOL;
print ' --delete-after - Delete the --logfile after a successful import (used by the' . PHP_EOL;
print ' web upload handler to clean up its staged copy).' . PHP_EOL . PHP_EOL;
}