Masato Taruishi
taru****@sourc*****
2004年 6月 23日 (水) 15:06:08 JST
=================================================================== RCS file: demo/haviewer/haviewer.rb,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- demo/haviewer/haviewer.rb 2004/06/22 18:58:47 1.8 +++ demo/haviewer/haviewer.rb 2004/06/23 06:06:08 1.9 @@ -67,50 +67,32 @@ end class Host - def initialize( host, type, viewer ) + def initialize( host, type ) super(); @host = host; - @viewer = viewer - @list = viewer.list - @row =****@list***** @status = "" @sign = Signal.new @type = type - update - @t = Thread.new { - while(1) do - snmp - update - @viewer.update - sleep 10 - end - } - end - - def update - @list.set_value(@row, 0, @sign.to_s) - @list.set_value(@row, 1, @host) - @list.set_value(@row, 2, @type) - @list.set_value(@row, 3, @status) - @list.set_value(@row, 4, @pid) - @list.set_value(@row, 5, @since) end def snmp - @status = "-----" - @pid = "-----" - @since = "-----" + status = "-----" + pid = "-----" + since = "-----" buf = `snmpget -v1 -c ultrapossum #{@host} #{OID_STATUS} #{OID_PID} #{OID_SINCE}` buf.each_line do |line| case line when /#{OID_STATUS}/ - @status = line.gsub(/^[^"]+"(.+)"\n/, '\1') + status = line.gsub(/^[^"]+"(.+)"\n/, '\1') when /#{OID_PID}/ - @pid = line.gsub(/^[^"]+"(.+)"\n/, '\1') + pid = line.gsub(/^[^"]+"(.+)"\n/, '\1') when /#{OID_SINCE}/ - @since = line.gsub(/^[^"]+"(.+)"\n/, '\1') + since = line.gsub(/^[^"]+"(.+)"\n/, '\1') end end + @status = status + @pid = pid + @since = since c = "" case @status @@ -129,84 +111,140 @@ end - attr_reader :row, :host + attr_reader :row, :host, :sign attr_accessor :type, :status, :pid, :since end + module View + + class UltraPossum + + def initialize + @list = Gtk::ListStore.new( String, String, String, String, String, String) + @columns = [] + Column.size.times do |i| + @columns << Gtk::TreeViewColumn.new(Column[i], Gtk::CellRendererText.new, { :markup => i }) + end + h = Host.new(`ultrapossum-config get MASTER`.gsub!(/MASTER="(.+)"\n/, '\1'), "master" ) + Thread.new { + while(1) do + h.snmp + sleep 10 + end + } + @row = {} + @row[h] =****@list***** + Hosts << h + backup = `ultrapossum-config get BACKUP`.gsub!(/BACKUP="(.+)"\n/, '\1') + `ultrapossum-config get SLAVES`.gsub!(/SLAVES="(.*)"\n/, '\1').split(' ').each do |s| + h = Host.new(s.chomp, (backup==s)?"master":"slave" ) + @row[h] =****@list***** + Hosts << h + Thread.new { + while(1) do + h.snmp + sleep 10 + end + } + end + end + + def update + @row.each do |host,row| + @list.set_value(row, 0, host.sign.to_s) + @list.set_value(row, 1, host.host) + @list.set_value(row, 2, host.type) + @list.set_value(row, 3, host.status) + @list.set_value(row, 4, host.pid) + @list.set_value(row, 5, host.since) + end + end + + def summary + master = "" + slave = "" + Hosts.each do |h| + if h.type == "master" + case h.status + when "ACTIVE" + master = Green + when "STANDBY" + slave = Green + when "FAOVER" + master = Yellow + when "STOP" + master = Red if master != Green && master != Yellow + when "DEAD" + master = Red if master != Green && master != Yellow + end + else + case h.status + when "RUN" + slave = Green + when "ACTIVE" + slave = Green + when "STOP" + slave = Red if slave != Green + when "DEAD" + slave = Red if slave != Green + end + end + end + return { "Master" => master, "Slave" => slave } + end + + attr_reader :list, :columns - def initialize(glade) - @glade = @glade = GladeXML.new(glade) {|handler| method(handler)} - @list = Gtk::ListStore.new( String, String, String, String, String, String) - @columns = [] - Column.size.times do |i| - @columns << Gtk::TreeViewColumn.new(Column[i], Gtk::CellRendererText.new, { :markup => i }) # {:text => i, :markup => 1}) - end - @columns.each do |val| - @glade["treeview1"].append_column(val) end - @glade["treeview1"].set_model(@list) + end + + + def initialize(glade) + @glade = @glade = GladeXML.new(glade) {|handler| method(handler)} + @treeview = @glade["treeview1"] @master_icon = Signal.new @slave_icon = Signal.new - + @view = nil update + end - h = Host.new(`ultrapossum-config get MASTER`.gsub!(/MASTER="(.+)"\n/, '\1'), "master", self) - Hosts << h - backup = `ultrapossum-config get BACKUP`.gsub!(/BACKUP="(.+)"\n/, '\1') - `ultrapossum-config get SLAVES`.gsub!(/SLAVES="(.*)"\n/, '\1').split(' ').each do |s| - h = Host.new(s.chomp, (backup==s)?"master":"slave", self) - Hosts << h + def set_view(view) + view.columns.each do |val| + @treeview.append_column(val) end - + @treeview.set_model(view.list) + @view = view end def update @glade["time"].set_text(`date | iconv --to UTF-8`.chomp!) @glade["uptime"].set_text(`uptime`.chomp!) + @view.update if @view + summary + end - master_icon = "" - slave_icon = "" - Hosts.each do |h| - if h.type == "master" - case h.status - when "ACTIVE" - master_icon = Green - when "STANDBY" - slave_icon = Green - when "FAOVER" - master_icon = Yellow - when "STOP" - master_icon = Red if master_icon != Green && master_icon != Yellow - when "DEAD" - master_icon = Red if master_icon != Green && master_icon != Yellow - end - else - case h.status - when "RUN" - slave_icon = Green - when "ACTIVE" - slave_icon = Green - when "STOP" - slave_icon = Red if slave_icon != Green - when "DEAD" - slave_icon = Red if slave_icon != Green - end - end - end - @master_icon.on(master_icon) - @slave_icon.on(slave_icon) + def summary + s = {} + s =****@view***** if @view + @master_icon.on(s["Master"]) + @slave_icon.on(s["Slave"]) @glade["master_icon"].set_markup(@master_icon.to_s) @glade["slave_icon"].set_markup(@slave_icon.to_s) - end - attr_reader :list + attr_reader :view end Gtk.init -HAViewer.new('haviewer.glade') + +h = HAViewer.new('haviewer.glade') +h.set_view( HAViewer::View::UltraPossum.new ) +Gtk.timeout_add(5000) { + h.update + true +} Gtk.main