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) ⇒ Switch

Returns a new instance of Switch.



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

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

	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

#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) ⇒ Object



311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/netutils/switch.rb', line 311

def self.get(name, type, platform = nil, firmware = nil, time = nil,
    ia = 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)
	end
	return sw
end

.retrieveObject

Raises:

  • (ArgumentError)


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

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



367
368
369
# File 'lib/netutils/switch.rb', line 367

def self.set_retrieve_all
	@@retrieve_all = true
end

.warnObject



399
400
401
# File 'lib/netutils/switch.rb', line 399

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

Instance Method Details

#acl_add(*arg) ⇒ Object



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

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

#acl_delete(*arg) ⇒ Object



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

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

#acl_exists?(*arg) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#arp_resolve(ia, vrf) ⇒ Object



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

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

#cmd(*argv) ⇒ Object



203
204
205
# File 'lib/netutils/switch.rb', line 203

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

#config_dump(dir = 'conf') ⇒ Object



388
389
390
391
392
393
394
395
396
397
# File 'lib/netutils/switch.rb', line 388

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



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

def config_get
	@config = @cli.config_get
end

#configureObject



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

def configure
	@cli.configure
end

#each_peerObject



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

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

#if_dumpObject



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

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

#if_dump_csvObject



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

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

#interface_getsObject



256
257
258
259
260
# File 'lib/netutils/switch.rb', line 256

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

#interface_name(name) ⇒ Object



262
263
264
# File 'lib/netutils/switch.rb', line 262

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

#interface_name_cli(name) ⇒ Object



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

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

#interface_noshutdown(port) ⇒ Object



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

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

#interface_shutdown(port) ⇒ Object



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

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

#ip_address_set(ia) ⇒ Object



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

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



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

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

#logoutObject



197
198
199
200
201
# File 'lib/netutils/switch.rb', line 197

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

#mac_address_table_get(ma, vlan) ⇒ Object



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

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

#macaddr_resolve(ia) ⇒ Object



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

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



220
221
222
# File 'lib/netutils/switch.rb', line 220

def maker
	@cli.maker
end

#maker_to_sObject



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

def maker_to_s
	@cli.maker_to_s
end

#name_set(name) ⇒ Object



169
170
171
172
173
# File 'lib/netutils/switch.rb', line 169

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

#neighbor_gets(*arg) ⇒ Object



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

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

#productObject



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

def product
	@cli.product
end

#promptObject



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

def prompt
	@cli.prompt
end

#route_gets(ia) ⇒ Object



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

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

#syslog(*arg) ⇒ Object



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

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

#unconfigureObject



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

def unconfigure
	@cli.unconfigure
end

#vrf_getsObject



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

def vrf_gets
	exec(__method__)
end