setStatusCode(AutodiscoveryJob::STATUS_STARTING);
$autodiscoveryJob->save();
exec("php autodiscovery/autodiscover.php " . $autodiscoveryJob->getId() . " > /dev/null", $tempOutput, $retVal);
if($retVal != 42) {
$error = "Failed to run external autodiscovery script. Return value: " . $retVal . "
Error:";
foreach($tempOutput as $output) {
$error .= $output . "
";
}
}
else {
// No need to show
//$success = "Restarted AutoDiscovery Job";
}
}
if(isset($_GET['delete'])) {
// We want to delete the job!
$autodiscoveryJob->delete();
unset($_GET['id']);
unset($autodiscoveryJob);
$success = "Removed Job and associated devices.";
}
}
if(isset($_GET['deviceId'])) {
// We want to review a specific device
$device = AutodiscoveryDevicePeer::retrieveByPK($_GET['deviceId']);
if(!$device) {
header("Location: autodiscovery.php");
exit();
}
if(isset($_GET['request']) && $_GET['request'] == 'recalc') {
// We want to recalculate template matches
$config = unserialize($autodiscoveryJob->getConfig());
$defaultTemplateId = $config->getVar("default_template");
if(!empty($defaultTemplateId)) {
$defaultTemplate = NagiosHostTemplatePeer::retrieveByPK($defaultTemplateId);
}
if(empty($defaultTemplate)) {
$defaultTemplate = null;
}
AutodiscoveryMatchMaker::match($device, $defaultTemplate);
$success = "Recalculated Matching Templates.";
}
}
if(isset($_GET['request']) && $_GET['request'] == 'status') {
// We're our AJAX client wanting status information
$result = array();
$autodiscoveryJob = AutodiscoveryJobPeer::retrieveByPK($_GET['id']);
if(!$autodiscoveryJob) {
$result['error'] = "Invalid job specified.";
print(json_encode($result));
exit();
}
// Okay, let's populate the status
$result['start_time'] = $autodiscoveryJob->getStartTime();
$result['status_code'] = $autodiscoveryJob->getStatusCode();
$result['status_text'] = $autodiscoveryJob->getStatus();
$result['status_change_time'] = $autodiscoveryJob->getStatusChangeTime();
// Build elapsed time
if(!in_array($autodiscoveryJob->getStatusCode(), array(AutoDiscoveryJob::STATUS_FAILED, AutoDiscoveryJob::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(AutodiscoveryLogEntryPeer::JOB, $_GET['id']);
$c->setLimit($_POST['rp']);
$c->setOffset(isset($_POST['page']) ? ($_POST['page'] - 1) * $_POST['rp'] : 0);
$c->addDescendingOrderByColumn(AutodiscoveryLogEntryPeer::ID);
$entries = $autodiscoveryJob->getAutodiscoveryLogEntrys($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(AutodiscoveryLogEntryPeer::JOB, $autodiscoveryJob->getId());
$results['page'] = $_POST['page'];
$results['total'] = AutodiscoveryLogEntryPeer::doCount($c);
?>
setVar("targets", $_POST['target']);
$config->setVar("nmap_binary", $_POST['nmap_binary']);
$config->setVar("traceroute_enabled", (empty($_POST['traceroute_enabled'])) ? true : true);
$config->setVar("default_template", $_POST['default_template']);
$autodiscoveryJob = new AutodiscoveryJob();
$autodiscoveryJob->setName($_POST['job_name']);
$autodiscoveryJob->setDescription($_POST['job_description']);
$autodiscoveryJob->setCmd(AutodiscoveryJob::CMD_START);
$autodiscoveryJob->setConfig(serialize($config));
$autodiscoveryJob->setStatus("Starting...");
$autodiscoveryJob->setStatusCode(AutodiscoveryJob::STATUS_STARTING);
$autodiscoveryJob->save();
// Attempt to execute the external auto-discovery script, fork it, and love it.
exec("php autodiscovery/autodiscover.php " . $autodiscoveryJob->getId() . " > /dev/null", $tempOutput, $retVal);
if($retVal != 42) {
$status_msg = "Failed to run external Autodiscovery script. Return value: " . $retVal . "
Error:";
foreach($tempOutput as $output) {
$status_msg .= $output . "
";
}
}
}
}
}
else if($_POST['request'] == "updateGeneral") {
if(trim($_POST['name']) != '' && trim($_POST['description']) != '') {
// Check to see first if a host or auto discovery device has that potential name
$c = new Criteria();
$c->add(NagiosHostPeer::NAME, trim($_POST['name']));
$c->setIgnoreCase(true);
$host = NagiosHostPeer::doSelectOne($c);
if(!$host) {
// Try a autodiscovery device?
$c = new Criteria();
$c->add(AutodiscoveryDevicePeer::NAME, trim($_POST['name']));
$c->setIgnoreCase(true);
$host = AutodiscoveryDevicePeer::doSelectOne($c);
}
if($host) {
$error = "A host already exists with that name. Must choose a unique name.";
}
else {
// Assign name and description
$device->setName(trim($_POST['name']));
$device->setDescription(trim($_POST['description']));
$device->save();
$success = "Updated discovered device's information.";
}
}
else {
$error = "Name and Description cannot be blank.";
}
}
else if($_POST['request'] == "assignTemplate") {
$hostTemplate = NagiosHostTemplatePeer::retrieveByPK($_POST['template']);
if(!$hostTemplate) {
$error = "That template no longer exists.";
}
else {
$device->setNagiosHostTemplate($hostTemplate);
$device->save();
$success = "Template assigned.";
}
}
else {
// We want to process our device list!
// First we check to see if there's any hosts now with the same name
foreach($_POST['selectedDevices'] as $deviceId) {
$device = AutodiscoveryDevicePeer::retrieveByPK($deviceId);
if(!$device) {
$error = "One of the devices provided no longer exists.";
continue;
}
$c = new Criteria();
$c->add(NagiosHostPeer::NAME, $device->getName());
$c->setIgnoreCase(true);
$host = NagiosHostPeer::doSelectOne($c);
if(!$host) {
$c = new Criteria();
$c->add(AutodiscoveryDevicePeer::NAME, $device->getName());
$c->setIgnoreCase(true);
$host = AutodiscoveryDevicePeer::doSelectOne($c);
if($host->getId() == $device->getId()) {
unset($host);
}
}
if(!empty($host)) {
$error = "A host already exists with the name of " . $device->getName() . ". Change the device's name before importing.";
}
}
if(empty($error)) {
$totalSuccess = 0;
// Okay, no errors, let's create our hosts!
foreach($_POST['selectedDevices'] as $deviceId) {
$device = AutodiscoveryDevicePeer::retrieveByPK($deviceId);
$tempHost = new NagiosHost();
$tempHost->setAddress($device->getAddress());
$tempHost->setName($device->getName());
$tempHost->setAlias($device->getDescription());
$tempHost->save();
// Now assign a template, if wanted
$template = $device->getNagiosHostTemplate();
if(!empty($template)) {
$inheritance = new NagiosHostTemplateInheritance();
$inheritance->setNagiosHost($tempHost);
$inheritance->setNagiosHostTemplateRelatedByTargetTemplate($template);
$inheritance->save();
}
// Now parent
$parent = $device->getNagiosHost();
if(!empty($parent)) {
$parentRelationship = new NagiosHostParent();
$parentRelationship->setNagiosHostRelatedByChildHost($tempHost);
$parentRelationship->setNagiosHostRelatedByParentHost($parent);
$parentRelationship->save();
}
$totalSuccess++;
$device->delete();
}
$success = $totalSuccess . " Device(s) Imported.";
}
}
}
print_header("AutoDiscovery");
if(isset($autodiscoveryJob)) {
?>
add(AutodiscoveryJobPeer::END_TIME, null);
$autodiscoveryJobs = autodiscoveryJobPeer::doSelect($c);
if($autodiscoveryJobs) {
print_window_header("Jobs In Progress", "100%");
?>
There appears to be running autodiscovery 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.
Name | Description | Start Time | Status | Actions | |
getName();?> | getDescription();?> | getStartTime();?> | getStatus();?> | View Job | Restart |
>
getName();?> on port getProtocol();?>/getPort(); ?>Product: getProduct(); ?>Version: getVersion(); ?> Extra Information: getExtraInfo(); ?> |