zabbix_manager 5.0.5 → 5.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +2 -4
- data/LICENSE.txt +1 -1
- data/README.md +48 -21
- data/lib/zabbix_manager/basic/basic_alias.rb +2 -0
- data/lib/zabbix_manager/basic/basic_extend.rb +11 -9
- data/lib/zabbix_manager/basic/basic_func.rb +9 -5
- data/lib/zabbix_manager/basic/basic_init.rb +6 -4
- data/lib/zabbix_manager/basic/basic_logic.rb +7 -5
- data/lib/zabbix_manager/classes/actions.rb +6 -4
- data/lib/zabbix_manager/classes/applications.rb +4 -2
- data/lib/zabbix_manager/classes/configurations.rb +6 -4
- data/lib/zabbix_manager/classes/drules.rb +5 -3
- data/lib/zabbix_manager/classes/errors.rb +10 -9
- data/lib/zabbix_manager/classes/events.rb +4 -3
- data/lib/zabbix_manager/classes/graphs.rb +13 -11
- data/lib/zabbix_manager/classes/hostgroups.rb +13 -11
- data/lib/zabbix_manager/classes/hosts.rb +36 -34
- data/lib/zabbix_manager/classes/httptests.rb +4 -2
- data/lib/zabbix_manager/classes/items.rb +34 -28
- data/lib/zabbix_manager/classes/maintenance.rb +4 -2
- data/lib/zabbix_manager/classes/mediatypes.rb +15 -13
- data/lib/zabbix_manager/classes/problems.rb +31 -30
- data/lib/zabbix_manager/classes/proxies.rb +9 -7
- data/lib/zabbix_manager/classes/roles.rb +19 -17
- data/lib/zabbix_manager/classes/screens.rb +6 -4
- data/lib/zabbix_manager/classes/scripts.rb +6 -4
- data/lib/zabbix_manager/classes/server.rb +3 -1
- data/lib/zabbix_manager/classes/templates.rb +18 -16
- data/lib/zabbix_manager/classes/triggers.rb +35 -17
- data/lib/zabbix_manager/classes/unusable.rb +2 -0
- data/lib/zabbix_manager/classes/usergroups.rb +11 -9
- data/lib/zabbix_manager/classes/usermacros.rb +38 -37
- data/lib/zabbix_manager/classes/users.rb +11 -9
- data/lib/zabbix_manager/classes/valuemaps.rb +5 -3
- data/lib/zabbix_manager/client.rb +40 -30
- data/lib/zabbix_manager/version.rb +3 -1
- data/lib/zabbix_manager.rb +36 -35
- data/zabbix_manager.gemspec +43 -0
- metadata +143 -19
- data/.idea/workspace.xml +0 -69
- data/.rubocop.yml +0 -13
- data/CODE_OF_CONDUCT.md +0 -84
- data/Gemfile +0 -10
- data/Rakefile +0 -8
- data/bin/console +0 -15
- data/bin/setup +0 -8
- data/zabbix_manager-5.0.1.gem +0 -0
- data/zabbix_manager-5.0.2.gem +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aa8b6fa1927ad9ff3012369cc92e302a7ef67c83e1dd8dcff2773257c10f9def
|
4
|
+
data.tar.gz: bcc2663a1d885d3e7632fc42eeb756de6aab34df75c289399f6464176fc6745b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7fd2a1c9d746706fa2959a8734bd258b862b43db5c1c6e240a80b25e2ec16c0d09eb1b9ad60dbb8b4ddea4b9ae310d026ae9b8f2cd331abbc94b04244a63420d
|
7
|
+
data.tar.gz: 62f0b3557a65b5fdf1ff2e2ccd1c3406febc9190ad4ecca052b7f9dc4227d8ac98f371aacf36cb72f62e2af52506e3be9b74f40d15cf6665d88f712b073d4a7c
|
data/CHANGELOG.md
CHANGED
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -1,41 +1,68 @@
|
|
1
|
-
#
|
1
|
+
# Ruby Zabbix Api Module
|
2
2
|
|
3
|
-
|
3
|
+
[![Gem Version](http://img.shields.io/gem/v/zabbix_manager.svg)][gem]
|
4
4
|
|
5
|
-
|
5
|
+
[gem]: https://rubygems.org/gems/zabbix_manager
|
6
|
+
|
7
|
+
Most codes borrowed from zabbixapi, but fit for my everyday works well!
|
8
|
+
Simple and lightweight ruby module for working with [Zabbix][Zabbix] via the [Zabbix API][Zabbix API]
|
6
9
|
|
7
|
-
|
10
|
+
## Installation
|
11
|
+
```sh
|
12
|
+
# latest
|
13
|
+
gem install zabbix_manager
|
8
14
|
|
9
|
-
|
10
|
-
gem
|
15
|
+
# specific version
|
16
|
+
gem install zabbix_manager -v 4.2.0
|
11
17
|
```
|
12
18
|
|
13
|
-
|
19
|
+
## Documentation
|
20
|
+
[http://rdoc.info/gems/zabbix_manager][documentation]
|
14
21
|
|
15
|
-
|
22
|
+
[documentation]: http://rdoc.info/gems/zabbix_manager
|
16
23
|
|
17
|
-
|
24
|
+
## Examples
|
18
25
|
|
19
|
-
$ gem install zabbix_manager
|
20
26
|
|
21
|
-
##
|
27
|
+
## Supported Ruby Versions
|
28
|
+
This library aims to support and is [tested against][github-ci] the following Ruby
|
29
|
+
versions:
|
22
30
|
|
23
|
-
|
31
|
+
* Ruby 2.5
|
32
|
+
* Ruby 2.6
|
33
|
+
* Ruby 2.7
|
34
|
+
* JRuby 9.2.10.0
|
24
35
|
|
25
|
-
|
36
|
+
If something doesn't work on one of these versions, it's a bug.
|
26
37
|
|
27
|
-
|
38
|
+
This library may inadvertently work (or seem to work) on other Ruby versions,
|
39
|
+
however support will only be provided for the versions listed above.
|
28
40
|
|
29
|
-
|
41
|
+
If you would like this library to support another Ruby version or
|
42
|
+
implementation, you may volunteer to be a maintainer. Being a maintainer
|
43
|
+
entails making sure all tests run and pass on that implementation. When
|
44
|
+
something breaks on your implementation, you will be responsible for providing
|
45
|
+
patches in a timely fashion. If critical issues for a particular implementation
|
46
|
+
exist at the time of a major release, support for that Ruby version may be
|
47
|
+
dropped.
|
30
48
|
|
31
|
-
##
|
49
|
+
## Dependencies
|
50
|
+
|
51
|
+
* net/http
|
52
|
+
* json
|
32
53
|
|
33
|
-
|
54
|
+
## Contributing
|
34
55
|
|
35
|
-
|
56
|
+
* Fork the project.
|
57
|
+
* Base your work on the master branch.
|
58
|
+
* Make your feature addition or bug fix, write tests, write documentation/examples.
|
59
|
+
* Commit, do not mess with rakefile, version.
|
60
|
+
* Make a pull request.
|
36
61
|
|
37
|
-
|
62
|
+
## Zabbix documentation
|
38
63
|
|
39
|
-
|
64
|
+
* [Zabbix Project Homepage][Zabbix]
|
65
|
+
* [Zabbix API docs][Zabbix API]
|
40
66
|
|
41
|
-
|
67
|
+
[Zabbix]: https://www.zabbix.com
|
68
|
+
[Zabbix API]: https://www.zabbix.com/documentation/5.2/manual/api
|
@@ -1,16 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class ZabbixManager
|
2
4
|
class Basic
|
3
5
|
# 新增 get_hostgroup_ids 方法,使用列表 flatten 功能拉平属组对象
|
4
6
|
def get_hostgroup_ids(data)
|
5
7
|
result = @client.api_request(
|
6
|
-
method:
|
8
|
+
method: "hostgroup.get",
|
7
9
|
params: {
|
8
10
|
output: "extend",
|
9
11
|
filter: {
|
10
12
|
name: [data].flatten
|
11
13
|
}
|
12
14
|
}
|
13
|
-
).map { |item| { groupid: item[
|
15
|
+
).map { |item| { groupid: item["groupid"] } }
|
14
16
|
|
15
17
|
# 检查是是否存在
|
16
18
|
result.empty? ? nil : result.flatten
|
@@ -20,19 +22,19 @@ class ZabbixManager
|
|
20
22
|
def get_or_create_hostgroups(data)
|
21
23
|
[data].flatten.each do |item|
|
22
24
|
# 是否存在设备属组,不存在则新建
|
23
|
-
|
24
|
-
|
25
|
+
|
26
|
+
result = get_hostgroup_ids(item)
|
27
|
+
if result.nil?
|
25
28
|
@client.api_request(
|
26
|
-
method:
|
29
|
+
method: "hostgroup.create",
|
27
30
|
params: {
|
28
31
|
name: item
|
29
32
|
}
|
30
|
-
)
|
31
|
-
rescue => e
|
32
|
-
ap e
|
33
|
+
)
|
33
34
|
end
|
35
|
+
rescue StandardError => e
|
36
|
+
ap e
|
34
37
|
end
|
35
38
|
end
|
36
|
-
|
37
39
|
end
|
38
40
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class ZabbixManager
|
2
4
|
class Basic
|
3
5
|
# Log messages to stdout when debugging
|
@@ -26,12 +28,13 @@ class ZabbixManager
|
|
26
28
|
# @param object [Array, Hash]
|
27
29
|
# @return [Array, Hash]
|
28
30
|
def symbolize_keys(object)
|
29
|
-
|
31
|
+
case object
|
32
|
+
when Array
|
30
33
|
object.each_with_index do |val, index|
|
31
34
|
object[index] = symbolize_keys(val)
|
32
35
|
end
|
33
|
-
|
34
|
-
object.
|
36
|
+
when Hash
|
37
|
+
object.each_key do |key|
|
35
38
|
object[key.to_sym] = symbolize_keys(object.delete(key))
|
36
39
|
end
|
37
40
|
end
|
@@ -62,9 +65,10 @@ class ZabbixManager
|
|
62
65
|
result = []
|
63
66
|
|
64
67
|
array.each do |e|
|
65
|
-
|
68
|
+
case e
|
69
|
+
when Array
|
66
70
|
result.push(normalize_array(e))
|
67
|
-
|
71
|
+
when Hash
|
68
72
|
result.push(normalize_hash(e))
|
69
73
|
else
|
70
74
|
result.push(e.to_s)
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class ZabbixManager
|
2
4
|
class Basic
|
3
5
|
# Initializes a new Basic object with ZabbixManager Client
|
@@ -12,7 +14,7 @@ class ZabbixManager
|
|
12
14
|
#
|
13
15
|
# @raise [ManagerError] Basic object does not directly support method_name
|
14
16
|
def method_name
|
15
|
-
raise ManagerError
|
17
|
+
raise ManagerError, "Can't call method_name here"
|
16
18
|
end
|
17
19
|
|
18
20
|
# Placeholder for inherited objects to provide default options
|
@@ -26,21 +28,21 @@ class ZabbixManager
|
|
26
28
|
#
|
27
29
|
# @return [String]
|
28
30
|
def keys
|
29
|
-
key
|
31
|
+
"#{key}s"
|
30
32
|
end
|
31
33
|
|
32
34
|
# Returns the object's id field name (identify) based on method_name + id
|
33
35
|
#
|
34
36
|
# @return [String]
|
35
37
|
def key
|
36
|
-
method_name
|
38
|
+
"#{method_name}id"
|
37
39
|
end
|
38
40
|
|
39
41
|
# Placeholder for inherited objects to provide object-specific id field name
|
40
42
|
#
|
41
43
|
# @raise [ManagerError] Basic object does not directly support identify
|
42
44
|
def identify
|
43
|
-
raise ManagerError
|
45
|
+
raise ManagerError, "Can't call identify here"
|
44
46
|
end
|
45
47
|
end
|
46
48
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class ZabbixManager
|
2
4
|
class Basic
|
3
5
|
# Create new Zabbix object using API (with defaults)
|
@@ -96,7 +98,7 @@ class ZabbixManager
|
|
96
98
|
filter: {
|
97
99
|
identify.to_sym => data[identify.to_sym]
|
98
100
|
},
|
99
|
-
output:
|
101
|
+
output: "extend"
|
100
102
|
}
|
101
103
|
)
|
102
104
|
end
|
@@ -131,7 +133,7 @@ class ZabbixManager
|
|
131
133
|
filter: {
|
132
134
|
key.to_sym => data[key.to_sym]
|
133
135
|
},
|
134
|
-
output:
|
136
|
+
output: "extend"
|
135
137
|
}
|
136
138
|
)
|
137
139
|
end
|
@@ -145,7 +147,7 @@ class ZabbixManager
|
|
145
147
|
result = {}
|
146
148
|
@client.api_request(
|
147
149
|
method: "#{method_name}.get",
|
148
|
-
params: { output:
|
150
|
+
params: { output: "extend" }
|
149
151
|
).each do |item|
|
150
152
|
result[item[identify]] = item[key]
|
151
153
|
end
|
@@ -165,7 +167,7 @@ class ZabbixManager
|
|
165
167
|
data = symbolize_keys(data) if data.key?(identify)
|
166
168
|
# raise an error if identify name was not supplied
|
167
169
|
name = data[identify.to_sym]
|
168
|
-
raise ManagerError
|
170
|
+
raise ManagerError, "#{identify} not supplied in call to get_id" if name.nil?
|
169
171
|
|
170
172
|
result = @client.api_request(
|
171
173
|
method: "#{method_name}.get",
|
@@ -174,7 +176,7 @@ class ZabbixManager
|
|
174
176
|
output: [key, identify]
|
175
177
|
}
|
176
178
|
)
|
177
|
-
id
|
179
|
+
id = nil
|
178
180
|
result.each { |item| id = item[key].to_i if item[identify] == data[identify.to_sym] }
|
179
181
|
id
|
180
182
|
end
|
@@ -1,17 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class ZabbixManager
|
2
4
|
class Actions < Basic
|
3
5
|
# The method name used for interacting with Actions via Zabbix API
|
4
6
|
#
|
5
7
|
# @return [String]
|
6
8
|
def method_name
|
7
|
-
|
9
|
+
"action"
|
8
10
|
end
|
9
11
|
|
10
12
|
# The id field name used for identifying specific Action objects via Zabbix API
|
11
13
|
#
|
12
14
|
# @return [String]
|
13
15
|
def identify
|
14
|
-
|
16
|
+
"name"
|
15
17
|
end
|
16
18
|
|
17
19
|
# Get full/extended Action object data from API
|
@@ -29,11 +31,11 @@ class ZabbixManager
|
|
29
31
|
filter: {
|
30
32
|
identify.to_sym => data[identify.to_sym]
|
31
33
|
},
|
32
|
-
output:
|
34
|
+
output: "extend",
|
33
35
|
selectOperations: "extend",
|
34
36
|
selectRecoveryOperations: "extend",
|
35
37
|
selectAcknowledgeOperations: "extend",
|
36
|
-
selectFilter: "extend"
|
38
|
+
selectFilter: "extend"
|
37
39
|
}
|
38
40
|
)
|
39
41
|
end
|
@@ -1,17 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class ZabbixManager
|
2
4
|
class Applications < Basic
|
3
5
|
# The method name used for interacting with Applications via Zabbix API
|
4
6
|
#
|
5
7
|
# @return [String]
|
6
8
|
def method_name
|
7
|
-
|
9
|
+
"application"
|
8
10
|
end
|
9
11
|
|
10
12
|
# The id field name used for identifying specific Application objects via Zabbix API
|
11
13
|
#
|
12
14
|
# @return [String]
|
13
15
|
def identify
|
14
|
-
|
16
|
+
"name"
|
15
17
|
end
|
16
18
|
|
17
19
|
# Get or Create Application object using Zabbix API
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class ZabbixManager
|
2
4
|
class Configurations < Basic
|
3
5
|
# @return [Boolean]
|
@@ -9,14 +11,14 @@ class ZabbixManager
|
|
9
11
|
#
|
10
12
|
# @return [String]
|
11
13
|
def method_name
|
12
|
-
|
14
|
+
"configuration"
|
13
15
|
end
|
14
16
|
|
15
17
|
# The id field name used for identifying specific Configuration objects via Zabbix API
|
16
18
|
#
|
17
19
|
# @return [String]
|
18
20
|
def identify
|
19
|
-
|
21
|
+
"host"
|
20
22
|
end
|
21
23
|
|
22
24
|
# Export configuration data using Zabbix API
|
@@ -26,7 +28,7 @@ class ZabbixManager
|
|
26
28
|
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
27
29
|
# @return [Hash]
|
28
30
|
def export(data)
|
29
|
-
@client.api_request(method:
|
31
|
+
@client.api_request(method: "configuration.export", params: data)
|
30
32
|
end
|
31
33
|
|
32
34
|
# Import configuration data using Zabbix API
|
@@ -36,7 +38,7 @@ class ZabbixManager
|
|
36
38
|
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
37
39
|
# @return [Hash]
|
38
40
|
def import(data)
|
39
|
-
@client.api_request(method:
|
41
|
+
@client.api_request(method: "configuration.import", params: data)
|
40
42
|
end
|
41
43
|
end
|
42
44
|
end
|
@@ -1,17 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class ZabbixManager
|
2
4
|
class Drules < Basic
|
3
5
|
# The method name used for interacting with Drules via Zabbix API
|
4
6
|
#
|
5
7
|
# @return [String]
|
6
8
|
def method_name
|
7
|
-
|
9
|
+
"drule"
|
8
10
|
end
|
9
11
|
|
10
12
|
# The id field name used for identifying specific Drule objects via Zabbix API
|
11
13
|
#
|
12
14
|
# @return [String]
|
13
15
|
def identify
|
14
|
-
|
16
|
+
"name"
|
15
17
|
end
|
16
18
|
|
17
19
|
# The default options used when creating Drule objects via Zabbix API
|
@@ -22,7 +24,7 @@ class ZabbixManager
|
|
22
24
|
name: nil,
|
23
25
|
iprange: nil,
|
24
26
|
delay: 3600,
|
25
|
-
status: 0
|
27
|
+
status: 0
|
26
28
|
}
|
27
29
|
end
|
28
30
|
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class ZabbixManager
|
2
4
|
class BaseError < RuntimeError
|
3
5
|
attr_accessor :response, :error, :error_message
|
@@ -9,15 +11,14 @@ class ZabbixManager
|
|
9
11
|
set_error! if @response
|
10
12
|
end
|
11
13
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
end
|
14
|
+
private
|
15
|
+
def set_error!
|
16
|
+
@error = @response["error"]
|
17
|
+
@error_message = "#{@error["message"]}: #{@error["data"]}"
|
18
|
+
rescue StandardError
|
19
|
+
@error = nil
|
20
|
+
@error_message = nil
|
21
|
+
end
|
21
22
|
end
|
22
23
|
|
23
24
|
class ManagerError < BaseError
|
@@ -1,18 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class ZabbixManager
|
2
4
|
class Events < Basic
|
3
5
|
# The method name used for interacting with Events via Zabbix API
|
4
6
|
#
|
5
7
|
# @return [String]
|
6
8
|
def method_name
|
7
|
-
|
9
|
+
"event"
|
8
10
|
end
|
9
11
|
|
10
12
|
# The id field name used for identifying specific Event objects via Zabbix API
|
11
13
|
#
|
12
14
|
# @return [String]
|
13
15
|
def identify
|
14
|
-
|
16
|
+
"name"
|
15
17
|
end
|
16
|
-
|
17
18
|
end
|
18
19
|
end
|
@@ -1,17 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class ZabbixManager
|
2
4
|
class Graphs < Basic
|
3
5
|
# The method name used for interacting with Graphs via Zabbix API
|
4
6
|
#
|
5
7
|
# @return [String]
|
6
8
|
def method_name
|
7
|
-
|
9
|
+
"graph"
|
8
10
|
end
|
9
11
|
|
10
12
|
# The id field name used for identifying specific Graph objects via Zabbix API
|
11
13
|
#
|
12
14
|
# @return [String]
|
13
15
|
def identify
|
14
|
-
|
16
|
+
"name"
|
15
17
|
end
|
16
18
|
|
17
19
|
# Get full/extended Graph data from Zabbix API
|
@@ -29,7 +31,7 @@ class ZabbixManager
|
|
29
31
|
search: {
|
30
32
|
identify.to_sym => data[identify.to_sym]
|
31
33
|
},
|
32
|
-
output:
|
34
|
+
output: "extend"
|
33
35
|
}
|
34
36
|
)
|
35
37
|
end
|
@@ -42,22 +44,22 @@ class ZabbixManager
|
|
42
44
|
# @return [Array] Returns array of Graph ids
|
43
45
|
def get_ids_by_host(data)
|
44
46
|
result = @client.api_request(
|
45
|
-
method:
|
47
|
+
method: "graph.get",
|
46
48
|
params: {
|
47
49
|
filter: {
|
48
50
|
host: data[:host]
|
49
51
|
},
|
50
|
-
output:
|
52
|
+
output: "extend"
|
51
53
|
}
|
52
54
|
)
|
53
55
|
|
54
|
-
result.
|
55
|
-
num = graph[
|
56
|
-
name = graph[
|
56
|
+
result.filter_map do |graph|
|
57
|
+
num = graph["graphid"]
|
58
|
+
name = graph["name"]
|
57
59
|
filter = data[:filter]
|
58
60
|
|
59
61
|
num if filter.nil? || /#{filter}/ =~ name
|
60
|
-
end
|
62
|
+
end
|
61
63
|
end
|
62
64
|
|
63
65
|
# Get Graph Item object using Zabbix API
|
@@ -68,10 +70,10 @@ class ZabbixManager
|
|
68
70
|
# @return [Hash]
|
69
71
|
def get_items(data)
|
70
72
|
@client.api_request(
|
71
|
-
method:
|
73
|
+
method: "graphitem.get",
|
72
74
|
params: {
|
73
75
|
graphids: [data],
|
74
|
-
output:
|
76
|
+
output: "extend"
|
75
77
|
}
|
76
78
|
)
|
77
79
|
end
|
@@ -1,37 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class ZabbixManager
|
2
4
|
class HostGroups < Basic
|
3
5
|
# The method name used for interacting with HostGroups via Zabbix API
|
4
6
|
#
|
5
7
|
# @return [String]
|
6
8
|
def method_name
|
7
|
-
|
9
|
+
"hostgroup"
|
8
10
|
end
|
9
11
|
|
10
12
|
# The id field name used for identifying specific HostGroup objects via Zabbix API
|
11
13
|
#
|
12
14
|
# @return [String]
|
13
15
|
def identify
|
14
|
-
|
16
|
+
"name"
|
15
17
|
end
|
16
18
|
|
17
19
|
# The key field name used for HostGroup objects via Zabbix API
|
18
20
|
#
|
19
21
|
# @return [String]
|
20
22
|
def key
|
21
|
-
|
23
|
+
"groupid"
|
22
24
|
end
|
23
25
|
|
24
26
|
# 新增 get_hostgroup_ids 方法,使用列表 flatten 功能拉平属组对象
|
25
27
|
def get_hostgroup_ids(data)
|
26
28
|
result = @client.api_request(
|
27
|
-
method:
|
29
|
+
method: "hostgroup.get",
|
28
30
|
params: {
|
29
31
|
output: "extend",
|
30
32
|
filter: {
|
31
33
|
name: [data].flatten
|
32
34
|
}
|
33
35
|
}
|
34
|
-
).map { |item| { groupid: item[
|
36
|
+
).map { |item| { groupid: item["groupid"] } }
|
35
37
|
|
36
38
|
# 检查是是否存在
|
37
39
|
result.empty? ? nil : result.flatten
|
@@ -41,17 +43,17 @@ class ZabbixManager
|
|
41
43
|
def get_or_create_hostgroups(data)
|
42
44
|
# 是否存在设备属组,不存在则新建
|
43
45
|
[data].flatten.each do |item|
|
44
|
-
|
45
|
-
|
46
|
+
result = get_hostgroup_ids(item)
|
47
|
+
if result.nil?
|
46
48
|
@client.api_request(
|
47
|
-
method:
|
49
|
+
method: "hostgroup.create",
|
48
50
|
params: {
|
49
51
|
name: item
|
50
52
|
}
|
51
|
-
)
|
52
|
-
rescue => e
|
53
|
-
ap e
|
53
|
+
)
|
54
54
|
end
|
55
|
+
rescue StandardError => e
|
56
|
+
ap e
|
55
57
|
end
|
56
58
|
end
|
57
59
|
end
|