Class: Zabbirc::Zabbix::Maintenance

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

Constant Summary collapse

CACHE_TTL =
1.minute

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

.cached(timestamp = nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/zabbirc/zabbix/maintenance.rb', line 52

def self.cached timestamp = nil
  if @maintenances_timestamp and @maintenances_timestamp < CACHE_TTL.ago
    @maintenances, @maintenances_timestamp = nil, nil
  end
  @maintenances_timestamp ||= Time.current
  @maintenances ||= Maintenance.get(selectHosts: :extend, selectGroups: :extend)

  if timestamp
    @maintenances.select{|maintenance| maintenance.active_range.cover?(timestamp) }
  else
    @maintenances
  end
end

.create(*options) ⇒ Object



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
48
49
50
# File 'lib/zabbirc/zabbix/maintenance.rb', line 16

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

  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,
      maintenance_type: (options[:without_data_collection] ? 1 : 0),
      timeperiods: [
          {
              timeperiod_type: 0,
              start_time: maint_start.to_i,
              period: duration
          }
      ]
  )

  r["maintenanceids"].first
end

.find(id, *options) ⇒ Object



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

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)


82
83
84
# File 'lib/zabbirc/zabbix/maintenance.rb', line 82

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

#active_rangeObject



78
79
80
# File 'lib/zabbirc/zabbix/maintenance.rb', line 78

def active_range
  active_since..active_till
end

#active_sinceObject



70
71
72
# File 'lib/zabbirc/zabbix/maintenance.rb', line 70

def active_since
  Time.at(super.to_i)
end

#active_tillObject



74
75
76
# File 'lib/zabbirc/zabbix/maintenance.rb', line 74

def active_till
  Time.at(super.to_i)
end

#data_collection_labelObject



86
87
88
# File 'lib/zabbirc/zabbix/maintenance.rb', line 86

def data_collection_label
  " $C,ORANGE$[NO-DATA-COL]$C,RST$" if maintenance_type.to_i == 1
end

#destroyObject



114
115
116
# File 'lib/zabbirc/zabbix/maintenance.rb', line 114

def destroy
  api.maintenance.delete [id]
end

#format_label(fmt) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/zabbirc/zabbix/maintenance.rb', line 94

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}").
      gsub("%data-collection-label", "#{data_collection_label}")
end

#labelObject



90
91
92
# File 'lib/zabbirc/zabbix/maintenance.rb', line 90

def label
  format_label "$C,PURPLE$|%sid|$C,RST$%data-collection-label %start $C,RED$->$C,RST$ %end $C,RED$>>$C,RST$ %name $C,ORANGE$%targets$C,RST$"
end

#shorten_idObject



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

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

#target_labelsObject



104
105
106
107
108
109
110
111
112
# File 'lib/zabbirc/zabbix/maintenance.rb', line 104

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