Class: ZabbixManager::Usermacros

Inherits:
Basic
  • Object
show all
Defined in:
lib/zabbix_manager/classes/usermacros.rb

Instance Method Summary collapse

Methods inherited from Basic

#add, #all, #default_options, #destroy, #dump_by_id, #get, #get_ids, #get_ids_by_identify, #get_key_ids, #get_key_ids_by_identify, #get_or_create_keys, #get_raw, #hash_equals?, #initialize, #key, #keys, #log, #merge_hashes, #normalize_array, #normalize_hash, #parse_keys, #request_raw, #symbolize_keys

Constructor Details

This class inherits a constructor from ZabbixManager::Basic

Instance Method Details

#create(data) ⇒ Object

通过 Zabbix API 创建新的 User 宏对象(使用默认值)



56
57
58
# File 'lib/zabbix_manager/classes/usermacros.rb', line 56

def create(data)
  request(data, "usermacro.create", "hostmacroids")
end

#create_global(data) ⇒ Object

通过 Zabbix API 创建新的 Global 宏对象(使用默认值)



61
62
63
# File 'lib/zabbix_manager/classes/usermacros.rb', line 61

def create_global(data)
  request(data, "usermacro.createglobal", "globalmacroids")
end

#create_or_update(data) ⇒ Object

通过 Zabbix API 创建或更新 User 宏对象



108
109
110
111
# File 'lib/zabbix_manager/classes/usermacros.rb', line 108

def create_or_update(data)
  hostmacroid = get_id(macro: data[:macro], hostid: data[:hostid])
  hostmacroid ? update(data.merge(hostmacroid: hostmacroid)) : create(data)
end

#create_or_update_global(data) ⇒ Object

通过 Zabbix API 创建或更新 Global 宏对象



114
115
116
117
# File 'lib/zabbix_manager/classes/usermacros.rb', line 114

def create_or_update_global(data)
  globalmacroid = get_id_global(macro: data[:macro], hostid: data[:hostid])
  globalmacroid ? update_global(data.merge(globalmacroid: globalmacroid)) : create_global(data)
end

#delete(data) ⇒ Object

通过 Zabbix API 删除 User 宏对象



66
67
68
69
# File 'lib/zabbix_manager/classes/usermacros.rb', line 66

def delete(data)
  data_delete = [data]
  request(data_delete, "usermacro.delete", "hostmacroids")
end

#delete_global(data) ⇒ Object

通过 Zabbix API 删除 Global 宏对象



72
73
74
75
# File 'lib/zabbix_manager/classes/usermacros.rb', line 72

def delete_global(data)
  data_delete = [data]
  request(data_delete, "usermacro.deleteglobal", "globalmacroids")
end

#get_full_data(data) ⇒ Object

通过 Zabbix API 获取完整/扩展的 User 宏数据



42
43
44
45
46
# File 'lib/zabbix_manager/classes/usermacros.rb', line 42

def get_full_data(data)
  log "[DEBUG] Call get_full_data with parameters: #{data.inspect}"

  request(data, "usermacro.get", "hostmacroid")
end

#get_full_data_global(data) ⇒ Object

通过 Zabbix API 获取完整/扩展的 Global 宏数据



49
50
51
52
53
# File 'lib/zabbix_manager/classes/usermacros.rb', line 49

def get_full_data_global(data)
  log "[DEBUG] Call get_full_data_global with parameters: #{data.inspect}"

  request(data, "usermacro.get", "globalmacroid")
end

#get_id(data) ⇒ Object

通过 Zabbix API 根据提供的数据获取 User 宏对象的 id

Raises:



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/zabbix_manager/classes/usermacros.rb', line 16

def get_id(data)
  log "[DEBUG] Call get_id with parameters: #{data.inspect}"

  data = symbolize_keys(data) if data.key?(identify)
  name = data[identify.to_sym]
  raise ZbxError, "#{identify} not supplied in call to get_id" if name.nil?

  result = request(data, "usermacro.get", "hostmacroid")

  !result&.empty? && result[0].key?("hostmacroid") ? result[0]["hostmacroid"].to_i : nil
end

#get_id_global(data) ⇒ Object

通过 Zabbix API 根据提供的数据获取 Global 宏对象的 id

Raises:



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/zabbix_manager/classes/usermacros.rb', line 29

def get_id_global(data)
  log "[DEBUG] Call get_id_global with parameters: #{data.inspect}"

  data = symbolize_keys(data) if data.key?(identify)
  name = data[identify.to_sym]
  raise ZbxError, "#{identify} not supplied in call to get_id_global" if name.nil?

  result = request(data, "usermacro.get", "globalmacroid")

  !result&.empty? && result[0].key?("globalmacroid") ? result[0]["globalmacroid"].to_i : nil
end

#get_or_create(data) ⇒ Object

通过 Zabbix API 获取或创建 User 宏对象



88
89
90
91
92
93
94
95
# File 'lib/zabbix_manager/classes/usermacros.rb', line 88

def get_or_create(data)
  log "[DEBUG] Call get_or_create with parameters: #{data.inspect}"

  unless (id = get_id(macro: data[:macro], hostid: data[:hostid]))
    id = create(data)
  end
  id
end

#get_or_create_global(data) ⇒ Object

通过 Zabbix API 获取或创建 Global 宏对象



98
99
100
101
102
103
104
105
# File 'lib/zabbix_manager/classes/usermacros.rb', line 98

def get_or_create_global(data)
  log "[DEBUG] Call get_or_create_global with parameters: #{data.inspect}"

  unless (id = get_id_global(macro: data[:macro], hostid: data[:hostid]))
    id = create_global(data)
  end
  id
end

#identifyObject

用于通过 Zabbix API 识别特定 User 宏对象的 id 字段名称



6
7
8
# File 'lib/zabbix_manager/classes/usermacros.rb', line 6

def identify
  "macro"
end

#method_nameObject

用于通过 Zabbix API 与 User 宏交互的方法名称



11
12
13
# File 'lib/zabbix_manager/classes/usermacros.rb', line 11

def method_name
  "usermacro"
end

#update(data) ⇒ Object

通过 Zabbix API 更新 User 宏对象



78
79
80
# File 'lib/zabbix_manager/classes/usermacros.rb', line 78

def update(data)
  request(data, "usermacro.update", "hostmacroids")
end

#update_global(data) ⇒ Object

通过 Zabbix API 更新 Global 宏对象



83
84
85
# File 'lib/zabbix_manager/classes/usermacros.rb', line 83

def update_global(data)
  request(data, "usermacro.updateglobal", "globalmacroids")
end