Example #1
0
 /**
  * Update a switches ports using SNMP polling
  *
  * There is an optional ``$results`` array which can be passed by reference. If
  * so, it will be indexed by the SNMP port index (or a decresing nagative index
  * beginning -1 if the port only exists in the database). The contents of this
  * associative array is:
  *
  *     "port"   => \Entities\SwitchPort object
  *     "bullet" =>
  *         - false for existing ports
  *         - "new" for newly found ports
  *         - "db" for ports that exist in the database only
  *
  * **Note:** It is assumed that the Doctrine2 Entity Manager is available in the
  * Zend registry as ``d2em`` in this function.
  *
  * @throws \OSS_SNMP\Exception
  *
  * @param \OSS_SNMP\SNMP $host An instance of \OSS_SNMP\SNMP for this switch
  * @param \OSS_Logger $logger An instance of the logger or false
  * @param array Call by reference to an array in which to store results as outlined above
  * @return \Entities\Switcher For fluent interfaces
  */
 public function snmpPollSwitchPorts($host, $logger = false, &$result = false)
 {
     // clone the ports currently known to this switch as we'll be playing with this array
     $existingPorts = clone $this->getPorts();
     // iterate over all the ports discovered on the switch:
     foreach ($host->useIface()->indexes() as $index) {
         // we're only interested in Ethernet ports here (right?)
         if ($host->useIface()->types()[$index] != \OSS_SNMP\MIBS\Iface::IF_TYPE_ETHERNETCSMACD) {
             continue;
         }
         // find the matching switchport that may already be in the database (or create a new one)
         $switchport = false;
         foreach ($existingPorts as $ix => $ep) {
             if ($ep->getIfIndex() == $index) {
                 $switchport = $ep;
                 if (is_array($result)) {
                     $result[$index] = ["port" => $switchport, 'bullet' => false];
                 }
                 if ($logger) {
                     $logger->info(" - {$this->getName()} - found pre-existing port for ifIndex {$index}");
                 }
                 // remove this from the array so later we'll know what ports exist only in the database
                 unset($existingPorts[$ix]);
                 break;
             }
         }
         $new = false;
         if (!$switchport) {
             // no existing port in database so we have found a new port
             $switchport = new \Entities\SwitchPort();
             $switchport->setSwitcher($this);
             $this->addPort($switchport);
             $switchport->setType(\Entities\SwitchPort::TYPE_UNSET);
             $switchport->setIfIndex($index);
             $switchport->setActive(true);
             \Zend_Registry::get('d2em')['default']->persist($switchport);
             if (is_array($result)) {
                 $result[$index] = ["port" => $switchport, 'bullet' => "new"];
             }
             $new = true;
             if ($logger) {
                 $logger->info("Found new port for {$this->getName()} with index {$index}");
             }
         }
         // update / set port details from SNMP
         $switchport->snmpUpdate($host, $logger);
     }
     if (count($existingPorts)) {
         $i = -1;
         foreach ($existingPorts as $ep) {
             if (is_array($result)) {
                 $result[$i--] = ["port" => $ep, 'bullet' => "db"];
             }
             if ($logger) {
                 $logger->warn("{$this->getName()} - port found in database with no matching port on the switch: " . " [{$ep->getId()}] {$ep->getName()}");
             }
         }
     }
     return $this;
 }
 public function addAction()
 {
     $this->addMessage("<h4>Use of this method is discouraged!</h4>\n\n                Switch ports are best added using the\n                <a href=\"https://github.com/inex/IXP-Manager/wiki/Updating-Switches-and-Ports-via-SNMP\">CLI scripts</a>\n                or the <em>View / Edit Ports (with SNMP poll)</em> option from the <a href=\"" . OSS_Utils::genUrl('switch', 'list') . "\">switch list page</a>. See <a href=\"" . "https://github.com/inex/IXP-Manager/wiki/Switch-and-Switch-Port-Management\">the documentation\n                for more information</a>.", OSS_Message::INFO, OSS_Message::TYPE_BLOCK);
     $this->view->form = $form = new IXP_Form_Switch_AddPorts();
     if ($this->getRequest()->isPost() && $form->isValid($_POST)) {
         if (!($switch = $this->getD2EM()->getRepository('\\Entities\\Switcher')->find($form->getValue('switchid')))) {
             throw new IXP_Exception('Unknown switch in request');
         }
         for ($i = 0; $i < $form->getValue('numports'); $i++) {
             $sp = new \Entities\SwitchPort();
             $sp->setSwitcher($switch);
             $sp->setType(intval($_POST['np_type' . $i]));
             $sp->setName(trim(stripslashes($_POST['np_name' . $i])));
             $sp->setActive(true);
             $this->getD2EM()->persist($sp);
         }
         $this->getD2EM()->flush();
         $msg = "{$form->getValue('numports')} new ports created for switch {$switch->getName()}.";
         $this->getLogger()->info($msg);
         $this->addMessage($msg, OSS_Message::SUCCESS);
         $this->redirect('switch-port/list/switch/' . $switch->getId());
     }
     $this->render('add-ports');
 }