Class: ZabbixManager::Basic

Inherits:
Object
  • Object
show all
Defined in:
lib/zabbix_manager/basic/basic_func.rb,
lib/zabbix_manager/basic/basic_init.rb,
lib/zabbix_manager/basic/basic_alias.rb,
lib/zabbix_manager/basic/basic_logic.rb,
lib/zabbix_manager/basic/basic_extend.rb

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ ZabbixManager::Client

Initializes a new Basic object with ZabbixManager Client

Parameters:



9
10
11
# File 'lib/zabbix_manager/basic/basic_init.rb', line 9

def initialize(client)
  @client = client
end

Instance Method Details

#add(data) ⇒ Integer, Boolean

Add new Zabbix object using API create

Parameters:

  • data (Hash)

Returns:

  • (Integer)

    The object id if a single object is created

  • (Boolean)

    True/False if multiple objects are created

Raises:

  • (ManagerError)

    Error returned when there is a problem with the Zabbix API call.

  • (HttpError)

    Error raised when HTTP status from Zabbix Server response is not a 200 OK.



22
23
24
# File 'lib/zabbix_manager/basic/basic_alias.rb', line 22

def add(data)
  create(data)
end

#allArray<Hash>

Get full/extended Zabbix data for all objects of type/class from API

Returns:

  • (Array<Hash>)

    Array of matching objects

Raises:

  • (ManagerError)

    Error returned when there is a problem with the Zabbix API call.

  • (HttpError)

    Error raised when HTTP status from Zabbix Server response is not a 200 OK.



146
147
148
149
150
151
152
153
154
155
# File 'lib/zabbix_manager/basic/basic_logic.rb', line 146

def all
  result = {}
  @client.api_request(
    method: "#{method_name}.get",
    params: { output: "extend" }
  ).each do |item|
    result[item[identify]] = item[key]
  end
  result
end

#create(data) ⇒ Integer, Boolean

Create new Zabbix object using API (with defaults)

Parameters:

  • data (Hash)

Returns:

  • (Integer)

    The object id if a single object is created

  • (Boolean)

    True/False if multiple objects are created

Raises:

  • (ManagerError)

    Error returned when there is a problem with the Zabbix API call.

  • (HttpError)

    Error raised when HTTP status from Zabbix Server response is not a 200 OK.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/zabbix_manager/basic/basic_logic.rb', line 12

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

  # 判断是否绑定默认选项并重新生成配置
  data_with_default = default_options.empty? ? data : merge_params(default_options, data)
  data_create       = [data_with_default]

  # 调用实例方法
  result = @client.api_request(method: "#{method_name}.create", params: data_create)
  # 判断是否执行成功并返回结果
  parse_keys result
end

#create_or_update(data) ⇒ Integer, Boolean

Create or update Zabbix object using API

Parameters:

  • data (Hash)

    Should include object’s id field name (identify) and id value

Returns:

  • (Integer)

    The object id if a single object is created

  • (Boolean)

    True/False if multiple objects are created

Raises:

  • (ManagerError)

    Error returned when there is a problem with the Zabbix API call.

  • (HttpError)

    Error raised when HTTP status from Zabbix Server response is not a 200 OK.



47
48
49
50
51
52
# File 'lib/zabbix_manager/basic/basic_logic.rb', line 47

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

  id = get_id(identify.to_sym => data[identify.to_sym])
  id ? update(data.merge(key.to_sym => id.to_s)) : create(data)
end

#create_raw(data) ⇒ Object

根据数据自动创建



200
201
202
203
204
205
206
207
# File 'lib/zabbix_manager/basic/basic_logic.rb', line 200

def create_raw(data)
  log "[DEBUG] Call create_raw with parameters: #{data.inspect}"
  # 请求创建数据
  @client.api_request(
    method: "#{method_name}.create",
    params: data
  )
end

#default_optionsHash

Placeholder for inherited objects to provide default options

Returns:

  • (Hash)


23
24
25
# File 'lib/zabbix_manager/basic/basic_init.rb', line 23

def default_options
  {}
end

#delete(data) ⇒ Integer, Boolean

Delete Zabbix object using API

Parameters:

  • data (Hash)

    Should include object’s id field name (identify) and id value

Returns:

  • (Integer)

    The object id if a single object is deleted

  • (Boolean)

    True/False if multiple objects are deleted

Raises:

  • (ManagerError)

    Error returned when there is a problem with the Zabbix API call.

  • (HttpError)

    Error raised when HTTP status from Zabbix Server response is not a 200 OK.



32
33
34
35
36
37
38
# File 'lib/zabbix_manager/basic/basic_logic.rb', line 32

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

  data_delete = [data]
  result      = @client.api_request(method: "#{method_name}.delete", params: data_delete)
  parse_keys result
end

#delete_raw(data) ⇒ Object

根据数据自动删除数据



220
221
222
223
224
225
226
227
# File 'lib/zabbix_manager/basic/basic_logic.rb', line 220

def delete_raw(data)
  log "[DEBUG] Call delete_raw with parameters: #{data.inspect}"
  # 请求创建数据
  @client.api_request(
    method: "#{method_name}.delete",
    params: data
  )
end

#destroy(data) ⇒ Integer, Boolean

Destroy Zabbix object using API delete

Parameters:

  • data (Hash)

    Should include object’s id field name (identify) and id value

Returns:

  • (Integer)

    The object id if a single object is deleted

  • (Boolean)

    True/False if multiple objects are deleted

Raises:

  • (ManagerError)

    Error returned when there is a problem with the Zabbix API call.

  • (HttpError)

    Error raised when HTTP status from Zabbix Server response is not a 200 OK.



33
34
35
# File 'lib/zabbix_manager/basic/basic_alias.rb', line 33

def destroy(data)
  delete(data)
end

#dump_by_id(data) ⇒ Hash

Dump Zabbix object data by key from API

Parameters:

  • data (Hash)

    Should include desired object’s key and value

Returns:

  • (Hash)

Raises:

  • (ManagerError)

    Error returned when there is a problem with the Zabbix API call.

  • (HttpError)

    Error raised when HTTP status from Zabbix Server response is not a 200 OK.



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/zabbix_manager/basic/basic_logic.rb', line 127

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

  @client.api_request(
    method: "#{method_name}.get",
    params: {
      filter: {
        key.to_sym => data[key.to_sym]
      },
      output: "extend"
    }
  )
end

#get(data) ⇒ Hash

Get Zabbix object data from API by id

Parameters:

  • data (Hash)

    Should include object’s id field name (identify) and id value

Returns:

  • (Hash)

Raises:

  • (ManagerError)

    Error returned when there is a problem with the Zabbix API call.

  • (HttpError)

    Error raised when HTTP status from Zabbix Server response is not a 200 OK.



11
12
13
# File 'lib/zabbix_manager/basic/basic_alias.rb', line 11

def get(data)
  get_full_data(data)
end

#get_full_data(data) ⇒ Hash

Get full/extended Zabbix object data from API

Parameters:

  • data (Hash)

    Should include object’s id field name (identify) and id value

Returns:

  • (Hash)

Raises:

  • (ManagerError)

    Error returned when there is a problem with the Zabbix API call.

  • (HttpError)

    Error raised when HTTP status from Zabbix Server response is not a 200 OK.



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/zabbix_manager/basic/basic_logic.rb', line 92

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

  @client.api_request(
    method: "#{method_name}.get",
    params: {
      filter: {
        identify.to_sym => data[identify.to_sym]
      },
      output: "extend"
    }
  )
end

#get_hostgroup_ids(data) ⇒ Object

新增 get_hostgroup_ids 方法,使用列表 flatten 功能拉平属组对象



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/zabbix_manager/basic/basic_extend.rb', line 6

def get_hostgroup_ids(data)
  result = @client.api_request(
    method: "hostgroup.get",
    params: {
      output: "extend",
      filter: {
        name: [data].flatten
      }
    }
  ).map { |item| { groupid: item["groupid"] } }

  # 检查是是否存在
  result.empty? ? nil : result.flatten
end

#get_id(data) ⇒ Integer

Get Zabbix object id from API based on provided data

Parameters:

  • data (Hash)

Returns:

  • (Integer)

    Zabbix object id

Raises:

  • (ManagerError)

    Error returned when there is a problem with the Zabbix API call or missing object’s id field name (identify).

  • (HttpError)

    Error raised when HTTP status from Zabbix Server response is not a 200 OK.



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/zabbix_manager/basic/basic_logic.rb', line 163

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

  # symbolize keys if the user used string keys instead of symbols
  data = symbolize_keys(data) if data.key?(identify)
  # raise an error if identify name was not supplied
  name = data[identify.to_sym]
  raise ManagerError, "#{identify} not supplied in call to get_id" if name.nil?

  result = @client.api_request(
    method: "#{method_name}.get",
    params: {
      filter: data,
      output: [key, identify]
    }
  )
  id = nil
  result.each { |item| id = item[key].to_i if item[identify] == data[identify.to_sym] }
  id
end

#get_or_create(data) ⇒ Integer

Get or Create Zabbix object using API

Parameters:

  • data (Hash)

    Should include object’s id field name (identify) and id value

Returns:

  • (Integer)

    Zabbix object id

Raises:

  • (ManagerError)

    Error returned when there is a problem with the Zabbix API call.

  • (HttpError)

    Error raised when HTTP status from Zabbix Server response is not a 200 OK.



190
191
192
193
194
195
196
197
# File 'lib/zabbix_manager/basic/basic_logic.rb', line 190

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

  unless (id = get_id(identify.to_sym => data[identify.to_sym]))
    id = create(data)
  end
  id
end

#get_or_create_hostgroups(data) ⇒ Object

新增 get_or_create_hostgroups 方法,查询或创建新的对象



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/zabbix_manager/basic/basic_extend.rb', line 22

def get_or_create_hostgroups(data)
  [data].flatten.each do |item|
    # 是否存在设备属组,不存在则新建

    result = get_hostgroup_ids(item)
    if result.nil?
      @client.api_request(
        method: "hostgroup.create",
        params: {
          name: item
        }
      )
    end
  rescue StandardError => e
    ap e
  end
end

#get_raw(data) ⇒ Hash

Get raw Zabbix object data from API

Parameters:

  • data (Hash)

Returns:

  • (Hash)

Raises:

  • (ManagerError)

    Error returned when there is a problem with the Zabbix API call.

  • (HttpError)

    Error raised when HTTP status from Zabbix Server response is not a 200 OK.



112
113
114
115
116
117
118
119
# File 'lib/zabbix_manager/basic/basic_logic.rb', line 112

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

  @client.api_request(
    method: "#{method_name}.get",
    params: data
  )
end

#hash_equals?(first_hash, second_hash) ⇒ Boolean

Compare two hashes for equality

Parameters:

  • first_hash (Hash)
  • second_hash (Hash)

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
# File 'lib/zabbix_manager/basic/basic_func.rb', line 17

def hash_equals?(first_hash, second_hash)
  normalized_first_hash = normalize_hash(first_hash)
  normalized_second_hash = normalize_hash(second_hash)

  hash1 = normalized_first_hash.merge(normalized_second_hash)
  hash2 = normalized_second_hash.merge(normalized_first_hash)
  hash1 == hash2
end

#identifyObject

Placeholder for inherited objects to provide object-specific id field name

Raises:

  • (ManagerError)

    Basic object does not directly support identify



44
45
46
# File 'lib/zabbix_manager/basic/basic_init.rb', line 44

def identify
  raise ManagerError, "Can't call identify here"
end

#keyString

Returns the object’s id field name (identify) based on method_name + id

Returns:

  • (String)


37
38
39
# File 'lib/zabbix_manager/basic/basic_init.rb', line 37

def key
  "#{method_name}id"
end

#keysString

Returns the object’s plural id field name (identify) based on key

Returns:

  • (String)


30
31
32
# File 'lib/zabbix_manager/basic/basic_init.rb', line 30

def keys
  "#{key}s"
end

#log(message) ⇒ Object

Log messages to stdout when debugging

Parameters:

  • message (String)


8
9
10
# File 'lib/zabbix_manager/basic/basic_func.rb', line 8

def log(message)
  puts message.to_s if @client.options[:debug]
end

#merge_params(first_hash, second_hash) ⇒ Hash

Merge two hashes into a single new hash

Parameters:

  • first_hash (Hash)
  • second_hash (Hash)

Returns:

  • (Hash)


102
103
104
105
# File 'lib/zabbix_manager/basic/basic_func.rb', line 102

def merge_params(first_hash, second_hash)
  new = first_hash.dup
  new.merge(second_hash)
end

#method_nameObject

Placeholder for inherited objects to provide object-specific method name

Raises:

  • (ManagerError)

    Basic object does not directly support method_name



16
17
18
# File 'lib/zabbix_manager/basic/basic_init.rb', line 16

def method_name
  raise ManagerError, "Can't call method_name here"
end

#mojo_update(method, data) ⇒ Object

mojo_update 请求补丁函数



79
80
81
82
# File 'lib/zabbix_manager/basic/basic_logic.rb', line 79

def mojo_update(method, data)
  log "[DEBUG] Call update with parameters: #{data.inspect}"
  @client.api_request(method: method, params: data)
end

#normalize_array(array) ⇒ Array

Normalize all array values to strings

Parameters:

  • array (Array)

Returns:

  • (Array)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/zabbix_manager/basic/basic_func.rb', line 64

def normalize_array(array)
  result = []

  array.each do |e|
    case e
    when Array
      result.push(normalize_array(e))
    when Hash
      result.push(normalize_hash(e))
    else
      result.push(e.to_s)
    end
  end

  result
end

#normalize_hash(hash) ⇒ Hash

Normalize all hash values to strings

Parameters:

  • hash (Hash)

Returns:

  • (Hash)


48
49
50
51
52
53
54
55
56
57
58
# File 'lib/zabbix_manager/basic/basic_func.rb', line 48

def normalize_hash(hash)
  result = hash.dup

  result.delete(:hostid) # TODO: remove to logig. TemplateID and HostID has different id

  result.each do |key, value|
    result[key] = value.is_a?(Array) ? normalize_array(value) : value.to_s
  end

  result
end

#parse_keys(data) ⇒ Integer, Boolean

Parse a data hash for id key or boolean to return

Parameters:

  • data (Hash)

Returns:

  • (Integer)

    The object id if a single object hash is provided with key

  • (Boolean)

    True/False if multiple class object hash is provided



86
87
88
89
90
91
92
93
94
95
# File 'lib/zabbix_manager/basic/basic_func.rb', line 86

def parse_keys(data)
  case data
  when Hash
    data.empty? ? nil : data[keys][0].to_i
  when TrueClass
    true
  when FalseClass
    false
  end
end

#symbolize_keys(object) ⇒ Array, Hash

Convert all hash/array keys to symbols

Parameters:

  • object (Array, Hash)

Returns:

  • (Array, Hash)


30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/zabbix_manager/basic/basic_func.rb', line 30

def symbolize_keys(object)
  case object
  when Array
    object.each_with_index do |val, index|
      object[index] = symbolize_keys(val)
    end
  when Hash
    object.each_key do |key|
      object[key.to_sym] = symbolize_keys(object.delete(key))
    end
  end
  object
end

#update(data, force = false) ⇒ Integer, Boolean

Update Zabbix object using API

Parameters:

  • data (Hash)

    Should include object’s id field name (identify) and id value

  • force (Boolean) (defaults to: false)

    Whether to force an object update even if provided data matches Zabbix

Returns:

  • (Integer)

    The object id if a single object is created

  • (Boolean)

    True/False if multiple objects are created

Raises:

  • (ManagerError)

    Error returned when there is a problem with the Zabbix API call.

  • (HttpError)

    Error raised when HTTP status from Zabbix Server response is not a 200 OK.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/zabbix_manager/basic/basic_logic.rb', line 62

def update(data, force = false)
  log "[DEBUG] Call update with parameters: #{data.inspect}"
  dump = {}
  dump_by_id(key.to_sym => data[key.to_sym]).each do |item|
    dump = symbolize_keys(item) if item[key].to_i == data[key.to_sym].to_i
  end
  if hash_equals?(dump, data) && !force
    log "[DEBUG] Equal keys #{dump} and #{data}, skip update"
    data[key.to_sym].to_i
  else
    data_update = [data]
    result      = @client.api_request(method: "#{method_name}.update", params: data_update)
    parse_keys result
  end
end

#update_raw(data) ⇒ Object

根据数据自动更新数据



210
211
212
213
214
215
216
217
# File 'lib/zabbix_manager/basic/basic_logic.rb', line 210

def update_raw(data)
  log "[DEBUG] Call update_raw with parameters: #{data.inspect}"
  # 请求创建数据
  @client.api_request(
    method: "#{method_name}.update",
    params: data
  )
end