Ejemplo n.º 1
0
 /**
  * Add new switch by polling it via SNMP
  */
 public function addBySnmpAction()
 {
     $this->view->sw = $sw = new \Entities\Switcher();
     $this->view->f = $f = new IXP_Form_Switch_AddBySNMP();
     if ($this->getRequest()->isPost() && $f->isValid($_POST)) {
         do {
             // ensure provided name and hostname are not in use
             if ($this->getD2R('\\Entities\\Switcher')->findOneBy(['name' => $f->getValue('name')]) || $this->getD2R('\\Entities\\Switcher')->findOneBy(['hostname' => $f->getValue('hostname')])) {
                 $this->addMessage('A switch already exists with the given name / hostname', OSS_Message::ERROR);
                 break;
             }
             // can we talk to it by SNMP and discover some basic details?
             try {
                 $snmp = new \OSS_SNMP\SNMP($f->getValue('hostname'), $f->getValue('snmppasswd'));
                 $vendor = $snmp->getPlatform()->getVendor();
             } catch (\OSS_SNMP\Exception $e) {
                 $this->addMessage("Could not query {$f->getValue('hostname')} via SNMP.\n                        Consider using the <a href=\"" . OSS_Utils::genUrl('switch', 'add') . "\">the manual add method</a>.", OSS_Message::ERROR);
                 break;
             }
             if ($vendor == 'Unknown') {
                 $this->addMessage("Could not interpret switch system description string - most likely\n                            because no platform interpretor exists for it.<br/><br/>Please see\n                            <a href=\"https://github.com/opensolutions/OSS_SNMP/wiki/Device-Discovery\">this OSS_SNMP page</a>\n                            and consider adding one.<br /><br />\n                            Otherwise use the <a href=\"" . OSS_Utils::genUrl('switch', 'add') . "\">the manual add method</a>.", OSS_Message::ERROR);
                 break;
             }
             if (!($eVendor = $this->getD2R('\\Entities\\Vendor')->findOneBy(['name' => $vendor]))) {
                 $this->addMessage("No vendor defined for [{$vendor}]. Please\n                        <a href=\"" . OSS_Utils::genUrl('vendor', 'add') . "\">add one first</a>.", OSS_Message::ERROR);
                 break;
             }
             // now we have a switch with all the necessary details, add it:
             $s = new Switcher();
             $s->setCabinet($this->getD2R('\\Entities\\Cabinet')->find($f->getValue('cabinetid')));
             $s->setVendor($eVendor);
             $s->setName($f->getValue('name'));
             $s->setHostname($f->getValue('hostname'));
             $s->setIpv4addr($this->_resolve($s->getHostname(), DNS_A));
             $s->setIpv6addr($this->_resolve($s->getHostname(), DNS_AAAA));
             $s->setSnmppasswd($f->getValue('snmppasswd'));
             $s->setInfrastructure($this->getD2R('\\Entities\\Infrastructure')->find($f->getValue('infrastructure')));
             $s->setSwitchtype($f->getValue('switchtype'));
             $s->setModel($snmp->getPlatform()->getModel());
             $s->setActive(true);
             $s->setOs($snmp->getPlatform()->getOs());
             $s->setOsDate($snmp->getPlatform()->getOsDate());
             $s->setOsVersion($snmp->getPlatform()->getOsVersion());
             $s->setLastPolled(new DateTime());
             $this->getD2EM()->persist($s);
             $this->getD2EM()->flush();
             // clear the cache
             $this->getD2R('\\Entities\\Switcher')->clearCache();
             $this->addMessage("Switch polled and added successfully! Please configure the ports found below.", OSS_Message::SUCCESS);
             $this->redirect('switch-port/snmp-poll/switch/' . $s->getId());
         } while (false);
     }
     $this->_display('add-by-snmp.phtml');
 }
 public function setSwitchtype($switchtype)
 {
     $this->__load();
     return parent::setSwitchtype($switchtype);
 }