forked from xcat2/xcat-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwitchHandler.pm
362 lines (285 loc) · 10.9 KB
/
SwitchHandler.pm
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/usr/bin/perl
package xCAT::SwitchHandler;
BEGIN
{
$::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : '/opt/xcat';
}
use lib "$::XCATROOT/lib/perl";
use strict;
use xCAT::Table;
use xCAT::Utils;
use xCAT::MsgUtils;
use xCAT::MacMap;
use IO::Select;
use IO::Handle;
use Sys::Syslog;
use Data::Dumper;
use POSIX qw/WNOHANG/;
use SNMP;
my $sysDescr = '.1.3.6.1.2.1.1.1';
sub new {
my $self = {};
my $proto = shift;
my $class = ref($proto) || $proto;
$self->{switch} = shift;
bless($self, $class);
return $self;
}
sub fill_sessionparms {
my $self = shift;
my %args = @_;
my $community = $args{community};
$self->{sessionparms} = $args{sessionents};
if ($self->{sessionparms}->{snmpversion}) {
if ($self->{sessionparms}->{snmpversion} =~ /3/) { #clean up to accept things like v3 or ver3 or 3, whatever.
$self->{sessionparms}->{snmpversion} = 3;
unless ($self->{sessionparms}->{auth}) {
$self->{sessionparms}->{auth} = 'md5'; #Default to md5 auth if not specified but using v3
}
} elsif ($self->{sessionparms}->{snmpversion} =~ /2/) {
$self->{sessionparms}->{snmpversion} = 2;
} else {
$self->{sessionparms}->{snmpversion} = 1; #Default to lowest common denominator, snmpv1
}
}
unless (defined $self->{sessionparms}->{password}) { #if no password set, inherit the community
$self->{sessionparms}->{password} = $community;
}
}
sub setoid {
my $session = shift;
my $oid = shift;
my $offset = shift;
my $value = shift;
my $type = shift;
unless ($type) { $type = 'INTEGER'; }
my $varbind = new SNMP::Varbind([ $oid, $offset, $value, $type ]);
my $data = $session->set($varbind);
if ($session->{ErrorStr}) { return (1, $session->{ErrorStr}); }
return 0, $varbind;
}
#---------------------------------------------------------
=head3 getsnmpsession
It gets an snmp session appropriate for a switch using the switches table
for guidance on the hows.
Arguments: vlan=> $vid if needed for community string indexing (optional)
=cut
#------------------------------------------------------------
sub getsnmpsession {
my $self = shift;
my $community = shift;
my $vlanid = shift;
my $switch = $self->{switch};
my $session;
my $sessionparams;
if (!$community) { $community = "private"; }
$self->{sitetab} = xCAT::Table->new('site');
my $tmp = $self->{sitetab}->getAttribs({ key => 'snmpc' }, 'value');
if ($tmp and $tmp->{value}) { $community = $tmp->{value} }
my $switchestab = xCAT::Table->new('switches', -create => 0);
my $ent = $switchestab->getNodeAttribs($switch, [qw(switch snmpversion username password privacy auth)]);
if ($ent) {
$self->fill_sessionparms(community => $community, sessionents => $ent);
}
$sessionparams = $self->{sessionparms};
my $snmpver = '1';
if ($sessionparams) {
$snmpver = $sessionparams->{snmpversion};
$community = $sessionparams->{password};
}
if ($snmpver ne '3') {
if ($vlanid) { $community .= '@' . $vlanid; }
$session = new SNMP::Session(
DestHost => $switch,
Version => $snmpver,
Community => $community,
UseNumeric => 1
);
} else { #we have snmp3
my %args = (
DestHost => $switch,
SecName => $sessionparams->{username},
AuthProto => uc($sessionparams->{auth}),
AuthPass => $community,
Version => $snmpver,
SecLevel => 'authNoPriv',
UseNumeric => 1
);
if ($vlanid) { $args{Context} = "vlan-" . $vlanid; }
if ($sessionparams->{privacy}) {
$args{SecLevel} = 'authPriv';
$args{PrivProto} = uc($sessionparams->{privacy});
$args{PrivPass} = $community;
$args{Retries} = 4;
$args{Timeout} = 1500000;
}
#print "args=" . Dumper(%args) . "\n";
$session = new SNMP::Session(%args);
}
#print "switch=$switch\n";
if (!$session) { return $session; }
#get the the switch brand name
my $tmp = xCAT::MacMap::walkoid($session, "$sysDescr", silentfail => 1);
my $swbrand;
if ($tmp->{0}) {
#print "Desc=" . $tmp->{0} . "\n";
my @switch_plugins = glob("$::XCATROOT/lib/perl/xCAT_plugin/vlan/*.pm");
foreach my $fn (@switch_plugins) {
$fn =~ /.*\/([^\/]*).pm$/;
my $module = $1;
#print "fn=$fn,modele=$module\n";
if (!eval { require "$fn" }) {
xCAT::MsgUtils->message("S", "Cannot load module $fn");
next;
} else {
no strict 'refs';
my $filter = ${ "xCAT_plugin::vlan::" . $module . "::" }{filter_string}->();
my $descr = $tmp->{0};
if ($descr =~ /$filter/) {
$self->{module} = $module;
#print "found it:$module\n";
last;
}
}
}
}
if (!exists($self->{module})) {
$self->{session} = 0;
return 0;
} else {
$self->{session} = $session;
return $session;
}
}
#--------------------------------------------------------------
=head3 get_vlan_ids
It gets the existing vlan IDs for the switch.
Returns: an array containing all the vlan ids for the switch
=cut
#-------------------------------------------------------------
sub get_vlan_ids {
my $self = shift;
my $session = $self->{session};
if (!$self->{session}) {
$session = $self->getsnmpsession();
}
unless ($session) { xCAT::MsgUtils->message("S", "Failed to communicate with " . $self->{switch} . " or find a plugin module for the switch."); return; }
no strict 'refs';
return ${ "xCAT_plugin::vlan::" . $self->{module} . "::" }{get_vlan_ids}->($session);
}
#--------------------------------------------------------------
=head3 get_vlanids_for_ports
It returns a hash pointer that contains the vlan id for each given port.
=cut
#-------------------------------------------------------------
sub get_vlanids_for_ports {
my $self = shift;
my @ports = @_;
my $session = $self->{session};
if (!$self->{session}) {
$session = $self->getsnmpsession();
}
unless ($session) { xCAT::MsgUtils->message("S", "Failed to communicate with " . $self->{switch} . " or find a plugin module for the switch."); return; }
no strict 'refs';
return ${ "xCAT_plugin::vlan::" . $self->{module} . "::" }{get_vlanids_for_ports}->($session, @ports);
}
#--------------------------------------------------------------
=head3 create_vlan
Creates a new vlan on the switch
Returns an array. (erorcode, errormsg). When errorcode=0, means no error.
=cut
#-------------------------------------------------------------
sub create_vlan {
my $self = shift;
my $vlan_id = shift;
my $vlan_name = "xcat_vlan_" . $vlan_id;
#print "create vlan get called.\n";
my $session = $self->{session};
if (!$self->{session}) {
$session = $self->getsnmpsession();
}
#my $session = $self->getsnmpsession();
unless ($session) { xCAT::MsgUtils->message("S", "Failed to communicate with " . $self->{switch} . " or find a plugin module for the switch."); return; }
#print Dumper($self->{sessionparms});
no strict 'refs';
return ${ "xCAT_plugin::vlan::" . $self->{module} . "::" }{create_vlan}->($session, $vlan_id);
}
#--------------------------------------------------------------
=head3 add_ports_to_vlan
Adds the given ports to the existing vlan
Returns an array. (erorcode, errormsg). When errorcode=0, means no error.
=cut
#-------------------------------------------------------------
sub add_ports_to_vlan {
my $self = shift;
my $vlan_id = shift;
my $portmode = shift;
my @ports = @_;
#print "vlan=$vlan_id, ports=@ports\n";
my $session = $self->{session};
if (!$self->{session}) {
$session = $self->getsnmpsession();
}
#my $session = $self->getsnmpsession();
unless ($session) { xCAT::MsgUtils->message("S", "Failed to communicate with " . $self->{switch} . " or find a plugin module for the switch."); return; }
no strict 'refs';
return ${ "xCAT_plugin::vlan::" . $self->{module} . "::" }{add_ports_to_vlan}->($session, $vlan_id, $portmode, @ports);
}
#-------------------------------------------------------
=head3 add_crossover_ports_to_vlan
It enables the vlan on the cross-over links.
Returns an array. (erorcode, errormsg). When errorcode=0, means no error.
=cut
#-------------------------------------------------------
sub add_crossover_ports_to_vlan {
my $self = shift;
my $vlan_id = shift;
my @switches = @_;
if (@switches == 0) { return (0, ""); }
my $session = $self->{session};
if (!$self->{session}) {
$session = $self->getsnmpsession();
}
#my $session = $self->getsnmpsession();
unless ($session) { xCAT::MsgUtils->message("S", "Failed to communicate with " . $self->{switch} . " or find a plugin module for the switch."); return; }
no strict 'refs';
return ${ "xCAT_plugin::vlan::" . $self->{module} . "::" }{add_crossover_ports_to_vlan}->($session, $vlan_id, $self->{switch}, @switches);
}
#--------------------------------------------------------------
=head3 remove_vlan
Remove a vlan from the switch
Returns an array. (erorcode, errormsg). When errorcode=0, means no error.
=cut
#-------------------------------------------------------------
sub remove_vlan {
my $self = shift;
my $vlan_id = shift;
my $session = $self->{session};
if (!$self->{session}) {
$session = $self->getsnmpsession();
}
#my $session = $self->getsnmpsession();
unless ($session) { xCAT::MsgUtils->message("S", "Failed to communicate with " . $self->{switch} . " or find a plugin module for the switch."); return; }
no strict 'refs';
return ${ "xCAT_plugin::vlan::" . $self->{module} . "::" }{remove_vlan}->($session, $vlan_id);
}
#--------------------------------------------------------------
=head3 remove_ports_from_vlan
Remove ports from a vlan
Returns an array. (erorcode, errormsg). When errorcode=0, means no error.
=cut
#-------------------------------------------------------------
sub remove_ports_from_vlan {
my $self = shift;
my $vlan_id = shift;
my @ports = @_;
my $session = $self->{session};
if (!$self->{session}) {
$session = $self->getsnmpsession();
}
#my $session = $self->getsnmpsession();
unless ($session) { xCAT::MsgUtils->message("S", "Failed to communicate with " . $self->{switch} . " or find a plugin module for the switch."); return; }
no strict 'refs';
return ${ "xCAT_plugin::vlan::" . $self->{module} . "::" }{remove_ports_from_vlan}->($session, $vlan_id, @ports);
}
1;