/
SnmpPoll.php
137 lines (112 loc) · 4.31 KB
/
SnmpPoll.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
namespace IXP\Console\Commands\Switches;
/*
* Copyright (C) 2009 - 2019 Internet Neutral Exchange Association Company Limited By Guarantee.
* All Rights Reserved.
*
* This file is part of IXP Manager.
*
* IXP Manager is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version v2.0 of the License.
*
* IXP Manager is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License v2.0
* along with IXP Manager. If not, see:
*
* http://www.gnu.org/licenses/gpl-2.0.html
*/
use IXP\Console\Commands\Command;
use D2EM;
use OSS_SNMP\{
Exception,
SNMP
};
use Entities\{
Switcher as SwitcherEntity
};
/**
* Class SnmpPoll
*
* @author Yann Robin <yann@islandbridgenetworks.ie>
* @author Barry O'Donovan <barry@islandbridgenetworks.ie>
* @package IXP\Console\Commands
* @copyright Copyright (C) 2009 - 2019 Internet Neutral Exchange Association Company Limited By Guarantee
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU GPL V2.0
*/
class SnmpPoll extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'switch:snmp-poll
{switch? : The name of the switch, if not name specified the command will loop over all switches}
{--noflush : If specified no modification will be made to the database}
{--log : Output detailed polling information to the log}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Poll and update switches and switch ports via SNMP';
/**
* Execute the console command.
*
* @return mixed
*
* @throws
*/
public function handle() {
if( $this->argument('switch') ) {
if( ! ( $switches = D2EM::getRepository( SwitcherEntity::class )->findBy( [ "name" => $this->argument('switch') ] ) ) ) {
$this->error( "ERR: No switch found with name: " . $this->argument('switch' ) );
return -1;
}
} else {
$switches = D2EM::getRepository( SwitcherEntity::class )->getPollable();
}
if( count( $switches ) ){
foreach( $switches as $s ) {
/** @var $s SwitcherEntity */
if( $s->getSnmppasswd() === null || trim( $s->getSnmppasswd() ) === '' ) {
if( !$this->isVerbosityQuiet() ) {
$this->info( "Skipping {$s->getName()} as no SNMP password set" );
}
continue;
}
if( !$this->isVerbosityQuiet() ) {
if( $s->getLastPolled() === null ) {
$this->info( "First time polling {$s->getName()} with SNMP request to {$s->getHostname()}" );
} else {
$this->info( "Polling {$s->getName()} with SNMP request to {$s->getHostname()}" );
}
}
try {
$sPolled = false;
$host = new SNMP( $s->getHostname(), $s->getSnmppasswd() );
$s->snmpPoll( $host, $this->option( 'log', false ) );
$sPolled = true;
$s->snmpPollSwitchPorts( $host, $this->option( 'log', false ) );
if( $this->option( 'noflush', false ) ){
$this->warn( ' *** --noflush parameter set - NO CHANGES MADE TO DATABASE' );
} else{
D2EM::flush();
}
} catch( Exception $e ) {
if( $sPolled ){
$this->error("ERROR: OSS_SNMP exception polling switch {$s->getName()} by SNMP");
} else {
$this->error("ERROR: OSS_SNMP exception polling switch ports for {$s->getName()} by SNMP");
}
}
}
}
return 0;
}
}