Class: Zabbix::ZabbixApi
- Inherits:
-
Object
- Object
- Zabbix::ZabbixApi
- Defined in:
- lib/zabbixapi/base.rb,
lib/zabbixapi/host.rb,
lib/zabbixapi/item.rb,
lib/zabbixapi/graph.rb,
lib/zabbixapi/group.rb,
lib/zabbixapi/screen.rb,
lib/zabbixapi/trigger.rb,
lib/zabbixapi/template.rb,
lib/zabbixapi/usermacro.rb,
lib/zabbixapi/application.rb
Instance Attribute Summary collapse
-
#debug ⇒ Object
Returns the value of attribute debug.
Instance Method Summary collapse
- #add_application(app_options) ⇒ Object
- #add_graph(graph) ⇒ Object
- #add_graph_to_screen(screen_id, graph_id, x, y) ⇒ Object
- #add_group(groupname) ⇒ Object
- #add_host(host_options) ⇒ Object
- #add_host_to_group(host_id, group_id) ⇒ Object
- #add_item(item) ⇒ Object
- #add_macro(host_id, macro_name, macro_value) ⇒ Object
- #add_screen(screen_name, hsize, vsize) ⇒ Object
- #add_template(template_options) ⇒ Object
- #add_trigger(trigger) ⇒ Object
- #auth ⇒ Object
- #del_all_graphs_from_screen(screen_id) ⇒ Object
- #do_request(message) ⇒ Object
- #get_graph_id(host_id, graph_name) ⇒ Object
- #get_graphs(host_id) ⇒ Object
- #get_group_id(pattern) ⇒ Object
- #get_host(params) ⇒ Object
- #get_host_id(hostname) ⇒ Object
- #get_item_id(host_id, item_name) ⇒ Object
- #get_macro(host_id, macro_name) ⇒ Object
- #get_screen_graph_ids(screen_id) ⇒ Object
- #get_screen_id(screen_name) ⇒ Object
- #get_screen_parameter(screen_name, param_name) ⇒ Object
- #get_template_id(template_name) ⇒ Object
- #get_template_ids_by_host(host_id) ⇒ Object
- #get_templates ⇒ Object
- #get_trigger_id(host_id, trigger_name) ⇒ Object
- #get_triggers_by_host(host_id) ⇒ Object
- #get_webitem_id(host_id, item_name) ⇒ Object
- #group_exist?(pattern) ⇒ Boolean
-
#initialize(api_url, api_user, api_password) ⇒ ZabbixApi
constructor
A new instance of ZabbixApi.
- #link_templates_with_hosts(templates_id, hosts_id) ⇒ Object
-
#merge_opt(a, b) ⇒ Object
Utils.
- #send_request(message) ⇒ Object
- #set_macro_value(host_id, macro_name, macro_value) ⇒ Object
- #set_screen_parameter(screen_id, param_name, param_value) ⇒ Object
- #unlink_templates_from_hosts(templates_id, hosts_id) ⇒ Object
- #update_item(item_id) ⇒ Object
- #update_trigger_status(trigger_id, status) ⇒ Object
Constructor Details
#initialize(api_url, api_user, api_password) ⇒ ZabbixApi
Returns a new instance of ZabbixApi.
28 29 30 31 32 33 34 35 |
# File 'lib/zabbixapi/base.rb', line 28 def initialize ( api_url, api_user, api_password ) @api_url = api_url @api_user = api_user @api_password = api_password @auth_id = nil @debug = false # Disable debug by default end |
Instance Attribute Details
#debug ⇒ Object
Returns the value of attribute debug.
26 27 28 |
# File 'lib/zabbixapi/base.rb', line 26 def debug @debug end |
Instance Method Details
#add_application(app_options) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/zabbixapi/application.rb', line 3 def add_application() = { 'hostid' => nil, 'name' => nil } application = merge_opt(, ) = { 'method' => 'application.create', 'params' => application } responce = send_request() unless responce.empty? then result = responce['applicationids'][0].to_i else result = nil end return result end |
#add_graph(graph) ⇒ Object
4 5 6 7 8 9 10 11 12 13 |
# File 'lib/zabbixapi/graph.rb', line 4 def add_graph(graph) = { 'method' => 'graph.create', 'params' => graph } response = send_request() return 0 end |
#add_graph_to_screen(screen_id, graph_id, x, y) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/zabbixapi/screen.rb', line 112 def add_graph_to_screen(screen_id, graph_id, x, y) = { 'method' => 'screen.addItems', 'params' => { 'screenids' => [ screen_id ], 'screenitems' => [ { 'resourcetype' => 'graph', 'resourceid' => graph_id, 'width' => '800', 'height' => '200', 'x' => x, 'y' => y, 'valign' => 'Middle', 'halign' => 'Centre', 'colspan' => '0', 'rowspan' => '0', 'elements' => '0', 'dynamic' => '0', 'url' => '0', 'style' => '0' } ] } } response = send_request() return response end |
#add_group(groupname) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/zabbixapi/group.rb', line 23 def add_group(groupname) = { 'method' => 'hostgroup.create', 'params' => { 'name' => groupname } } response = send_request() response ? response['groupids'] : nil end |
#add_host(host_options) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/zabbixapi/host.rb', line 4 def add_host() host_default = { 'host' => nil, 'port' => 10050, 'status' => 0, 'useip' => 0, 'dns' => '', 'ip' => '0.0.0.0', 'proxy_hostid' => 0, 'groups' => [], 'templates' => [], 'useipmi' => 0, 'ipmi_ip' => '', 'ipmi_port' => 623, 'ipmi_authtype' => 0, 'ipmi_privilege' => 0, 'ipmi_username' => '', 'ipmi_password' => '' } ['groups'].map! { |group_id| {'groupid' => group_id} } ['templates'].map! { |template_id| {'templateid' => template_id} } host = merge_opt(host_default, ) = { 'method' => 'host.create', 'params' => host } response = send_request() unless response.empty? then result = response['hostids'][0].to_i else result = nil end result end |
#add_host_to_group(host_id, group_id) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/zabbixapi/group.rb', line 36 def add_host_to_group(host_id, group_id) = { 'method' => 'hostgroup.massAdd', 'params' => { 'groups' => [ group_id ], 'hosts' => [ host_id ] } } response = send_request() response.empty? ? false : true end |
#add_item(item) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/zabbixapi/item.rb', line 22 def add_item(item) # Default item options # See: http://www.zabbix.com/documentation/1.8/api/item = { 'description' => nil, 'key_' => nil, 'hostid' => nil, 'delay' => 60, 'history' => 60, 'status' => 0, 'type' => 7, 'snmp_community' => '', 'snmp_oid' => '', 'value_type' => 3, 'data_type' => 0, 'trapper_hosts' => 'localhost', 'snmp_port' => 161, 'units' => '', 'multiplier' => 0, 'delta' => 0, 'snmpv3_securityname' => '', 'snmpv3_securitylevel' => 0, 'snmpv3_authpassphrase' => '', 'snmpv3_privpassphrase' => '', 'formula' => 0, 'trends' => 365, 'logtimefmt' => '', 'valuemapid' => 0, 'delay_flex' => '', 'authtype' => 0, 'username' => '', 'password' => '', 'publickey' => '', 'privatekey' => '', 'params' => '', 'ipmi_sensor' => '', 'applications' => '', 'templateid' => 0 } .merge!(item) = { 'method' => 'item.create', 'params' => [ ] } response = send_request() unless response.empty? then result = response['itemids'][0] else result = nil end return result end |
#add_macro(host_id, macro_name, macro_value) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/zabbixapi/usermacro.rb', line 4 def add_macro(host_id, macro_name, macro_value) = { 'method' => 'Usermacro.create', 'params' => { 'hostid' => host_id, 'macro' => macro_name, 'value'=> macro_value } } response = send_request() if hostmacroids = response['hostmacroids'] then result = hostmacroids else result = nil end return result end |
#add_screen(screen_name, hsize, vsize) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/zabbixapi/screen.rb', line 144 def add_screen(screen_name, hsize, vsize) = { 'method' => 'screen.create', 'params' => { 'name' => screen_name, 'hsize' => hsize, 'vsize' => vsize } } response = send_request() unless response.empty? then result = response['screenids'][0] else result = nil end return result end |
#add_template(template_options) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/zabbixapi/template.rb', line 4 def add_template() template_default = { 'host' => nil, 'groups' => [], } ['groups'].map! { |group_id| {'groupid' => group_id} } template = merge_opt(template_default, ) = { 'method' => 'template.create', 'params' => template } response = send_request() if not ( response.empty? ) then result = response['templateids'][0].to_i else result = nil end return result end |
#add_trigger(trigger) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/zabbixapi/trigger.rb', line 4 def add_trigger(trigger) = { 'method' => 'trigger.create', 'params' => [ trigger ] } response = send_request() unless response.empty? then result = response['triggerids'][0] else result = nil end return result end |
#auth ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/zabbixapi/base.rb', line 105 def auth() return @auth_id if @auth_id = { 'auth' => nil, 'method' => 'user.authenticate', 'params' => { 'user' => @api_user, 'password' => @api_password, '0' => '0' } } begin @auth_id = do_request() rescue RuntimeError => e raise AuthError.new(e.) end end |
#del_all_graphs_from_screen(screen_id) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/zabbixapi/screen.rb', line 94 def del_all_graphs_from_screen(screen_id) = { 'method' => 'screen.deleteItems', 'params' => { 'screenids' => [ screen_id ], } } response = send_request() if ( response ) then return response else return nil end end |
#do_request(message) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/zabbixapi/base.rb', line 37 def do_request() id = rand 100_000 ['id'] = id ['jsonrpc'] = '2.0' = JSON.generate() uri = URI.parse(@api_url) http = Net::HTTP.new(uri.host, uri.port) if ( uri.scheme == "https" ) then http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE end request = Net::HTTP::Post.new(uri.request_uri) request.add_field('Content-Type', 'application/json-rpc') request.body=() begin puts "[ZBXAPI] : INFO : Do request. Body => #{request.body}" if @debug response = http.request(request) rescue ::SocketError => e puts "[ZBXAPI] : ERROR : SocketError => #{e.}" if @debug raise Zabbix::SocketError.new(e.) end if @debug puts "[ZBXAPI] : INFO : Response start" response.each_header do |key,value| puts "#{key}:#{value}" end puts response puts "[ZBXAPI] : INFO : Response end" end if response.code != "200" raise Zabbix::ResponseCodeError.new("Responce code from [" + @api_url + "] is #{response.code}") end response_body_hash = JSON.parse(response.body) if error = response_body_hash['error'] = error['message'] error_data = error['data'] error_code = error['code'] = "Code: [" + error_code.to_s + "]. Message: [" + + "]. Data: [" + error_data + "]." case error_code.to_s when '-32602' raise Zabbix::AlreadyExist.new() else raise Zabbix::ResponseError.new() end end result = response_body_hash['result'] result end |
#get_graph_id(host_id, graph_name) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/zabbixapi/graph.rb', line 15 def get_graph_id(host_id, graph_name) = { 'method' => 'graph.get', 'params' => { 'filter' => { 'name' => graph_name, 'hostid' => host_id } } } response = send_request() unless ( response.empty? ) then result = response[0]['graphid'] else result = nil end end |
#get_graphs(host_id) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/zabbixapi/graph.rb', line 36 def get_graphs(host_id) = { 'method' => 'graph.get', 'params' => { 'extendoutput' => '1', 'filter' => { 'hostid' => host_id } } } response = send_request() unless ( response.empty? ) then result = {} response.each() do |graph| graph_id = graph['graphid'] graph_name = graph['name'] result[graph_id] = graph_name end else result = nil end return result end |
#get_group_id(pattern) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/zabbixapi/group.rb', line 3 def get_group_id(pattern) = { 'method' => 'hostgroup.get', 'params' => { 'filter' => { 'name' => pattern } } } response = send_request() response.empty? ? nil : response[0]['groupid'] end |
#get_host(params) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/zabbixapi/host.rb', line 68 def get_host(params) = { 'method' => 'host.get', 'params' => params } response = send_request() unless response.empty? then result = response[0] else result = nil end result end |
#get_host_id(hostname) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/zabbixapi/host.rb', line 46 def get_host_id(hostname) = { 'method' => 'host.get', 'params' => { 'filter' => { 'host' => hostname } } } response = send_request() unless response.empty? then result = response[0]['hostid'].to_i else result = nil end result end |
#get_item_id(host_id, item_name) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/zabbixapi/item.rb', line 108 def get_item_id(host_id, item_name) = { 'method' => 'item.get', 'params' => { 'filter' => { 'hostid' => host_id, 'description' => item_name } } } response = send_request() unless ( response.empty? ) then result = response[0]['itemid'] else result = nil end return result end |
#get_macro(host_id, macro_name) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/zabbixapi/usermacro.rb', line 26 def get_macro(host_id, macro_name) = { 'method' => 'Usermacro.get', 'params' => { 'hostids' => host_id, 'macros' => macro_name, 'extendoutput' => '1' } } response = send_request() unless response.empty? then if hostmacroid = response[0]['hostmacroid'] then macro_id = hostmacroid macro_value = response[0]['value'] result = { 'id' => macro_id, 'value'=> macro_value } else result = nil end else result = nil end return result end |
#get_screen_graph_ids(screen_id) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/zabbixapi/screen.rb', line 45 def get_screen_graph_ids(screen_id) = { 'method' => 'screen.get', 'params' => { 'extendoutput' => '1', 'select_screenitems' => '1', 'screenids' => [ screen_id ] } } response = send_request() unless ( response.empty?) then result = [] screenitems = response[0]['screenitems'] screenitems.each() do |item| if ( item['resourcetype'].to_i == 0 ) then result << item['resourceid'] end end else result = nil end return result end |
#get_screen_id(screen_name) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/zabbixapi/screen.rb', line 4 def get_screen_id(screen_name) = { 'method' => 'screen.get', 'params' => { 'filter' => { 'name' => screen_name } } } response = send_request() unless response.empty? then result = response[0]['screenid'] else result = nil end end |
#get_screen_parameter(screen_name, param_name) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/zabbixapi/screen.rb', line 24 def get_screen_parameter(screen_name, param_name) = { 'method' => 'screen.get', 'params' => { 'extendoutput' => '1', 'filter' => { 'name' => screen_name } } } response = send_request() unless response.empty? then result = response[0][param_name] else result nil end end |
#get_template_id(template_name) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/zabbixapi/template.rb', line 80 def get_template_id(template_name) = { 'method' => 'template.get', 'params' => { 'filter' => { 'host' => template_name } } } response = send_request() result = nil result = response.first["templateid"] if (response.is_a?(Array) && response.first.is_a?(Hash)) # API > 1.8.3 result = response[response.keys.first]["templateid"] if (response.is_a?(Hash) && response[response.keys.first].is_a?(Hash)) # API <= 1.8.3 result end |
#get_template_ids_by_host(host_id) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/zabbixapi/template.rb', line 31 def get_template_ids_by_host(host_id) = { 'method' => 'template.get', 'params' => { 'hostids' => [ host_id ] } } response = send_request() unless ( response.empty? ) then result = [] response.each_key() do |template_id| result << template_id end else result = nil end return result end |
#get_templates ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/zabbixapi/template.rb', line 54 def get_templates() = { 'method' => 'template.get', 'params' => { 'extendoutput' => '0' } } response = send_request() unless response.empty? then template_ids = response.keys() result = {} template_ids.each() do |template_id| template_name = response[template_id]['host'] result[template_id] = template_name end else result = nil end return result end |
#get_trigger_id(host_id, trigger_name) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/zabbixapi/trigger.rb', line 23 def get_trigger_id(host_id, trigger_name) = { 'method' => 'trigger.get', 'params' => { 'filter' => { 'hostid' => host_id, 'description' => trigger_name } } } response = send_request() unless response.empty? then result = response[0]['triggerid'] else result = nil end return result end |
#get_triggers_by_host(host_id) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/zabbixapi/trigger.rb', line 46 def get_triggers_by_host(host_id) = { 'method' => 'trigger.get', 'params' => { 'filter' => { 'hostid' => host_id, }, 'extendoutput' => '1' } } response = send_request() unless response.empty? then result = {} response.each do |trigger| trigger_id = trigger['triggerid'] description = trigger['description'] result[trigger_id] = description end else result = {} end return result end |
#get_webitem_id(host_id, item_name) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/zabbixapi/item.rb', line 84 def get_webitem_id(host_id, item_name) = { 'method' => 'item.get', 'params' => { 'filter' => { 'hostid' => host_id, 'type' => 9, 'key_' => "web.test.time[eva.ru,Get main page,resp]" }, 'webitems' => 1 } } response = send_request() unless ( response.empty? ) then result = response[0]['itemid'] else result = nil end return result end |
#group_exist?(pattern) ⇒ Boolean
18 19 20 21 |
# File 'lib/zabbixapi/group.rb', line 18 def group_exist?(pattern) group_id = get_groups_id(pattern) group_id ? true : false end |
#link_templates_with_hosts(templates_id, hosts_id) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/zabbixapi/template.rb', line 98 def link_templates_with_hosts(templates_id, hosts_id) if templates_id.class == Array then = templates_id else = [ templates_id ] end if hosts_id == Array then = hosts_id else = [ hosts_id ] end = { 'method' => 'template.massAdd', 'params' => { 'hosts' => , 'templates' => } } response = send_request() return response end |
#merge_opt(a, b) ⇒ Object
Utils.
127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/zabbixapi/base.rb', line 127 def merge_opt(a, b) c = {} b.each_pair do |key, value| if a.has_key?(key) then c[key] = value end end return a.merge(c) end |
#send_request(message) ⇒ Object
100 101 102 103 |
# File 'lib/zabbixapi/base.rb', line 100 def send_request() ['auth'] = auth() do_request() end |
#set_macro_value(host_id, macro_name, macro_value) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/zabbixapi/usermacro.rb', line 58 def set_macro_value(host_id, macro_name, macro_value) = { 'method' => 'usermacro.updateValue', 'params' => { 'hostid' => host_id, 'macro' => macro_name, 'value' => macro_value } } response = send_request() return true end |
#set_screen_parameter(screen_id, param_name, param_value) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/zabbixapi/screen.rb', line 73 def set_screen_parameter(screen_id, param_name, param_value) = { 'method' => 'screen.update', 'params' => { param_name => param_value, 'screenid' => screen_id } } response = send_request() if not ( response.empty? ) then result = true else result = false end return result end |
#unlink_templates_from_hosts(templates_id, hosts_id) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/zabbixapi/template.rb', line 125 def unlink_templates_from_hosts(templates_id, hosts_id) if templates_id.class == Array then = templates_id else = [ templates_id ] end if hosts_id == Array then = hosts_id else = [ hosts_id ] end = { 'method' => 'template.massRemove', 'params' => { 'hosts' => , 'templates' => , 'force' => '1' } } response = send_request() return response end |
#update_item(item_id) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/zabbixapi/item.rb', line 131 def update_item(item_id) = { 'method' => 'item.update', 'params' => { 'itemid' => item_id, 'status' => 0 } } response = send_request() end |
#update_trigger_status(trigger_id, status) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/zabbixapi/trigger.rb', line 74 def update_trigger_status(trigger_id, status) = { 'method' => 'trigger.update_status', 'params' => { 'triggerid' => trigger_id, 'status' => status } } response = send_request() unless response.empty? then result = response['triggerids'][0] else result = nil end return result end |