Class: ZabbixApi::Basic
- Inherits:
-
Object
- Object
- ZabbixApi::Basic
- 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
Actions, Applications, Configurations, Drules, Events, Graphs, HostGroups, Hosts, HttpTests, Items, Maintenance, Mediatypes, Proxies, Screens, Scripts, Templates, Triggers, Usergroups, Usermacros, Users, ValueMaps
Instance Method Summary collapse
-
#add(data) ⇒ Integer, Boolean
Add new Zabbix object using API create.
-
#all ⇒ Array<Hash>
Get full/extended Zabbix data for all objects of type/class from API.
-
#create(data) ⇒ Integer, Boolean
Create new Zabbix object using API (with defaults).
-
#create_or_update(data) ⇒ Integer, Boolean
Create or update Zabbix object using API.
-
#default_options ⇒ Hash
Placeholder for inherited objects to provide default options.
-
#delete(data) ⇒ Integer, Boolean
Delete Zabbix object using API.
-
#destroy(data) ⇒ Integer, Boolean
Destroy Zabbix object using API delete.
-
#dump_by_id(data) ⇒ Hash
Dump Zabbix object data by key from API.
-
#get(data) ⇒ Hash
Get Zabbix object data from API by id.
-
#get_full_data(data) ⇒ Hash
Get full/extended Zabbix object data from API.
-
#get_id(data) ⇒ Integer
Get Zabbix object id from API based on provided data.
-
#get_or_create(data) ⇒ Integer
Get or Create Zabbix object using API.
-
#get_raw(data) ⇒ Hash
Get raw Zabbix object data from API.
-
#hash_equals?(first_hash, second_hash) ⇒ Boolean
Compare two hashes for equality.
-
#indentify ⇒ Object
Placeholder for inherited objects to provide object-specific id field name.
-
#initialize(client) ⇒ ZabbixApi::Client
constructor
Initializes a new Basic object with ZabbixApi Client.
-
#key ⇒ String
Returns the object’s id field name (indentify) based on method_name + id.
-
#keys ⇒ String
Returns the object’s plural id field name (indentify) based on key.
-
#log(message) ⇒ Object
Log messages to stdout when debugging.
-
#merge_params(first_hash, second_hash) ⇒ Hash
Merge two hashes into a single new hash.
-
#method_name ⇒ Object
Placeholder for inherited objects to provide object-specific method name.
-
#normalize_array(array) ⇒ Array
Normalize all array values to strings.
-
#normalize_hash(hash) ⇒ Hash
Normalize all hash values to strings.
-
#parse_keys(data) ⇒ Integer, Boolean
Parse a data hash for id key or boolean to return.
-
#symbolize_keys(object) ⇒ Array, Hash
Convert all hash/array keys to symbols.
-
#update(data, force = false) ⇒ Integer, Boolean
Update Zabbix object using API.
Constructor Details
#initialize(client) ⇒ ZabbixApi::Client
Initializes a new Basic object with ZabbixApi Client
7 8 9 |
# File 'lib/zabbixapi/basic/basic_init.rb', line 7 def initialize(client) @client = client end |
Instance Method Details
#add(data) ⇒ Integer, Boolean
Add new Zabbix object using API create
20 21 22 |
# File 'lib/zabbixapi/basic/basic_alias.rb', line 20 def add(data) create(data) end |
#all ⇒ Array<Hash>
Get full/extended Zabbix data for all objects of type/class from API
132 133 134 135 136 137 138 |
# File 'lib/zabbixapi/basic/basic_logic.rb', line 132 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) ⇒ Integer, Boolean
Create new Zabbix object using API (with defaults)
10 11 12 13 14 15 16 17 |
# File 'lib/zabbixapi/basic/basic_logic.rb', line 10 def create(data) log "[DEBUG] Call create with parameters: #{data.inspect}" data_with_default = .empty? ? data : merge_params(, 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) ⇒ Integer, Boolean
Create or update Zabbix object using API
41 42 43 44 45 46 |
# File 'lib/zabbixapi/basic/basic_logic.rb', line 41 def create_or_update(data) log "[DEBUG] Call create_or_update with parameters: #{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 ⇒ Hash
Placeholder for inherited objects to provide default options
21 22 23 |
# File 'lib/zabbixapi/basic/basic_init.rb', line 21 def {} end |
#delete(data) ⇒ Integer, Boolean
Delete Zabbix object using API
26 27 28 29 30 31 32 |
# File 'lib/zabbixapi/basic/basic_logic.rb', line 26 def delete(data) log "[DEBUG] Call delete with parameters: #{data.inspect}" data_delete = [data] result = @client.api_request(method: "#{method_name}.delete", params: data_delete) parse_keys result end |
#destroy(data) ⇒ Integer, Boolean
Destroy Zabbix object using API delete
31 32 33 |
# File 'lib/zabbixapi/basic/basic_alias.rb', line 31 def destroy(data) delete(data) end |
#dump_by_id(data) ⇒ Hash
Dump Zabbix object data by key from API
113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/zabbixapi/basic/basic_logic.rb', line 113 def dump_by_id(data) log "[DEBUG] Call dump_by_id with parameters: #{data.inspect}" @client.api_request( method: "#{method_name}.get", params: { filter: { key.to_sym => data[key.to_sym] }, output: 'extend' } ) end |
#get(data) ⇒ Hash
Get Zabbix object data from API by id
9 10 11 |
# File 'lib/zabbixapi/basic/basic_alias.rb', line 9 def get(data) get_full_data(data) end |
#get_full_data(data) ⇒ Hash
Get full/extended Zabbix object data from API
78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/zabbixapi/basic/basic_logic.rb', line 78 def get_full_data(data) log "[DEBUG] Call get_full_data with parameters: #{data.inspect}" @client.api_request( method: "#{method_name}.get", params: { filter: { indentify.to_sym => data[indentify.to_sym] }, output: 'extend' } ) end |
#get_id(data) ⇒ Integer
Get Zabbix object id from API based on provided data
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/zabbixapi/basic/basic_logic.rb', line 146 def get_id(data) log "[DEBUG] Call get_id with parameters: #{data.inspect}" # symbolize keys if the user used string keys instead of symbols data = symbolize_keys(data) if data.key?(indentify) # raise an error if indentify name was not supplied name = data[indentify.to_sym] raise ApiError.new("#{indentify} not supplied in call to get_id") if name.nil? result = @client.api_request( method: "#{method_name}.get", params: { filter: data, output: [key, indentify] } ) id = nil result.each { |item| id = item[key].to_i if item[indentify] == data[indentify.to_sym] } id end |
#get_or_create(data) ⇒ Integer
Get or Create Zabbix object using API
172 173 174 175 176 177 178 179 |
# File 'lib/zabbixapi/basic/basic_logic.rb', line 172 def get_or_create(data) log "[DEBUG] Call get_or_create with parameters: #{data.inspect}" unless (id = get_id(indentify.to_sym => data[indentify.to_sym])) id = create(data) end id end |
#get_raw(data) ⇒ Hash
Get raw Zabbix object data from API
98 99 100 101 102 103 104 105 |
# File 'lib/zabbixapi/basic/basic_logic.rb', line 98 def get_raw(data) log "[DEBUG] Call get_raw with parameters: #{data.inspect}" @client.api_request( method: "#{method_name}.get", params: data ) end |
#hash_equals?(first_hash, second_hash) ⇒ Boolean
Compare two hashes for equality
15 16 17 18 19 20 21 22 |
# File 'lib/zabbixapi/basic/basic_func.rb', line 15 def hash_equals?(first_hash, second_hash) normalized_first_hash = normalize_hash(first_hash) normalized_second_hash = normalize_hash(second_hash) hash1 = normalized_first_hash.merge(normalized_second_hash) hash2 = normalized_second_hash.merge(normalized_first_hash) hash1 == hash2 end |
#indentify ⇒ Object
Placeholder for inherited objects to provide object-specific id field name
42 43 44 |
# File 'lib/zabbixapi/basic/basic_init.rb', line 42 def indentify raise ApiError.new("Can't call indentify here") end |
#key ⇒ String
Returns the object’s id field name (indentify) based on method_name + id
35 36 37 |
# File 'lib/zabbixapi/basic/basic_init.rb', line 35 def key method_name + 'id' end |
#keys ⇒ String
Returns the object’s plural id field name (indentify) based on key
28 29 30 |
# File 'lib/zabbixapi/basic/basic_init.rb', line 28 def keys key + 's' end |
#log(message) ⇒ Object
Log messages to stdout when debugging
6 7 8 |
# File 'lib/zabbixapi/basic/basic_func.rb', line 6 def log() puts .to_s if @client.[:debug] end |
#merge_params(first_hash, second_hash) ⇒ Hash
Merge two hashes into a single new hash
98 99 100 101 |
# File 'lib/zabbixapi/basic/basic_func.rb', line 98 def merge_params(first_hash, second_hash) new = first_hash.dup new.merge(second_hash) end |
#method_name ⇒ Object
Placeholder for inherited objects to provide object-specific method name
14 15 16 |
# File 'lib/zabbixapi/basic/basic_init.rb', line 14 def method_name raise ApiError.new("Can't call method_name here") end |
#normalize_array(array) ⇒ Array
Normalize all array values to strings
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/zabbixapi/basic/basic_func.rb', line 61 def normalize_array(array) result = [] array.each do |e| if e.is_a?(Array) result.push(normalize_array(e)) elsif e.is_a?(Hash) result.push(normalize_hash(e)) else result.push(e.to_s) end end result end |
#normalize_hash(hash) ⇒ Hash
Normalize all hash values to strings
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/zabbixapi/basic/basic_func.rb', line 45 def normalize_hash(hash) result = hash.dup result.delete(:hostid) # TODO: remove to logig. TemplateID and HostID has different id result.each do |key, value| result[key] = value.is_a?(Array) ? normalize_array(value) : value.to_s end result end |
#parse_keys(data) ⇒ Integer, Boolean
Parse a data hash for id key or boolean to return
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/zabbixapi/basic/basic_func.rb', line 82 def parse_keys(data) case data when Hash data.empty? ? nil : data[keys][0].to_i when TrueClass true when FalseClass false end end |
#symbolize_keys(object) ⇒ Array, Hash
Convert all hash/array keys to symbols
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/zabbixapi/basic/basic_func.rb', line 28 def symbolize_keys(object) if object.is_a?(Array) object.each_with_index do |val, index| object[index] = symbolize_keys(val) end elsif object.is_a?(Hash) object.keys.each do |key| object[key.to_sym] = symbolize_keys(object.delete(key)) end end object end |
#update(data, force = false) ⇒ Integer, Boolean
Update Zabbix object using API
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/zabbixapi/basic/basic_logic.rb', line 56 def update(data, force = false) log "[DEBUG] Call update with parameters: #{data.inspect}" 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] Equal keys #{dump} and #{data}, skip update" 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 |