/** * Adds an object to the instance pool. * * Propel keeps cached copies of objects in an instance pool when they are retrieved * from the database. In some cases -- especially when you override doSelect*() * methods in your stub classes -- you may need to explicitly add objects * to the cache in order to ensure that the same objects are always returned by doSelect*() * and retrieveByPK*() calls. * * @param AutodiscoveryDevice $value A AutodiscoveryDevice object. * @param string $key (optional) key to use for instance map (for performance boost if key was already calculated externally). */ public static function addInstanceToPool(AutodiscoveryDevice $obj, $key = null) { if (Propel::isInstancePoolingEnabled()) { if ($key === null) { $key = (string) $obj->getId(); } // if key === null self::$instances[$key] = $obj; } }
public static function match(AutodiscoveryDevice $device, NagiosHostTemplate $defaultTemplate = null) { // Delete previous matches $c = new Criteria(); $c->add(AutodiscoveryDeviceTemplateMatchPeer::DEVICE_ID, $device->getId()); AutodiscoveryDeviceTemplateMatchPeer::doDelete($c); $templates = NagiosHostTemplatePeer::doSelect(new Criteria()); $templateMatches = array(); foreach ($templates as $template) { $templateValues = $template->getValues(); $complexity = 0; $match = 0; $serviceFilters = $template->getNagiosHostTemplateAutodiscoveryServices(); $inheritedServiceFilters = $template->getInheritedNagiosAutodiscoveryServiceFilters(); $serviceFilters = array_merge($serviceFilters, $inheritedServiceFilters); if (!empty($templateValues['autodiscovery_address_filter']) && $templateValues['autodiscovery_address_filter']['value'] != '') { $complexity++; if (preg_match($templateValues['autodiscovery_address_filter']['value'], $device->getAddress())) { $match++; } } if (!empty($templateValues['autodiscovery_hostname_filter']) && $templateValues['autodiscovery_hostname_filter']['value'] != '') { $complexity++; if (preg_match($templateValues['autodiscovery_hostname_filter']['value'], $device->getHostname())) { $match++; } } if (!empty($templateValues['autodiscovery_os_family_filter']) && $templateValues['autodiscovery_os_family_filter']['value'] != '') { $complexity++; if (preg_match($templateValues['autodiscovery_os_family_filter']['value'], $device->getOsfamily())) { $match++; } } if (!empty($templateValues['autodiscovery_os_generation_filter']) && $templateValues['autodiscovery_os_generation_filter']['value'] != '') { $complexity++; if (preg_match($templateValues['autodiscovery_os_generation_filter']['value'], $device->getOsgen())) { $match++; } } if (!empty($templateValues['autodiscovery_os_vendor_filter']) && $templateValues['autodiscovery_os_vendor_filter']['value'] != '') { $complexity++; if (preg_match($templateValues['autodiscovery_os_vendor_filter']['value'], $device->getOsvendor())) { $match++; } } // Checked bases, let's now check service filters $complexity += count($serviceFilters); foreach ($serviceFilters as $filter) { foreach ($device->getAutodiscoveryDeviceServices() as $service) { if ($filter->getPort() == $service->getPort() && $filter->getProtocol() == $service->getProtocol()) { // Okay, we're ALMOST found...let's see if we have any other additional filters. $tempMatch = true; if ($filter->getName() != '') { if (!preg_match($filter->getName(), $service->getName())) { $tempMatch = false; } } if ($filter->getProduct() != '') { if (!preg_match($filter->getProduct(), $service->getProduct())) { $tempMatch = false; } } if ($filter->getVersion() != '') { if (!preg_match($filter->getVersion(), $service->getVersion())) { $tempMatch = false; } } if ($filter->getExtraInformation() != '') { if (!preg_match($filter->getExtraInformation(), $service->getExtraInformation())) { $tempMatch = false; } } if ($tempMatch) { $match++; } } } } // Okay, we got everything, let's determine the percentage. if ($complexity == 0) { // Blank template, no auto-discovery features used. $percentage = 0; continue; } else { $percentage = (int) ((double) $match / (double) $complexity * 100); } if ($percentage == 0) { continue; } // Store the template into the array $templateMatches[$percentage][$complexity][] = $template; } // Okay, let's now create the matches $percentages = array_keys($templateMatches); $assigned = false; for ($percentageCounter = 0; $percentageCounter < count($percentages); $percentageCounter++) { $complexities = array_keys($templateMatches[$percentages[$percentageCounter]]); $complexities = array_reverse($complexities); for ($complexityCount = 0; $complexityCount < count($complexities); $complexityCount++) { foreach ($templateMatches[$percentages[$percentageCounter]][$complexities[$complexityCount]] as $template) { $match = new AutodiscoveryDeviceTemplateMatch(); $match->setAutodiscoveryDevice($device); $match->setNagiosHostTemplate($template); $match->setPercent($percentages[$percentageCounter]); $match->setComplexity($complexities[$complexityCount]); $match->save(); // Add the highest match as the template to assign if (!$assigned) { $assigned = true; $device->setNagiosHostTemplate($template); } } } } // If Not assigned, assign default template if (!$assigned && !empty($defaultTemplate)) { $device->setNagiosHostTemplate($defaultTemplate); } $device->save(); }
/** * Declares an association between this object and a AutodiscoveryDevice object. * * @param AutodiscoveryDevice $v * @return AutodiscoveryDeviceTemplateMatch The current object (for fluent API support) * @throws PropelException */ public function setAutodiscoveryDevice(AutodiscoveryDevice $v = null) { if ($v === null) { $this->setDeviceId(NULL); } else { $this->setDeviceId($v->getId()); } $this->aAutodiscoveryDevice = $v; // Add binding for other direction of this n:n relationship. // If this object has already been added to the AutodiscoveryDevice object, it will not be re-added. if ($v !== null) { $v->addAutodiscoveryDeviceTemplateMatch($this); } return $this; }
public function discover() { $job = $this->getJob(); $job->addNotice("Starting discovery..."); $config = unserialize($job->getConfig()); $targets = $config->getVar("targets"); // Each target is valid // We have a list of targets $nmap_flags = "--max-rtt-timeout 100 --max-retries 0 -sS -O -A -v -oX " . $this->xmlFile; $exec_line = "sudo " . $config->getVar("nmap_binary") . " " . $nmap_flags . " "; foreach ($targets as $target) { $exec_line .= $target . " "; } $job->addNotice("Executing nmap via: " . $exec_line . "\n"); $completed = false; // Open the process $handle = popen($exec_line, 'r'); // Only need it for reading while (!feof($handle)) { // nmap is running! $line = fgets($handle); if (strlen(trim($line))) { $job->addNotice("NMAP: " . $line); } if (strpos($line, "Nmap done") === 0 || strpos($line, "Nmap finished") === 0) { // Nmap is finished $completed = true; } } if (!$completed) { $job->addError("Nmap failed to complete successfully. Could be permissions issue (check sudo access). Aborting discovery."); return false; } $job->addNotice("Nmap finished discovery. Now reading in result XML."); $nmapXML = simplexml_load_file($this->xmlFile); // Read in hosts $job->addNotice("Number of hosts: " . count($nmapXML->host)); foreach ($nmapXML->host as $host) { $job->addNotice('Found ' . $host->address[0]['addr'] . ':' . $host->hostnames[0]->hostname[0]['name'] . '.'); $c = new Criteria(); $c1 = $c->getNewCriterion(NagiosHostPeer::ADDRESS, $host->address[0]['addr']); $c2 = $c->getNewCriterion(NagiosHostPeer::ADDRESS, $host->hostnames[0]->hostname[0]['name']); $c1->addOr($c2); $c->add($c1); $tempHost = NagiosHostPeer::doSelectOne($c); if ($tempHost) { $job->addNotice("Device appears to already be in configuration under hostname: " . $host->getName() . ". Skipping."); continue; } $device = new AutodiscoveryDevice(); $device->setAutodiscoveryJob($job); $device->setAddress($host->address[0]['addr']); if (empty($host->hostnames[0]->hostname[0]['name'])) { $device->setName($host->address[0]['addr']); $device->setDescription($host->address[0]['addr']); } else { $device->setName($host->hostnames[0]->hostname[0]['name']); $device->setDescription($host->hostnames[0]->hostname[0]['name']); } $device->setHostname($host->hostnames[0]->hostname[0]['name']); // Right now, if it exists, we pick the top-level matching os string (since it's the highest scored) if (!empty($host->os[0]->osclass[0]['osfamily'])) { // We found os family data $device->setOsfamily($host->os[0]->osclass[0]['osfamily']); } if (!empty($host->os[0]->osclass[0]['osgen'])) { // We found os family data $device->setOsgen($host->os[0]->osclass[0]['osgen']); } if (!empty($host->os[0]->osclass[0]['vendor'])) { // We found os family data $device->setOsvendor($host->os[0]->osclass[0]['vendor']); } $device->save(); // Add services $job->addNotice("Number of ports for this host: " . count($host->ports[0]->port)); foreach ($host->ports[0]->port as $port) { // $port signifies as port / possible service, we'll grab that $service = new AutodiscoveryDeviceService(); $service->setProtocol($port['protocol']); $service->setPort($port['portid']); if (!empty($port->service[0]['name'])) { $service->setName($port->service[0]['name']); } if (!empty($port->service[0]['product'])) { $service->setProduct($port->service[0]['product']); } if (!empty($port->service[0]['version'])) { $service->setVersion($port->service[0]['version']); } if (!empty($port->service[0]['extrainfo'])) { $service->setExtrainfo($port->service[0]['extrainfo']); } $service->setAutodiscoveryDevice($device); $service->save(); } } $job->addNotice("Added devices and services."); return true; }