Class: ZabbixMaintenance

Inherits:
Object
  • Object
show all
Defined in:
lib/zabbix_maintenance.rb

Overview

Communicates with Zabbix through API and performs actions on maintenance objects.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, user, password, title: 'capistrano auto maintenance') ⇒ ZabbixMaintenance

Returns a new instance of ZabbixMaintenance.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/zabbix_maintenance.rb', line 8

def initialize(url, user, password, title: 'capistrano auto maintenance')
  begin
    @zbx = ZabbixApi.connect(url: url, user: user, password: password)
  rescue RuntimeError => e
    if e.message =~ /password is incorrect/
      fail 'Login failed - incorrect password.'
    else
      fail "Error while connecting to Zabbix: #{e}"
    end
  end

  @maint_title = title
  @id = nil
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/zabbix_maintenance.rb', line 6

def id
  @id
end

#maint_titleObject (readonly)

Returns the value of attribute maint_title.



6
7
8
# File 'lib/zabbix_maintenance.rb', line 6

def maint_title
  @maint_title
end

#zbxObject (readonly)

Returns the value of attribute zbx.



6
7
8
# File 'lib/zabbix_maintenance.rb', line 6

def zbx
  @zbx
end

Instance Method Details

#authenticated?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/zabbix_maintenance.rb', line 65

def authenticated?
  !(@zbx.nil? || @zbx.client.auth.nil?)
end

#create(groupids, period: 3600) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/zabbix_maintenance.rb', line 41

def create(groupids, period: 3600)
  maint_params = {
    name: @maint_title,
    active_since: Time.now.to_i - 200,
    active_till: Time.now.to_i + 866_00,
    groupids: groupids,
    timeperiods: [{ period: period }]
  }
  ret = @zbx.query method: 'maintenance.create', params: maint_params
  @id = ret['maintenanceids'].first.to_i
end

#create_or_replace(*args) ⇒ Object



36
37
38
39
# File 'lib/zabbix_maintenance.rb', line 36

def create_or_replace(*args)
  delete(id: maint_id) if exists?
  create(*args)
end

#delete(id: @id) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/zabbix_maintenance.rb', line 53

def delete(id: @id)
  ret = @zbx.query method: 'maintenance.delete', params: [id]
  unless ret['maintenanceids'] == [id]
    fail "Maintenance id:#{id} has not been deleted"
  end
  true
end

#exists?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/zabbix_maintenance.rb', line 61

def exists?
  @zbx.query method: 'maintenance.exists', params: { name: @maint_title }
end

#maint_idObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/zabbix_maintenance.rb', line 23

def maint_id
  ret = @zbx.query method: 'maintenance.get', params: {
    'search' => {
      'name' => @maint_title
    }
  }
  begin
    ret.first['maintenanceid'].to_i
  rescue KeyError
    nil
  end
end