public function valid()
 {
     $values = $this->getSegment()->getValues();
     $job = $this->getEngine()->getJob();
     // Check contact existence
     if (isset($values['members'])) {
         foreach ($values['members'] as $hostValues) {
             if ($hostValues['value'] == "*") {
                 // Means every host
                 continue;
             }
             $c = new Criteria();
             $c->add(NagiosHostPeer::NAME, $hostValues['value']);
             $host = NagiosHostPeer::doSelectOne($c);
             if (empty($host)) {
                 $job->addNotice("The host specified by " . $hostValues['value'] . " was not found.  Setting this host group as queued.");
                 return false;
             }
             $host->clearAllReferences(true);
         }
     }
     if (isset($values['hostgroup_members'])) {
         foreach ($values['hostgroup_members'] as $hostGroupValues) {
             $c = new Criteria();
             $c->add(NagiosHostgroupPeer::NAME, $hostGroupValues['value']);
             $host = NagiosHostgroupPeer::doSelectOne($c);
             if (empty($host)) {
                 $job->addNotice("The host group specified by " . $hostValues['value'] . " was not found.  Setting this host group as queued.");
                 return false;
             }
             $host->clearAllReferences(true);
         }
     }
     return true;
 }
Exemple #2
0
 public function getByName($name)
 {
     $c = new Criteria();
     $c->add(NagiosHostPeer::NAME, $name);
     $c->setIgnoreCase(true);
     $host = NagiosHostPeer::doSelectOne($c);
     if (!$host) {
         return false;
     }
     return $host;
 }
 /**
  * Get the associated NagiosHost object
  *
  * @param      PropelPDO Optional Connection object.
  * @return     NagiosHost The associated NagiosHost object.
  * @throws     PropelException
  */
 public function getNagiosHost(PropelPDO $con = null)
 {
     if ($this->aNagiosHost === null && $this->host !== null) {
         $c = new Criteria(NagiosHostPeer::DATABASE_NAME);
         $c->add(NagiosHostPeer::ID, $this->host);
         $this->aNagiosHost = NagiosHostPeer::doSelectOne($c, $con);
         /* The following can be used additionally to
         		   guarantee the related object contains a reference
         		   to this object.  This level of coupling may, however, be
         		   undesirable since it could result in an only partially populated collection
         		   in the referenced object.
         		   $this->aNagiosHost->addNagiosEscalations($this);
         		 */
     }
     return $this->aNagiosHost;
 }
Exemple #4
0
                    $tempGroup = NagiosContactGroupPeer::retrieveByPk($_POST['host_manage']['contactgroup_add']['contactgroup_id']);
                    if ($tempGroup) {
                        $membership = new NagiosHostContactgroup();
                        $membership->setHost($_GET['id']);
                        $membership->setNagiosContactGroup($tempGroup);
                        $membership->save();
                        $success = "New Host Contact Group Link added.";
                    }
                }
            } else {
                if ($_POST['request'] == 'parent_add') {
                    // Wants to add a parent
                    $c = new Criteria();
                    $c->add(NagiosHostPeer::NAME, $_POST['parenthost']);
                    $c->setIgnoreCase(true);
                    $parentHost = NagiosHostPeer::doSelectOne($c);
                    if (!$parentHost) {
                        $error = "The host named " . $_POST['parenthost'] . " was not found.";
                    } else {
                        $tempParent = new NagiosHostParent();
                        $tempParent->setChildHost($host->getId());
                        $tempParent->setParentHost($parentHost->getId());
                        $tempParent->save();
                        $success = "Parent added";
                    }
                }
            }
        }
    }
}
if (isset($_GET['id'])) {
Exemple #5
0
 function addParentByName($name)
 {
     $parentId = $this->getParentHost();
     $c = new Criteria();
     $c->add(NagiosHostPeer::NAME, $name);
     $c->setIgnoreCase(true);
     $host = NagiosHostPeer::doSelectOne($c);
     if (!$host) {
         return false;
     }
     if (empty($parentId)) {
         $this->setParentHost($host->getId());
         return true;
     }
     // Okay, let's first see if there's a parent relationship around
     $id = $this->getId();
     if (!empty($id)) {
         $c = new Criteria();
         $c->add(NagiosHostParentPeer::CHILD_HOST_TEMPLATE, $this->getId());
         $c->add(NagiosHostParentPeer::PARENT_HOST, $host->getId());
         $relationship = NagiosHostParentPeer::doSelectOne($c);
         if ($relationship) {
             return false;
         }
     }
     // Okay, relationship doesn't exist, let's add it!
     $relationship = new NagiosHostParent();
     $relationship->setNagiosHostTemplate($this);
     $relationship->setNagiosHostRelatedByParentHost($host);
     $relationship->save();
     return true;
 }
Exemple #6
0
 foreach ($devices as $device) {
     $autodiscoveryJob->addNotice("Attempting Traceroute to " . $device->getAddress());
     $tr = Net_Traceroute::factory();
     if (!PEAR::isError($tr)) {
         $response = $tr->traceroute($device->getAddress());
         $hops = $response->getHops();
         $numOfHops = count($hops);
         $found = false;
         for ($counter = $numOfHops - 1; $counter >= 0; $counter--) {
             // Check the farthest hop first
             $c = new Criteria();
             $c1 = $c->getNewCriterion(NagiosHostPeer::ADDRESS, $hops[$counter]['ip']);
             $c2 = $c->getNewCriterion(NagiosHostPeer::ADDRESS, $hops[$counter['machine']]);
             $c1->addOr($c2);
             $c->add($c1);
             $host = NagiosHostPeer::doSelectOne($c);
             if ($host) {
                 // Found parent
                 $autodiscoveryJob->addNotice("Found parent: " . $host->getName());
                 $device->setProposedParent($host->getId());
                 $device->save();
                 $found = true;
             }
         }
         if (!$found) {
             $autodiscoveryJob->addNotice("Could not find a suitable parent.  Setting as a top-level device.");
         }
     } else {
         $autodiscoveryJob->addNotce("Failed to run Traceroute.  Not using it.");
     }
 }
Exemple #7
0
 public function valid()
 {
     $values = $this->getSegment()->getValues();
     $job = $this->getEngine()->getJob();
     if (isset($values['use'])) {
         // We need to use a template
         $job->addNotice("This Host uses a template: " . $values['use'][0]['value']);
         $template = NagiosHostTemplatePeer::getByName($values['use'][0]['value']);
         if (empty($template)) {
             if (!isset($values['name'][0]['value'])) {
                 $job->addNotice("That template is not found yet. Setting this host (" . $values['host_name'][0]['value'] . ") as queued.");
             } else {
                 $job->addNotice("That template is not found yet. Setting this host template (" . $values['name'][0]['value'] . ") as queued.");
             }
             return false;
         }
     }
     // Check time period existence
     if (isset($values['check_period'])) {
         $c = new Criteria();
         $c->add(NagiosTimeperiodPeer::NAME, $values['check_period'][0]['value']);
         $timePeriod = NagiosTimeperiodPeer::doSelectOne($c);
         if (empty($timePeriod)) {
             $job->addNotice("The time period specified by " . $values['check_period'][0]['value'] . " was not found.");
             return false;
         }
         $timePeriod->clearAllReferences(true);
     }
     if (isset($values['notification_period'])) {
         $c = new Criteria();
         $c->add(NagiosTimeperiodPeer::NAME, $values['notification_period'][0]['value']);
         $timePeriod = NagiosTimeperiodPeer::doSelectOne($c);
         if (empty($timePeriod)) {
             $job->addNotice("The time period specified by " . $values['notification_period'][0]['value'] . " was not found.");
             return false;
         }
         $timePeriod->clearAllReferences(true);
     }
     // Check command existence
     if (isset($values['check_command'])) {
         $params = explode("!", $values['check_command'][0]['value']);
         $c = new Criteria();
         $c->add(NagiosCommandPeer::NAME, $params[0]);
         $command = NagiosCommandPeer::doSelectOne($c);
         if (empty($command)) {
             $job->addNotice("The command specified by " . $params[0] . " was not found.");
             return false;
         }
         $command->clearAllReferences(true);
     }
     if (isset($values['event_handler'])) {
         $c = new Criteria();
         $c->add(NagiosCommandPeer::NAME, $values['event_handler'][0]['value']);
         $command = NagiosCommandPeer::doSelectOne($c);
         if (empty($command)) {
             $job->addNotice("The command specified by " . $values['event_handler'][0]['value'] . " was not found.");
             return false;
         }
         $command->clearAllReferences(true);
     }
     // Check contact groups
     if (isset($values['contact_groups'])) {
         foreach ($values['contact_groups'] as $contactGroupValues) {
             $c = new Criteria();
             $c->add(NagiosContactGroupPeer::NAME, $contactGroupValues['value']);
             $contactgroup = NagiosContactGroupPeer::doSelectOne($c);
             if (empty($contactgroup)) {
                 $job->addNotice("The contact group specified by " . $contactGroupValues['value'] . " was not found.");
                 return false;
             }
             $contactgroup->clearAllReferences();
         }
     }
     if (isset($values['contacts'])) {
         foreach ($values['contacts'] as $contactValues) {
             $c = new Criteria();
             $c->add(NagiosContactPeer::NAME, $contactValues['value']);
             $contactgroup = NagiosContactPeer::doSelectOne($c);
             if (empty($contactgroup)) {
                 $job->addNotice("The contact specified by " . $contactValues['value'] . " was not found.");
                 return false;
             }
             $contactgroup->clearAllReferences();
         }
     }
     // Check host groups
     if (isset($values['hostgroups'])) {
         foreach ($values['hostgroups'] as $hostGroupValues) {
             $c = new Criteria();
             $c->add(NagiosHostgroupPeer::NAME, $hostGroupValues['value']);
             $hostgroup = NagiosHostgroupPeer::doSelectOne($c);
             if (empty($hostgroup)) {
                 $job->addNotice("The host group specified by " . $hostGroupValues['value'] . " was not found.");
                 return false;
             }
             $hostgroup->clearAllReferences();
         }
     }
     // Check parents
     if (isset($values['parents'])) {
         foreach ($values['parents'] as $parentValues) {
             $c = new Criteria();
             $c->add(NagiosHostPeer::NAME, $parentValues['value']);
             $host = NagiosHostPeer::doSelectOne($c);
             if (empty($host)) {
                 $job->addNotice("The host specified by " . $parentValues['value'] . " was not found.");
                 return false;
             }
             $host->clearAllReferences();
         }
     }
     return true;
 }
 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;
 }