-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.php
executable file
·426 lines (383 loc) · 12.7 KB
/
import.php
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
<?php
/*
Lilac - A Nagios Configuration Tool
Copyright (C) 2007 Taylor Dondich
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.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*
Lilac Import Page, Displays Menu, and Statistics
*/
include_once('includes/config.inc');
include_once('importer/classes.inc.php');
include_once('ImportJob.php');
include_once('ImportLogEntry.php');
// Better to load our engines!
$availableEngines = ImportEngine::getAvailableEngines();
if(isset($_GET['action']) && $_GET['action'] == "engineConfig") {
// Need to send over engine configuration
$engineClass = $_GET['className'];
$engine = new $engineClass(null); // Wonky, I know.
$engine->renderConfig();
die();
}
if(isset($_GET['id'])) {
$importJob = ImportJobPeer::retrieveByPK($_GET['id']);
if(!$importJob) {
unset($importJob);
}
if(isset($_GET['action']) && $_GET['action'] == "restart") {
$importJob->setStatusCode(ImportJob::STATUS_STARTING);
$importJob->setStartTime(time());
$importJob->setStatus("Starting...");
$importJob->save();
exec("php importer/import.php " . $importJob->getId() . " > /dev/null", $tempOutput, $retVal);
if($retVal != 42) {
$error = "Failed to run external importer script. Return value: " . $retVal . "<br /> Error:";
foreach($tempOutput as $output) {
$error .= $output . "<br />";
}
}
else {
// No need to show
//$success = "Restarted Import Job";
}
}
if(isset($_GET['delete'])) {
// We want to delete the job!
$importJob->delete();
unset($_GET['id']);
unset($importJob);
$success = "Removed Job";
}
}
if(isset($_GET['request']) && $_GET['request'] == 'status') {
// We're our AJAX client wanting status information
$result = array();
$importJob = ImportJobPeer::retrieveByPK($_GET['id']);
if(!$importJob) {
$result['error'] = "Invalid job specified.";
print(json_encode($result));
exit();
}
// Okay, let's populate the status
$result['start_time'] = $importJob->getStartTime();
$result['status_code'] = $importJob->getStatusCode();
$result['status_text'] = $importJob->getStatus();
$result['status_change_time'] = $importJob->getStatusChangeTime();
// Build elapsed time
if(!in_array($importJob->getStatusCode(), array(ImportJob::STATUS_FAILED, ImportJob::STATUS_FINISHED))) {
$target = time();
}
else {
$target = strtotime($result['status_change_time']);
}
$start = strtotime($result['start_time']);
$total = $target - $start;
$hours = (int)($total / 3600);
$total = $total % 3600;
$minutes = (int)($total / 60);
$seconds = $total % 60;
$result['elapsed_time'] = $hours . " Hours " . $minutes . " Minutes " . $seconds . " Seconds";
print(json_encode($result));
exit();
}
if(isset($_GET['request']) && $_GET['request'] == 'fetch') {
// We're our AJAX client wanting to get new log data
$result = array();
$c = new Criteria();
$c->add(ImportLogEntryPeer::JOB, $_GET['id']);
$c->setLimit($_POST['rp']);
$c->setOffset(isset($_POST['page']) ? ($_POST['page'] - 1) * $_POST['rp'] : 0);
$c->addDescendingOrderByColumn(ImportLogEntryPeer::ID);
$entries = $importJob->getImportLogEntrys($c);
foreach($entries as $entry) {
$results['rows'][] = array('id' => $entry->getId(), 'cell' => array( $entry->getTime(),
$entry->getReadableType($entry->getType()),
$entry->getText()));
}
$c = new Criteria();
$c->add(ImportLogEntryPeer::JOB, $importJob->getId());
$results['page'] = $_POST['page'];
$results['total'] = ImportLogEntryPeer::doCount($c);
?>
<?php
print(json_encode($results));
exit();
}
if(isset($_POST['request'])) {
if(!strlen(trim($_POST['job_name']))) {
$error = "Job name must be provided.";
}
else {
// Instantiate an engine
$engineClass = $_POST['job_engine'];
$engine = new $engineClass(null); // Wonky, I know.
$error = $engine->validateConfig();
}
if(empty($error)) {
// All is good. let's create our job.
$config = new ImportConfig($engineClass);
$engine->buildConfig($config);
$importJob = new ImportJob();
$importJob->setName($_POST['job_name']);
$importJob->setDescription($_POST['job_description']);
$importJob->setCmd(ImportJob::CMD_START);
$importJob->setConfig(serialize($config));
$importJob->setStartTime(time());
$importJob->setStatus("Starting...");
$importJob->setStatusCode(ImportJob::STATUS_STARTING);
$importJob->save();
// Attempt to execute the external importer script, fork it, and love it.
exec("php importer/import.php " . $importJob->getId() . " > /dev/null", $tempOutput, $retVal);
if($retVal != 42) {
$error = "Failed to run external importer script. Return value: " . $retVal . "<br /> Error:";
foreach($tempOutput as $output) {
$error .= $output . "<br />";
}
}
}
}
print_header("Importer");
if(isset($importJob)) {
?>
<script type="text/javascript">
$(document).ready(function() {
$("#joblog").flexigrid({
url: 'import.php?id=<?php echo $importJob->getId();?>&request=fetch',
dataType: 'json', // type of data loaded
errormsg: 'There was a problem retrieving the error log. Refresh and try again',
colModel: [
{display: 'Time', name: 'name', width: 100, sortable: true, align: 'left'},
{display: 'Type', name: 'type', width: 100, sortable: true, align: 'left'},
{display: 'Text', name: 'text', width: 1200, sortable: true, align: 'left'}
],
resizable: false, //resizable table
sortname: "time",
sortorder: 'asc',
usepager: true,
procmsg: 'Grabbing Log Entries, please wait ...',
title: false,
showToggleBtn: false, //show or hide column toggle popup
useRp: true,
rp: 50,
height: 600
});
<?php
if(!in_array($importJob->getStatusCode(), array(ImportJob::STATUS_FINISHED, ImportJob::STATUS_FAILED))) {
// Add check timer
?>
var timer = 0;
$(document).everyTime(2000, "status", function() {
// Call ajax
$.getJSON("import.php?id=<?php echo $importJob->getId();?>&request=status&tok=" + Math.random() , function(data) {
$("#jobstatus").html(data.status_text);
$("#elapsedtime").html(data.elapsed_time);
if(data.status_code == <?php echo ImportJob::STATUS_FINISHED;?> || data.status_code == <?php echo ImportJob::STATUS_FAILED;?>) {
if(data.status_code == <?php echo ImportJob::STATUS_FINISHED;?>) {
$("#completemsg").show("slow").fadeIn("slow");
}
$(document).stopTime("status");
}
});
}, 0, true);
<?php
}
?>
});
</script>
<?php
}
?>
<style type="text/css">
fieldset {
border:1px solid #CCCCCC;
margin:1em 0pt;
padding:1em;
}
legend {
font-weight:bold;
}
label {
display:block;
}
.checks label {
width:15em;
display: inline;
float: none;
}
.checks p {
padding: 2px;
}
}
</style>
<?php
if(isset($status_msg)) {
?>
<div align="center" class="statusmsg"><?php echo $status_msg;?></div><br />
<?php
}
if(!isset($importJob)) {
$c = new Criteria();
$importJobs = ImportJobPeer::doSelect($c);
if($importJobs) {
print_window_header("Defined Import Jobs", "100%");
?>
There appears to be existing import jobs. There should only be one running. If there are multiple showing as running, you should cancel them or purge them. Click on a job to
view it's progress and it's log.
<table class="jobs">
<tr>
<td><strong>Name</strong></td>
<td><strong>Description</strong></td>
<td><strong>Start Time</strong></td>
<td><strong>Status</strong></td>
<td colspan="2"><strong>Actions</strong></td>
</tr>
<?php
foreach($importJobs as $job) {
?>
<tr>
<td><?php echo $job->getName();?></td>
<td><?php echo $job->getDescription();?></td>
<td><?php echo $job->getStartTime();?></td>
<td><?php echo $job->getStatus();?></td>
<td><a href="import.php?id=<?php echo $job->getId();?>">View Job</a></td>
<td><a href="import.php?id=<?php echo $job->getId();?>&action=restart">Restart</a></td>
</tr>
<?php
}
?>
</table>
<?php
print_window_footer();
}
print_window_header("Create New Import Job", "100%", "center");
?>
<script type="text/javascript">
$(function() {
$("#job_engine_select").attr("value", ""); // Force the user to make a choice.
$("#job_engine_select").change(function() {
if(this.value == "") {
$("#engine_config").html("Choose an Engine to use for your Import Job from Above.");
$("#import_submit").css("display", "none");
}
else {
$("#engine_config").html("Loading Engine Configuration...");
$("#engine_config").load("import.php?action=engineConfig&className=" + this.value, null, function() {
$("#import_submit").css("display", "inline");
});
}
});
});
</script>
To begin an import of existing configuration files, a Import Job must be defined. Configure your import job below. Once created, your import job will begin in the background.
You will be able to check on the status of your import and view it's log as it continues running. You are advised to NOT edit anything in Lilac while your import is
running.
<br />
<form name="import_job" method="post" action="import.php">
<input type="hidden" name="request" value="import" />
<p>
<fieldset>
<legend>Job Definition</legend>
<label for="job_name">Job Name</label>
<input id="job_name" name="job_name" type="text" size="100" maxlength="255" />
<label for="job_description">Job Description</label>
<textarea id="job_description" name="job_description" name="job_description" rows="5" cols="80" /></textarea>
<label for="job_engine">Import Engine To Use</label>
<select id="job_engine_select" name="job_engine">
<option value="">Select An Engine To Use </option>
<?php
foreach($availableEngines as $engine) {
?>
<option value="<?php echo $engine['class'];?>"><?php echo $engine['name'] . " - " . $engine['description'];?></option>
<?php
}
?>
</select>
</fieldset>
</p>
<div id="engine_config">
Choose an Engine to use for your Import Job from Above.
</div>
<input id="import_submit" style="display: none;" type="submit" value="Begin Import" />
<?php
print_window_footer();
}
else {
?>
<?php
$stats = $importJob->getStats();
if($stats) {
$stats = unserialize($stats);
}
print_window_header("Job Details", "100%");
?>
<strong>Job Name:</strong> <?php echo $importJob->getName();?><br />
<strong>Job Id:</strong> <?php echo $importJob->getId();?><br />
<?php echo $importJob->getDescription();?>
<br />
<br />
<strong>Start Time:</strong> <?php echo $importJob->getStartTime();?><br />
<br />
<?php
if(!in_array($importJob->getStatusCode(), array(ImportJob::STATUS_FAILED, ImportJob::STATUS_FINISHED) )) {
?>
<strong>Elapsed Time:</strong> <span id="elapsedtime">Unknown</span>
<?php
}
else {
if($importJob->getStatusCode() == ImportJob::STATUS_FAILED) {
?>
<strong>Time of Failure:</strong> <?php echo $importJob->getStatusChangeTime();?>
<?php
}
else {
?>
<strong>Time When Completed:</strong> <?php echo $importJob->getStatusChangeTime();?>
<?php
}
}
?>
<br />
<strong>Current Status:</strong> <span id="jobstatus"><?php echo $importJob->getStatus();?></span><br />
<br />
<strong>Job Supplemental:</strong>
<ul>
<?php
$config = unserialize($importJob->getConfig());
$engineClass = $config->getEngineClass();
$engine = new $engineClass($importJob);
$engine->showJobSupplemental();
?>
<div id="completemsg" class="roundedcorner_success_box" <?php if($importJob->getStatusCode() != ImportJob::STATUS_FINISHED ) { ?>style="display: none;"<?php } ?>>
<div class="roundedcorner_success_top"><div></div></div>
<div class="roundedcorner_success_content">
Import Job Complete. Content Imported Successfully.
</div>
<div class="roundedcorner_success_bottom"><div></div></div>
</div>
<a href="import.php?id=<?php echo $importJob->getId();?>&action=restart">Restart Job</a> | <a href="import.php?id=<?php echo $importJob->getId();?>&delete=1" onclick="javascript:confirmDelete();">Remove Job</a> | <a href="import.php">Return To Import Menu</a>
<?php
print_window_footer();
print_window_header("Job Log");
?>
<div id="joblog">
</div>
<?php
print_window_footer();
}
?>
<br />
<br />
<?php
print_footer();
?>