Class: Roma::Watch::Main

Inherits:
Object
  • Object
show all
Defined in:
lib/roma/tools/roma_watcher.rb

Overview

Mailer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Main

Returns a new instance of Main.



54
55
56
57
58
59
60
61
# File 'lib/roma/tools/roma_watcher.rb', line 54

def initialize config
  @conf = config
  @log = Logger.new @conf['log']['path'], @conf['log']['rotate']
  @nodelist_inf = {}
  @errors = {}
  @subject_prefix = @conf['mail']['subject_prefix']
  @mailer = Mailer.new @conf['mail']['from'], @conf['mail']['to'], @conf['mail']['mailer']
end

Instance Attribute Details

#confObject (readonly)

Returns the value of attribute conf.



48
49
50
# File 'lib/roma/tools/roma_watcher.rb', line 48

def conf
  @conf
end

#errorsObject (readonly)

Returns the value of attribute errors.



51
52
53
# File 'lib/roma/tools/roma_watcher.rb', line 51

def errors
  @errors
end

#logObject (readonly)

Returns the value of attribute log.



49
50
51
# File 'lib/roma/tools/roma_watcher.rb', line 49

def log
  @log
end

#mailerObject (readonly)

Returns the value of attribute mailer.



52
53
54
# File 'lib/roma/tools/roma_watcher.rb', line 52

def mailer
  @mailer
end

#nodelist_infObject (readonly)

Returns the value of attribute nodelist_inf.



50
51
52
# File 'lib/roma/tools/roma_watcher.rb', line 50

def nodelist_inf
  @nodelist_inf
end

Instance Method Details

#check_nodesObject



107
108
109
110
# File 'lib/roma/tools/roma_watcher.rb', line 107

def check_nodes
  check_vital
  check_splitbrain
end

#check_splitbrainObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/roma/tools/roma_watcher.rb', line 120

def check_splitbrain
  @log.debug "start checking a splitbrain"
  all_ring = []
  @nodelist_inf.each { |node, ring|
    all_ring << ring unless all_ring.include? ring
  }

  if all_ring.size != 1
    emsg = ""
    all_ring.each { |ring|
      emsg += "#{ring.join(',')}\r\n"
    }
    @mailer.send_mail(@subject_prefix + Message::ERROR_SPLIT_BRAIN, emsg)
  end
  @log.debug "end checking a splitbrain"
end

#check_vitalObject



112
113
114
115
116
117
118
# File 'lib/roma/tools/roma_watcher.rb', line 112

def check_vital
  @log.debug "start checking the vital"
  @errors.each { |node, emsg|
    @mailer.send_mail(@subject_prefix + Message::ERROR_NODE_DOWN, emsg)
  }
  @log.debug "end checking the vital"
end

#watchObject



63
64
65
66
67
68
69
70
# File 'lib/roma/tools/roma_watcher.rb', line 63

def watch
  @log.info "start watching a ROMA"
  watch_nodes
  @log.info "end watching"
  @log.info "start checking a ROMA"
  check_nodes
  @log.info "end checking"
end

#watch_node(node) ⇒ Object



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
# File 'lib/roma/tools/roma_watcher.rb', line 79

def watch_node node
  @log.debug "start watching a node: #{node}"
  host, port = node.split(':')
  sock = nil
  begin
    timeout(@conf['timeout'].to_i) {
      line = nil
      TCPSocket.open(host, port) do |sock|
        sock.puts Message::COMMAND_NODELIST
        line = sock.gets.chomp!
        sock.puts Message::COMMAND_QUIT
      end
      @log.debug "end watching a node: #{node}"
      line.split(' ')
    }
  rescue Exception => e
    emsg = "Catch an error when checking a node #{node}: #{e.to_s}"
    @log.error emsg
    if (cnt ||= 0; cnt += 1) < @conf['retry']['count'].to_i
      @log.info "retry: #{cnt} times"
      sleep @conf['retry']['period'].to_i
      retry
    end
    @errors[node] = emsg
    nil
  end
end

#watch_nodesObject



72
73
74
75
76
77
# File 'lib/roma/tools/roma_watcher.rb', line 72

def watch_nodes
  @conf['roma'].each { |node|
    nodes = watch_node node
    @nodelist_inf[node] = nodes if nodes
  }
end