Module: Nagios::Gearman::Downtime

Defined in:
lib/nagios-gearman-downtime.rb,
lib/nagios-gearman-downtime/version.rb

Constant Summary collapse

VERSION =
"1.0.4"

Class Method Summary collapse

Class Method Details

.aes256_encrypt(key, data) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/nagios-gearman-downtime.rb', line 75

def self.aes256_encrypt(key, data)
  key = null_padding(key)
  aes = OpenSSL::Cipher.new('AES-256-ECB')
  aes.encrypt
  aes.key = key
  aes.update(data) + aes.final
end

.build_payload(args) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/nagios-gearman-downtime.rb', line 38

def self.build_payload(args)
  object_info = create_object_type(args.delete(:object_type), args.delete(:object_name))

  if object_info.match(/enable|disable/i)
    common_args = ''
  else
    common_args = ";#{args.delete(:start_time)};#{args.delete(:end_time)};#{args.delete(:fixed)};#{args.delete(:trigger_id)};#{args.delete(:duration)};#{args.delete(:author)};#{args.delete(:comment)}"
  end
  
  payload = "[#{Time.now.to_i}] #{object_info}#{common_args}"
  logger.info "payload before encryption: #{payload}"

  if args.delete(:encryption)
    begin
      payload = aes256_encrypt(args.delete(:key), payload)
    rescue Exception => e
      logger.error "unable to encrypt payload: #{e}"
    end
  end
  payload
end

.create_object_type(obj, obj_name) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/nagios-gearman-downtime.rb', line 14

def self.create_object_type(obj, obj_name)
  case obj
  when 'host' 
    object_info = "SCHEDULE_HOST_SVC_DOWNTIME;#{obj_name}"
  when 'hostgroup_host' 
    object_info = "SCHEDULE_HOSTGROUP_HOST_DOWNTIME;#{obj_name}"
  when 'hostgroup_svc' 
    object_info = "SCHEDULE_HOSTGROUP_SVC_DOWNTIME;#{obj_name}"
  when 'servicegroup_host' 
    object_info = "SCHEDULE_SERVICEGROUP_HOST_DOWNTIME;#{obj_name}"
  when 'servicegroup_svc' 
    object_info = "SCHEDULE_SERVICEGROUP_SVC_DOWNTIME;#{obj_name}"
  when 'service' 
    object_info = "SCHEDULE_HOSTGROUP_HOST_DOWNTIME;#{obj_name}"
  when 'enable_servicegroup'
    object_info = "ENABLE_SERVICEGROUP_SVC_NOTIFICATIONS;#{obj_name}"
  when 'disable_servicegroup'
    object_info = "DISABLE_SERVICEGROUP_SVC_NOTIFICATIONS;#{obj_name}"
  end
  logger.info "object_info: #{object_info}"
  object_info
end

.loggerObject



7
8
9
10
11
# File 'lib/nagios-gearman-downtime.rb', line 7

def self.logger
  @@logger = Logger.new(STDOUT)
  @@logger.level = Logger::DEBUG
  @@logger        
end

.null_padding(key) ⇒ Object



83
84
85
86
# File 'lib/nagios-gearman-downtime.rb', line 83

def self.null_padding(key)
  padding = (32 - key.bytesize) if(key.kind_of?(String) && 32 != key.bytesize)
  key += "\0" * padding
end

.send_external_cmd(options) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/nagios-gearman-downtime.rb', line 60

def self.send_external_cmd(options)
  logger.info "send_external_cmd options: #{options}"
  client  = ::Gearman::Client.new(options[:gearman_job_server])
  taskset = ::Gearman::TaskSet.new(client)
  encoded_job = Base64.encode64(options[:job])
  task = ::Gearman::Task.new(options[:queue], encoded_job) 
  begin
    logger.info "[client] Sending task: #{task.inspect}"
    result = taskset.add_task(task)
    logger.info "result: #{result}"
  rescue Exception => e
    logger.error "Send external command failed: #{e}"
  end
end