Class: ZabbixApi::Basic
- Inherits:
-
Object
show all
- Defined in:
- lib/zabbixapi/basic/basic_func.rb,
lib/zabbixapi/basic/basic_init.rb,
lib/zabbixapi/basic/basic_alias.rb,
lib/zabbixapi/basic/basic_logic.rb
Direct Known Subclasses
Configurations, Graphs, HostGroups, Hosts, Items, Mediatypes, Proxies, Screens, Templates, Triggers, Usergroups, Usermacros, Users
Instance Method Summary
collapse
Constructor Details
#initialize(client) ⇒ Basic
Returns a new instance of Basic.
4
5
6
|
# File 'lib/zabbixapi/basic/basic_init.rb', line 4
def initialize(client)
@client = client
end
|
Instance Method Details
#add(data) ⇒ Object
8
9
10
|
# File 'lib/zabbixapi/basic/basic_alias.rb', line 8
def add(data)
create(data)
end
|
#all ⇒ Object
76
77
78
79
80
81
82
|
# File 'lib/zabbixapi/basic/basic_logic.rb', line 76
def all
result = {}
@client.api_request(:method => "#{method_name}.get", :params => {:output => "extend"}).each do |item|
result[item[indentify]] = item[key]
end
result
end
|
#create(data) ⇒ Object
4
5
6
7
8
9
10
11
|
# File 'lib/zabbixapi/basic/basic_logic.rb', line 4
def create(data)
log "[DEBUG] Call create with parametrs: #{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) ⇒ Object
21
22
23
24
25
26
|
# File 'lib/zabbixapi/basic/basic_logic.rb', line 21
def create_or_update(data)
log "[DEBUG] Call create_or_update with parametrs: #{data.inspect}"
id = get_id(indentify.to_sym => data[indentify.to_sym])
id ? update(data.merge(key.to_sym => id.to_s)) : create(data)
end
|
#default_options ⇒ Object
12
13
14
|
# File 'lib/zabbixapi/basic/basic_init.rb', line 12
def default_options
{}
end
|
#delete(data) ⇒ Object
13
14
15
16
17
18
19
|
# File 'lib/zabbixapi/basic/basic_logic.rb', line 13
def delete(data)
log "[DEBUG] Call delete with parametrs: #{data.inspect}"
data_delete = [data]
result = @client.api_request(:method => "#{method_name}.delete", :params => data_delete)
parse_keys result
end
|
#destroy(data) ⇒ Object
12
13
14
|
# File 'lib/zabbixapi/basic/basic_alias.rb', line 12
def destroy(data)
delete(data)
end
|
#dump_by_id(data) ⇒ Object
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/zabbixapi/basic/basic_logic.rb', line 62
def dump_by_id(data)
log "[DEBUG] Call dump_by_id with parametrs: #{data.inspect}"
@client.api_request(
:method => "#{method_name}.get",
:params => {
:filter => {
key.to_sym => data[key.to_sym]
},
:output => "extend"
}
)
end
|
#get(data) ⇒ Object
4
5
6
|
# File 'lib/zabbixapi/basic/basic_alias.rb', line 4
def get(data)
get_full_data(data)
end
|
#get_full_data(data) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/zabbixapi/basic/basic_logic.rb', line 48
def get_full_data(data)
log "[DEBUG] Call get_full_data with parametrs: #{data.inspect}"
@client.api_request(
:method => "#{method_name}.get",
:params => {
:filter => {
indentify.to_sym => data[indentify.to_sym]
},
:output => "extend"
}
)
end
|
#get_id(data) ⇒ Object
84
85
86
87
88
89
90
91
|
# File 'lib/zabbixapi/basic/basic_logic.rb', line 84
def get_id(data)
log "[DEBUG] Call get_id with parametrs: #{data.inspect}"
result = symbolize_keys( get_full_data(data) )
id = nil
result.each { |item| id = item[key.to_sym].to_i if item[indentify.to_sym] == data[indentify.to_sym] }
id
end
|
#get_or_create(data) ⇒ Object
93
94
95
96
97
98
99
100
|
# File 'lib/zabbixapi/basic/basic_logic.rb', line 93
def get_or_create(data)
log "[DEBUG] Call get_or_create with parametrs: #{data.inspect}"
unless (id = get_id(data))
id = create(data)
end
id
end
|
#hash_equals?(a, b) ⇒ Boolean
8
9
10
11
12
13
14
|
# File 'lib/zabbixapi/basic/basic_func.rb', line 8
def hash_equals?(a, b)
a_new = normalize_hash(a)
b_new = normalize_hash(b)
hash1 = a_new.merge(b_new)
hash2 = b_new.merge(a_new)
hash1 == hash2
end
|
#indentify ⇒ Object
24
25
26
|
# File 'lib/zabbixapi/basic/basic_init.rb', line 24
def indentify
raise "Can't call indentify here"
end
|
#key ⇒ Object
20
21
22
|
# File 'lib/zabbixapi/basic/basic_init.rb', line 20
def key
method_name + "id"
end
|
#keys ⇒ Object
16
17
18
|
# File 'lib/zabbixapi/basic/basic_init.rb', line 16
def keys
key + "s"
end
|
#log(message) ⇒ Object
4
5
6
|
# File 'lib/zabbixapi/basic/basic_func.rb', line 4
def log(message)
puts "#{message}" if @client.options[:debug]
end
|
#merge_params(a, b) ⇒ Object
49
50
51
52
|
# File 'lib/zabbixapi/basic/basic_func.rb', line 49
def merge_params(a, b)
new = a.dup
new.merge(b)
end
|
#method_name ⇒ Object
8
9
10
|
# File 'lib/zabbixapi/basic/basic_init.rb', line 8
def method_name
raise "Can't call method_name here"
end
|
#normalize_hash(hash) ⇒ Object
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/zabbixapi/basic/basic_func.rb', line 22
def normalize_hash(hash)
result = hash.dup
result.delete(:hostid) result.each do |key, value|
case value
when Array
result.delete(key)
else
result[key] = value.to_s
end
end
result
end
|
#parse_keys(data) ⇒ Object
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/zabbixapi/basic/basic_func.rb', line 36
def parse_keys(data)
case data
when Hash
data.empty? ? nil : data[keys][0].to_i
when TrueClass
true
when FalseClass
false
else
nil
end
end
|
#symbolize_keys(obj) ⇒ Object
16
17
18
19
20
|
# File 'lib/zabbixapi/basic/basic_func.rb', line 16
def symbolize_keys(obj)
return obj.inject({}){|memo,(k,v)| memo[k.to_sym] = symbolize_keys(v); memo} if obj.is_a? Hash
return obj.inject([]){|memo,v | memo << symbolize_keys(v); memo} if obj.is_a? Array
obj
end
|
#update(data) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/zabbixapi/basic/basic_logic.rb', line 28
def update(data)
log "[DEBUG] Call update with parametrs: #{data.inspect}"
dump = {}
item_id = data[key.to_sym].to_i
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)
log "[DEBUG] Equal keys #{dump} and #{data}, skip update"
item_id
else
data_update = [data]
result = @client.api_request(:method => "#{method_name}.update", :params => data_update)
parse_keys result
end
end
|