zabbixapi-fotonsi 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,42 @@
1
+ = Ruby Zabbix Api Module.
2
+
3
+ Simple and lightweight ruby module for work with zabbix api version 1.8.2
4
+
5
+ You can:
6
+ * Create host/template/application/items/triggers and screens;
7
+ * Get info about all zabbix essences;
8
+
9
+ == Installation
10
+
11
+ gem install zabbixapi
12
+
13
+ == Get Start.
14
+
15
+ * Get hostid from zabbix api:
16
+
17
+ zbx = Zabbix::ZabbixApi.new('https://zabbix.example.com', 'login', 'password')
18
+ hostid = zbx.get_host_id('my.example.com')
19
+
20
+ p hostid
21
+
22
+ == Dependencies
23
+
24
+ * net/http
25
+ * net/https
26
+ * json
27
+
28
+ == Use examples
29
+
30
+ * zabbix_la - LoadAverage template
31
+
32
+ cd examples
33
+ ruby zabbix_la -E development -g Templates
34
+
35
+ * -E - env from examples/config.yml (like RAILS_ENV)
36
+ * -g - group in zabbix for templates
37
+
38
+ == Zabbix documentation
39
+
40
+ * Zabbix Project Homepage -> http://zabbix.com/
41
+ * Zabbix Api Draft docs -> http://www.zabbix.com/documentation/1.8/api
42
+
@@ -0,0 +1,29 @@
1
+ require 'base'
2
+
3
+ module Zabbix
4
+ class ZabbixApi
5
+ def add_application(app_options)
6
+
7
+ app_options_default = {
8
+ 'hostid' => nil,
9
+ 'name' => nil
10
+ }
11
+
12
+ application = merge_opt(app_options_default, app_options)
13
+ message = {
14
+ 'method' => 'application.create',
15
+ 'params' => application
16
+ }
17
+
18
+ responce = send_request(message)
19
+
20
+ if not ( responce.empty? ) then
21
+ result = responce['applicationids'][0].to_i
22
+ else
23
+ result = nil
24
+ end
25
+
26
+ return result
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,126 @@
1
+ #!/usr/bin/ruby
2
+
3
+ require 'json'
4
+ require 'net/http'
5
+ require 'net/https'
6
+
7
+ module Zabbix
8
+
9
+ class JsonMessage < RuntimeError
10
+ end
11
+
12
+ class ResponceCodeError < RuntimeError
13
+ end
14
+
15
+ class ResponceBodyHash < RuntimeError
16
+ end
17
+
18
+ class InvalidAnswerId < RuntimeError
19
+ end
20
+
21
+ class Error < RuntimeError
22
+ end
23
+
24
+ class ZabbixApi
25
+
26
+ def initialize ( api_url, api_user, api_password )
27
+ @api_url = api_url
28
+ @api_user = api_user
29
+ @api_password = api_password
30
+ end
31
+
32
+ def do_request(message)
33
+
34
+ id = rand 100_000
35
+
36
+ message['id'] = id
37
+ message['jsonrpc'] = '2.0'
38
+
39
+ message_json = JSON.generate(message)
40
+
41
+ # puts message.inspect
42
+
43
+ uri = URI.parse(@api_url)
44
+ http = Net::HTTP.new(uri.host, uri.port)
45
+
46
+ if ( uri.scheme == "https" ) then
47
+ http.use_ssl = true
48
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
49
+ end
50
+
51
+ request = Net::HTTP::Post.new(uri.request_uri)
52
+ request.add_field('Content-Type', 'application/json-rpc')
53
+ request.body=(message_json)
54
+
55
+ # TODO сделать проверку невозможности подключения.
56
+ responce = http.request(request)
57
+
58
+ if ( responce.code != "200" ) then
59
+ raise Zabbix::ResponceCodeError.new("Responce code from [" + @api_url + "] is " + responce.code)
60
+ end
61
+
62
+ responce_body_hash = JSON.parse(responce.body)
63
+
64
+ #if not ( responce_body_hash['id'] == id ) then
65
+ # raise Zabbix::InvalidAnswerId.new("Wrong ID in zabbix answer")
66
+ #end
67
+
68
+
69
+ # Check errors in zabbix answer. If error exist - raise exception Zabbix::Error
70
+
71
+ # puts responce_body_hash.inspect
72
+
73
+ if ( error = responce_body_hash['error'] ) then
74
+ error_message = error['message']
75
+ error_data = error['data']
76
+ error_code = error['code']
77
+
78
+ e_message = "Code: [" + error_code.to_s + "]. Message: [" + error_message +\
79
+ "]. Data: [" + error_data + "]."
80
+
81
+ raise Zabbix::Error.new(e_message)
82
+ end
83
+
84
+ result = responce_body_hash['result']
85
+
86
+ return result
87
+ end
88
+
89
+ def send_request(message)
90
+ message['auth'] = auth()
91
+ do_request(message)
92
+ end
93
+
94
+ def auth()
95
+
96
+ auth_message = {
97
+ 'auth' => nil,
98
+ 'method' => 'user.authenticate',
99
+ 'params' => {
100
+ 'user' => @api_user,
101
+ 'password' => @api_password,
102
+ '0' => '0'
103
+ }
104
+ }
105
+
106
+ auth_id = do_request(auth_message)
107
+
108
+ return auth_id
109
+ end
110
+
111
+ def merge_opt(a, b)
112
+
113
+ c = {}
114
+
115
+ b.each_pair do |key, value|
116
+
117
+ if ( a.has_key?(key) ) then
118
+ c[key] = value
119
+ end
120
+
121
+ end
122
+
123
+ return a.merge(c)
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,68 @@
1
+ module Zabbix
2
+ class ZabbixApi
3
+
4
+ def add_graph(graph)
5
+ message = {
6
+ 'method' => 'graph.create',
7
+ 'params' => graph
8
+ }
9
+
10
+ responce = send_request(message)
11
+
12
+ puts "DEBUG: #{responce.inspect}"
13
+
14
+ return 0
15
+ end
16
+
17
+ def get_graph_id(host_id, graph_name)
18
+
19
+ message = {
20
+ 'method' => 'graph.get',
21
+ 'params' => {
22
+ 'filter' => {
23
+ 'name' => graph_name,
24
+ 'hostid' => host_id
25
+ }
26
+ }
27
+ }
28
+
29
+ responce = send_request(message)
30
+
31
+ unless ( responce.empty? ) then
32
+ result = responce[0]['graphid']
33
+ else
34
+ result = nil
35
+ end
36
+ end
37
+
38
+ def get_graphs(host_id)
39
+
40
+ message = {
41
+ 'method' => 'graph.get',
42
+ 'params' => {
43
+ 'extendoutput' => '1',
44
+ 'filter' => {
45
+ 'hostid' => host_id
46
+ }
47
+ }
48
+ }
49
+
50
+ responce = send_request(message)
51
+
52
+ unless ( responce.empty? ) then
53
+ result = {}
54
+
55
+ responce.each() do |graph|
56
+ graph_id = graph['graphid']
57
+ graph_name = graph['name']
58
+
59
+ result[graph_id] = graph_name
60
+ end
61
+ else
62
+ result = nil
63
+ end
64
+
65
+ return result
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,78 @@
1
+ module Zabbix
2
+
3
+ class ZabbixApi
4
+ def get_group_id(pattern)
5
+
6
+ message = {
7
+ 'method' => 'hostgroup.get',
8
+ 'params' => {
9
+ 'filter' => {
10
+ 'name' => pattern
11
+ }
12
+ }
13
+ }
14
+
15
+ responce = send_request(message)
16
+
17
+ if not ( responce.empty? ) then
18
+ result = responce[0]['groupid']
19
+ else
20
+ result = nil
21
+ end
22
+
23
+ return result
24
+ end
25
+
26
+ def group_exist?(pattern)
27
+
28
+ group_id = get_group_id(pattern)
29
+
30
+ if ( group_id ) then
31
+ return true
32
+ else
33
+ return false
34
+ end
35
+ end
36
+
37
+ def add_group(groupname)
38
+
39
+ message = {
40
+ 'method' => 'hostgroup.create',
41
+ 'params' => {
42
+ 'name' => groupname
43
+ }
44
+ }
45
+
46
+ responce = send_request(message)
47
+
48
+ if ( responce ) then
49
+ result = responce['groupids']
50
+ else
51
+ result = nil
52
+ end
53
+
54
+ return result
55
+ end
56
+
57
+ def add_host_to_group(host_id, group_id)
58
+
59
+ message = {
60
+ 'method' => 'hostgroup.massAdd',
61
+ 'params' => {
62
+ 'groups' => [ group_id ],
63
+ 'hosts' => [ host_id ]
64
+ }
65
+ }
66
+
67
+ responce = send_request(message)
68
+
69
+ if not ( responce.empty? ) then
70
+ result = true
71
+ else
72
+ result = false
73
+ end
74
+
75
+ return result
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,139 @@
1
+ module Zabbix
2
+
3
+ # Examples:
4
+ # * Create host in zabbix
5
+ #
6
+ # zbx = Zabbix::ZabbixApi.new(url, user, password)
7
+ # host_options => {
8
+ # host => 'host.example.org',
9
+ # ip => '127.0.0.1',
10
+ # groups => [10001, 10002],
11
+ # }
12
+ # host_id = zbx.add_host(host_options)
13
+
14
+ # Method for creation host in zabbix.
15
+ # * Input parameter - hash <tt>host_options</tt>. Available keys in hash:
16
+ # - host - hostname. Type: string. Default: nil;
17
+ # - port - zabbix agent pont. Type: int. Default: 10050;
18
+ # - status - host status. Type: int. Possible values: 0 - monitored, 1 - not monitored. Default: 0;
19
+ # - 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);
20
+ # - ip - host's ip address. Used for monitoring host if useip set to 1. Default: '0.0.0.0';
21
+ # - proxy_hostid - host_id of zabbix proxy (if necessary). See <tt>get_host_id</tt>. Default: 0 (don't use proxy server);
22
+ # - groups - array of groups that belong host. Default: [].
23
+ # - templates - array of templates that belong host. Default: [].
24
+ # - useipmi - Use or not ipmi. Default: 0 (don't use ipmi);
25
+ # - ipmi_ip - Default: '';
26
+ # - ipmi_port - Default: 623;
27
+ # - ipmi_authtype - Default: 0;
28
+ # - ipmi_privilege - Default: 0;
29
+ # - ipmi_username - Default: '';
30
+ # - ipmi_password - Default: '';
31
+ class ZabbixApi
32
+ def add_host(host_options)
33
+
34
+ host_default = {
35
+ 'host' => nil,
36
+ 'port' => 10050,
37
+ 'status' => 0,
38
+ 'useip' => 0,
39
+ 'dns' => '',
40
+ 'ip' => '0.0.0.0',
41
+ 'proxy_hostid' => 0,
42
+ 'groups' => [],
43
+ 'templates' => [],
44
+ 'useipmi' => 0,
45
+ 'ipmi_ip' => '',
46
+ 'ipmi_port' => 623,
47
+ 'ipmi_authtype' => 0,
48
+ 'ipmi_privilege' => 0,
49
+ 'ipmi_username' => '',
50
+ 'ipmi_password' => ''
51
+ }
52
+
53
+ host_options['groups'].map! { |group_id| {'groupid' => group_id} }
54
+ host_options['templates'].map! { |template_id| {'templateid' => template_id} } if host_options['templates']
55
+
56
+ host = merge_opt(host_default, host_options)
57
+
58
+ message = {
59
+ 'method' => 'host.create',
60
+ 'params' => host
61
+ }
62
+
63
+ responce = send_request(message)
64
+
65
+ if not ( responce.empty? ) then
66
+ result = responce['hostids'][0].to_i
67
+ else
68
+ result = nil
69
+ end
70
+
71
+ return result
72
+ end
73
+
74
+ # Method for updating host in zabbix.
75
+ # * Input parameter - hash <tt>host_options</tt>. Available keys in hash:
76
+ # - host - hostname. Type: string. Default: nil;
77
+ # - port - zabbix agent pont. Type: int. Default: 10050;
78
+ # - status - host status. Type: int. Possible values: 0 - monitored, 1 - not monitored. Default: 0;
79
+ # - 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);
80
+ # - ip - host's ip address. Used for monitoring host if useip set to 1. Default: '0.0.0.0';
81
+ # - proxy_hostid - host_id of zabbix proxy (if necessary). See <tt>get_host_id</tt>. Default: 0 (don't use proxy server);
82
+ # - groups - array of groups that belong host. Default: [].
83
+ # - templates - array of templates that belong host. Default: [].
84
+ # - useipmi - Use or not ipmi. Default: 0 (don't use ipmi);
85
+ # - ipmi_ip - Default: '';
86
+ # - ipmi_port - Default: 623;
87
+ # - ipmi_authtype - Default: 0;
88
+ # - ipmi_privilege - Default: 0;
89
+ # - ipmi_username - Default: '';
90
+ # - ipmi_password - Default: '';
91
+ def update_host(host_options)
92
+
93
+ host_options['groups'].map! { |group_id| {'groupid' => group_id} } if host_options['groups']
94
+ host_options['templates'].map! { |template_id| {'templateid' => template_id} } if host_options['templates']
95
+
96
+ message = {
97
+ 'method' => 'host.update',
98
+ 'params' => host_options
99
+ }
100
+
101
+ responce = send_request(message)
102
+
103
+ if not ( responce.empty? ) then
104
+ result = responce['hostids'][0].to_i
105
+ else
106
+ result = nil
107
+ end
108
+
109
+ return result
110
+ end
111
+
112
+ # Method for retrieving host id from zabbix by hostname.
113
+ # * Non optional input parameters:
114
+ # - hostname - Type: String.
115
+ # * Return:
116
+ # - host_id - Return finded host_id for passed hostname. If host not found in zabbix - return nil
117
+ def get_host_id(hostname)
118
+
119
+ message = {
120
+ 'method' => 'host.get',
121
+ 'params' => {
122
+ 'filter' => {
123
+ 'host' => hostname
124
+ }
125
+ }
126
+ }
127
+
128
+ responce = send_request(message)
129
+
130
+ if not ( responce.empty? ) then
131
+ result = responce[0]['hostid'].to_i
132
+ else
133
+ result = nil
134
+ end
135
+
136
+ return result
137
+ end
138
+ end
139
+ end
@@ -0,0 +1,128 @@
1
+ module Zabbix
2
+ class ZabbixApi
3
+ def add_item(item)
4
+
5
+ item_options = {
6
+ 'description' => nil,
7
+ 'key_' => nil,
8
+ 'hostid' => nil,
9
+ 'delay' => 60,
10
+ 'history' => 60,
11
+ 'status' => 0,
12
+ 'type' => 7,
13
+ 'snmp_community' => '',
14
+ 'snmp_oid' => '',
15
+ 'value_type' => 3,
16
+ 'data_type' => 0,
17
+ 'trapper_hosts' => 'localhost',
18
+ 'snmp_port' => 161,
19
+ 'units' => '',
20
+ 'multiplier' => 0,
21
+ 'delta' => 0,
22
+ 'snmpv3_securityname' => '',
23
+ 'snmpv3_securitylevel' => 0,
24
+ 'snmpv3_authpassphrase' => '',
25
+ 'snmpv3_privpassphrase' => '',
26
+ 'formula' => 0,
27
+ 'trends' => 365,
28
+ 'logtimefmt' => '',
29
+ 'valuemapid' => 0,
30
+ 'delay_flex' => '',
31
+ 'authtype' => 0,
32
+ 'username' => '',
33
+ 'password' => '',
34
+ 'publickey' => '',
35
+ 'privatekey' => '',
36
+ 'params' => '',
37
+ 'ipmi_sensor' => '',
38
+ 'applications' => '',
39
+ 'templateid' => 0
40
+ }
41
+
42
+
43
+ item_options.merge!(item)
44
+
45
+ message = {
46
+ 'method' => 'item.create',
47
+ 'params' => [ item_options ]
48
+ }
49
+
50
+
51
+ responce = send_request(message)
52
+
53
+ unless ( responce.empty? ) then
54
+ result = responce['itemids'][0]
55
+ else
56
+ result = nil
57
+ end
58
+
59
+ return result
60
+ end
61
+
62
+
63
+ def get_webitem_id(host_id, item_name)
64
+ message = {
65
+ 'method' => 'item.get',
66
+ 'params' => {
67
+ 'filter' => {
68
+ 'hostid' => host_id,
69
+ 'type' => 9,
70
+ 'key_' => "web.test.time[eva.ru,Get main page,resp]"
71
+ },
72
+ 'webitems' => 1
73
+ }
74
+ }
75
+
76
+ responce = send_request(message)
77
+
78
+ p responce
79
+
80
+ unless ( responce.empty? ) then
81
+ result = responce[0]['itemid']
82
+ else
83
+ result = nil
84
+ end
85
+
86
+ return result
87
+
88
+ end
89
+
90
+ def get_item_id(host_id, item_name)
91
+ message = {
92
+ 'method' => 'item.get',
93
+ 'params' => {
94
+ 'filter' => {
95
+ 'hostid' => host_id,
96
+ 'description' => item_name
97
+ }
98
+ }
99
+ }
100
+
101
+ responce = send_request(message)
102
+
103
+ unless ( responce.empty? ) then
104
+ result = responce[0]['itemid']
105
+ else
106
+ result = nil
107
+ end
108
+
109
+ return result
110
+
111
+ end
112
+
113
+ def update_item(item_id)
114
+
115
+ message = {
116
+ 'method' => 'item.update',
117
+ 'params' => {
118
+ 'itemid' => item_id,
119
+ 'status' => 0
120
+ }
121
+ }
122
+
123
+ responce = send_request(message)
124
+
125
+ p responce
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,78 @@
1
+ module Zabbix
2
+
3
+ class ZabbixApi
4
+ def get_proxy_id(pattern)
5
+
6
+ message = {
7
+ 'method' => 'proxy.get',
8
+ 'params' => {
9
+ 'filter' => {
10
+ 'name' => pattern
11
+ }
12
+ }
13
+ }
14
+
15
+ responce = send_request(message)
16
+
17
+ if not ( responce.empty? ) then
18
+ result = responce[0]['proxyid']
19
+ else
20
+ result = nil
21
+ end
22
+
23
+ return result
24
+ end
25
+
26
+ def proxy_exist?(pattern)
27
+
28
+ proxy_id = get_proxy_id(pattern)
29
+
30
+ if ( proxy_id ) then
31
+ return true
32
+ else
33
+ return false
34
+ end
35
+ end
36
+
37
+ def add_proxy(proxyname)
38
+
39
+ message = {
40
+ 'method' => 'proxy.create',
41
+ 'params' => {
42
+ 'name' => proxyname
43
+ }
44
+ }
45
+
46
+ responce = send_request(message)
47
+
48
+ if ( responce ) then
49
+ result = responce['proxyid']
50
+ else
51
+ result = nil
52
+ end
53
+
54
+ return result
55
+ end
56
+
57
+ # def add_host_to_proxy(host_id, proxy_id)
58
+ #
59
+ # message = {
60
+ # 'method' => 'proxy.massAdd',
61
+ # 'params' => {
62
+ # 'proxys' => [ proxy_id ],
63
+ # 'hosts' => [ host_id ]
64
+ # }
65
+ # }
66
+ #
67
+ # responce = send_request(message)
68
+ #
69
+ # if not ( responce.empty? ) then
70
+ # result = true
71
+ # else
72
+ # result = false
73
+ # end
74
+ #
75
+ # return result
76
+ # end
77
+ end
78
+ end
@@ -0,0 +1,169 @@
1
+ module Zabbix
2
+ class ZabbixApi
3
+
4
+ def get_screen_id(screen_name)
5
+
6
+ message = {
7
+ 'method' => 'screen.get',
8
+ 'params' => {
9
+ 'filter' => {
10
+ 'name' => screen_name
11
+ }
12
+ }
13
+ }
14
+
15
+ responce = send_request(message)
16
+
17
+ if not ( responce.empty? ) then
18
+ result = responce[0]['screenid']
19
+ else
20
+ result = nil
21
+ end
22
+ end
23
+
24
+ def get_screen_parameter(screen_name, param_name)
25
+
26
+ message = {
27
+ 'method' => 'screen.get',
28
+ 'params' => {
29
+ 'extendoutput' => '1',
30
+ 'filter' => {
31
+ 'name' => screen_name
32
+ }
33
+ }
34
+ }
35
+
36
+
37
+ responce = send_request(message)
38
+
39
+ if not ( responce.empty? ) then
40
+ result = responce[0][param_name]
41
+ else
42
+ result nil
43
+ end
44
+ end
45
+
46
+ def get_screen_graph_ids(screen_id)
47
+
48
+ message = {
49
+ 'method' => 'screen.get',
50
+ 'params' => {
51
+ 'extendoutput' => '1',
52
+ 'select_screenitems' => '1',
53
+ 'screenids' => [ screen_id ]
54
+ }
55
+ }
56
+
57
+ responce = send_request(message)
58
+
59
+ p responce
60
+
61
+ unless ( responce.empty?) then
62
+ result = []
63
+ screenitems = responce[0]['screenitems']
64
+ screenitems.each() do |item|
65
+ if ( item['resourcetype'].to_i == 0 ) then
66
+ result << item['resourceid']
67
+ end
68
+ end
69
+ else
70
+ result = nil
71
+ end
72
+
73
+ return result
74
+ end
75
+
76
+ def set_screen_parameter(screen_id, param_name, param_value)
77
+
78
+ message = {
79
+ 'method' => 'screen.update',
80
+ 'params' => {
81
+ param_name => param_value,
82
+ 'screenid' => screen_id
83
+ }
84
+ }
85
+
86
+ responce = send_request(message)
87
+
88
+ if not ( responce.empty? ) then
89
+ result = true
90
+ else
91
+ result = false
92
+ end
93
+
94
+ return result
95
+ end
96
+
97
+ def del_all_graphs_from_screen(screen_id)
98
+
99
+ message = {
100
+ 'method' => 'screen.deleteItems',
101
+ 'params' => {
102
+ 'screenids' => [ screen_id ],
103
+ }
104
+ }
105
+
106
+ responce = send_request(message)
107
+
108
+ if ( responce ) then
109
+ return responce
110
+ else
111
+ return nil
112
+ end
113
+ end
114
+
115
+ def add_graph_to_screen(screen_id, graph_id, x, y)
116
+
117
+ message = {
118
+ 'method' => 'screen.addItems',
119
+ 'params' => {
120
+ 'screenids' => [ screen_id ],
121
+ 'screenitems' => [
122
+ {
123
+ 'resourcetype' => 'graph',
124
+ 'resourceid' => graph_id,
125
+ 'width' => '800',
126
+ 'height' => '200',
127
+ 'x' => x,
128
+ 'y' => y,
129
+ 'valign' => 'Middle',
130
+ 'halign' => 'Centre',
131
+ 'colspan' => '0',
132
+ 'rowspan' => '0',
133
+ 'elements' => '0',
134
+ 'dynamic' => '0',
135
+ 'url' => '0',
136
+ 'style' => '0'
137
+ }
138
+ ]
139
+ }
140
+ }
141
+
142
+ responce = send_request(message)
143
+
144
+ return responce
145
+ end
146
+
147
+ def add_screen(screen_name, hsize, vsize)
148
+
149
+ message = {
150
+ 'method' => 'screen.create',
151
+ 'params' => {
152
+ 'name' => screen_name,
153
+ 'hsize' => hsize,
154
+ 'vsize' => vsize
155
+ }
156
+ }
157
+
158
+ responce = send_request(message)
159
+
160
+ if not ( responce.empty? ) then
161
+ result = responce['screenids'][0]
162
+ else
163
+ result = nil
164
+ end
165
+
166
+ return result
167
+ end
168
+ end
169
+ end
@@ -0,0 +1,166 @@
1
+ module Zabbix
2
+
3
+ class ZabbixApi
4
+
5
+ def add_template(template_options)
6
+
7
+ template_default = {
8
+ 'host' => nil,
9
+ 'groups' => [],
10
+ }
11
+
12
+ template_options['groups'].map! { |group_id| {'groupid' => group_id} }
13
+
14
+ template = merge_opt(template_default, template_options)
15
+
16
+ message = {
17
+ 'method' => 'template.create',
18
+ 'params' => template
19
+ }
20
+
21
+ responce = send_request(message)
22
+
23
+ if not ( responce.empty? ) then
24
+ result = responce['templateids'][0].to_i
25
+ else
26
+ result = nil
27
+ end
28
+
29
+ return result
30
+ end
31
+
32
+ def get_template_ids_by_host(host_id)
33
+
34
+ message = {
35
+ 'method' => 'template.get',
36
+ 'params' => {
37
+ 'hostids' => [ host_id ]
38
+ }
39
+ }
40
+
41
+ responce = send_request(message)
42
+
43
+ unless ( responce.empty? ) then
44
+ result = []
45
+ responce.each_key() do |template_id|
46
+ result << template_id
47
+ end
48
+ else
49
+ result = nil
50
+ end
51
+
52
+ return result
53
+ end
54
+
55
+ def get_templates()
56
+
57
+ message = {
58
+ 'method' => 'template.get',
59
+ 'params' => {
60
+ 'extendoutput' => '0'
61
+ }
62
+ }
63
+
64
+ responce = send_request(message)
65
+
66
+
67
+ unless ( responce.empty? ) then
68
+
69
+ template_ids = responce.keys()
70
+
71
+ result = {}
72
+
73
+ template_ids.each() do |template_id|
74
+ template_name = responce[template_id]['host']
75
+ result[template_id] = template_name
76
+ end
77
+ else
78
+ result = nil
79
+ end
80
+
81
+ return result
82
+ end
83
+
84
+ def get_template_id(template_name)
85
+
86
+ message = {
87
+ 'method' => 'template.get',
88
+ 'params' => {
89
+ 'filter' => {
90
+ 'host' => template_name
91
+ }
92
+ }
93
+ }
94
+
95
+ responce = send_request(message)
96
+
97
+ if not ( responce.empty? ) then
98
+ result = if responce.is_a?(Hash)
99
+ responce.keys[0]
100
+ elsif responce.is_a?(Array)
101
+ responce[0]['templateid']
102
+ end
103
+ else
104
+ result = nil
105
+ end
106
+
107
+ return result
108
+
109
+ end
110
+
111
+ def link_templates_with_hosts(templates_id, hosts_id)
112
+
113
+ if ( templates_id.class == Array ) then
114
+ message_templates_id = templates_id
115
+ else
116
+ message_templates_id = [ templates_id ]
117
+ end
118
+
119
+ if ( hosts_id == Array ) then
120
+ message_hosts_id = hosts_id
121
+ else
122
+ message_hosts_id = [ hosts_id ]
123
+ end
124
+
125
+ message = {
126
+ 'method' => 'template.massAdd',
127
+ 'params' => {
128
+ 'hosts' => message_hosts_id,
129
+ 'templates' => message_templates_id
130
+ }
131
+ }
132
+
133
+ responce = send_request(message)
134
+
135
+ return responce
136
+ end
137
+
138
+ def unlink_templates_from_hosts(templates_id, hosts_id)
139
+
140
+ if ( templates_id.class == Array ) then
141
+ message_templates_id = templates_id
142
+ else
143
+ message_templates_id = [ templates_id ]
144
+ end
145
+
146
+ if ( hosts_id == Array ) then
147
+ message_hosts_id = hosts_id
148
+ else
149
+ message_hosts_id = [ hosts_id ]
150
+ end
151
+
152
+ message = {
153
+ 'method' => 'template.massRemove',
154
+ 'params' => {
155
+ 'hosts' => message_hosts_id,
156
+ 'templates' => message_templates_id,
157
+ 'force' => '1'
158
+ }
159
+ }
160
+
161
+ responce = send_request(message)
162
+
163
+ return responce
164
+ end
165
+ end
166
+ end
@@ -0,0 +1,98 @@
1
+ module Zabbix
2
+ class ZabbixApi
3
+
4
+ def add_trigger(trigger)
5
+
6
+ message = {
7
+ 'method' => 'trigger.create',
8
+ 'params' => [ trigger ]
9
+ }
10
+
11
+
12
+ responce = send_request(message)
13
+
14
+
15
+ unless ( responce.empty? ) then
16
+ result = responce['triggerids'][0]
17
+ else
18
+ result = nil
19
+ end
20
+
21
+
22
+ return result
23
+
24
+ end
25
+
26
+ def get_trigger_id(host_id, trigger_name)
27
+
28
+ message = {
29
+ 'method' => 'trigger.get',
30
+ 'params' => {
31
+ 'filter' => {
32
+ 'hostid' => host_id,
33
+ 'description' => trigger_name
34
+ }
35
+ }
36
+ }
37
+
38
+ responce = send_request(message)
39
+
40
+ unless ( responce.empty? ) then
41
+ result = responce[0]['triggerid']
42
+ else
43
+ result = nil
44
+ end
45
+
46
+ return result
47
+ end
48
+
49
+ def get_triggers_by_host(host_id)
50
+
51
+ message = {
52
+ 'method' => 'trigger.get',
53
+ 'params' => {
54
+ 'filter' => {
55
+ 'hostid' => host_id,
56
+ },
57
+ 'extendoutput' => '1'
58
+ }
59
+ }
60
+
61
+ responce = send_request(message)
62
+
63
+ unless ( responce.empty? ) then
64
+ result = {}
65
+ responce.each do |trigger|
66
+ trigger_id = trigger['triggerid']
67
+ description = trigger['description']
68
+ result[trigger_id] = description
69
+ end
70
+ else
71
+ result = {}
72
+ end
73
+
74
+ return result
75
+ end
76
+
77
+ def update_trigger_status(trigger_id, status)
78
+
79
+ message = {
80
+ 'method' => 'trigger.update_status',
81
+ 'params' => {
82
+ 'triggerid' => trigger_id,
83
+ 'status' => status
84
+ }
85
+ }
86
+
87
+ responce = send_request(message)
88
+
89
+ unless ( responce.empty? ) then
90
+ result = responce['triggerids'][0]
91
+ else
92
+ result = nil
93
+ end
94
+
95
+ return result
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,75 @@
1
+ module Zabbix
2
+
3
+ class ZabbixApi
4
+ def add_macro(host_id, macro_name, macro_value)
5
+
6
+ message = {
7
+ 'method' => 'Usermacro.create',
8
+ 'params' => {
9
+ 'hostid' => host_id,
10
+ 'macro' => macro_name,
11
+ 'value'=> macro_value
12
+ }
13
+ }
14
+
15
+ responce = send_request(message)
16
+
17
+ if ( hostmacroids = responce['hostmacroids'] ) then
18
+ result = hostmacroids
19
+ else
20
+ result = nil
21
+ end
22
+
23
+ return result
24
+ end
25
+
26
+ def get_macro(host_id, macro_name)
27
+
28
+ message = {
29
+ 'method' => 'Usermacro.get',
30
+ 'params' => {
31
+ 'hostids' => host_id,
32
+ 'macros' => macro_name,
33
+ 'extendoutput' => '1'
34
+ }
35
+ }
36
+
37
+ responce = send_request(message)
38
+
39
+ if not ( responce.empty?) then
40
+ if ( hostmacroid = responce[0]['hostmacroid'] ) then
41
+ macro_id = hostmacroid
42
+ macro_value = responce[0]['value']
43
+
44
+ result = {
45
+ 'id' => macro_id,
46
+ 'value'=> macro_value
47
+ }
48
+ else
49
+ result = nil
50
+ end
51
+ else
52
+ result = nil
53
+ end
54
+
55
+ return result
56
+ end
57
+
58
+ def set_macro_value(host_id, macro_name, macro_value)
59
+
60
+ message = {
61
+ 'method' => 'usermacro.updateValue',
62
+ 'params' => {
63
+ 'hostid' => host_id,
64
+ 'macro' => macro_name,
65
+ 'value' => macro_value
66
+ }
67
+ }
68
+
69
+ responce = send_request(message)
70
+
71
+ # Проверять ответ бесполезно. В ответ всегда возвращается запрос.
72
+ return true
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,11 @@
1
+ require 'base'
2
+ require 'graph'
3
+ require 'group'
4
+ require 'proxy'
5
+ require 'host'
6
+ require 'item'
7
+ require 'screen'
8
+ require 'template'
9
+ require 'trigger'
10
+ require 'usermacro'
11
+ require 'application'
@@ -0,0 +1,22 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Gem::Specification.new do |spec|
6
+
7
+ spec.version = '0.1.5'
8
+ spec.rubygems_version = '1.3.1'
9
+ spec.name = 'zabbixapi-fotonsi'
10
+ spec.summary = 'Ruby module for work with zabbix api. Forked from http://github.com/verm666/RubyZabbixApi (author: Eduard Snesarev)'
11
+
12
+ spec.email = 'admin@foton.es'
13
+ spec.author = 'Fotón, Sistemas Inteligentes'
14
+ spec.homepage = 'https://github.com/fotonsi/zabbixapi'
15
+ spec.description = 'Ruby module for work with zabbix api. Forked from http://github.com/verm666/RubyZabbixApi (author: Eduard Snesarev)'
16
+
17
+ spec.has_rdoc = true
18
+ spec.extra_rdoc_files = 'README.rdoc'
19
+
20
+
21
+ spec.files = FileList["lib/*.rb", "bin/*", "spec/*", 'zabbixapi.gemspec', 'README.rdoc'].to_a
22
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zabbixapi-fotonsi
3
+ version: !ruby/object:Gem::Version
4
+ hash: 17
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 5
10
+ version: 0.1.5
11
+ platform: ruby
12
+ authors:
13
+ - "Fot\xC3\xB3n, Sistemas Inteligentes"
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-05-16 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: "Ruby module for work with zabbix api. Forked from http://github.com/verm666/RubyZabbixApi (author: Eduard Snesarev)"
23
+ email: admin@foton.es
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - README.rdoc
30
+ files:
31
+ - lib/application.rb
32
+ - lib/base.rb
33
+ - lib/graph.rb
34
+ - lib/group.rb
35
+ - lib/host.rb
36
+ - lib/item.rb
37
+ - lib/proxy.rb
38
+ - lib/screen.rb
39
+ - lib/template.rb
40
+ - lib/trigger.rb
41
+ - lib/usermacro.rb
42
+ - lib/zabbixapi.rb
43
+ - zabbixapi.gemspec
44
+ - README.rdoc
45
+ has_rdoc: true
46
+ homepage: https://github.com/fotonsi/zabbixapi
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options: []
51
+
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ hash: 3
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ requirements: []
73
+
74
+ rubyforge_project:
75
+ rubygems_version: 1.6.2
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: "Ruby module for work with zabbix api. Forked from http://github.com/verm666/RubyZabbixApi (author: Eduard Snesarev)"
79
+ test_files: []
80
+