/** * 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'); }
*/ // This is an example script for OSS_SNMP // // It needs to be called with a hostname / IP address and a community string if (count($argv) != 3) { echo <<<HELPTEXT OSS_SNMP - A PHP SNMP library for people who hate SNMP MIBs and OIDs! Copyright (c) 2012, Open Source Solutions Limited, Dublin, Ireland All rights reserved. See: https://github.com/opensolutions/OSS_SNMP/ This is an example script to show how to use OSS_SNMP. It requires two arguments: - the IP address of hostname of a SNMP capable host - the SNMP v2 community string for that host For example: {$argv[0]} 192.168.10.20 public HELPTEXT; exit(1); } require_once dirname(__FILE__) . '/../OSS_SNMP/SNMP.php'; $host = new \OSS_SNMP\SNMP($argv[1], $argv[2]); echo "\n\nPlatform details for {$argv[1]}:\n" . "\nVendor: " . $host->getPlatform()->getVendor() . "\nModel: " . $host->getPlatform()->getModel() . "\nOS: " . $host->getPlatform()->getOs() . "\nOS Version: " . $host->getPlatform()->getOsVersion(); echo "\n\n"; exit(0);