Class: ZabbixManager::Mediatypes

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

Instance Method Summary collapse

Methods inherited from Basic

#add, #all, #create, #delete, #destroy, #dump_by_id, #get, #get_full_data, #get_ids, #get_ids_by_identify, #get_key_ids, #get_key_ids_by_identify, #get_or_create, #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_or_update(data) ⇒ Object

创建或更新媒体类型对象



72
73
74
75
76
77
# File 'lib/zabbix_manager/classes/mediatypes.rb', line 72

def create_or_update(data)
  log "[DEBUG] 调用 create_or_update 方法,参数: #{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

#default_optionsObject

默认选项,用于通过 Zabbix API 创建媒体类型对象



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

def default_options
  {
    name: "", # 媒体类型名称
    description: "", # 描述
    type: 0, # 0 - 电子邮件,1 - 外部脚本,2 - 短信,3 - Jabber,100 - EzTexting
    smtp_server: "",
    smtp_helo: "",
    smtp_email: "", # Zabbix 服务器的电子邮件地址
    exec_path: "",  # 外部脚本的名称
    gsm_modem: "",  # GSM modem 的串口设备名称
    username: "", # Zabbix 服务器使用的 Jabber 用户名
    passwd: "" # Zabbix 服务器使用的 Jabber 密码
  }
end

#get_id(data) ⇒ Object

从 API 获取媒体类型对象的 id

Raises:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/zabbix_manager/classes/mediatypes.rb', line 53

def get_id(data)
  log "[DEBUG] 调用 get_id 方法,参数: #{data.inspect}"
  data = symbolize_keys(data) if data.key?(identify)
  name = data[identify.to_sym]
  raise ZbxError, "未提供 #{identify} 参数,#{data} (#{method_name})" if name.nil?

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

#identifyObject

识别特定媒体类型对象的 id 字段名称



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

def identify
  "name"
end

#method_nameObject

与 Zabbix API 交互的媒体类型方法名称



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

def method_name
  "mediatype"
end

#update(data, force = false) ⇒ Object

使用 API 更新媒体类型对象



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/zabbix_manager/classes/mediatypes.rb', line 32

def update(data, force = false)
  log "[DEBUG] 调用 update 方法,参数: #{data.inspect}"
  if data[key.to_sym].nil?
    data[key.to_sym] = get_id(data)
    log "[DEBUG] 用 ID 丰富数据: #{data.inspect}"
  end
  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] 键 #{dump}#{data} 相等,跳过更新"
    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