/**
  * 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      AutodiscoveryDeviceTemplateMatch $value A AutodiscoveryDeviceTemplateMatch object.
  * @param      string $key (optional) key to use for instance map (for performance boost if key was already calculated externally).
  */
 public static function addInstanceToPool(AutodiscoveryDeviceTemplateMatch $obj, $key = null)
 {
     if (Propel::isInstancePoolingEnabled()) {
         if ($key === null) {
             $key = (string) $obj->getId();
         }
         // if key === null
         self::$instances[$key] = $obj;
     }
 }
Ejemplo n.º 2
0
 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();
 }