-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
603 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace SwitcherCore\Modules\AlliedTelesisSwitch; | ||
|
||
use SnmpWrapper\Oid; | ||
use SwitcherCore\Modules\AbstractModule; | ||
use SwitcherCore\Modules\General\Switches\AbstractInterfaces; | ||
use SwitcherCore\Modules\General\Switches\FdbDot1Bridge; | ||
use SwitcherCore\Modules\Helper; | ||
|
||
class Counters extends \SwitcherCore\Modules\General\Switches\Counters | ||
{ | ||
use InterfacesTrait; | ||
public function run($params = []) | ||
{ | ||
$oids = [ | ||
$this->oids->getOidByName('if.HCInOctets')->getOid(), | ||
$this->oids->getOidByName('if.HCOutOctets')->getOid(), | ||
]; | ||
|
||
if($params['interface']) { | ||
$interface = $this->parseInterface($params['interface']); | ||
foreach ($oids as $num=>$oid) { | ||
$oids[$num] .= ".{$interface['_snmp_id']}"; | ||
} | ||
} | ||
|
||
$oidObjects = []; | ||
foreach ($oids as $oid) { | ||
$oidObjects[] = Oid::init($oid); | ||
} | ||
|
||
$this->response = $this->formatResponse($this->snmp->walk($oidObjects)); | ||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace SwitcherCore\Modules\AlliedTelesisSwitch; | ||
|
||
use SwitcherCore\Modules\AbstractModule; | ||
use SwitcherCore\Modules\General\Switches\AbstractInterfaces; | ||
use SwitcherCore\Modules\General\Switches\FdbDot1Bridge; | ||
use SwitcherCore\Modules\Helper; | ||
|
||
class Descriptions extends \SwitcherCore\Modules\General\Switches\Descriptions | ||
{ | ||
use InterfacesTrait; | ||
|
||
function getPrettyFiltered($filter = []) | ||
{ | ||
$pretty = parent::getPrettyFiltered($filter); // TODO: Change the autogenerated stub | ||
|
||
return $pretty; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace SwitcherCore\Modules\AlliedTelesisSwitch; | ||
|
||
use SwitcherCore\Modules\AbstractModule; | ||
use SwitcherCore\Modules\General\Switches\AbstractInterfaces; | ||
use SwitcherCore\Modules\General\Switches\FdbDot1Bridge; | ||
use SwitcherCore\Modules\Helper; | ||
|
||
class Errors extends \SwitcherCore\Modules\General\Switches\Errors | ||
{ | ||
use InterfacesTrait; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace SwitcherCore\Modules\AlliedTelesisSwitch; | ||
|
||
use SwitcherCore\Modules\AbstractModule; | ||
use SwitcherCore\Modules\General\Switches\AbstractInterfaces; | ||
use SwitcherCore\Modules\General\Switches\FdbDot1Bridge; | ||
use SwitcherCore\Modules\Helper; | ||
|
||
class Fdb extends FdbDot1Bridge | ||
{ | ||
use InterfacesTrait; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace SwitcherCore\Modules\AlliedTelesisSwitch; | ||
|
||
class InterfacesList extends \SwitcherCore\Modules\General\Switches\InterfacesList | ||
{ | ||
use InterfacesTrait; | ||
public function getPrettyFiltered($filter = []) | ||
{ | ||
return parent::getPrettyFiltered($filter); // TODO: Change the autogenerated stub | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
<?php | ||
|
||
namespace SwitcherCore\Modules\AlliedTelesisSwitch; | ||
|
||
use DI\Container; | ||
use DI\DependencyException; | ||
use DI\NotFoundException; | ||
use Monolog\Logger; | ||
use SnmpWrapper\MultiWalkerInterface; | ||
use SnmpWrapper\Oid; | ||
use SwitcherCore\Config\Objects\Model; | ||
use SwitcherCore\Config\OidCollector; | ||
use SwitcherCore\Modules\Helper; | ||
use SwitcherCore\Switcher\CacheInterface; | ||
use SwitcherCore\Switcher\Device; | ||
use SwitcherCore\Switcher\Objects\WrappedResponse; | ||
|
||
trait InterfacesTrait | ||
{ | ||
/** | ||
* @var array | WrappedResponse[] | ||
*/ | ||
protected $response; | ||
|
||
/** | ||
* @var OidCollector | ||
*/ | ||
protected $oids; | ||
|
||
|
||
/** | ||
* @var MultiWalkerInterface | ||
*/ | ||
protected $snmp; | ||
|
||
/** | ||
* @var Model | ||
*/ | ||
protected $model; | ||
|
||
/** | ||
* @var Container | ||
*/ | ||
protected $container; | ||
|
||
|
||
/** | ||
* @var Logger | ||
*/ | ||
protected $logger; | ||
|
||
/** | ||
* @var Device | ||
*/ | ||
protected $device; | ||
|
||
|
||
function parseInterface($iface, $parseBy = 'id') | ||
{ | ||
$ifaces = $this->getInterfacesIds(); | ||
if($parseBy == 'id') { | ||
if(!isset($ifaces[$iface])) { | ||
throw new \Exception("Incorrect interface ID"); | ||
} | ||
return $ifaces[$iface]; | ||
} | ||
$filtered = array_filter($ifaces, function ($i) use ($iface, $parseBy) { | ||
return isset($i[$parseBy]) && $i[$parseBy] == $iface; | ||
}); | ||
if(count($filtered) > 0) { | ||
return array_values($filtered)[0]; | ||
} | ||
throw new \Exception("Interface with name {$iface} not found"); | ||
} | ||
|
||
private $_interfaces; | ||
|
||
function getInterfacesIds() | ||
{ | ||
if ($this->_interfaces) { | ||
return $this->_interfaces; | ||
} | ||
if ($info = $this->getCache('INTERFACES', true)) { | ||
$this->_interfaces = $info; | ||
return $info; | ||
} | ||
$response = $this->snmp->walk([ | ||
Oid::init($this->oids->getOidByName('if.Name')->getOid()), | ||
]); | ||
$responses = []; | ||
foreach ($response as $resp) { | ||
$name = $this->oids->findOidById($resp->getOid()); | ||
if($resp->getError()) { | ||
throw new \Exception("Error walk {$name->getOid()} on device {$this->device->getIp()}"); | ||
} | ||
$responses[$name->getName()] = $resp->getResponse(); | ||
} | ||
|
||
$ifaces = []; | ||
foreach ($responses['if.Name'] as $r) { | ||
if (preg_match('/^(e|g)([0-9]{1,4})$/', $r->getValue(), $m)) { | ||
$id = Helper::getIndexByOid($r->getOid()); | ||
$ifaces[Helper::getIndexByOid($r->getOid())] = [ | ||
'id' => (int)$id, | ||
'name' => $r->getValue(), | ||
'_snmp_id' => $id, | ||
]; | ||
} | ||
} | ||
$this->_interfaces = $ifaces; | ||
$this->setCache("INTERFACES", $ifaces, 600, true); | ||
return $ifaces; | ||
} | ||
|
||
/** | ||
* | ||
* Method for working with cache. | ||
* Cache method generate unique prefix key for isolating over device and modules | ||
* | ||
* @param $key | ||
* @return mixed|null | ||
* @throws DependencyException | ||
* @throws NotFoundException | ||
*/ | ||
protected function getCache($key, $withoutClass = false) | ||
{ | ||
if (!$this->container->has(CacheInterface::class)) { | ||
return null; | ||
} | ||
$cache = $this->container->get(CacheInterface::class); | ||
if ($withoutClass) { | ||
$key = "_" . $this->device->getIp() . ":" . $key; | ||
} else { | ||
$key = get_class($this) . ":" . $this->device->getIp() . ":" . $key; | ||
} | ||
return $cache->get($key); | ||
} | ||
|
||
/** | ||
* | ||
* Method for set value to cache | ||
* Cache method generate unique prefix key for isolating over device and modules | ||
* | ||
* @param $key | ||
* @param mixed $value Any value, not allow streams | ||
* @param int $timeout Timeouts in sec | ||
* @return bool | ||
* @throws DependencyException | ||
* @throws NotFoundException | ||
*/ | ||
protected function setCache($key, $value, $timeout = -1, $withoutClass = false) | ||
{ | ||
if (!$this->device->getIp()) { | ||
throw new NotFoundException("Incorrect injected device, without device"); | ||
} | ||
if (!$this->container->has(CacheInterface::class)) { | ||
$this->logger->notice("Cache interface not setted"); | ||
return false; | ||
} | ||
if ($withoutClass) { | ||
$key = "_" . $this->device->getIp() . ":" . $key; | ||
} else { | ||
$key = get_class($this) . ":" . $this->device->getIp() . ":" . $key; | ||
} | ||
$this->container->get(CacheInterface::class)->set($key, $value, $timeout); | ||
return true; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace SwitcherCore\Modules\AlliedTelesisSwitch; | ||
|
||
use SwitcherCore\Modules\AbstractModule; | ||
use SwitcherCore\Modules\General\Switches\AbstractInterfaces; | ||
use SwitcherCore\Modules\General\Switches\FdbDot1Bridge; | ||
use SwitcherCore\Modules\Helper; | ||
|
||
class LinkInfo extends \SwitcherCore\Modules\General\Switches\LinkInfo | ||
{ | ||
use InterfacesTrait; | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
src/Modules/AlliedTelesisSwitch/MultiRawConsoleCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace SwitcherCore\Modules\AlliedTelesisSwitch; | ||
|
||
use SwitcherCore\Modules\AbstractModule; | ||
|
||
class MultiRawConsoleCommand extends AbstractModule | ||
{ | ||
public function run($params = []) | ||
{ | ||
if(!isset($params['commands'])) { | ||
throw new \Exception("Commands parameter is required"); | ||
} | ||
if(!is_array($params['commands']) && is_string($params['commands'])) { | ||
$commands = explode("\n", $params['commands']); | ||
} else { | ||
$commands = $params['commands']; | ||
} | ||
$response = []; | ||
foreach ($commands as $command) { | ||
if(preg_match("/\<\s*?exception *?['\"](.*)['\"].*?\>/", $command, $match)) { | ||
throw new \Exception($match[1]); | ||
} | ||
if(preg_match('/\<\s*?sleep *?([0-9]{1,3}).*?\>/', $command, $match)) { | ||
sleep($match[1]); | ||
} | ||
$resp = $this->getModule('console_command')->run(['command' => trim($command)])->getPretty(); | ||
$response[] = $resp; | ||
if(!$resp['success'] && $params['break_on_error'] == 'yes') { | ||
break; | ||
} | ||
} | ||
$this->response = $response; | ||
|
||
return $this; | ||
} | ||
|
||
public function getPretty() | ||
{ | ||
return $this->response; | ||
} | ||
|
||
public function getPrettyFiltered($filter = [], $fromCache = false) | ||
{ | ||
return $this->response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace SwitcherCore\Modules\AlliedTelesisSwitch; | ||
|
||
class ParseInterface extends \SwitcherCore\Modules\General\Switches\ParseInterface | ||
{ | ||
use InterfacesTrait; | ||
public function getPrettyFiltered($filter = []) | ||
{ | ||
|
||
return parent::getPrettyFiltered($filter); // TODO: Change the autogenerated stub | ||
} | ||
} |
Oops, something went wrong.