Class: Zabbirc::Zabbix::Maintenance

Inherits:
Resource::Base show all
Defined in:
lib/zabbirc/zabbix/maintenance.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource::Base

#[], api, #api, #id, id_attr_name, #initialize, #method_missing, model_name, set_id_attr_name, set_model_name

Methods included from Resource::Associations

#has_many, #has_one

Methods included from Resource::Finders

#find, #get

Constructor Details

This class inherits a constructor from Zabbirc::Zabbix::Resource::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Zabbirc::Zabbix::Resource::Base

Class Method Details

.create(*options) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/zabbirc/zabbix/maintenance.rb', line 15

def self.create *options
  default_options = {
      host_ids: [],
      host_group_ids: []
  }

  options = options.extract_options!
  options = options.reverse_merge(default_options)

  duration = options[:duration]
  name = options[:name]
  host_ids = options[:host_ids]
  host_group_ids = options[:host_group_ids]
  maint_start = Time.current
  maint_end = maint_start + duration

  r = api.maintenance.create(
      name: name,
      active_since: maint_start.to_i,
      active_till: maint_end.to_i,
      hostids: host_ids,
      groupids: host_group_ids,
      timeperiods: [
          {
              timeperiod_type: 0,
              start_time: maint_start.to_i,
              period: duration
          }
      ]
  )

  r["maintenanceids"].first
end

.find(id, *options) ⇒ Object



8
9
10
11
12
13
# File 'lib/zabbirc/zabbix/maintenance.rb', line 8

def self.find id, *options
  options = options.extract_options!
  options[:selectHosts] = :extend
  options[:selectGroups] = :extend
  super(id, options)
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/zabbirc/zabbix/maintenance.rb', line 61

def active?
  (active_since..active_till).cover? Time.current
end

#active_sinceObject



53
54
55
# File 'lib/zabbirc/zabbix/maintenance.rb', line 53

def active_since
  Time.at(super.to_i)
end

#active_tillObject



57
58
59
# File 'lib/zabbirc/zabbix/maintenance.rb', line 57

def active_till
  Time.at(super.to_i)
end

#destroyObject



88
89
90
# File 'lib/zabbirc/zabbix/maintenance.rb', line 88

def destroy
  api.maintenance.delete [id]
end

#format_label(fmt) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/zabbirc/zabbix/maintenance.rb', line 69

def format_label fmt
  fmt.gsub("%start", "#{active_since.to_formatted_s(:short)}").
      gsub("%end", "#{active_till.to_formatted_s(:short)}").
      gsub("%name", "#{name}").
      gsub("%id", "#{id}").
      gsub("%sid", "#{shorten_id}").
      gsub("%targets", "#{target_labels}")
end

#labelObject



65
66
67
# File 'lib/zabbirc/zabbix/maintenance.rb', line 65

def label
  format_label "|%sid| %start -> %end >> %name %targets"
end

#shorten_idObject



49
50
51
# File 'lib/zabbirc/zabbix/maintenance.rb', line 49

def shorten_id
  @shorten_id ||= Zabbirc.maintenances_id_shortener.get_shorten_id id
end

#target_labelsObject



78
79
80
81
82
83
84
85
86
# File 'lib/zabbirc/zabbix/maintenance.rb', line 78

def target_labels
  host_names = hosts.collect(&:name)
  group_names = groups.collect(&:name)

  r = ""
  r << "Hosts: #{host_names.join(", ")}" if host_names.present?
  r << "Host Groups: #{group_names.join(", ")}" if group_names.present?
  r
end