Class: Taskr::Models::LogEntry

Inherits:
Base
  • Object
show all
Defined in:
lib/taskr/models.rb

Defined Under Namespace

Classes: ActionLogger

Class Method Summary collapse

Class Method Details

.log(level, action_or_task, data) ⇒ Object



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
# File 'lib/taskr/models.rb', line 234

def log(level, action_or_task, data)
  level = level.upcase
  if action_or_task.kind_of? TaskAction
    action = action_or_task
    task = action.task
  elsif action_or_task.kind_of? Task
    action = nil
    task = action_or_task
  elsif action_or_task.kind_of? Integer
    action = TaskAction.find(action_or_task)
    task = action.task
  elsif action_or_task.kind_of? Taskr::Actions::Base
    action = action_or_task.task_action
    task = action.task
  else
    raise ArgumentError, "#{action_or_task.inspect} is not a valid Task or TaskAction!"
  end
  
  threshold = Taskr::Conf[:task_log][:level].upcase if Taskr::Conf[:task_log] && Taskr::Conf[:task_log][:level]
  
  if threshold.blank? || 
      ['DEBUG', 'INFO', 'WARN', 'ERROR'].index(threshold) <= ['DEBUG', 'INFO', 'WARN', 'ERROR'].index(level) 
    LogEntry.create(
      :level => level,
      :timestamp => Time.now,
      :task => task,
      :task_action => action,
      :data => data
    )
  end
  
  # Send SNMP traps through net-snmp if Taskr is configured to send them.
  if Taskr::Conf[:snmp] && Taskr::Conf[:snmp][:send_traps]
    send_snmp_trap(level, task, action, data)
  end
end

.logger_for_action(action) ⇒ Object

Produces a Logger-like class that will create log entries for the given TaskAction. The returned object exploses behaviour much like a standard Ruby Logger, so that it can be used in place of a Logger when necessary.



274
275
276
# File 'lib/taskr/models.rb', line 274

def logger_for_action(action)
  ActionLogger.new(action)
end

.send_snmp_trap(level, task, action, data) ⇒ Object



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
# File 'lib/taskr/models.rb', line 284

def send_snmp_trap(level, task, action, data)
  snmp_community = Taskr::Conf[:snmp][:community]
  enterprise_oid = Taskr::Conf[:snmp][:enterprise_oid] || '1.3.6.1.4.1.55555.7007'
  to_host        = Taskr::Conf[:snmp][:to_host]
  snmp_persistent_dir = Taskr::Conf[:snmp][:snmp_persistent_dir] || '/tmp'
  my_host        = ENV['HOSTNAME'] || `hostname`.strip || 'taskr'
  
  
  task_oid = '1.3.6.1.4.1.55555.7007.1'
  task_typ = 's'
  task_val = task.to_s.gsub(/"/, '\"')
  
  
  # see http://www.oid-info.com/get/1.3.6.1.4.1.9.5.1.14.4.1.2
  level_oid = '1.3.6.1.4.1.9.5.1.14.4.1.2'
  level_typ = 'i'
  case level
  when 'DEBUG'  then level_val = 8
  when 'INFO'   then level_val = 7
  when 'WARN'   then level_val = 5
  when 'ERROR'  then level_val = 4
  end

  # see http://www.oid-info.com/get/1.3.6.1.4.1.9.9.41.1.2.3.1.4
  sevr_oid = '1.3.6.1.4.1.9.9.41.1.2.3.1.4'
  sevr_typ = 's'
  sevr_val = level
  
  # TODO: make msg format configurable
  msg = ("Taskr #{level} on task #{task}[#{action}]: #{data}").gsub(/"/, '\"')
  
  # see http://www.oid-info.com/get/1.3.6.1.4.1.9.9.41.1.2.3.1.5
  msg_oid = '1.3.6.1.4.1.9.9.41.1.2.3.1.5'
  msg_typ = 's'
  msg_val = msg
  
  cmd = %{snmptrap -v 1 -c #{snmp_community} #{to_host} #{enterprise_oid} #{my_host} 6 #{level_val} '' \
#{level_oid} #{level_typ} "#{level_val}" \
#{sevr_oid} #{sevr_typ} "#{sevr_val}" \
#{task_oid} #{task_typ} "#{task_val}" \
#{msg_oid} #{msg_typ} "#{msg_val}"}
  
  # Band-aid fix for bug in Net-SNMP.
  # See http://sourceforge.net/tracker/index.php?func=detail&aid=1588455&group_id=12694&atid=112694
  ENV['SNMP_PERSISTENT_DIR'] ||= snmp_persistent_dir
  
  $LOG.debug "SENDING SNMP TRAP: #{cmd}"
  
  `#{cmd}`
end