Class: MibBrowser

Inherits:
Object
  • Object
show all
Defined in:
lib/gui/mib_browser.rb

Overview

mib browser window class

Instance Method Summary collapse

Constructor Details

#initializeMibBrowser

show the main window of “mib browser” opinion in the menu “Tools”.# Here I replace the old gtk function with gtk widgets. # Dylan,Yin May18th,2009 #



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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
# File 'lib/gui/mib_browser.rb', line 187

def initialize()

@inthread=false
mibb = Gtk::Window.new
mibb.set_size_request(480,720)
mibb.set_title "MIB Browser"
mibb.resizable=true
mibb.window_position=Gtk::Window::POS_CENTER

frame = Gtk::Frame::new
frame.border_width=10
frame.show
mibb.add frame

icon_name="#{PIXMAP_PATH}/logo_icon.png"
if FileTest.exist?(icon_name)
	Gtk::Window.set_default_icon(icon_name)
end
mibb.show

mibb.signal_connect("delete_event") {
	if @inthread
		true
	else
		false
	end
}
mibb.signal_connect("destroy") {
		mibb.destroy
}
mibb.signal_connect("key_press_event") {|w,e|
	if !@inthread
		if e.keyval == 65307  #Esc key press	  
			mibb.destroy
		end
	end
}

vboxb = Gtk::VBox.new(false,5)
vboxb.show
frame.add vboxb
#vboxb.set_homogeneous(false) 

label_node = Gtk::Label.new
label_node.set_alignment(0,0.5)
label_node.width_chars=11
label_node.set_markup("<b><big>MIB File</big></b>",false)
label_node.show
@file_box = Gtk::ComboBox.new
@file_box.set_size_request(350,30)
#@file_box.append_text("UCD-DEMO-MIB")
#@file_box.append_text("SNMPv2-SMI")
@file_box.append_text("SNMPv2-MIB")
#@file_box.append_text("RFC1213-MIB")
#@file_box.append_text("RFC-1215")
#@file_box.append_text("RFC1155-SMI")
@file_box.set_active(0)
@file_box.show
@file_box.signal_connect("changed") {
  p="#{GNMSLIB}"+'/monitor/client/snmp/mibs/'
  fichier=p+@file_box.active_text+".txt"
  clear_tree=true
  create_tree(clear_tree,fichier)
}


hhboxL = Gtk::HBox.new(false,0)
hhboxL.show
hhboxL.pack_start_defaults(label_node)
hhboxL.pack_start_defaults(@file_box)

#vboxb.pack_start_defaults(hhboxL)
vboxb.add hhboxL

hhbox = Gtk::HBox.new
hhbox.show
vboxb.add hhbox

hbox1 = Gtk::HBox.new
hbox1.show


###################
#new added--by ydh#
###################
@treestore = Gtk::TreeStore.new(String,String,String,String,String)


@ctree = Gtk::TreeView.new(@treestore)
@ctree.selection.mode = Gtk::SELECTION_NONE
@ctree.enable_search=true
@ctree.enable_tree_lines=true

# Create a renderer
renderer = Gtk::CellRendererText.new

# Add column using our renderer
col = Gtk::TreeViewColumn.new("Label", renderer, :text => 2)	
@ctree.append_column(col)
@ctree.headers_visible=false
@ctree.show

###################
hbox1.add @ctree

scrolled_mibb = Gtk::ScrolledWindow.new(nil, nil)
scrolled_mibb.border_width=0
scrolled_mibb.set_policy(Gtk::POLICY_AUTOMATIC,Gtk::POLICY_AUTOMATIC)
scrolled_mibb.set_size_request(-1,270)
hhbox.pack_start_defaults(scrolled_mibb)
 
scrolled_mibb.add_with_viewport(hbox1)
scrolled_mibb.show

##############################
#handle the selected tree node
##############################
selection=@ctree.selection
selection.set_mode(Gtk::SELECTION_SINGLE)
selection.set_select_function do |selection, model, path, currently_selected|

  label=""
  oid=""
  object_type=""
  description_str=""
  #handle the leaf node
  iter = model.get_iter(path)
  if iter != nil
   label=iter[0]
   oid=iter[1]
   object_type=iter[3]
   description_str=iter[4]
  end
  #loop to get the oid and label
  while path.up!
  iter = model.get_iter(path)
  if iter != nil
   if iter[0] == "."
      #label="."+label
      #oid="."+oid
     break
   end
   label=iter[0]+"."+label
   oid=iter[1]+"."+oid
  else
     break
  end 

  end 

  if label != nil && oid != nil
  @label_entry.text=label
  @oid_entry.text=oid
  @objecttype_entry.text=object_type
  @description_entry.text=description_str
  
  end
  # allow selection state to change
  true
end

##create the first tree
p="#{GNMSLIB}"+'/monitor/client/snmp/mibs/'
fichier=p+@file_box.active_text+".txt"

clear_tree=true
create_tree(clear_tree,fichier)
####################################
#add labels and entries
####################################

objecttype_label = Gtk::Label.new("Object Type")
objecttype_label.set_alignment(0,0.5)
objecttype_label.width_chars=11
objecttype_label.show
@objecttype_entry = Gtk::Entry.new
@objecttype_entry.set_size_request(380,30)
@objecttype_entry.editable=false
@objecttype_entry.show

hhbox2 = Gtk::HBox.new(false,0)
hhbox2.show
hhbox2.pack_start_defaults(objecttype_label)
hhbox2.pack_start_defaults(@objecttype_entry)

oid_label = Gtk::Label.new("OID")
oid_label.set_alignment(0,0.5)
oid_label.width_chars=11
oid_label.show
@oid_entry = Gtk::Entry.new
@oid_entry.set_size_request(380,30)
@oid_entry.editable=false
@oid_entry.show

hhbox3 = Gtk::HBox.new(false,0)
hhbox3.show
hhbox3.pack_start_defaults(oid_label)
hhbox3.pack_start_defaults(@oid_entry)


label_label = Gtk::Label.new("Label")
label_label.set_alignment(0,0.5)
label_label.width_chars=11
label_label.show
@label_entry = Gtk::Entry.new
@label_entry.set_size_request(380,30)
@label_entry.editable=false
@label_entry.show

hhbox4 = Gtk::HBox.new(false,0)
hhbox4.show
hhbox4.pack_start_defaults(label_label)
hhbox4.pack_start_defaults(@label_entry)


description_label = Gtk::Label.new("Description")
description_label.set_alignment(0,0.5)
description_label.width_chars=11
description_label.show
@description_entry = Gtk::Entry.new
@description_entry.set_size_request(380,30)
@description_entry.editable=false
@description_entry.show

hhbox5 = Gtk::HBox.new(false,0)
hhbox5.show
hhbox5.pack_start_defaults(description_label)
hhbox5.pack_start_defaults(@description_entry)

host_label = Gtk::Label.new("Host")
host_label.set_alignment(0,0.5)
host_label.width_chars=11
host_label.show
@host_box = Gtk::ComboBox.new
@host_box.set_size_request(380,30)
$host.each_key {|ip|
	@host_box.append_text(ip)	
}
@host_box.set_active(0)
@host_box.show

hhbox6 = Gtk::HBox.new(false,0)
hhbox6.show
hhbox6.pack_start_defaults(host_label)
hhbox6.pack_start_defaults(@host_box)

#vboxb.pack_start_defaults(hhbox2)
#vboxb.pack_start_defaults(hhbox3)
#vboxb.pack_start_defaults(hhbox4)
#vboxb.pack_start_defaults(hhbox5)
#vboxb.pack_start_defaults(hhbox6)

#############################
#add "Get" and "Set" buttons
#############################

hhbox7=Gtk::HButtonBox.new
hhbox7.show
hhbox7.set_layout_style(Gtk::ButtonBox::END)

button_get=Gtk::Button.new("Get")
button_put=Gtk::Button.new("Set")
############################
#snmp get request
############################
button_get.signal_connect("clicked") {

	if @oid_entry.text != ""
		if (@objecttype_entry.text.index("SEQUENCE OF") == nil) && (@objecttype_entry.text != "unknown")
			$log.debug("get oid_entry = #{@objecttype_entry.text}")
			temp=@oid_entry.text+".0"

			Thread.start {
				@inthread=true
				button_get.set_sensitive(false)
				button_put.set_sensitive(false)
				puts "h1"
				response = get_snmp_OID($host[@host_box.active_text], temp)
				puts "end h1"
				@text_buffer.text=response.to_s unless !@text_buffer
				button_get.set_sensitive(true)
				button_put.set_sensitive(true)
				@inthread=false
			}				
		else
			temp=@oid_entry.text
			Thread.start {
				@inthread=true
				button_get.set_sensitive(false)
				button_put.set_sensitive(false)
				response = walk_snmp_OID($host[@host_box.active_text], temp)
				if response != "Error"
					@text_buffer.text=response.join("\n").ascii unless !@text_buffer
				else
					@text_buffer.text=response
				end
				button_get.set_sensitive(true)
				button_put.set_sensitive(true)
				@inthread=false
			}
		end
	end
}
button_get.show
hhbox7.add button_get
############################
#snmp set request
############################
button_put.signal_connect("clicked") {
	if (@text_buffer.text !=nil) && (@oid_entry.text != "")
		set_value=@text_buffer.text
		if @objecttype_entry.text.index("SEQUENCE OF") ==nil && @objecttype_entry.text!= "unknown"
			#print "oid_entry=#{@objecttype_entry.text}\n"
			temp=@oid_entry.text+".0"
		else
			temp=@oid_entry.text
		end
		#print "set_value=#{set_value}\n"
		Thread.start {
			@inthread=true
			button_get.set_sensitive(false)
			button_put.set_sensitive(false)
      				result=set_snmp($host[@host_box.active_text], temp, set_value)
			if result == "Error"
				@text_buffer.text=result
			end
			button_get.set_sensitive(true)
			button_put.set_sensitive(true)
			@inthread=false
		}
	end
}
button_put.show
hhbox7.add button_put

value_label = Gtk::Label.new("Value")
value_label.width_chars=15
value_label.show
value_label.set_alignment(0,0.5)

hhbox8 = Gtk::HBox.new(false,10)
hhbox8.show
hhbox8.add value_label



##############################
#add "value" text view
##############################
@text_buffer=Gtk::TextBuffer.new
@text_value  = Gtk::TextView.new(@text_buffer)
@text_value.set_size_request(-1,100)
@text_value.show

scrolled_window = Gtk::ScrolledWindow.new(nil, nil)
scrolled_window.border_width=8
scrolled_window.set_policy(Gtk::POLICY_AUTOMATIC,Gtk::POLICY_AUTOMATIC)  
  
scrolled_window.show
scrolled_window.add(@text_value)


###################################
#add the "Clear" and "Close" button
###################################

hhbox9=Gtk::HButtonBox.new
hhbox9.show

button_clear=Gtk::Button.new(Gtk::Stock::CLEAR)
button_clear.signal_connect("clicked") {
	@text_buffer.text=""
}
button_clear.show
hhbox9.add button_clear
button_close=Gtk::Button.new(Gtk::Stock::CLOSE)
button_close.signal_connect("clicked") {
	if !@inthread
		@treestore=nil
		mibb.destroy
	end
}
button_close.show
hhbox9.add button_close


vboxb.add hhbox2
vboxb.add hhbox3
vboxb.add hhbox4
vboxb.add hhbox5
vboxb.add hhbox6
vboxb.add hhbox7
vboxb.add hhbox8

vboxb.pack_start(scrolled_window, true, true, 0)
vboxb.add hhbox9

end

Instance Method Details

#add_node_to_tree(tstore, parent, child, value, otype_str, des_str) ⇒ Object

append a new node to a treestore



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
# File 'lib/gui/mib_browser.rb', line 9

def add_node_to_tree(tstore,parent,child,value,otype_str,des_str)
    
    flag=false
    
    tstore.each do |model, path, iter|
	
	if iter[0]== parent ####check if the same node has been added
	##add new node
	iter = model.get_iter(path)
	node= tstore.append(iter)
	node[0]=child
	node[1]=value
	node[2]=child+"("+value+")"
	node[3]=otype_str
	node[4]=des_str
	flag=true
	end
    end
    if !flag
	node= tstore.append(nil)
	node[0]="."
	node[1]="."
	node[2]="."
	node[3]="unknown"
	node[4]=""
	node= tstore.append(node)
	node[0]=parent
	node[1]="1"
	node[2]=parent+"(1)"
	node[3]="unknown"
	node[4]=""
	node= tstore.append(node)
	node[0]=child
	node[1]=value
	node[2]=child+"("+value+")"
	node[3]=otype_str
	node[4]=des_str
    end
end

#create_tree(flag, file) ⇒ Object

create and show the tree



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
# File 'lib/gui/mib_browser.rb', line 52

def create_tree(flag,file) 
	if flag
	@treestore.clear
	end
	

	if !FileTest.exist?(file)
  		$log.error("File #{file} doesnt exist")
  		return
	end

	fic = File.new(file,'r')
	lign=fic.gets().chomp();
	while lign != nil

		if lign.index("MACRO") != nil
  
  		  while lign.index("END") == nil
    			if lign.index("BEGIN") != nil
      				while lign.index("END") == nil
					lign=fic.gets()
      				end
    			end
    			lign=fic.gets()
  		  end
		end
	##get the import file and create tree for it
		if lign.index("IMPORTS") != nil
	          #print "*******************************************"
  		    while lign.index("FROM") == nil
    			lign=fic.gets()
  		    end
	            $_ = lign
	            temp= $_.gsub(/\s+/," ")
	            temp=temp.gsub(/\sFROM\s/,"")
	            temp=temp.split(/\ /)
	            if temp[0] !=nil
	                 p="#{GNMSLIB}"+'/monitor/client/snmp/mibs/'
	                fichier=p+temp[0]+".txt"
	                #print "temp=#{fichier}\n"
	               clear_tree=false
	               create_tree(clear_tree,fichier)
	            end                  



	        end
               #The handling to RFC1155-SMI is different with other file
               if file.index("RFC1155-SMI") !=nil
                    lign=lign.lstrip
               end
	if lign[0,1] >= 'a' && lign[0,1] <='z' && lign[0,1] != ' ' && lign[0,1] != '' && lign[0,1] != '\n' && lign[0,1] != '}' && lign[0,2] != '--' && lign.index("MACRO") == nil && lign.index("EXPORTS") == nil && lign.index("IMPORTS") == nil && lign.index("BEGIN") == nil && lign.index("END") == nil
  	tlign = lign.split(/ /)
	#print tlign,"\n"
  	nom=tlign[0].chomp()
  	#print "nom=#{nom}\n"
               object_type="unknown"
	description_str=""



	flag=false
  	while lign.index(" ::= {") == nil
    		lign=fic.gets()
    		if lign != nil
      			lign=lign.chomp
	           if flag
	              temp=lign.gsub(/\s+\"/,"")
	              temp=temp.gsub(/\.\"(.*)/,"")
	              temp=temp.gsub(/\s+/," ")
	              temp=temp.gsub(/::=(.*)/,"")
	              description_str=description_str+temp
	           end
	           if lign.index("SYNTAX") !=nil
	           lign.gsub(/SYNTAX\s*(\w+)/){|s|}
	           object_type=$1
	           if object_type == "SEQUENCE"
	                  if lign.index("SEQUENCE OF") !=nil
	                  lign.gsub(/SEQUENCE OF (\w+)/){|s|}
	                  object_type="SEQUENCE OF "+$1
	                  end
	           end
	           end
	           if lign.index("DESCRIPTION") !=nil
	             flag=true
	           end
    		end
  	end
	$_ = lign
	temp=$_.gsub(/.*\{ /,"")
	temp=temp.gsub(/\}.*/,"")
	older, oid = temp.split(/\ /)
	#print "older= #{older},oid= #{oid}\n"
               
               #consture the tree for "RFC1155-SMI"

               if nom == "internet," && older =="iso"
                   if file.index("RFC1155-SMI") !=nil
                       nom="internet"
                       older1="dod"
                       oid1="6"
                       older2="org"
                       oid2="3"
                       older3="iso"
                       oid="1"
                       #oid3="1"
                       add_node_to_tree(@treestore,older3,older2,oid2,object_type,description_str)
                       add_node_to_tree(@treestore,older2,older1,oid1,object_type,description_str)
                       add_node_to_tree(@treestore,older1,nom,oid,object_type,description_str)
                       
                       lign=fic.gets()
			if lign != nil
		  		lign = lign.chomp
			end
                       next
                     end
               end

	add_node_to_tree(@treestore,older,nom,oid,object_type,description_str)
	end #end if
	lign=fic.gets()
	if lign != nil
  		lign = lign.chomp
	end
	end # end while
	fic.close()
	
end