zabbix_manager 5.1.0 → 5.1.1.pre.alpha1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +10 -2
  3. data/{LICENSE.txt → LICENSE} +6 -6
  4. data/README.md +3 -2
  5. data/lib/zabbix_manager/basic/basic_alias.rb +6 -8
  6. data/lib/zabbix_manager/basic/basic_func.rb +17 -18
  7. data/lib/zabbix_manager/basic/basic_init.rb +21 -18
  8. data/lib/zabbix_manager/basic/basic_logic.rb +143 -139
  9. data/lib/zabbix_manager/classes/actions.rb +1 -1
  10. data/lib/zabbix_manager/classes/applications.rb +8 -7
  11. data/lib/zabbix_manager/classes/configurations.rb +2 -2
  12. data/lib/zabbix_manager/classes/drules.rb +6 -6
  13. data/lib/zabbix_manager/classes/errors.rb +1 -1
  14. data/lib/zabbix_manager/classes/graphs.rb +6 -6
  15. data/lib/zabbix_manager/classes/hostgroups.rb +0 -34
  16. data/lib/zabbix_manager/classes/hostinterfaces.rb +26 -0
  17. data/lib/zabbix_manager/classes/hosts.rb +35 -153
  18. data/lib/zabbix_manager/classes/httptests.rb +3 -3
  19. data/lib/zabbix_manager/classes/items.rb +51 -49
  20. data/lib/zabbix_manager/classes/mediatypes.rb +5 -5
  21. data/lib/zabbix_manager/classes/problems.rb +43 -61
  22. data/lib/zabbix_manager/classes/proxies.rb +4 -24
  23. data/lib/zabbix_manager/classes/roles.rb +7 -7
  24. data/lib/zabbix_manager/classes/screens.rb +3 -3
  25. data/lib/zabbix_manager/classes/templates.rb +17 -33
  26. data/lib/zabbix_manager/classes/triggers.rb +31 -73
  27. data/lib/zabbix_manager/classes/usergroups.rb +6 -6
  28. data/lib/zabbix_manager/classes/usermacros.rb +38 -37
  29. data/lib/zabbix_manager/classes/users.rb +4 -4
  30. data/lib/zabbix_manager/classes/valuemaps.rb +8 -7
  31. data/lib/zabbix_manager/client.rb +70 -45
  32. data/lib/zabbix_manager/version.rb +2 -1
  33. data/lib/zabbix_manager.rb +18 -38
  34. data/zabbix_manager.gemspec +7 -11
  35. metadata +22 -66
  36. data/CODE_OF_CONDUCT.md +0 -84
  37. data/lib/zabbix_manager/basic/basic_extend.rb +0 -40
@@ -2,6 +2,9 @@
2
2
 
3
3
  class ZabbixManager
4
4
  class Problems < Basic
5
+ require "active_support"
6
+ require "active_support/core_ext/date/calculations"
7
+
5
8
  # The method name used for interacting with Hosts via Zabbix API
6
9
  #
7
10
  # @return [String]
@@ -16,33 +19,17 @@ class ZabbixManager
16
19
  "name"
17
20
  end
18
21
 
19
- # The key field name used for Problem objects via Zabbix API
20
- # However, Problem object does not have a unique identifier
21
- #
22
- # @return [String]
23
- def key
24
- "problemid"
25
- end
26
-
27
- # Returns the object's plural id field name (identify) based on key
28
- # However, Problem object does not have a unique identifier
29
- #
30
- # @return [String]
31
- def keys
32
- "problemids"
33
- end
34
-
35
22
  # Dump Problem object data by key from Zabbix API
36
23
  #
37
24
  # @param data [Hash] Should include desired object's key and value
38
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
25
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
39
26
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
40
27
  # @return [Hash]
41
28
  def dump_by_id(data)
42
- log "[DEBUG] Call dump_by_id with parameters: #{data.inspect}"
29
+ log "[DEBUG] Call #{method_name}.dump_by_id with parameters: #{data.inspect}"
43
30
 
44
31
  @client.api_request(
45
- method: "problem.get",
32
+ method: "#{method_name}.get",
46
33
  params: {
47
34
  filter: {
48
35
  identify.to_sym => data[identify.to_sym]
@@ -55,37 +42,37 @@ class ZabbixManager
55
42
  # Get full/extended Problem data from Zabbix API
56
43
  #
57
44
  # @param data [Hash] Should include object's id field name (identify) and id value
58
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
45
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
59
46
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
60
47
  # @return [Hash]
61
48
  def get_full_data(data)
62
- log "[DEBUG] Call get_full_data with parameters: #{data.inspect}"
49
+ log "[DEBUG] Call #{method_name}.get_full_data with parameters: #{data.inspect}"
63
50
 
64
51
  data = symbolize_keys(data)
65
52
 
66
53
  @client.api_request(
67
54
  method: "#{method_name}.get",
68
55
  params: {
69
- filter: {
56
+ filter: {
70
57
  identify.to_sym => data[identify.to_sym]
71
58
  },
72
- eventids: data[:eventids] || nil,
73
- groupids: data[:groupids] || nil,
74
- hostids: data[:hostids] || nil,
75
- objectids: data[:objectids] || nil,
76
- applicationids: data[:applicationids] || nil,
77
- tags: data[:tags] || nil,
78
- time_from: data[:time_from] || nil,
79
- time_till: data[:time_till] || nil,
80
- eventid_from: data[:eventid_from] || nil,
81
- eventid_till: data[:eventid_till] || nil,
82
- recent: data[:recent] || false,
83
- sortfield: data[:sortfield] || ["eventid"],
84
- sortorder: data[:sortorder] || "DESC",
85
- countOutput: data[:countOutput] || nil,
86
- output: "extend",
87
- selectAcknowledges: "extend",
88
- selectTags: "extend",
59
+ eventids: data[:eventids] || nil,
60
+ groupids: data[:groupids] || nil,
61
+ hostids: data[:hostids] || nil,
62
+ objectids: data[:objectids] || nil,
63
+ applicationids: data[:applicationids] || nil,
64
+ tags: data[:tags] || nil,
65
+ time_from: data[:time_from] || nil,
66
+ time_till: data[:time_till] || nil,
67
+ eventid_from: data[:eventid_from] || nil,
68
+ eventid_till: data[:eventid_till] || nil,
69
+ recent: data[:recent] || false,
70
+ sortfield: data[:sortfield] || ["eventid"],
71
+ sortorder: data[:sortorder] || "DESC",
72
+ countOutput: data[:countOutput] || nil,
73
+ output: "extend",
74
+ selectAcknowledges: "extend",
75
+ selectTags: "extend",
89
76
  selectSuppressionData: "extend"
90
77
  }
91
78
  )
@@ -93,42 +80,37 @@ class ZabbixManager
93
80
 
94
81
  # Get full/extended Zabbix data for Problem objects from API
95
82
  #
96
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
83
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
97
84
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
98
- # @return [Array<Hash>] Array of matching objects
85
+ # @return [Hash] Array of matching objects
99
86
  def all
100
87
  get_full_data({})
101
88
  end
102
89
 
103
- def remove_problem
104
- # 设置时间区间
105
- time_from = 180.days.ago.at_beginning_of_day.to_i
106
- time_till = 14.days.ago.at_beginning_of_day.to_i
107
-
108
- # 抓取制定区间的数据
109
- data = get_full_data(time_from: time_from, time_till: time_till)
90
+ # Acknowledged problems according to the given parameters.
91
+ # @note 自动确认14天前的问题单
92
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
93
+ # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
94
+ # @return [Array, NilClass] Array of matching objects
95
+ def ack_problem
96
+ time_from = 90.days.ago.at_beginning_of_day.to_i
97
+ time_till = 7.days.ago.at_beginning_of_day.to_i
110
98
  event_ids = []
111
99
 
112
- # 收集所有的 eventid
113
- data.each do |item|
100
+ get_full_data(time_from: time_from, time_till: time_till).each do |item|
114
101
  event_ids << item["eventid"]
115
102
  end
103
+ return if event_ids.empty?
116
104
 
117
- # 返回 event_ids
118
- ack_event event_ids
119
- end
120
-
121
- def ack_event(eventids)
122
- # 请求后端
123
- @client.api_request(
105
+ result = @client.api_request(
124
106
  method: "event.acknowledge",
125
107
  params: {
126
- eventids: eventids,
127
- action: 2,
128
- message: " RUBY SCRIPT 自动关闭"
108
+ eventids: event_ids,
109
+ action: 2,
110
+ message: "本次告警通过 zabbix_api 关闭"
129
111
  }
130
112
  )
131
- # 返回运行结果
113
+ result.empty? ? nil : result.map { |i| { eventids: i["eventids"] } }
132
114
  end
133
115
  end
134
116
  end
@@ -19,9 +19,9 @@ class ZabbixManager
19
19
  # Delete Proxy object using Zabbix API
20
20
  #
21
21
  # @param data [Array] Should include array of proxyid's
22
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
22
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
23
23
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
24
- # @return [Integer] The Proxy object id that was deleted
24
+ # @return [Integer, NilClass] The Proxy object id that was deleted
25
25
  def delete(data)
26
26
  result = @client.api_request(method: "proxy.delete", params: data)
27
27
  result.empty? ? nil : result["proxyids"][0].to_i
@@ -30,7 +30,7 @@ class ZabbixManager
30
30
  # Check if a Proxy object is readable using Zabbix API
31
31
  #
32
32
  # @param data [Array] Should include array of proxyid's
33
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
33
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
34
34
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
35
35
  # @return [Boolean] Returns true if the given proxies are readable
36
36
  def isreadable(data)
@@ -40,31 +40,11 @@ class ZabbixManager
40
40
  # Check if a Proxy object is writable using Zabbix API
41
41
  #
42
42
  # @param data [Array] Should include array of proxyid's
43
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
43
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
44
44
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
45
45
  # @return [Boolean] Returns true if the given proxies are writable
46
46
  def iswritable(data)
47
47
  @client.api_request(method: "proxy.iswritable", params: data)
48
48
  end
49
-
50
- # 新增代理服务器查询接口
51
- def get_proxy_id(proxy)
52
- # 边界条件判断
53
- return nil if proxy.nil?
54
-
55
- # 请求后端接口,只支持单个代理节点
56
- result = @client.api_request(
57
- method: "proxy.get",
58
- params: {
59
- output: "extend",
60
- filter: {
61
- host: proxy
62
- }
63
- }
64
- )
65
-
66
- # 返回代理节点
67
- result.empty? ? nil : result[0]["proxyid"]
68
- end
69
49
  end
70
50
  end
@@ -26,9 +26,9 @@ class ZabbixManager
26
26
  # Set permissions for usergroup using Zabbix API
27
27
  #
28
28
  # @param data [Hash] Needs to include usrgrpids and hostgroupids along with permissions to set
29
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
29
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
30
30
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
31
- # @return [Integer] Zabbix object id (usergroup)
31
+ # @return [Integer, NilClass] Zabbix object id (usergroup)
32
32
  def rules(data)
33
33
  rules = data[:rules] || 2
34
34
  result = @client.api_request(
@@ -45,7 +45,7 @@ class ZabbixManager
45
45
  #
46
46
  # @deprecated Zabbix has removed massAdd in favor of update.
47
47
  # @param data [Hash] Needs to include userids and usrgrpids to mass add users to groups
48
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
48
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
49
49
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
50
50
  # @return [Integer] Zabbix object id (usergroup)
51
51
  def add_user(data)
@@ -55,7 +55,7 @@ class ZabbixManager
55
55
  # Dump Role object data by key from Zabbix API
56
56
  #
57
57
  # @param data [Hash] Should include desired object's key and value
58
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
58
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
59
59
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
60
60
  # @return [Hash]
61
61
  def dump_by_id(data)
@@ -74,7 +74,7 @@ class ZabbixManager
74
74
  # Get Role ids by Role Name from Zabbix API
75
75
  #
76
76
  # @param data [Hash] Should include host value to query for matching graphs
77
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
77
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
78
78
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
79
79
  # @return [Array] Returns array of Graph ids
80
80
  def get_ids_by_name(data)
@@ -96,9 +96,9 @@ class ZabbixManager
96
96
  # Update users in Userroles using Zabbix API
97
97
  #
98
98
  # @param data [Hash] Needs to include userids and usrgrpids to mass update users in groups
99
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
99
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
100
100
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
101
- # @return [Integer] Zabbix object id (usergroup)
101
+ # @return [Integer, NilClass] Zabbix object id (usergroup)
102
102
  def update_users(data)
103
103
  user_groups = data[:usrgrpids].map do |t|
104
104
  {
@@ -38,9 +38,9 @@ class ZabbixManager
38
38
  # Delete Screen object using Zabbix API
39
39
  #
40
40
  # @param data [String, Array] Should include id's of the screens to delete
41
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
41
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
42
42
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
43
- # @return [Integer] Zabbix object id
43
+ # @return [Integer, NilClass] Zabbix object id
44
44
  def delete(data)
45
45
  result = @client.api_request(method: "screen.delete", params: [data])
46
46
  result.empty? ? nil : result["screenids"][0].to_i
@@ -49,7 +49,7 @@ class ZabbixManager
49
49
  # Get or Create Screen object for Host using Zabbix API
50
50
  #
51
51
  # @param data [Hash] Needs to include screen_name and graphids to properly identify Screens via Zabbix API
52
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
52
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
53
53
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
54
54
  # @return [Integer] Zabbix object id
55
55
  def get_or_create_for_host(data)
@@ -19,9 +19,9 @@ class ZabbixManager
19
19
  # Delete Template object using Zabbix API
20
20
  #
21
21
  # @param data [Array] Should include array of templateid's
22
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
22
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
23
23
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
24
- # @return [Integer] The Template object id that was deleted
24
+ # @return [Integer, NilClass] The Template object id that was deleted
25
25
  def delete(data)
26
26
  result = @client.api_request(method: "template.delete", params: [data])
27
27
  result.empty? ? nil : result["templateids"][0].to_i
@@ -30,7 +30,7 @@ class ZabbixManager
30
30
  # Get Template ids for Host from Zabbix API
31
31
  #
32
32
  # @param data [Hash] Should include host value to query for matching templates
33
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
33
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
34
34
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
35
35
  # @return [Array] Returns array of Template ids
36
36
  def get_ids_by_host(data)
@@ -42,27 +42,28 @@ class ZabbixManager
42
42
  # Get or Create Template object using Zabbix API
43
43
  #
44
44
  # @param data [Hash] Needs to include host to properly identify Templates via Zabbix API
45
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
45
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
46
46
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
47
- # @return [Integer] Zabbix object id
47
+ # @return [Integer, TrueClass, FalseClass] Zabbix object id
48
48
  def get_or_create(data)
49
- unless (templateid = get_id(host: data[:host]))
50
- templateid = create(data)
49
+ if (templateid = get_id(host: data[:host]))
50
+ templateid
51
+ else
52
+ create(data)
51
53
  end
52
- templateid
53
54
  end
54
55
 
55
56
  # Mass update Templates for Hosts using Zabbix API
56
57
  #
57
58
  # @param data [Hash] Should include hosts_id array and templates_id array
58
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
59
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
59
60
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
60
61
  # @return [Boolean]
61
62
  def mass_update(data)
62
63
  result = @client.api_request(
63
64
  method: "template.massUpdate",
64
65
  params: {
65
- hosts: data[:hosts_id].map { |t| { hostid: t } },
66
+ hosts: data[:hosts_id].map { |t| { hostid: t } },
66
67
  templates: data[:templates_id].map { |t| { templateid: t } }
67
68
  }
68
69
  )
@@ -72,14 +73,14 @@ class ZabbixManager
72
73
  # Mass add Templates to Hosts using Zabbix API
73
74
  #
74
75
  # @param data [Hash] Should include hosts_id array and templates_id array
75
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
76
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
76
77
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
77
78
  # @return [Boolean]
78
79
  def mass_add(data)
79
80
  result = @client.api_request(
80
81
  method: "template.massAdd",
81
82
  params: {
82
- hosts: data[:hosts_id].map { |t| { hostid: t } },
83
+ hosts: data[:hosts_id].map { |t| { hostid: t } },
83
84
  templates: data[:templates_id].map { |t| { templateid: t } }
84
85
  }
85
86
  )
@@ -89,37 +90,20 @@ class ZabbixManager
89
90
  # Mass remove Templates to Hosts using Zabbix API
90
91
  #
91
92
  # @param data [Hash] Should include hosts_id array and templates_id array
92
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
93
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
93
94
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
94
95
  # @return [Boolean]
95
96
  def mass_remove(data)
96
97
  result = @client.api_request(
97
98
  method: "template.massRemove",
98
99
  params: {
99
- hostids: data[:hosts_id],
100
+ hostids: data[:hosts_id],
100
101
  templateids: data[:templates_id],
101
- groupids: data[:group_id],
102
- force: 1
102
+ groupids: data[:group_id],
103
+ force: 1
103
104
  }
104
105
  )
105
106
  result.empty? ? false : true
106
107
  end
107
-
108
- # 新增 get_template_id 方法,使用列表 flatten 功能拉平数组对象
109
- def get_template_ids(data)
110
- # 直接调后端接口
111
- result = @client.api_request(
112
- method: "template.get",
113
- params: {
114
- output: "extend",
115
- filter: {
116
- host: [data].flatten
117
- }
118
- }
119
- ).map { |temp| [templateid: temp["templateid"]] }
120
-
121
- # 返回 template 模板数组对象
122
- result.empty? ? nil : result.flatten
123
- end
124
108
  end
125
109
  end
@@ -16,10 +16,21 @@ class ZabbixManager
16
16
  "description"
17
17
  end
18
18
 
19
+ # The default options used when creating zabbix triggers
20
+ # @note 缺省的 trigger 参数
21
+ # @return [Hash]
22
+ def default_options
23
+ {
24
+ recovery_mode: 1,
25
+ type: 0,
26
+ manual_close: 1
27
+ }
28
+ end
29
+
19
30
  # Dump Trigger object data by key from Zabbix API
20
31
  #
21
32
  # @param data [Hash] Should include desired object's key and value
22
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
33
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
23
34
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
24
35
  # @return [Hash]
25
36
  def dump_by_id(data)
@@ -41,9 +52,9 @@ class ZabbixManager
41
52
  # Safely update Trigger object using Zabbix API by deleting and replacing trigger
42
53
  #
43
54
  # @param data [Hash] Needs to include description and hostid to properly identify Triggers via Zabbix API
44
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
55
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
45
56
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
46
- # @return [Integer] Zabbix object id
57
+ # @return [Integer, TrueClass, FalseClass, NilClass] Zabbix object id
47
58
  def safe_update(data)
48
59
  log "[DEBUG] Call safe_update with parameters: #{data.inspect}"
49
60
 
@@ -83,94 +94,41 @@ class ZabbixManager
83
94
  # Get or Create Trigger object using Zabbix API
84
95
  #
85
96
  # @param data [Hash] Needs to include description and hostid to properly identify Triggers via Zabbix API
86
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
97
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
87
98
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
88
- # @return [Integer] Zabbix object id
99
+ # @return [Integer, TrueClass, FalseClass, NilClass] Zabbix object id
89
100
  def get_or_create(data)
90
- log "[DEBUG] Call get_or_create with parameters: #{data.inspect}"
101
+ log "[DEBUG] Call #{method_name}.get_or_create with parameters: #{data.inspect}"
91
102
 
92
- unless (id = get_id(description: data[:description], hostid: data[:hostid]))
93
- id = create(data)
103
+ if (id = get_id(description: data[:description], hostid: data[:hostid]))
104
+ id
105
+ else
106
+ create(data)
94
107
  end
95
- id
96
108
  end
97
109
 
98
110
  # Create or update Trigger object using Zabbix API
99
111
  #
100
112
  # @param data [Hash] Needs to include description and hostid to properly identify Triggers via Zabbix API
101
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
113
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
102
114
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
103
- # @return [Integer] Zabbix object id
115
+ # @return [Integer, NilClass, TrueClass, FalseClass] Zabbix object id
104
116
  def create_or_update(data)
105
117
  triggerid = get_id(description: data[:description], hostid: data[:hostid])
106
-
107
118
  triggerid ? update(data.merge(triggerid: triggerid)) : create(data)
108
119
  end
109
120
 
110
- # 测试数据
111
- def mojo_data
112
- data = {
113
- comments: "MOJO1",
114
- opdata: "MOJO_OPDATA",
115
- priority: 1,
116
- description: "MOJO1",
117
- expression: "{SZX1-ISP-SW7:net.if.in[ifHCInOctets.1].avg(15m)}>=450000000 or {SZX1-ISP-SW7:net.if.out[ifHCOutOctets.1].avg(15m)}>=450000000",
118
- recovery_expression: "{SZX1-ISP-SW7:net.if.in[ifHCInOctets.1].avg(15m)}<=350000000 or {SZX1-ISP-SW7:net.if.out[ifHCOutOctets.1].avg(15m)}<=350000000"
119
- }
120
-
121
- create_trigger data.stringify_keys!
122
- end
123
121
 
124
- # 设置触发器
125
- def create_trigger(data = {})
126
- data = data.with_indifferent_access
127
- # 请求生成触发器
128
- result = @client.api_request(
129
- method: "trigger.create",
130
- params: {
131
- comments: data["comments"],
132
- priority: data["priority"],
133
- description: data["description"],
134
- expression: data["expression"],
135
- recovery_expression: data["recovery_expression"],
136
- opdata: data["opdata"],
137
- recovery_mode: 1,
138
- type: 0,
139
- manual_close: 1
140
- }
141
- )
142
- # 检查是是否存在
143
- result.empty? ? nil : result["triggerids"]
144
- end
145
-
146
- # 更新触发器
147
- def update_trigger(triggerid, data = {})
148
- data = data.with_indifferent_access
149
- # 请求生成触发器
150
- result = @client.api_request(
151
- method: "trigger.update",
152
- params: {
153
- triggerid: triggerid,
154
- comments: data["comments"],
155
- priority: data["priority"],
156
- description: data["description"],
157
- expression: data["expression"],
158
- recovery_expression: data["recovery_expression"],
159
- opdata: data["opdata"],
160
- recovery_mode: 1,
161
- type: 0,
162
- manual_close: 1
163
- }
164
- )
165
- # ap result
166
- # 检查是是否存在
167
- result.empty? ? nil : result["triggerids"]
168
- end
169
-
170
- # 添加触发器依赖项
122
+ # Add dependsOnTrigger using Zabbix API
123
+ #
124
+ # @param triggerid [Integer] identify for trigger via Zabbix API
125
+ # @param depend_on_trigger_id [Integer] identify for dependsOnTrigger via Zabbix API
126
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
127
+ # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
128
+ # @return [Integer, NilClass] Zabbix object id
171
129
  def add_trigger_dependency(triggerid, depend_on_trigger_id)
172
130
  log "[DEBUG] Call add_trigger_dependency with parameters: #{triggerid} #{depend_on_trigger_id}"
173
- # 请求生成触发器
131
+
174
132
  result = @client.api_request(
175
133
  method: "trigger.adddependencies",
176
134
  params: {
@@ -26,9 +26,9 @@ class ZabbixManager
26
26
  # Set permissions for usergroup using Zabbix API
27
27
  #
28
28
  # @param data [Hash] Needs to include usrgrpids and hostgroupids along with permissions to set
29
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
29
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
30
30
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
31
- # @return [Integer] Zabbix object id (usergroup)
31
+ # @return [Integer, NilClass] Zabbix object id (usergroup)
32
32
  def permissions(data)
33
33
  permission = data[:permission] || 2
34
34
  result = @client.api_request(
@@ -45,9 +45,9 @@ class ZabbixManager
45
45
  #
46
46
  # @deprecated Zabbix has removed massAdd in favor of update.
47
47
  # @param data [Hash] Needs to include userids and usrgrpids to mass add users to groups
48
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
48
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
49
49
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
50
- # @return [Integer] Zabbix object id (usergroup)
50
+ # @return [Integer, NilClass] Zabbix object id (usergroup)
51
51
  def add_user(data)
52
52
  update_users(data)
53
53
  end
@@ -55,9 +55,9 @@ class ZabbixManager
55
55
  # Update users in usergroups using Zabbix API
56
56
  #
57
57
  # @param data [Hash] Needs to include userids and usrgrpids to mass update users in groups
58
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
58
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
59
59
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
60
- # @return [Integer] Zabbix object id (usergroup)
60
+ # @return [Integer, NilClass] Zabbix object id (usergroup)
61
61
  def update_users(data)
62
62
  user_groups = data[:usrgrpids].map do |t|
63
63
  {