/** * Show BGP Summary table * * @see https://github.com/opensolutions/NOCtools/wiki/BGP-Summary */ public function summaryAction() { if ($this->getRequest()->isPost()) { $this->view->bgpDevice = $device = $this->_getParam('bgpDevice'); try { $host = new \OSS_SNMP\SNMP($device, $this->_options['community']); $peers = $host->useBGP()->peerDetails(true); } catch (\OSS_SNMP\Exception $e) { $this->addMessage("Could not query BGP peer information via SNMP from " . $device, OSS_Message::ERROR); return; } ksort($peers, SORT_NUMERIC); $this->view->peers = $peers; } }
function OSS_SNMP_Lookup($dev, $snmplookup, $oid = null) { // This is find out the name of the function that called this to make the error logging more descriptive $caller = "OSS_SNMP_Lookup"; $failed = false; $snmpHost = new OSS_SNMP\SNMP($dev->PrimaryIP, $dev->SNMPCommunity, $dev->SNMPVersion, $dev->v3SecurityLevel, $dev->v3AuthProtocol, $dev->v3AuthPassphrase, $dev->v3PrivProtocol, $dev->v3PrivPassphrase); $snmpresult = false; try { $snmpresult = is_null($oid) ? $snmpHost->useSystem()->{$snmplookup}(true) : $snmpHost->get($oid); } catch (Exception $e) { IncrementFailures($dev->DeviceID); $failed = true; error_log("PowerDistribution::{$caller}({$dev->DeviceID}) " . $e->getMessage()); } if (!$failed) { ResetFailures($dev->DeviceID); } return $snmpresult; }
/** * Query Asterisk for details via SNMP */ public function ajaxGetForHostAction() { $host = $this->_getParam('host', null); if ($host) { $device = new \OSS_SNMP\SNMP($host, $this->_options['community']); $details['astVersion'] = $device->useAsterisk()->version(); $details['astTag'] = $device->useAsterisk()->tag(); $details['astUptime'] = $this->_timeticksToString($device->useAsterisk()->uptime()); $details['astReload'] = $this->_timeticksToString($device->useAsterisk()->reloadTime()); $details['astCallsProcessed'] = $device->useAsterisk()->callsProcessed(); $details['astCallsActive'] = $device->useAsterisk()->callsActive(); $details['calls'] = $device->useAsterisk_Channels()->channelDetails(true); unset($device); $this->getResponse()->setHeader('Content-Type', 'application/json')->setBody(Zend_Json::encode($details))->sendResponse(); exit(0); } }
See: https://github.com/opensolutions/OSS_SNMP/ This is an example script to show how to use OSS_SNMP. It requires two or three 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]); if (count($argv) == 3) { echo "\nNumber of interfaces on {$argv[1]}: " . $host->useIface()->numberofInterfaces() . "\n\n"; echo "ID: Name - Index - Type - State - Media Available - Jack Type - State Exits - Jabber State - Autoneg\n\n"; foreach ($host->useIface()->names() as $id => $name) { if ($id > 1100) { continue; } echo "{$id}: {$name} - {$host->useMAU()->index()[$id]}" . " - {$host->useMAU()->types(true)[$id]}" . " - {$host->useMAU()->states(true)[$id]}" . " - {$host->useMAU()->mediaAvailable(true)[$id]}" . " - {$host->useMAU()->jackTypes(true)[$id]}" . " - {$host->useMAU()->mediaAvailableStateExits()[$id]}" . " - {$host->useMAU()->jabberStates(true)[$id]}" . " - " . ($host->useMAU()->autonegSupported()[$id] ? 'Y' : 'N') . " - " . ($host->useMAU()->autonegAdminState()[$id] ? 'Y' : 'N') . "\n"; } echo "\n"; exit(0); } exit(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'); }
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 (with Asterisk SNMP enabled) - 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\n"; echo "BGP version running on {$argv[1]}: " . $host->useBGP()->version() . "\n"; echo "BGP - local ASN: " . $host->useBGP()->localASN() . "\n"; echo "BGP - identifier: " . $host->useBGP()->identifier() . "\n"; #echo "BGP - peers: \n\n"; #print_r( $host->useBGP()->peerDetails(1) ); echo "\n\n"; exit(0);
/** * Utility function to get the RSTP port roles for a given device on a given VLAN * * @see rstpTopologyAction() * @param string $device The device to query *@param string $type Either 'mst' or else 'rstp' assumed * @param int $instance The VLAN / MST instance id to query * @return array Array of roles for participating ports (empty array of none or if device could not be queried) */ private function _stpTopologyPortRoles($device, $type, $instance) { try { $_h = new \OSS_SNMP\SNMP($device, $this->_options['community']); if ($type == 'mst') { return $_h->useCisco_MST()->portRoles($instance, true); } else { return $_h->useCisco_RSTP()->portRoles($instance, true); } } catch (Exception $e) { return array(); } }
// 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 "\nSystem information for {$argv[1]}:\n\n"; print_r($host->useSystem()->getAll()); echo "\n\n"; exit(0);
private static function OSS_SNMP_Lookup($dev, $snmplookup, $portid = null, $baseoid = null) { // This is find out the name of the function that called this to make the error logging more descriptive $caller = debug_backtrace(); $caller = $caller[1]['function']; // Since we don't really let the user specify the version right now here's a stop gap // Try the default version of 2c first $snmpHost = new OSS_SNMP\SNMP($dev->PrimaryIP, $dev->SNMPCommunity, $dev->SNMPVersion, $dev->v3SecurityLevel, $dev->v3AuthProtocol, $dev->v3AuthPassphrase, $dev->v3PrivProtocol, $dev->v3PrivPassphrase); try { $snmpHost->useSystem()->name(); } catch (Exception $e) { // That shit the bed so drop down to 1 $snmpHost = new OSS_SNMP\SNMP($dev->PrimaryIP, $dev->SNMPCommunity, 1); } $snmpresult = false; try { $snmpresult = is_null($portid) ? $snmpHost->useIface()->{$snmplookup}(true) : $snmpHost->get($baseOID . ".{$portid}"); } catch (Exception $e) { error_log("SwitchInfo::{$caller}({$dev->DeviceID}) " . $e->getMessage()); } return $snmpresult; }
public function cliPortRolesDeltaAction() { $devices = []; OSS_Debug::dd($devices); foreach ($this->_devices as $d) { $devices[$d] = []; try { $device = new \OSS_SNMP\SNMP($d, $this->_options['community']); $devices[$d]['instances'] = $device->useCisco_SMST()->instances(); foreach ($devices[$d]['instances'] as $iid => $iname) { $devices[$d]['instance_roles'][$iid] = $device->useCisco_MST()->portRoles($iid, true); } print_r($devices[$d]); } catch (\OSS_SNMP\Exception $e) { continue; return; } } }
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 (with Asterisk SNMP enabled) - 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\n"; echo "Asterisk version running on {$argv[1]}: " . $host->useAsterisk()->version() . "\n"; echo "Asterisk SVN tag running on {$argv[1]}: " . $host->useAsterisk()->tag() . "\n"; echo "Asterisk on {$argv[1]} up for: " . $host->useAsterisk()->uptime() / 100 / 60 / 60 . " hours\n"; echo "Asterisk on {$argv[1]} reloaded: " . $host->useAsterisk()->reloadTime() / 100 / 60 / 60 . " hours ago\n"; echo "Asterisk PID: " . $host->useAsterisk()->pid() . "\n"; echo "Asterisk control socket: " . $host->useAsterisk()->controlSocket() . "\n"; echo "Calls active: " . $host->useAsterisk()->callsActive() . "\n"; echo "Calls processed: " . $host->useAsterisk()->callsProcessed() . "\n"; echo "Modules compiled in: " . $host->useAsterisk()->modules() . "\n"; echo "\n\n"; echo "Indications defined: " . $host->useAsterisk_Indications()->number() . "\n"; echo "Default indications zone: " . $host->useAsterisk_Indications()->defaultZone() . "\n"; echo "Indication country codes:\n\n"; print_r($host->useAsterisk_Indications()->countryCodes());
// port interfaces to ignore. // // The array key is the hostname. The value is the shortened snmp name. $ignoreports = []; // parse the command line arguments parseArguments(); // create a memcache key: $MCKEY = 'NAGIOS_CHECK_PORT_ERRORS_' . md5($cmdargs['host']); if (!class_exists('Memcache')) { die("ERROR: php5-memcache is required\n"); } $mc = new Memcache(); $mc->connect('localhost', 11211) or die("ERROR: Could not connect to memcache on localhost:11211\n"); _log("Connected to Memcache with version: " . $mc->getVersion(), LOG__DEBUG); require 'OSS_SNMP/OSS_SNMP/SNMP.php'; $snmp = new \OSS_SNMP\SNMP($cmdargs['host'], $cmdargs['community']); // get interface types for later filtering $types = $snmp->useIface()->types(); $names = filterForType($snmp->useIface()->names(), $types, OSS_SNMP\MIBS\Iface::IF_TYPE_ETHERNETCSMACD); _log("Found " . count($names) . " physical ethernet ports", LOG__DEBUG); // get current error counters $ifInErrors = filterForType($snmp->useIface()->inErrors(), $types, OSS_SNMP\MIBS\Iface::IF_TYPE_ETHERNETCSMACD); $ifOutErrors = filterForType($snmp->useIface()->outErrors(), $types, OSS_SNMP\MIBS\Iface::IF_TYPE_ETHERNETCSMACD); _log("Found " . count($ifInErrors) . " entries for in errors on physical ethernet ports", LOG__DEBUG); _log("Found " . count($ifOutErrors) . " entries for out errors on physical ethernet ports", LOG__DEBUG); // delete unwanted entries foreach (array_keys($ignoreports) as $hostkey) { if ($cmdargs['host'] == $hostkey) { $portid = array_keys($names, $ignoreports[$hostkey])[0]; if (isset($portid)) { unset($ifInErrors[$portid]);
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 (with Asterisk SNMP enabled) - 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 "\nSystem information for {$argv[1]}:\n\n"; print_r($host->useSystem()->getAll()); echo "\n\n"; 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"; echo "Temperature alarm: " . ($host->useExtreme_System_Common()->overTemperatureAlarm() ? 'YES' : 'NO') . "\n"; echo "Temperature : " . $host->useExtreme_System_Common()->currentTemperature() . "C\n"; echo "\n\n"; print_r($host->useExtreme_SwMonitor_Memory()->percentUsage()); echo "\n\n"; exit(0);
- the IP address of hostname of a SNMP capable host - the SNMP v2 community string for that host - the index of the interface to show details for If the third argument is missing, it will print interface indexes and names. 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]); if (count($argv) == 3) { echo "\nNumber of interfaces on {$argv[1]}: " . $host->useIface()->numberofInterfaces() . "\n\n"; echo "ID: Name - Descrition - Type - Admin/Operational State\n\n"; foreach ($host->useIface()->names() as $id => $name) { echo "{$id}: {$name} - {$host->useIface()->descriptions()[$id]} - {$host->useIface()->types(1)[$id]}" . " - {$host->useIface()->adminStates(1)[$id]}/{$host->useIface()->operationStates(1)[$id]}\n"; } echo "\n"; exit(0); } $names = $host->useIface()->names(); $id = $argv[3]; if (!isset($names[$id])) { echo "Unknown interface index!\n"; exit(2); }
// Create application, bootstrap, and run $application = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini'); Zend_Registry::set('bootstrap', $application->getBootstrap()); $application->getBootstrap()->bootstrap('OSSAutoLoader'); $application->getBootstrap()->bootstrap('doctrine2'); $em = $application->getBootstrap()->getResource('doctrine2'); $config = $application->getOption('resources'); array_shift($argv); $vlans = $argv; if (!count($vlans)) { die("USAGE: private-vlans.php \$vlan1 \$vlan2 \$vlan3 ...\n\n"); } // get all switches $switches = $em->getRepository('\\Entities\\Switcher')->getAndCache(true, \Entities\Switcher::TYPE_SWITCH); foreach ($switches as $switch) { $sw = new \OSS_SNMP\SNMP($switch->getHostname(), $switch->getSnmppasswd()); foreach ($vlans as $tag) { if (VERBOSE) { echo "Polling {$switch->getName()} for VLAN {$tag}\n"; } // get ports for the given VLAN(s) $vlanIfIndex = $sw->useExtreme_Vlan()->ifVlanIdsToIfIndexes()[$tag]; // does our VLAN exist? if (!($vlan = $em->getRepository('\\Entities\\Vlan')->findOneBy(['number' => $tag]))) { $vlan = new \Entities\Vlan(); $vlan->setName($sw->useExtreme_Vlan()->ifDescriptions()[$vlanIfIndex]); $vlan->setNumber($tag); $vlan->setPrivate(true); if (DBWRITE) { if (VERBOSE) { echo "Adding VLAN {$tag} to database\n";
$dev = new Device(); $cab = new Cabinet(); $taginsert = ""; // Ajax functions // SNMP Test if (isset($_POST['snmptest'])) { // Parse through the post data and pull in site defaults if necessary $community = $_POST['SNMPCommunity'] == "" ? $config->ParameterArray["SNMPCommunity"] : $_POST['SNMPCommunity']; $version = $_POST['SNMPVersion'] == "" ? $config->ParameterArray["SNMPVersion"] : $_POST['SNMPVersion']; $v3SecurityLevel = $_POST['v3SecurityLevel'] == "" ? $config->ParameterArray["v3SecurityLevel"] : $_POST['v3SecurityLevel']; $v3AuthProtocol = $_POST['v3AuthProtocol'] == "" ? $config->ParameterArray["v3AuthProtocol"] : $_POST['v3AuthProtocol']; $v3AuthPassphrase = $_POST['v3AuthPassphrase'] == "" ? $config->ParameterArray["v3AuthPassphrase"] : $_POST['v3AuthPassphrase']; $v3PrivProtocol = $_POST['v3PrivProtocol'] == "" ? $config->ParameterArray["v3PrivProtocol"] : $_POST['v3PrivProtocol']; $v3PrivPassphrase = $_POST['v3PrivPassphrase'] == "" ? $config->ParameterArray["v3PrivPassphrase"] : $_POST['v3PrivPassphrase']; // Init the snmp handler $snmpHost = new OSS_SNMP\SNMP($_POST['PrimaryIP'], $community, $version, $v3SecurityLevel, $v3AuthProtocol, $v3AuthPassphrase, $v3PrivProtocol, $v3PrivPassphrase); // Try to connect to keep us from killing the system on a failure $error = false; try { $snmpresults = $snmpHost->useSystem()->name(); } catch (Exception $e) { $error = true; } // Show the end user something to make them feel good about it being correct if (!$error) { foreach ($snmpHost->realWalk('1.3.6.1.2.1.1') as $oid => $value) { print "{$oid} => {$value} <br>\n"; } } else { print __("Something isn't working correctly"); }
*/ // 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);
/** * Recursivily crawls all CDP neighbours to build up a flat array of all devices * indexed by the CDP device id. * * Array form is same as that returned by neighbours() * * @see neighbours() * @param array $devices Unless you're doing something funky, just pass an empty array. This is where the result will be found. * @param string $device CDP device ID of next host to crawl. On first pass, set to null - used internally when recursing * @param array $ignore An array of CDP device IDs to *ignore*. I.e. to not include in recursive crawling * @return array The resultant array of all crawled devices (same as that passed in the @param $devices parameter) */ public function crawl(&$devices = array(), $device = null, $ignore = array()) { if (!count($devices)) { $device = $this->id(); $devices[$device] = $this->neighbours(true, $ignore); } foreach ($devices[$device] as $feNeighbour => $feConnections) { if (in_array($feNeighbour, $ignore)) { if (isset($devices[$device][$feNeighbour])) { unset($devices[$device][$feNeighbour]); } continue; } if (!isset($devices[$feNeighbour])) { $snmp = new \OSS_SNMP\SNMP($feNeighbour, $this->getSNMP()->getCommunity()); try { $devices[$feNeighbour] = $snmp->useCisco_CDP()->neighbours(true, $ignore); unset($snmp); $this->crawl($devices, $feNeighbour, $ignore); } catch (\OSS_SNMP\Exception $e) { // this device did not respond / have CDP enabled / CDP available - skip unset($devices[$feNeighbour]); } } } return $devices; }
/** * AJAX function to provide a JSON list of VLANs configured on a particular device. * * For example, used to dynamically populate dropdowns in @see VlanController::compareAction() * * @param string $host The SNMP addressable hostname of the device to query for VLAN IDs and names * @return JSON Encoded array of [vlanId] => 'vlanName' entries. */ public function ajaxGetForHostAction() { $host = $this->_getParam('host', null); if ($host) { try { $device = new \OSS_SNMP\SNMP($host, $this->_options['community']); $vlans = $device->useCisco_VTP()->vlanNames(); unset($device); } catch (\OSS_SNMP\Exception $e) { return; } $this->getResponse()->setHeader('Content-Type', 'application/json')->setBody(Zend_Json::encode($vlans))->sendResponse(); exit(0); } }