Skip to content

Commit

Permalink
Merge pull request #225 from TripleSD/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
AntonMZ authored Mar 27, 2020
2 parents 26f8d1a + 1ef69c9 commit ebe8001
Show file tree
Hide file tree
Showing 28 changed files with 376 additions and 91 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/laravel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ jobs:
- name: Run SNMP server (Mikrotik hardware)
run: python /home/runner/work/moring/moring/utils/snmp-server/snmp-server.py -p 9005 -c /home/runner/work/moring/moring/utils/snmp-server/configs/HardwareMikrotik.py &

- name: Run SNMP server (Empty vendor)
run: python /home/runner/work/moring/moring/utils/snmp-server/snmp-server.py -p 9006 -c /home/runner/work/moring/moring/utils/snmp-server/configs/EmptyVendor.py &

- name: Check SNMP server 1
run: snmpwalk -v2c -c public 127.0.0.1:9001

Expand All @@ -68,6 +71,9 @@ jobs:
- name: Check SNMP server 5
run: snmpwalk -v2c -c public 127.0.0.1:9005

- name: Check SNMP server 6
run: snmpwalk -v2c -c public 127.0.0.1:9006

- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.testing.example', '.env');"

Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
[v.0.1.4 (Build.023)](https://github.com/TripleSD/moring/releases/tag/0.1.4)
- **(New)** - Добавлены новые автотесты для SNMP классов
- **(New)** - Добавлена проверка на повторное добавление SNMP устройства
- **(Upd)** - Дополнены текущие автотесты для SNMP классов
- **(Upd)** - Выполнен рефакторинг SNMP классов
- **(Upd)** - Выполнен рефакторинг Device классов
- **(Fix)** - Исправлена передача порта SNMP устройства из формы в обработчик

[v.0.1.3 (Build.020)](https://github.com/TripleSD/moring/releases/tag/0.1.3)
- **(Add)** - Добавлен пакет [delimitry/snmp-server](https://github.com/delimitry/snmp-server) для эмуляции snmp устройств
- **(Upd)** - Осуществлен рефакторинг SNMP классов для ускоения запроса и получения SNMP информации
Expand Down
12 changes: 6 additions & 6 deletions app/Console/Commands/SnmpDevicesChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ public function handle(int $device_id = null)

foreach ($devices as $device) {
try {
$deviceConnection['hostname'] = $device->hostname;
$deviceConnection['title'] = $device->title;
$deviceConnection['snmpCommunity'] = $device->snmp_community;
$deviceConnection['snmpPort'] = $device->snmp_port;
$deviceConnection['snmpVersion'] = $device->snmp_version;
$deviceData = $this->devicesRepository->getDeviceData($deviceConnection);
$deviceConnection['hostname'] = $device->hostname;
$deviceConnection['title'] = $device->title;
$deviceConnection['community'] = $device->snmp_community;
$deviceConnection['port'] = $device->snmp_port;
$deviceConnection['snmpVersion'] = $device->snmp_version;
$deviceData = $this->devicesRepository->getDeviceData($deviceConnection);
$this->devicesRepository->update($deviceData, $device->id);
} catch (\Exception $exception) {
if ($this->settingsController->getTelegramStatus() === 1) {
Expand Down
11 changes: 11 additions & 0 deletions app/Http/Controllers/Admin/Network/NetworkDevicesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Http\Controllers\Controller;
use App\Http\Requests\Sites\UpdateAndStoreDeviceRequest;
use App\Models\Devices;
use App\Repositories\Devices\DevicesLogsRepository;
use App\Repositories\Devices\DevicesRepository;
use App\Repositories\Devices\DevicesVendorsRepository;
Expand Down Expand Up @@ -107,6 +108,16 @@ public function update(UpdateAndStoreDeviceRequest $request)
public function store(UpdateAndStoreDeviceRequest $request): RedirectResponse
{
try {
$device = Devices::where('hostname', $request->hostname)
->where('snmp_version', $request->snmp_version)
->where('snmp_port', $request->snmp_port)
->where('snmp_community', $request->snmp_community)
->get();

if ($device->isNotEmpty()) {
throw new Exception('Данное устройство уже есть в списке');
}

$deviceConnection = $this->deviceRepository->setDataConnection($request);
$deviceData = $this->deviceRepository->getDeviceData($deviceConnection);
$this->deviceRepository->store($deviceData);
Expand Down
34 changes: 17 additions & 17 deletions app/Repositories/Devices/DevicesRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ public function setDataConnection($request): array
$deviceData = [];

// Getting vars from template
$deviceData['hostname'] = $request->input('hostname'); // Hostname device
$deviceData['title'] = $request->input('title'); // Short description
$deviceData['snmpCommunity'] = $request->input('snmp_community'); // Device community
$deviceData['snmpPort'] = $request->input('snmp_port'); // Device snmp port
$deviceData['snmpVersion'] = $request->input('snmp_version'); // Device snmp version 1/2/3
$deviceData['hostname'] = $request->input('hostname'); // Hostname device
$deviceData['title'] = $request->input('title'); // Short description
$deviceData['community'] = $request->input('snmp_community'); // Device community
$deviceData['port'] = $request->input('snmp_port'); // Device snmp port
$deviceData['snmpVersion'] = $request->input('snmp_version'); // Device snmp version 1/2/3

return (array) $deviceData;
}
Expand All @@ -140,29 +140,29 @@ public function setDataConnection($request): array
public function getDeviceData(array $varsConnection): array
{
try {
$connection = new SnmpRepository();
$snmpFlow = $connection->startSession($varsConnection)->walk('SNMPv2-MIB::sysDescr.0');
$snmpObject = new SnmpRepository();
$snmpFlow = $snmpObject->startSession($varsConnection);
$vendorNameRawString = $snmpObject->getVendorNameRawString($snmpFlow);
} catch (Exception $e) {
throw new Exception('Устройство не отвечает');
}

$vendor = new Vendor();
$vendorName = $vendor->parseName($snmpFlow);
$vendorName = $vendor->parseName($vendorNameRawString);

if ($vendorName == null) {
throw new Exception('Не удалось определить производителя.');
}

/** @var VendorInterface $device */
$snmpFlow = $connection->startSession($varsConnection);
$device = $vendor->getVendorClass($vendorName);
$device = $vendor->getVendorClass($vendorName);

//Set vars
$deviceData['hostname'] = $varsConnection['hostname'];
$deviceData['title'] = $varsConnection['title'];
$deviceData['snmpCommunity'] = $varsConnection['snmpCommunity'];
$deviceData['snmpPort'] = $varsConnection['snmpPort'];
$deviceData['snmpVersion'] = $varsConnection['snmpVersion'];
$deviceData['hostname'] = $varsConnection['hostname'];
$deviceData['title'] = $varsConnection['title'];
$deviceData['community'] = $varsConnection['community'];
$deviceData['port'] = $varsConnection['port'];
$deviceData['snmpVersion'] = $varsConnection['snmpVersion'];

// Get & set vars from device
$deviceData['location'] = $device->getLocation($snmpFlow);
Expand Down Expand Up @@ -210,8 +210,8 @@ public function store(array $deviceData, $deviceId = null): void
$device->serial_number = $deviceData['serialNumber'];
$device->packets_version = $deviceData['packetsVersion'];
$device->platform_type = $deviceData['platformType'];
$device->snmp_port = $deviceData['snmpPort'];
$device->snmp_community = $deviceData['snmpCommunity'];
$device->snmp_port = $deviceData['port'];
$device->snmp_community = $deviceData['community'];
$device->snmp_version = $deviceData['snmpVersion'];
$device->save();
}
Expand Down
10 changes: 7 additions & 3 deletions app/Repositories/Snmp/SnmpRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ class SnmpRepository extends Repository
{
public function startSession($varsConnection)
{
return new SNMP(SNMP::VERSION_2c, $varsConnection['hostname'], $varsConnection['snmpCommunity']);
return new SNMP(
SNMP::VERSION_2c,
$varsConnection['hostname'] . ':' . $varsConnection['port'], $varsConnection['community']
);
}

public function name($nameArray)
public function getVendorNameRawString($snmpFlow)
{
return $nameArray['1.3.6.1.2.1.1.1.0'];
/** @var SNMP $snmpFlow */
return $snmpFlow->get('1.3.6.1.2.1.1.1.0');
}
}
20 changes: 10 additions & 10 deletions app/Repositories/Snmp/Vendor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@

class Vendor
{
public function parseName(array $snmpFlow): string
public function parseName(string $rawString): string
{
if (isset($snmpFlow['SNMPv2-MIB::sysDescr.0'])) {
if (preg_match('/RouterOS/', $snmpFlow['SNMPv2-MIB::sysDescr.0'])) {
return 'Mikrotik';
if (isset($rawString)) {
if (preg_match('/RouterOS/', $rawString)) {
return (string) 'Mikrotik';
}

if (preg_match('/Cisco/', $snmpFlow['SNMPv2-MIB::sysDescr.0'])) {
return 'Cisco';
if (preg_match('/Cisco/', $rawString)) {
return (string) 'Cisco';
}

if (preg_match('/[D][EG][S]/', $snmpFlow['SNMPv2-MIB::sysDescr.0'])) {
return 'DLink';
if (preg_match('/[D][EG][S]/', $rawString)) {
return (string) 'DLink';
}

if (preg_match('/MES/', $snmpFlow['SNMPv2-MIB::sysDescr.0'])) {
return 'Eltex';
if (preg_match('/MES/', $rawString)) {
return (string) 'Eltex';
}
}

Expand Down
4 changes: 4 additions & 0 deletions app/Repositories/Snmp/Vendors/Cisco.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public function getFirmwareVersion($snmpFlow): string
public function getPacketsVersion($snmpFlow): string
{
/* @var SNMP $snmpFlow */
$snmpFlow->close();

return (string) null;
}

Expand Down Expand Up @@ -115,6 +117,8 @@ public function getSerialNumber($snmpFlow): string
public function getLicenseLevel($snmpFlow): string
{
/* @var SNMP $snmpFlow */
$snmpFlow->close();

return (string) null;
}

Expand Down
6 changes: 6 additions & 0 deletions app/Repositories/Snmp/Vendors/DLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public function getModel($snmpFlow): string
public function getFirmware($snmpFlow): string
{
/* @var SNMP $snmpFlow */
$snmpFlow->close();

return (string) 'D-Link Linux';
}

Expand All @@ -50,6 +52,8 @@ public function getFirmwareVersion($snmpFlow): string
public function getPacketsVersion($snmpFlow): string
{
/* @var SNMP $snmpFlow */
$snmpFlow->close();

return (string) null;
}

Expand Down Expand Up @@ -111,6 +115,8 @@ public function getSerialNumber($snmpFlow): string
public function getLicenseLevel($snmpFlow): string
{
/* @var SNMP $snmpFlow */
$snmpFlow->close();

return (string) null;
}

Expand Down
6 changes: 6 additions & 0 deletions app/Repositories/Snmp/Vendors/Eltex.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public function getModel($snmpFlow): string
public function getFirmware($snmpFlow): string
{
/* @var SNMP $snmpFlow */
$snmpFlow->close();

return (string) 'Eltex Linux';
}

Expand All @@ -50,6 +52,8 @@ public function getFirmwareVersion($snmpFlow): string
public function getPacketsVersion($snmpFlow): string
{
/* @var SNMP $snmpFlow */
$snmpFlow->close();

return (string) null;
}

Expand Down Expand Up @@ -111,6 +115,8 @@ public function getSerialNumber($snmpFlow): string
public function getLicenseLevel($snmpFlow): string
{
/* @var SNMP $snmpFlow */
$snmpFlow->close();

return (string) null;
}

Expand Down
5 changes: 4 additions & 1 deletion commands/testing_local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ PID4=$!
python utils/snmp-server/snmp-server.py -p 9005 -c utils/snmp-server/configs/HardwareMikrotik.py &
PID5=$!

python utils/snmp-server/snmp-server.py -p 9006 -c utils/snmp-server/configs/EmptyVendor.py &
PID6=$!

echo 'Starting PHPunit'
#./vendor/bin/phpunit --coverage-html report_coverage
./vendor/bin/phpunit
#./vendor/bin/phpunit --filter testGetSerialNumber tests/Unit/SNMP/CloudMikrotikEmptyOIDTest.php

echo 'Killing SNMP servers'
kill $PID1 $PID2 $PID3 $PID4 $PID5
kill $PID1 $PID2 $PID3 $PID4 $PID5 $PID6

echo 'Removing .env file'
rm -f .env
Expand Down
4 changes: 2 additions & 2 deletions config/moring.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

return [
'build' => '020',
'version' => '0.1.3',
'build' => '023',
'version' => '0.1.4',
'createYear' => '2019',
'bridgeUrl' => 'http://api.moring.ru/v1',
'bridgeCurrentMoringVersionUrl' => '/moring/version',
Expand Down
5 changes: 3 additions & 2 deletions tests/Unit/SNMP/CiscoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ public function testGetPlatformType(): void

private function setVarsConnection()
{
$array['hostname'] = '127.0.0.1:9002';
$array['snmpCommunity'] = 'public';
$array['hostname'] = '127.0.0.1:9002';
$array['community'] = 'public';
$array['port'] = 161;

return $array;
}
Expand Down
5 changes: 3 additions & 2 deletions tests/Unit/SNMP/CloudMikrotikExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ public function testGetFirmwareVersion(): void

private function setVarsConnection()
{
$array['hostname'] = '0.0.0.0';
$array['snmpCommunity'] = 'public';
$array['hostname'] = '0.0.0.0';
$array['community'] = 'public';
$array['port'] = 161;

return $array;
}
Expand Down
5 changes: 3 additions & 2 deletions tests/Unit/SNMP/CloudMikrotikTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ public function testGetPlatformType(): void

private function setVarsConnection()
{
$array['hostname'] = '127.0.0.1:9001';
$array['snmpCommunity'] = 'public';
$array['hostname'] = '127.0.0.1:9001';
$array['community'] = 'public';
$array['port'] = 161;

return $array;
}
Expand Down
Loading

0 comments on commit ebe8001

Please sign in to comment.