zabbix_manager 5.0.1 → 5.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.idea/workspace.xml +69 -0
- data/lib/zabbix_manager/basic/basic_alias.rb +37 -0
- data/lib/zabbix_manager/basic/basic_extend.rb +38 -0
- data/lib/zabbix_manager/basic/basic_func.rb +103 -0
- data/lib/zabbix_manager/basic/basic_init.rb +46 -0
- data/lib/zabbix_manager/basic/basic_logic.rb +227 -0
- data/lib/zabbix_manager/classes/actions.rb +41 -0
- data/lib/zabbix_manager/classes/applications.rb +43 -0
- data/lib/zabbix_manager/classes/configurations.rb +42 -0
- data/lib/zabbix_manager/classes/drules.rb +55 -0
- data/lib/zabbix_manager/classes/errors.rb +28 -0
- data/lib/zabbix_manager/classes/events.rb +18 -0
- data/lib/zabbix_manager/classes/graphs.rb +111 -0
- data/lib/zabbix_manager/classes/hostgroups.rb +58 -0
- data/lib/zabbix_manager/classes/hosts.rb +218 -0
- data/lib/zabbix_manager/classes/httptests.rb +54 -0
- data/lib/zabbix_manager/classes/items.rb +146 -0
- data/lib/zabbix_manager/classes/maintenance.rb +17 -0
- data/lib/zabbix_manager/classes/mediatypes.rb +109 -0
- data/lib/zabbix_manager/classes/problems.rb +133 -0
- data/lib/zabbix_manager/classes/proxies.rb +68 -0
- data/lib/zabbix_manager/classes/roles.rb +114 -0
- data/lib/zabbix_manager/classes/screens.rb +94 -0
- data/lib/zabbix_manager/classes/scripts.rb +35 -0
- data/lib/zabbix_manager/classes/server.rb +16 -0
- data/lib/zabbix_manager/classes/templates.rb +123 -0
- data/lib/zabbix_manager/classes/triggers.rb +166 -0
- data/lib/zabbix_manager/classes/unusable.rb +8 -0
- data/lib/zabbix_manager/classes/usergroups.rb +73 -0
- data/lib/zabbix_manager/classes/usermacros.rb +228 -0
- data/lib/zabbix_manager/classes/users.rb +64 -0
- data/lib/zabbix_manager/classes/valuemaps.rb +50 -0
- data/lib/zabbix_manager/client.rb +158 -0
- data/lib/zabbix_manager/version.rb +1 -1
- data/zabbix_manager-5.0.1.gem +0 -0
- data/zabbix_manager-5.0.2.gem +0 -0
- metadata +81 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 034a6a763df4c8337459dafae939113d20d243bb3a03edca7c53cb8f7e4735f5
|
4
|
+
data.tar.gz: 5cc9e8342583fff642f3e8190b4f92329e52076b94cc182e899bdea36ca1dda4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c70af5c6c152c2a6673fd4580038180525c88d0eb40589bf4708894fc0cb7a73e9b59daf8de101823739cceceb799cea82f87e6bc6f787457e80196e68486a28
|
7
|
+
data.tar.gz: 2ea559104053fd3b3bd5b9e6c7026062e80108dbf5861ff17ac4c0a316655be22850f082683a8292992b63afbf036ab5aa224dbd8bb85b6057bd324a140475d9
|
data/.idea/workspace.xml
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<project version="4">
|
3
|
+
<component name="AutoImportSettings">
|
4
|
+
<option name="autoReloadType" value="SELECTIVE" />
|
5
|
+
</component>
|
6
|
+
<component name="ChangeListManager">
|
7
|
+
<list default="true" id="9f25f96c-828f-4aea-8886-dcbae369ba60" name="变更" comment="">
|
8
|
+
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
9
|
+
<change beforePath="$PROJECT_DIR$/lib/zabbix_manager/version.rb" beforeDir="false" afterPath="$PROJECT_DIR$/lib/zabbix_manager/version.rb" afterDir="false" />
|
10
|
+
</list>
|
11
|
+
<option name="SHOW_DIALOG" value="false" />
|
12
|
+
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
13
|
+
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
14
|
+
<option name="LAST_RESOLUTION" value="IGNORE" />
|
15
|
+
</component>
|
16
|
+
<component name="Git.Settings">
|
17
|
+
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
|
18
|
+
</component>
|
19
|
+
<component name="JsBowerSettings">
|
20
|
+
<bower-package value="" />
|
21
|
+
<bower.json value="" />
|
22
|
+
</component>
|
23
|
+
<component name="ProjectId" id="265x23SpJKPxcHuoXgA68nnhQDg" />
|
24
|
+
<component name="ProjectViewState">
|
25
|
+
<option name="hideEmptyMiddlePackages" value="true" />
|
26
|
+
<option name="showLibraryContents" value="true" />
|
27
|
+
</component>
|
28
|
+
<component name="PropertiesComponent">
|
29
|
+
<property name="RunOnceActivity.OpenProjectViewOnStart" value="true" />
|
30
|
+
<property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
|
31
|
+
<property name="WebServerToolWindowFactoryState" value="false" />
|
32
|
+
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
|
33
|
+
<property name="node.js.detected.package.eslint" value="true" />
|
34
|
+
<property name="node.js.selected.package.eslint" value="(autodetect)" />
|
35
|
+
<property name="node.js.selected.package.tslint" value="(autodetect)" />
|
36
|
+
<property name="nodejs_interpreter_path" value="node" />
|
37
|
+
<property name="nodejs_package_manager_path" value="yarn" />
|
38
|
+
<property name="settings.editor.selected.configurable" value="org.jetbrains.plugins.ruby.settings.RubyActiveModuleSdkConfigurable" />
|
39
|
+
<property name="vue.rearranger.settings.migration" value="true" />
|
40
|
+
</component>
|
41
|
+
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="应用程序级" UseSingleDictionary="true" transferred="true" />
|
42
|
+
<component name="SpringUtil" SPRING_PRE_LOADER_OPTION="true" RAKE_SPRING_PRE_LOADER_OPTION="true" RAILS_SPRING_PRE_LOADER_OPTION="true" />
|
43
|
+
<component name="TaskManager">
|
44
|
+
<task active="true" id="Default" summary="默认任务">
|
45
|
+
<changelist id="9f25f96c-828f-4aea-8886-dcbae369ba60" name="变更" comment="" />
|
46
|
+
<created>1646727041577</created>
|
47
|
+
<option name="number" value="Default" />
|
48
|
+
<option name="presentableId" value="Default" />
|
49
|
+
<updated>1646727041577</updated>
|
50
|
+
<workItem from="1646727043249" duration="3509000" />
|
51
|
+
<workItem from="1646732978147" duration="8000" />
|
52
|
+
</task>
|
53
|
+
<servers />
|
54
|
+
</component>
|
55
|
+
<component name="TypeScriptGeneratedFilesManager">
|
56
|
+
<option name="version" value="3" />
|
57
|
+
</component>
|
58
|
+
<component name="Vcs.Log.Tabs.Properties">
|
59
|
+
<option name="TAB_STATES">
|
60
|
+
<map>
|
61
|
+
<entry key="MAIN">
|
62
|
+
<value>
|
63
|
+
<State />
|
64
|
+
</value>
|
65
|
+
</entry>
|
66
|
+
</map>
|
67
|
+
</option>
|
68
|
+
</component>
|
69
|
+
</project>
|
@@ -0,0 +1,37 @@
|
|
1
|
+
class ZabbixManager
|
2
|
+
class Basic
|
3
|
+
# Get Zabbix object data from API by id
|
4
|
+
#
|
5
|
+
# @param data [Hash] Should include object's id field name (identify) and id value
|
6
|
+
# @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
|
7
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
8
|
+
# @return [Hash]
|
9
|
+
def get(data)
|
10
|
+
get_full_data(data)
|
11
|
+
end
|
12
|
+
|
13
|
+
# Add new Zabbix object using API create
|
14
|
+
#
|
15
|
+
# @param data [Hash]
|
16
|
+
# @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
|
17
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
18
|
+
# @return [Integer] The object id if a single object is created
|
19
|
+
# @return [Boolean] True/False if multiple objects are created
|
20
|
+
def add(data)
|
21
|
+
create(data)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Destroy Zabbix object using API delete
|
25
|
+
#
|
26
|
+
# @param data [Hash] Should include object's id field name (identify) and id value
|
27
|
+
# @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
|
28
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
29
|
+
# @return [Integer] The object id if a single object is deleted
|
30
|
+
# @return [Boolean] True/False if multiple objects are deleted
|
31
|
+
def destroy(data)
|
32
|
+
delete(data)
|
33
|
+
end
|
34
|
+
|
35
|
+
def method_name; end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
class ZabbixManager
|
2
|
+
class Basic
|
3
|
+
# 新增 get_hostgroup_ids 方法,使用列表 flatten 功能拉平属组对象
|
4
|
+
def get_hostgroup_ids(data)
|
5
|
+
result = @client.api_request(
|
6
|
+
method: 'hostgroup.get',
|
7
|
+
params: {
|
8
|
+
output: "extend",
|
9
|
+
filter: {
|
10
|
+
name: [data].flatten
|
11
|
+
}
|
12
|
+
}
|
13
|
+
).map { |item| { groupid: item['groupid'] } }
|
14
|
+
|
15
|
+
# 检查是是否存在
|
16
|
+
result.empty? ? nil : result.flatten
|
17
|
+
end
|
18
|
+
|
19
|
+
# 新增 get_or_create_hostgroups 方法,查询或创建新的对象
|
20
|
+
def get_or_create_hostgroups(data)
|
21
|
+
[data].flatten.each do |item|
|
22
|
+
# 是否存在设备属组,不存在则新建
|
23
|
+
begin
|
24
|
+
result = get_hostgroup_ids(item)
|
25
|
+
@client.api_request(
|
26
|
+
method: 'hostgroup.create',
|
27
|
+
params: {
|
28
|
+
name: item
|
29
|
+
}
|
30
|
+
) if result.nil?
|
31
|
+
rescue => e
|
32
|
+
ap e
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
class ZabbixManager
|
2
|
+
class Basic
|
3
|
+
# Log messages to stdout when debugging
|
4
|
+
#
|
5
|
+
# @param message [String]
|
6
|
+
def log(message)
|
7
|
+
puts message.to_s if @client.options[:debug]
|
8
|
+
end
|
9
|
+
|
10
|
+
# Compare two hashes for equality
|
11
|
+
#
|
12
|
+
# @param first_hash [Hash]
|
13
|
+
# @param second_hash [Hash]
|
14
|
+
# @return [Boolean]
|
15
|
+
def hash_equals?(first_hash, second_hash)
|
16
|
+
normalized_first_hash = normalize_hash(first_hash)
|
17
|
+
normalized_second_hash = normalize_hash(second_hash)
|
18
|
+
|
19
|
+
hash1 = normalized_first_hash.merge(normalized_second_hash)
|
20
|
+
hash2 = normalized_second_hash.merge(normalized_first_hash)
|
21
|
+
hash1 == hash2
|
22
|
+
end
|
23
|
+
|
24
|
+
# Convert all hash/array keys to symbols
|
25
|
+
#
|
26
|
+
# @param object [Array, Hash]
|
27
|
+
# @return [Array, Hash]
|
28
|
+
def symbolize_keys(object)
|
29
|
+
if object.is_a?(Array)
|
30
|
+
object.each_with_index do |val, index|
|
31
|
+
object[index] = symbolize_keys(val)
|
32
|
+
end
|
33
|
+
elsif object.is_a?(Hash)
|
34
|
+
object.keys.each do |key|
|
35
|
+
object[key.to_sym] = symbolize_keys(object.delete(key))
|
36
|
+
end
|
37
|
+
end
|
38
|
+
object
|
39
|
+
end
|
40
|
+
|
41
|
+
# Normalize all hash values to strings
|
42
|
+
#
|
43
|
+
# @param hash [Hash]
|
44
|
+
# @return [Hash]
|
45
|
+
def normalize_hash(hash)
|
46
|
+
result = hash.dup
|
47
|
+
|
48
|
+
result.delete(:hostid) # TODO: remove to logig. TemplateID and HostID has different id
|
49
|
+
|
50
|
+
result.each do |key, value|
|
51
|
+
result[key] = value.is_a?(Array) ? normalize_array(value) : value.to_s
|
52
|
+
end
|
53
|
+
|
54
|
+
result
|
55
|
+
end
|
56
|
+
|
57
|
+
# Normalize all array values to strings
|
58
|
+
#
|
59
|
+
# @param array [Array]
|
60
|
+
# @return [Array]
|
61
|
+
def normalize_array(array)
|
62
|
+
result = []
|
63
|
+
|
64
|
+
array.each do |e|
|
65
|
+
if e.is_a?(Array)
|
66
|
+
result.push(normalize_array(e))
|
67
|
+
elsif e.is_a?(Hash)
|
68
|
+
result.push(normalize_hash(e))
|
69
|
+
else
|
70
|
+
result.push(e.to_s)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
result
|
75
|
+
end
|
76
|
+
|
77
|
+
# Parse a data hash for id key or boolean to return
|
78
|
+
#
|
79
|
+
# @param data [Hash]
|
80
|
+
# @return [Integer] The object id if a single object hash is provided with key
|
81
|
+
# @return [Boolean] True/False if multiple class object hash is provided
|
82
|
+
def parse_keys(data)
|
83
|
+
case data
|
84
|
+
when Hash
|
85
|
+
data.empty? ? nil : data[keys][0].to_i
|
86
|
+
when TrueClass
|
87
|
+
true
|
88
|
+
when FalseClass
|
89
|
+
false
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
# Merge two hashes into a single new hash
|
94
|
+
#
|
95
|
+
# @param first_hash [Hash]
|
96
|
+
# @param second_hash [Hash]
|
97
|
+
# @return [Hash]
|
98
|
+
def merge_params(first_hash, second_hash)
|
99
|
+
new = first_hash.dup
|
100
|
+
new.merge(second_hash)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
class ZabbixManager
|
2
|
+
class Basic
|
3
|
+
# Initializes a new Basic object with ZabbixManager Client
|
4
|
+
#
|
5
|
+
# @param client [ZabbixManager::Client]
|
6
|
+
# @return [ZabbixManager::Client]
|
7
|
+
def initialize(client)
|
8
|
+
@client = client
|
9
|
+
end
|
10
|
+
|
11
|
+
# Placeholder for inherited objects to provide object-specific method name
|
12
|
+
#
|
13
|
+
# @raise [ManagerError] Basic object does not directly support method_name
|
14
|
+
def method_name
|
15
|
+
raise ManagerError.new("Can't call method_name here")
|
16
|
+
end
|
17
|
+
|
18
|
+
# Placeholder for inherited objects to provide default options
|
19
|
+
#
|
20
|
+
# @return [Hash]
|
21
|
+
def default_options
|
22
|
+
{}
|
23
|
+
end
|
24
|
+
|
25
|
+
# Returns the object's plural id field name (identify) based on key
|
26
|
+
#
|
27
|
+
# @return [String]
|
28
|
+
def keys
|
29
|
+
key + 's'
|
30
|
+
end
|
31
|
+
|
32
|
+
# Returns the object's id field name (identify) based on method_name + id
|
33
|
+
#
|
34
|
+
# @return [String]
|
35
|
+
def key
|
36
|
+
method_name + 'id'
|
37
|
+
end
|
38
|
+
|
39
|
+
# Placeholder for inherited objects to provide object-specific id field name
|
40
|
+
#
|
41
|
+
# @raise [ManagerError] Basic object does not directly support identify
|
42
|
+
def identify
|
43
|
+
raise ManagerError.new("Can't call identify here")
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,227 @@
|
|
1
|
+
class ZabbixManager
|
2
|
+
class Basic
|
3
|
+
# Create new Zabbix object using API (with defaults)
|
4
|
+
#
|
5
|
+
# @param data [Hash]
|
6
|
+
# @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
|
7
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
8
|
+
# @return [Integer] The object id if a single object is created
|
9
|
+
# @return [Boolean] True/False if multiple objects are created
|
10
|
+
def create(data)
|
11
|
+
log "[DEBUG] Call create with parameters: #{data.inspect}"
|
12
|
+
|
13
|
+
# 判断是否绑定默认选项并重新生成配置
|
14
|
+
data_with_default = default_options.empty? ? data : merge_params(default_options, data)
|
15
|
+
data_create = [data_with_default]
|
16
|
+
|
17
|
+
# 调用实例方法
|
18
|
+
result = @client.api_request(method: "#{method_name}.create", params: data_create)
|
19
|
+
# 判断是否执行成功并返回结果
|
20
|
+
parse_keys result
|
21
|
+
end
|
22
|
+
|
23
|
+
# Delete Zabbix object using API
|
24
|
+
#
|
25
|
+
# @param data [Hash] Should include object's id field name (identify) and id value
|
26
|
+
# @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
|
27
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
28
|
+
# @return [Integer] The object id if a single object is deleted
|
29
|
+
# @return [Boolean] True/False if multiple objects are deleted
|
30
|
+
def delete(data)
|
31
|
+
log "[DEBUG] Call delete with parameters: #{data.inspect}"
|
32
|
+
|
33
|
+
data_delete = [data]
|
34
|
+
result = @client.api_request(method: "#{method_name}.delete", params: data_delete)
|
35
|
+
parse_keys result
|
36
|
+
end
|
37
|
+
|
38
|
+
# Create or update Zabbix object using API
|
39
|
+
#
|
40
|
+
# @param data [Hash] Should include object's id field name (identify) and id value
|
41
|
+
# @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
|
42
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
43
|
+
# @return [Integer] The object id if a single object is created
|
44
|
+
# @return [Boolean] True/False if multiple objects are created
|
45
|
+
def create_or_update(data)
|
46
|
+
log "[DEBUG] Call create_or_update with parameters: #{data.inspect}"
|
47
|
+
|
48
|
+
id = get_id(identify.to_sym => data[identify.to_sym])
|
49
|
+
id ? update(data.merge(key.to_sym => id.to_s)) : create(data)
|
50
|
+
end
|
51
|
+
|
52
|
+
# Update Zabbix object using API
|
53
|
+
#
|
54
|
+
# @param data [Hash] Should include object's id field name (identify) and id value
|
55
|
+
# @param force [Boolean] Whether to force an object update even if provided data matches Zabbix
|
56
|
+
# @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
|
57
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
58
|
+
# @return [Integer] The object id if a single object is created
|
59
|
+
# @return [Boolean] True/False if multiple objects are created
|
60
|
+
def update(data, force = false)
|
61
|
+
log "[DEBUG] Call update with parameters: #{data.inspect}"
|
62
|
+
dump = {}
|
63
|
+
dump_by_id(key.to_sym => data[key.to_sym]).each do |item|
|
64
|
+
dump = symbolize_keys(item) if item[key].to_i == data[key.to_sym].to_i
|
65
|
+
end
|
66
|
+
if hash_equals?(dump, data) && !force
|
67
|
+
log "[DEBUG] Equal keys #{dump} and #{data}, skip update"
|
68
|
+
data[key.to_sym].to_i
|
69
|
+
else
|
70
|
+
data_update = [data]
|
71
|
+
result = @client.api_request(method: "#{method_name}.update", params: data_update)
|
72
|
+
parse_keys result
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
# mojo_update 请求补丁函数
|
77
|
+
def mojo_update(method, data)
|
78
|
+
log "[DEBUG] Call update with parameters: #{data.inspect}"
|
79
|
+
@client.api_request(method: method, params: data)
|
80
|
+
end
|
81
|
+
|
82
|
+
# MOJO_UPDATE
|
83
|
+
|
84
|
+
# Get full/extended Zabbix object data from API
|
85
|
+
#
|
86
|
+
# @param data [Hash] Should include object's id field name (identify) and id value
|
87
|
+
# @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
|
88
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
89
|
+
# @return [Hash]
|
90
|
+
def get_full_data(data)
|
91
|
+
log "[DEBUG] Call get_full_data with parameters: #{data.inspect}"
|
92
|
+
|
93
|
+
@client.api_request(
|
94
|
+
method: "#{method_name}.get",
|
95
|
+
params: {
|
96
|
+
filter: {
|
97
|
+
identify.to_sym => data[identify.to_sym]
|
98
|
+
},
|
99
|
+
output: 'extend'
|
100
|
+
}
|
101
|
+
)
|
102
|
+
end
|
103
|
+
|
104
|
+
# Get raw Zabbix object data from API
|
105
|
+
#
|
106
|
+
# @param data [Hash]
|
107
|
+
# @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
|
108
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
109
|
+
# @return [Hash]
|
110
|
+
def get_raw(data)
|
111
|
+
log "[DEBUG] Call get_raw with parameters: #{data.inspect}"
|
112
|
+
|
113
|
+
@client.api_request(
|
114
|
+
method: "#{method_name}.get",
|
115
|
+
params: data
|
116
|
+
)
|
117
|
+
end
|
118
|
+
|
119
|
+
# Dump Zabbix object data by key from API
|
120
|
+
#
|
121
|
+
# @param data [Hash] Should include desired object's key and value
|
122
|
+
# @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
|
123
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
124
|
+
# @return [Hash]
|
125
|
+
def dump_by_id(data)
|
126
|
+
log "[DEBUG] Call dump_by_id with parameters: #{data.inspect}"
|
127
|
+
|
128
|
+
@client.api_request(
|
129
|
+
method: "#{method_name}.get",
|
130
|
+
params: {
|
131
|
+
filter: {
|
132
|
+
key.to_sym => data[key.to_sym]
|
133
|
+
},
|
134
|
+
output: 'extend'
|
135
|
+
}
|
136
|
+
)
|
137
|
+
end
|
138
|
+
|
139
|
+
# Get full/extended Zabbix data for all objects of type/class from API
|
140
|
+
#
|
141
|
+
# @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
|
142
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
143
|
+
# @return [Array<Hash>] Array of matching objects
|
144
|
+
def all
|
145
|
+
result = {}
|
146
|
+
@client.api_request(
|
147
|
+
method: "#{method_name}.get",
|
148
|
+
params: { output: 'extend' }
|
149
|
+
).each do |item|
|
150
|
+
result[item[identify]] = item[key]
|
151
|
+
end
|
152
|
+
result
|
153
|
+
end
|
154
|
+
|
155
|
+
# Get Zabbix object id from API based on provided data
|
156
|
+
#
|
157
|
+
# @param data [Hash]
|
158
|
+
# @raise [ManagerError] Error returned when there is a problem with the Zabbix API call or missing object's id field name (identify).
|
159
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
160
|
+
# @return [Integer] Zabbix object id
|
161
|
+
def get_id(data)
|
162
|
+
log "[DEBUG] Call get_id with parameters: #{data.inspect}"
|
163
|
+
|
164
|
+
# symbolize keys if the user used string keys instead of symbols
|
165
|
+
data = symbolize_keys(data) if data.key?(identify)
|
166
|
+
# raise an error if identify name was not supplied
|
167
|
+
name = data[identify.to_sym]
|
168
|
+
raise ManagerError.new("#{identify} not supplied in call to get_id") if name.nil?
|
169
|
+
|
170
|
+
result = @client.api_request(
|
171
|
+
method: "#{method_name}.get",
|
172
|
+
params: {
|
173
|
+
filter: data,
|
174
|
+
output: [key, identify]
|
175
|
+
}
|
176
|
+
)
|
177
|
+
id = nil
|
178
|
+
result.each { |item| id = item[key].to_i if item[identify] == data[identify.to_sym] }
|
179
|
+
id
|
180
|
+
end
|
181
|
+
|
182
|
+
# Get or Create Zabbix object using API
|
183
|
+
#
|
184
|
+
# @param data [Hash] Should include object's id field name (identify) and id value
|
185
|
+
# @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
|
186
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
187
|
+
# @return [Integer] Zabbix object id
|
188
|
+
def get_or_create(data)
|
189
|
+
log "[DEBUG] Call get_or_create with parameters: #{data.inspect}"
|
190
|
+
|
191
|
+
unless (id = get_id(identify.to_sym => data[identify.to_sym]))
|
192
|
+
id = create(data)
|
193
|
+
end
|
194
|
+
id
|
195
|
+
end
|
196
|
+
|
197
|
+
# 根据数据自动创建
|
198
|
+
def create_raw(data)
|
199
|
+
log "[DEBUG] Call create_raw with parameters: #{data.inspect}"
|
200
|
+
# 请求创建数据
|
201
|
+
@client.api_request(
|
202
|
+
method: "#{method_name}.create",
|
203
|
+
params: data
|
204
|
+
)
|
205
|
+
end
|
206
|
+
|
207
|
+
# 根据数据自动更新数据
|
208
|
+
def update_raw(data)
|
209
|
+
log "[DEBUG] Call update_raw with parameters: #{data.inspect}"
|
210
|
+
# 请求创建数据
|
211
|
+
@client.api_request(
|
212
|
+
method: "#{method_name}.update",
|
213
|
+
params: data
|
214
|
+
)
|
215
|
+
end
|
216
|
+
|
217
|
+
# 根据数据自动删除数据
|
218
|
+
def delete_raw(data)
|
219
|
+
log "[DEBUG] Call delete_raw with parameters: #{data.inspect}"
|
220
|
+
# 请求创建数据
|
221
|
+
@client.api_request(
|
222
|
+
method: "#{method_name}.delete",
|
223
|
+
params: data
|
224
|
+
)
|
225
|
+
end
|
226
|
+
end
|
227
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
class ZabbixManager
|
2
|
+
class Actions < Basic
|
3
|
+
# The method name used for interacting with Actions via Zabbix API
|
4
|
+
#
|
5
|
+
# @return [String]
|
6
|
+
def method_name
|
7
|
+
'action'
|
8
|
+
end
|
9
|
+
|
10
|
+
# The id field name used for identifying specific Action objects via Zabbix API
|
11
|
+
#
|
12
|
+
# @return [String]
|
13
|
+
def identify
|
14
|
+
'name'
|
15
|
+
end
|
16
|
+
|
17
|
+
# Get full/extended Action object data from API
|
18
|
+
#
|
19
|
+
# @param data [Hash] Should include object's id field name (identify) and id value
|
20
|
+
# @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
|
21
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
22
|
+
# @return [Hash]
|
23
|
+
def get_full_data(data)
|
24
|
+
log "[DEBUG] Call get_full_data with parameters: #{data.inspect}"
|
25
|
+
|
26
|
+
@client.api_request(
|
27
|
+
method: "#{method_name}.get",
|
28
|
+
params: {
|
29
|
+
filter: {
|
30
|
+
identify.to_sym => data[identify.to_sym]
|
31
|
+
},
|
32
|
+
output: 'extend',
|
33
|
+
selectOperations: "extend",
|
34
|
+
selectRecoveryOperations: "extend",
|
35
|
+
selectAcknowledgeOperations: "extend",
|
36
|
+
selectFilter: "extend",
|
37
|
+
}
|
38
|
+
)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
class ZabbixManager
|
2
|
+
class Applications < Basic
|
3
|
+
# The method name used for interacting with Applications via Zabbix API
|
4
|
+
#
|
5
|
+
# @return [String]
|
6
|
+
def method_name
|
7
|
+
'application'
|
8
|
+
end
|
9
|
+
|
10
|
+
# The id field name used for identifying specific Application objects via Zabbix API
|
11
|
+
#
|
12
|
+
# @return [String]
|
13
|
+
def identify
|
14
|
+
'name'
|
15
|
+
end
|
16
|
+
|
17
|
+
# Get or Create Application object using Zabbix API
|
18
|
+
#
|
19
|
+
# @param data [Hash] Needs to include name and hostid to properly identify Applications via Zabbix API
|
20
|
+
# @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
|
21
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
22
|
+
# @return [Integer] Zabbix object id
|
23
|
+
def get_or_create(data)
|
24
|
+
log "[DEBUG] Call get_or_create with parameters: #{data.inspect}"
|
25
|
+
|
26
|
+
unless (id = get_id(name: data[:name], hostid: data[:hostid]))
|
27
|
+
id = create(data)
|
28
|
+
end
|
29
|
+
id
|
30
|
+
end
|
31
|
+
|
32
|
+
# Create or update Application object using Zabbix API
|
33
|
+
#
|
34
|
+
# @param data [Hash] Needs to include name and hostid to properly identify Applications via Zabbix API
|
35
|
+
# @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
|
36
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
37
|
+
# @return [Integer] Zabbix object id
|
38
|
+
def create_or_update(data)
|
39
|
+
applicationid = get_id(name: data[:name], hostid: data[:hostid])
|
40
|
+
applicationid ? update(data.merge(applicationid: applicationid)) : create(data)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
class ZabbixManager
|
2
|
+
class Configurations < Basic
|
3
|
+
# @return [Boolean]
|
4
|
+
def array_flag
|
5
|
+
true
|
6
|
+
end
|
7
|
+
|
8
|
+
# The method name used for interacting with Configurations via Zabbix API
|
9
|
+
#
|
10
|
+
# @return [String]
|
11
|
+
def method_name
|
12
|
+
'configuration'
|
13
|
+
end
|
14
|
+
|
15
|
+
# The id field name used for identifying specific Configuration objects via Zabbix API
|
16
|
+
#
|
17
|
+
# @return [String]
|
18
|
+
def identify
|
19
|
+
'host'
|
20
|
+
end
|
21
|
+
|
22
|
+
# Export configuration data using Zabbix API
|
23
|
+
#
|
24
|
+
# @param data [Hash]
|
25
|
+
# @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
|
26
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
27
|
+
# @return [Hash]
|
28
|
+
def export(data)
|
29
|
+
@client.api_request(method: 'configuration.export', params: data)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Import configuration data using Zabbix API
|
33
|
+
#
|
34
|
+
# @param data [Hash]
|
35
|
+
# @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
|
36
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
37
|
+
# @return [Hash]
|
38
|
+
def import(data)
|
39
|
+
@client.api_request(method: 'configuration.import', params: data)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|