Class: Zabbix::ZabbixApi
- Inherits:
-
Object
- Object
- Zabbix::ZabbixApi
- Defined in:
- lib/host.rb,
lib/base.rb,
lib/item.rb,
lib/graph.rb,
lib/group.rb,
lib/screen.rb,
lib/trigger.rb,
lib/template.rb,
lib/usermacro.rb,
lib/application.rb
Overview
Method for creation host in zabbix.
-
Input parameter - hash
host_options
. Available keys in hash:-
host - hostname. Type: string. Default: nil;
-
port - zabbix agent pont. Type: int. Default: 10050;
-
status - host status. Type: int. Possible values: 0 - monitored, 1 - not monitored. Default: 0;
-
useip - use ip or dns name for monitoring host. Possible values: 0 - don’t use ip (use dns name), 1 - use ip (don’t use dns name);
-
ip - host’s ip address. Used for monitoring host if useip set to 1. Default: ‘0.0.0.0’;
-
proxy_hostid - host_id of zabbix proxy (if necessary). See
get_host_id
. Default: 0 (don’t use proxy server); -
groups - array of groups that belong host. Default: [].
-
useipmi - Use or not ipmi. Default: 0 (don’t use ipmi);
-
ipmi_ip - Default: ”;
-
ipmi_port - Default: 623;
-
ipmi_authtype - Default: 0;
-
ipmi_privilege - Default: 0;
-
ipmi_username - Default: ”;
-
ipmi_password - Default: ”;
-
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_id(hostname) ⇒ Object
Method for retrieving host id from zabbix by hostname.
- #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
- #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.
26 27 28 29 30 |
# File 'lib/base.rb', line 26 def initialize ( api_url, api_user, api_password ) @api_url = api_url @api_user = api_user @api_password = api_password end |
Instance Method Details
#add_application(app_options) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/application.rb', line 5 def add_application() = { 'hostid' => nil, 'name' => nil } application = merge_opt(, ) = { 'method' => 'application.create', 'params' => application } responce = send_request() if not ( 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 14 15 |
# File 'lib/graph.rb', line 4 def add_graph(graph) = { 'method' => 'graph.create', 'params' => graph } responce = send_request() puts "DEBUG: #{responce.inspect}" return 0 end |
#add_graph_to_screen(screen_id, graph_id, x, y) ⇒ Object
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 143 144 145 |
# File 'lib/screen.rb', line 115 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' } ] } } responce = send_request() return responce end |
#add_group(groupname) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/group.rb', line 37 def add_group(groupname) = { 'method' => 'hostgroup.create', 'params' => { 'name' => groupname } } responce = send_request() if ( responce ) then result = responce['groupids'] else result = nil end return result end |
#add_host(host_options) ⇒ Object
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 |
# File 'lib/host.rb', line 31 def add_host() host_default = { 'host' => nil, 'port' => 10050, 'status' => 0, 'useip' => 0, 'dns' => '', 'ip' => '0.0.0.0', 'proxy_hostid' => 0, 'groups' => [], 'useipmi' => 0, 'ipmi_ip' => '', 'ipmi_port' => 623, 'ipmi_authtype' => 0, 'ipmi_privilege' => 0, 'ipmi_username' => '', 'ipmi_password' => '' } ['groups'].map! { |group_id| {'groupid' => group_id} } host = merge_opt(host_default, ) = { 'method' => 'host.create', 'params' => host } responce = send_request() if not ( responce.empty? ) then result = responce['hostids'][0].to_i else result = nil end return result end |
#add_host_to_group(host_id, group_id) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/group.rb', line 57 def add_host_to_group(host_id, group_id) = { 'method' => 'hostgroup.massAdd', 'params' => { 'groups' => [ group_id ], 'hosts' => [ host_id ] } } responce = send_request() if not ( responce.empty? ) then result = true else result = false end return result end |
#add_item(item) ⇒ Object
3 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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/item.rb', line 3 def add_item(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' => [ ] } responce = send_request() unless ( responce.empty? ) then result = responce['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/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 } } responce = send_request() if ( hostmacroids = responce['hostmacroids'] ) then result = hostmacroids else result = nil end return result end |
#add_screen(screen_name, hsize, vsize) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/screen.rb', line 147 def add_screen(screen_name, hsize, vsize) = { 'method' => 'screen.create', 'params' => { 'name' => screen_name, 'hsize' => hsize, 'vsize' => vsize } } responce = send_request() if not ( responce.empty? ) then result = responce['screenids'][0] else result = nil end return result end |
#add_template(template_options) ⇒ Object
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 |
# File 'lib/template.rb', line 5 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 } responce = send_request() if not ( responce.empty? ) then result = responce['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 22 23 24 |
# File 'lib/trigger.rb', line 4 def add_trigger(trigger) = { 'method' => 'trigger.create', 'params' => [ trigger ] } responce = send_request() unless ( responce.empty? ) then result = responce['triggerids'][0] else result = nil end return result end |
#auth ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/base.rb', line 94 def auth() = { 'auth' => nil, 'method' => 'user.authenticate', 'params' => { 'user' => @api_user, 'password' => @api_password, '0' => '0' } } auth_id = do_request() return auth_id end |
#del_all_graphs_from_screen(screen_id) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/screen.rb', line 97 def del_all_graphs_from_screen(screen_id) = { 'method' => 'screen.deleteItems', 'params' => { 'screenids' => [ screen_id ], } } responce = send_request() if ( responce ) then return responce else return nil end end |
#do_request(message) ⇒ Object
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 83 84 85 86 87 |
# File 'lib/base.rb', line 32 def do_request() id = rand 100_000 ['id'] = id ['jsonrpc'] = '2.0' = JSON.generate() # puts message.inspect 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=() # TODO сделать проверку невозможности подключения. responce = http.request(request) if ( responce.code != "200" ) then raise Zabbix::ResponceCodeError.new("Responce code from [" + @api_url + "] is " + responce.code) end responce_body_hash = JSON.parse(responce.body) #if not ( responce_body_hash['id'] == id ) then # raise Zabbix::InvalidAnswerId.new("Wrong ID in zabbix answer") #end # Check errors in zabbix answer. If error exist - raise exception Zabbix::Error # puts responce_body_hash.inspect if ( error = responce_body_hash['error'] ) then = error['message'] error_data = error['data'] error_code = error['code'] = "Code: [" + error_code.to_s + "]. Message: [" + +\ "]. Data: [" + error_data + "]." raise Zabbix::Error.new() end result = responce_body_hash['result'] return result end |
#get_graph_id(host_id, graph_name) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/graph.rb', line 17 def get_graph_id(host_id, graph_name) = { 'method' => 'graph.get', 'params' => { 'filter' => { 'name' => graph_name, 'hostid' => host_id } } } responce = send_request() unless ( responce.empty? ) then result = responce[0]['graphid'] else result = nil end end |
#get_graphs(host_id) ⇒ Object
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 |
# File 'lib/graph.rb', line 38 def get_graphs(host_id) = { 'method' => 'graph.get', 'params' => { 'extendoutput' => '1', 'filter' => { 'hostid' => host_id } } } responce = send_request() unless ( responce.empty? ) then result = {} responce.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
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/group.rb', line 4 def get_group_id(pattern) = { 'method' => 'hostgroup.get', 'params' => { 'filter' => { 'name' => pattern } } } responce = send_request() if not ( responce.empty? ) then result = responce[0]['groupid'] else result = nil end return result end |
#get_host_id(hostname) ⇒ Object
Method for retrieving host id from zabbix by hostname.
-
Non optional input parameters:
-
hostname - Type: String.
-
-
Return:
-
host_id - Return finded host_id for passed hostname. If host not found in zabbix - return nil
-
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/host.rb', line 76 def get_host_id(hostname) = { 'method' => 'host.get', 'params' => { 'filter' => { 'host' => hostname } } } responce = send_request() if not ( responce.empty? ) then result = responce[0]['hostid'].to_i else result = nil end return result end |
#get_item_id(host_id, item_name) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/item.rb', line 90 def get_item_id(host_id, item_name) = { 'method' => 'item.get', 'params' => { 'filter' => { 'hostid' => host_id, 'description' => item_name } } } responce = send_request() unless ( responce.empty? ) then result = responce[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/usermacro.rb', line 26 def get_macro(host_id, macro_name) = { 'method' => 'Usermacro.get', 'params' => { 'hostids' => host_id, 'macros' => macro_name, 'extendoutput' => '1' } } responce = send_request() if not ( responce.empty?) then if ( hostmacroid = responce[0]['hostmacroid'] ) then macro_id = hostmacroid macro_value = responce[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
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 |
# File 'lib/screen.rb', line 46 def get_screen_graph_ids(screen_id) = { 'method' => 'screen.get', 'params' => { 'extendoutput' => '1', 'select_screenitems' => '1', 'screenids' => [ screen_id ] } } responce = send_request() p responce unless ( responce.empty?) then result = [] screenitems = responce[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/screen.rb', line 4 def get_screen_id(screen_name) = { 'method' => 'screen.get', 'params' => { 'filter' => { 'name' => screen_name } } } responce = send_request() if not ( responce.empty? ) then result = responce[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 44 |
# File 'lib/screen.rb', line 24 def get_screen_parameter(screen_name, param_name) = { 'method' => 'screen.get', 'params' => { 'extendoutput' => '1', 'filter' => { 'name' => screen_name } } } responce = send_request() if not ( responce.empty? ) then result = responce[0][param_name] else result nil end end |
#get_template_id(template_name) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/template.rb', line 84 def get_template_id(template_name) = { 'method' => 'template.get', 'params' => { 'filter' => { 'host' => template_name } } } responce = send_request() if not ( responce.empty? ) then result = responce.keys[0] else result = nil end return result end |
#get_template_ids_by_host(host_id) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/template.rb', line 32 def get_template_ids_by_host(host_id) = { 'method' => 'template.get', 'params' => { 'hostids' => [ host_id ] } } responce = send_request() unless ( responce.empty? ) then result = [] responce.each_key() do |template_id| result << template_id end else result = nil end return result end |
#get_templates ⇒ Object
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/template.rb', line 55 def get_templates() = { 'method' => 'template.get', 'params' => { 'extendoutput' => '0' } } responce = send_request() unless ( responce.empty? ) then template_ids = responce.keys() result = {} template_ids.each() do |template_id| template_name = responce[template_id]['host'] result[template_id] = template_name end else result = nil end return result end |
#get_trigger_id(host_id, trigger_name) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/trigger.rb', line 26 def get_trigger_id(host_id, trigger_name) = { 'method' => 'trigger.get', 'params' => { 'filter' => { 'hostid' => host_id, 'description' => trigger_name } } } responce = send_request() unless ( responce.empty? ) then result = responce[0]['triggerid'] else result = nil end return result end |
#get_triggers_by_host(host_id) ⇒ Object
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 |
# File 'lib/trigger.rb', line 49 def get_triggers_by_host(host_id) = { 'method' => 'trigger.get', 'params' => { 'filter' => { 'hostid' => host_id, }, 'extendoutput' => '1' } } responce = send_request() unless ( responce.empty? ) then result = {} responce.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
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 |
# File 'lib/item.rb', line 63 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 } } responce = send_request() p responce unless ( responce.empty? ) then result = responce[0]['itemid'] else result = nil end return result end |
#group_exist?(pattern) ⇒ Boolean
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/group.rb', line 26 def group_exist?(pattern) group_id = get_groups_id(pattern) if ( group_id ) then return true else return false end end |
#link_templates_with_hosts(templates_id, hosts_id) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/template.rb', line 107 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' => } } responce = send_request() return responce end |
#merge_opt(a, b) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/base.rb', line 111 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
89 90 91 92 |
# File 'lib/base.rb', line 89 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 73 |
# File 'lib/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 } } responce = send_request() # Проверять ответ бесполезно. В ответ всегда возвращается запрос. return true end |
#set_screen_parameter(screen_id, param_name, param_value) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/screen.rb', line 76 def set_screen_parameter(screen_id, param_name, param_value) = { 'method' => 'screen.update', 'params' => { param_name => param_value, 'screenid' => screen_id } } responce = send_request() if not ( responce.empty? ) then result = true else result = false end return result end |
#unlink_templates_from_hosts(templates_id, hosts_id) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/template.rb', line 134 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' } } responce = send_request() return responce end |
#update_item(item_id) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/item.rb', line 113 def update_item(item_id) = { 'method' => 'item.update', 'params' => { 'itemid' => item_id, 'status' => 0 } } responce = send_request() p responce end |
#update_trigger_status(trigger_id, status) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/trigger.rb', line 77 def update_trigger_status(trigger_id, status) = { 'method' => 'trigger.update_status', 'params' => { 'triggerid' => trigger_id, 'status' => status } } responce = send_request() unless ( responce.empty? ) then result = responce['triggerids'][0] else result = nil end return result end |