Class: Switch

Inherits:
Object
  • Object
show all
Defined in:
lib/netutils/switch.rb

Defined Under Namespace

Modules: Type Classes: Peer, Peers, Port, PortName, Ports

Constant Summary collapse

@@retrieve_all =
false
@@db =
Hash.new
@@unretrieved =
OnceQueue.new
@@warn =
Array.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, ia, retrieve = true, parent = nil) ⇒ Switch

Returns a new instance of Switch.



158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/netutils/switch.rb', line 158

def initialize(name, type, ia, retrieve = true, parent = nil)
	name_set(name)
	@type = type
	@ports = Ports.new
	@retrieve = retrieve
	@cli = nil
	ip_address_set(ia)
	@parent = parent
	@note = nil

	return self
end

Instance Attribute Details

#firmwareObject

Returns the value of attribute firmware.



151
152
153
# File 'lib/netutils/switch.rb', line 151

def firmware
  @firmware
end

#iaObject (readonly)

Returns the value of attribute ia.



150
151
152
# File 'lib/netutils/switch.rb', line 150

def ia
  @ia
end

#nameObject (readonly)

Returns the value of attribute name.



150
151
152
# File 'lib/netutils/switch.rb', line 150

def name
  @name
end

#noteObject

Returns the value of attribute note.



151
152
153
# File 'lib/netutils/switch.rb', line 151

def note
  @note
end

#parentObject (readonly)

Returns the value of attribute parent.



150
151
152
# File 'lib/netutils/switch.rb', line 150

def parent
  @parent
end

#platformObject

Returns the value of attribute platform.



151
152
153
# File 'lib/netutils/switch.rb', line 151

def platform
  @platform
end

#portsObject (readonly)

Returns the value of attribute ports.



150
151
152
# File 'lib/netutils/switch.rb', line 150

def ports
  @ports
end

#timeObject

Returns the value of attribute time.



151
152
153
# File 'lib/netutils/switch.rb', line 151

def time
  @time
end

#typeObject (readonly)

Returns the value of attribute type.



150
151
152
# File 'lib/netutils/switch.rb', line 150

def type
  @type
end

Class Method Details

.get(name, type, platform = nil, firmware = nil, time = nil, ia = nil, parent = nil) ⇒ Object



314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/netutils/switch.rb', line 314

def self.get(name, type, platform = nil, firmware = nil, time = nil,
    ia = nil, parent = nil)
	# XXX should lock
	if @@db.key?(name)
		sw = @@db[name]
		sw.platform = platform if sw.platform == nil
		sw.firmware = firmware if sw.firmware == nil
		sw.time = time if sw.time == nil
		sw.ip_address_set(ia)
	else
		sw = Switch.new(name, type, ia, @@retrieve_all, parent)
	end
	return sw
end

.retrieveObject

Raises:

  • (ArgumentError)


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
363
364
365
366
367
368
# File 'lib/netutils/switch.rb', line 329

def self.retrieve
	raise(ArgumentError, 'no user defined') if USERS.empty?
	raise(ArgumentError, 'no password defined') if PASSWORDS.empty?
	if ENABLES.empty?
		raise(ArgumentError, 'no enable password defined')
	end

	thread_concurrency = 64
	for i in 1..thread_concurrency do
		Thread.new do
			while true
				sw = @@unretrieved.dequeue
				begin
					yield sw
					sw.neighbor_gets
				rescue Errno::ECONNREFUSED,
				    Errno::ECONNRESET,
				    Errno::EPERM,
				    Timeout::Error => e
					msg = "WARNING: cannot " +
					    "retrieve #{sw.name} " +
					    "(#{sw.ia}): #{e}"
					puts msg
					@@warn.push(msg)
					@@unretrieved.error
				rescue => e
					msg = "WARNING: #{sw.name} " +
					    "(#{sw.ia}): #{e}"
					puts msg
					@@warn.push(msg)
				ensure
					@@unretrieved.done(sw)
				end
			end
		end
	end
	@@unretrieved.wait_all
	puts 'Finished!!'
	puts "Total: #{@@unretrieved.total}, Error: #{@@unretrieved.errors}"
end

.set_retrieve_allObject



370
371
372
# File 'lib/netutils/switch.rb', line 370

def self.set_retrieve_all
	@@retrieve_all = true
end

.warnObject



402
403
404
# File 'lib/netutils/switch.rb', line 402

def self.warn
	@@warn.each { |w| puts w }
end

Instance Method Details

#acl_add(*arg) ⇒ Object



285
286
287
# File 'lib/netutils/switch.rb', line 285

def acl_add(*arg)
	exec(__method__, *arg)
end

#acl_delete(*arg) ⇒ Object



289
290
291
# File 'lib/netutils/switch.rb', line 289

def acl_delete(*arg)
	exec(__method__, *arg)
end

#acl_exists?(*arg) ⇒ Boolean

Returns:

  • (Boolean)


281
282
283
# File 'lib/netutils/switch.rb', line 281

def acl_exists?(*arg)
	exec(__method__, *arg)
end

#arp_resolve(ia, vrf) ⇒ Object



251
252
253
# File 'lib/netutils/switch.rb', line 251

def arp_resolve(ia, vrf)
	exec(__method__, ia, vrf)
end

#cmd(*argv) ⇒ Object



206
207
208
# File 'lib/netutils/switch.rb', line 206

def cmd(*argv)
	@cli.cmd(*argv)
end

#config_dump(dir = 'conf') ⇒ Object



391
392
393
394
395
396
397
398
399
400
# File 'lib/netutils/switch.rb', line 391

def config_dump(dir = 'conf')
	begin
		Dir.mkdir(dir)
	rescue Errno::EEXIST
	end
	path = "#{dir}/#{@name}-#{@ia}.conf"
	f = File.open(path, 'w')
	f.puts @config
	f.close
end

#config_getObject



387
388
389
# File 'lib/netutils/switch.rb', line 387

def config_get
	@config = @cli.config_get
end

#configureObject



210
211
212
# File 'lib/netutils/switch.rb', line 210

def configure
	@cli.configure
end

#each_peerObject



374
375
376
# File 'lib/netutils/switch.rb', line 374

def each_peer
	@ports.each { |p| p.peers.each { |peer| yield peer } }
end

#if_dumpObject



378
379
380
381
# File 'lib/netutils/switch.rb', line 378

def if_dump
	puts "#{@name}: #{@platform}, #{@time}, #{@ia.to_s}"
	@ports.each { |p| p.dump('  ') }
end

#if_dump_csvObject



383
384
385
# File 'lib/netutils/switch.rb', line 383

def if_dump_csv
	@ports.each { |p| p.dump_csv(@ia.to_s + ',' + @name) }
end

#interface_getsObject



259
260
261
262
263
# File 'lib/netutils/switch.rb', line 259

def interface_gets
	raise "ERROR: cannot get interfaces twice" if @interface_got
	@interface_got = true
	exec(__method__, self)
end

#interface_name(name) ⇒ Object



265
266
267
# File 'lib/netutils/switch.rb', line 265

def interface_name(name)
	exec(__method__, self, name)
end

#interface_name_cli(name) ⇒ Object



269
270
271
# File 'lib/netutils/switch.rb', line 269

def interface_name_cli(name)
	exec(__method__, name)
end

#interface_noshutdown(port) ⇒ Object



277
278
279
# File 'lib/netutils/switch.rb', line 277

def interface_noshutdown(port)
	exec(__method__, port)
end

#interface_shutdown(port) ⇒ Object



273
274
275
# File 'lib/netutils/switch.rb', line 273

def interface_shutdown(port)
	exec(__method__, port)
end

#ip_address_set(ia) ⇒ Object



177
178
179
180
181
182
183
184
185
# File 'lib/netutils/switch.rb', line 177

def ip_address_set(ia)
	return if ! ia
	return if @ia
	@ia = ia
	case @type
	when Type::ROUTER, Type::SWITCH
		@@unretrieved.enqueue(self) if @retrieve
	end
end

#loginObject



187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/netutils/switch.rb', line 187

def 
	return if @cli
	@cli = CLI.new(@name, @ia)
	@cli.
	name_set(@cli.name) if ! @name
	#
	# retrieve interfaces because many commands require
	# interface name.
	# XXX: should improve this...
	#
	interface_gets
end

#logoutObject



200
201
202
203
204
# File 'lib/netutils/switch.rb', line 200

def logout
	return if ! @cli
	@cli.logout
	@cli = nil
end

#mac_address_table_get(ma, vlan) ⇒ Object



255
256
257
# File 'lib/netutils/switch.rb', line 255

def mac_address_table_get(ma, vlan)
	exec(__method__, self, ma, vlan)
end

#macaddr_resolve(ia) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/netutils/switch.rb', line 297

def macaddr_resolve(ia)
	vrf_gets.each do |name, vrf|
		arp = arp_resolve(ia, vrf)
		next if ! arp
		#
		# bark here if static ARP entry is found because
		# static ARP entry may be for this system itself.
		#
		if arp.static
			raise "ERROR: Static MAC address " +
			    "found for #{ia}"
		end
		return arp.ma, vrf, arp.interface
	end
	raise "No MAC address found for #{ia}"
end

#makerObject



223
224
225
# File 'lib/netutils/switch.rb', line 223

def maker
	@cli.maker
end

#maker_to_sObject



227
228
229
# File 'lib/netutils/switch.rb', line 227

def maker_to_s
	@cli.maker_to_s
end

#name_set(name) ⇒ Object



171
172
173
174
175
# File 'lib/netutils/switch.rb', line 171

def name_set(name)
	raise "Already name is set: #{@name}" if @name
	@name = name
	@@db[name] = self if name
end

#neighbor_gets(*arg) ⇒ Object



293
294
295
# File 'lib/netutils/switch.rb', line 293

def neighbor_gets(*arg)
	exec(__method__, self, *arg)
end

#productObject



231
232
233
# File 'lib/netutils/switch.rb', line 231

def product
	@cli.product
end

#promptObject



235
236
237
# File 'lib/netutils/switch.rb', line 235

def prompt
	@cli.prompt
end

#route_gets(ia) ⇒ Object



243
244
245
# File 'lib/netutils/switch.rb', line 243

def route_gets(ia)
	exec(__method__, ia)
end

#syslog(*arg) ⇒ Object



239
240
241
# File 'lib/netutils/switch.rb', line 239

def syslog(*arg)
	exec(__method__, *arg)
end

#unconfigureObject



214
215
216
# File 'lib/netutils/switch.rb', line 214

def unconfigure
	@cli.unconfigure
end

#vrf_getsObject



247
248
249
# File 'lib/netutils/switch.rb', line 247

def vrf_gets
	exec(__method__)
end