Skip to content

Commit

Permalink
Fix C-Data unreg
Browse files Browse the repository at this point in the history
  • Loading branch information
meklis committed May 12, 2024
1 parent b6c6adf commit bd2317c
Show file tree
Hide file tree
Showing 19 changed files with 603 additions and 0 deletions.
5 changes: 5 additions & 0 deletions configs/oids/global.oids.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@
- {name: dot1q.VlanStaticForbiddenEgressPorts, oid: .1.3.6.1.2.1.17.7.1.4.3.1.3, access: read} #RETURNED HEX-STRING, must wrapped to binary. num of byte = num port; 1 = on port
- {name: dot1q.VlanStaticUntaggedPorts, oid: .1.3.6.1.2.1.17.7.1.4.3.1.4, access: read } #RETURNED HEX-STRING, must wrapped to binary. num of byte = num port; 1 = on port
- {name: dot1q.VlanStaticRowStatus, oid: .1.3.6.1.2.1.17.7.1.4.3.1.5, access: read } #RETURNED HEX-STRING, must wrapped to binary. num of byte = num port; 1 = on port

- {name: dot1q.VlanCurrentEgressPorts, oid: .1.3.6.1.2.1.17.7.1.4.2.1.4, access: read} #RETURNED HEX-STRING, must wrapped to binary. num of byte = num port; 1 = on port
- {name: dot1q.VlanCurrentUntaggedPorts, oid: .1.3.6.1.2.1.17.7.1.4.2.1.5, access: read } #RETURNED HEX-STRING, must wrapped to binary. num of byte = num port; 1 = on port


#qBridgeMIB - dot1qBase
- {name: dot1q.Pvid, oid: .1.3.6.1.2.1.17.7.1.4.5.1.1, access: read}
- {name: dot1q.NumVlans, oid: .1.3.6.1.2.1.17.7.1.1.4, access: read}
Expand Down
36 changes: 36 additions & 0 deletions src/Modules/AlliedTelesisSwitch/Counters.php
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;
}
}
20 changes: 20 additions & 0 deletions src/Modules/AlliedTelesisSwitch/Descriptions.php
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;
}
}
13 changes: 13 additions & 0 deletions src/Modules/AlliedTelesisSwitch/Errors.php
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;
}
13 changes: 13 additions & 0 deletions src/Modules/AlliedTelesisSwitch/Fdb.php
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;
}
12 changes: 12 additions & 0 deletions src/Modules/AlliedTelesisSwitch/InterfacesList.php
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
}
}
169 changes: 169 additions & 0 deletions src/Modules/AlliedTelesisSwitch/InterfacesTrait.php
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;
}

}
14 changes: 14 additions & 0 deletions src/Modules/AlliedTelesisSwitch/LinkInfo.php
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 src/Modules/AlliedTelesisSwitch/MultiRawConsoleCommand.php
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;
}
}
13 changes: 13 additions & 0 deletions src/Modules/AlliedTelesisSwitch/ParseInterface.php
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
}
}
Loading

0 comments on commit bd2317c

Please sign in to comment.