Class: Wakame::Monitor::Service

Inherits:
Object
  • Object
show all
Includes:
Wakame::Monitor
Defined in:
lib/wakame/monitor/service.rb

Defined Under Namespace

Classes: CommandChecker, PidFileChecker, ServiceChecker

Constant Summary

Constants included from Wakame::Monitor

STATUS_FAIL, STATUS_OFFLINE, STATUS_ONLINE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Wakame::Monitor

#disable, #enable, included, #publish_to

Constructor Details

#initializeService

Returns a new instance of Service.



130
131
132
133
# File 'lib/wakame/monitor/service.rb', line 130

def initialize
  @status = Wakame::Service::STATUS_ONLINE
  @checkers = {}
end

Instance Attribute Details

#checkersObject (readonly)

Returns the value of attribute checkers.



128
129
130
# File 'lib/wakame/monitor/service.rb', line 128

def checkers
  @checkers
end

Instance Method Details

#check_status(svc_id) ⇒ Object



171
172
173
174
175
176
177
178
# File 'lib/wakame/monitor/service.rb', line 171

def check_status(svc_id)
  chk = @checkers[svc_id]
  if chk
    chk.status
  else
    raise RuntimeError, "#{self.class}: Specified service id was not found: #{svc_id}"
  end
end

#find_checker(svc_id) ⇒ Object



140
141
142
# File 'lib/wakame/monitor/service.rb', line 140

def find_checker(svc_id)
  @checkers[svc_id]
end

#register(svc_id, checker_type, *args) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/wakame/monitor/service.rb', line 144

def register(svc_id, checker_type, *args)
  chk = @checkers[svc_id]
  if chk
    unregister(svc_id)
  end
  case checker_type.to_sym
  when :pidfile
    chk = PidFileChecker.new(svc_id, self, args[0], args[1])
  when :command
    chk = CommandChecker.new(svc_id, self, args[0], args[1])
  else
    raise "Unsupported checker type: #{checker_type}"
  end
  chk.start
  @checkers[svc_id]=chk
  Wakame.log.info("#{self.class}: Registered service checker for #{svc_id}")
end

#reload(config) ⇒ Object

Reloading the service monitor with new configuration.

Example of Hash data in config: config =

'svc_id_abcde'=>{:type=>:pidfile, :path=>'/var/run/a.pid', :interval=>5,
'svc_id_12345'=>:cmdline=>'/bin/ps -ef | grep processname', :interval=>10

}



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
# File 'lib/wakame/monitor/service.rb', line 193

def reload(config)
  unregister_all

  reg_single = proc { |svc_id, data|
    data[:interval] ||= 5

    case data[:type]
    when :pidfile
      register(svc_id, data[:type], data[:path], data[:interval])
    when :command
      register(svc_id, data[:type], data[:cmdline], data[:interval])
    else
      raise "Unsupported checker type: #{data[:type]}"
    end
  }
  
  config.each { |svc_id, data|
    if data.is_a?(Array)
      # TODO: Multiple monitors for single service ID
      raise "TODO: Multiple monitors for single service ID"
      data.each { |d|
        reg_single.call(svc_id, d)
      }
    elsif data.is_a?(Hash)
      reg_single.call(svc_id, data)
    else
    end
  }
end

#send_event(a) ⇒ Object



135
136
137
138
# File 'lib/wakame/monitor/service.rb', line 135

def send_event(a)
  #Wakame.log.debug("Sending back the event: #{a.class}")
  publish_to('agent_event', a.marshal)
end

#unregister(svc_id) ⇒ Object



162
163
164
165
166
167
168
169
# File 'lib/wakame/monitor/service.rb', line 162

def unregister(svc_id)
  chk = @checkers[svc_id]
  if chk
    chk.stop
    @checkers.delete(svc_id)
    Wakame.log.info("#{self.class}: Unregistered service checker for #{svc_id}")
  end
end

#unregister_allObject



180
181
182
183
184
# File 'lib/wakame/monitor/service.rb', line 180

def unregister_all
  @checkers.keys.each { |svc_id|
    unregister(svc_id)
  }
end