zabbixapi 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +28 -0
- data/lib/1.rb +8 -0
- data/lib/base.rb +119 -0
- data/lib/graph.rb +55 -0
- data/lib/group.rb +78 -0
- data/lib/host.rb +96 -0
- data/lib/item.rb +127 -0
- data/lib/screen.rb +169 -0
- data/lib/template.rb +162 -0
- data/lib/trigger.rb +94 -0
- data/lib/usermacro.rb +75 -0
- data/lib/zabbixapi.rb +9 -0
- data/zabbixapi.gemspec +21 -0
- metadata +79 -0
data/README.rdoc
ADDED
@@ -0,0 +1,28 @@
|
|
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
|
+
== Get Start.
|
10
|
+
|
11
|
+
* Get hostid from zabbix api:
|
12
|
+
|
13
|
+
zbx = Zabbix::ZabbixApi.new('https://zabbix.example.com', 'login', 'password')
|
14
|
+
hostid = zbx.get_host_id('my.example.com')
|
15
|
+
|
16
|
+
p hostid
|
17
|
+
|
18
|
+
== Dependencies
|
19
|
+
|
20
|
+
* net/http
|
21
|
+
* net/https
|
22
|
+
* json
|
23
|
+
|
24
|
+
== Zabbix documentation
|
25
|
+
|
26
|
+
* Zabbix Project Homepage -> http://zabbix.com/
|
27
|
+
* Zabbix Api Draft docs -> http://www.zabbix.com/documentation/1.8/api
|
28
|
+
|
data/lib/1.rb
ADDED
data/lib/base.rb
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
require 'net/http'
|
5
|
+
require 'net/https'
|
6
|
+
|
7
|
+
module ZabbixApi
|
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
|
+
def initialize ( api_url, api_user, api_password )
|
25
|
+
@api_url = api_url
|
26
|
+
@api_user = api_user
|
27
|
+
@api_password = api_password
|
28
|
+
end
|
29
|
+
|
30
|
+
def do_request(message)
|
31
|
+
|
32
|
+
id = rand 100_000
|
33
|
+
|
34
|
+
message['id'] = id
|
35
|
+
message['jsonrpc'] = '2.0'
|
36
|
+
|
37
|
+
message_json = JSON.generate(message)
|
38
|
+
|
39
|
+
uri = URI.parse(@api_url)
|
40
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
41
|
+
|
42
|
+
if ( uri.scheme == "https" ) then
|
43
|
+
http.use_ssl = true
|
44
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
45
|
+
end
|
46
|
+
|
47
|
+
request = Net::HTTP::Post.new(uri.request_uri)
|
48
|
+
request.add_field('Content-Type', 'application/json-rpc')
|
49
|
+
request.body=(message_json)
|
50
|
+
|
51
|
+
# TODO сделать проверку невозможности подключения.
|
52
|
+
responce = http.request(request)
|
53
|
+
|
54
|
+
if ( responce.code != "200" ) then
|
55
|
+
raise Zabbix::ResponceCodeError.new("Responce code from [" + @api_url + "] is " + responce.code)
|
56
|
+
end
|
57
|
+
|
58
|
+
responce_body_hash = JSON.parse(responce.body)
|
59
|
+
|
60
|
+
#if not ( responce_body_hash['id'] == id ) then
|
61
|
+
# raise Zabbix::InvalidAnswerId.new("Wrong ID in zabbix answer")
|
62
|
+
#end
|
63
|
+
|
64
|
+
|
65
|
+
# Check errors in zabbix answer. If error exist - raise exception Zabbix::Error
|
66
|
+
if ( error = responce_body_hash['error'] ) then
|
67
|
+
error_message = error['message']
|
68
|
+
error_data = error['data']
|
69
|
+
error_code = error['code']
|
70
|
+
|
71
|
+
e_message = "Code: [" + error_code.to_s + "]. Message: [" + error_message +\
|
72
|
+
"]. Data: [" + error_data + "]."
|
73
|
+
|
74
|
+
raise Zabbix::Error.new(e_message)
|
75
|
+
end
|
76
|
+
|
77
|
+
result = responce_body_hash['result']
|
78
|
+
|
79
|
+
return result
|
80
|
+
end
|
81
|
+
|
82
|
+
def send_request(message)
|
83
|
+
message['auth'] = auth()
|
84
|
+
do_request(message)
|
85
|
+
end
|
86
|
+
|
87
|
+
def auth()
|
88
|
+
|
89
|
+
auth_message = {
|
90
|
+
'auth' => nil,
|
91
|
+
'method' => 'user.authenticate',
|
92
|
+
'params' => {
|
93
|
+
'user' => @api_user,
|
94
|
+
'password' => @api_password,
|
95
|
+
'0' => '0'
|
96
|
+
}
|
97
|
+
}
|
98
|
+
|
99
|
+
auth_id = do_request(auth_message)
|
100
|
+
|
101
|
+
return auth_id
|
102
|
+
end
|
103
|
+
|
104
|
+
def merge_opt(a, b)
|
105
|
+
|
106
|
+
c = {}
|
107
|
+
|
108
|
+
b.each_pair do |key, value|
|
109
|
+
|
110
|
+
if ( a.has_key?(key) ) then
|
111
|
+
c[key] = value
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
115
|
+
|
116
|
+
return a.merge(c)
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
data/lib/graph.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
module Zabbix
|
2
|
+
class ZabbixApi
|
3
|
+
|
4
|
+
def get_graph_id(host_id, graph_name)
|
5
|
+
|
6
|
+
message = {
|
7
|
+
'method' => 'graph.get',
|
8
|
+
'params' => {
|
9
|
+
'filter' => {
|
10
|
+
'name' => graph_name,
|
11
|
+
'hostid' => host_id
|
12
|
+
}
|
13
|
+
}
|
14
|
+
}
|
15
|
+
|
16
|
+
responce = send_request(message)
|
17
|
+
|
18
|
+
unless ( responce.empty? ) then
|
19
|
+
result = responce[0]['graphid']
|
20
|
+
else
|
21
|
+
result = nil
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def get_graphs(host_id)
|
26
|
+
|
27
|
+
message = {
|
28
|
+
'method' => 'graph.get',
|
29
|
+
'params' => {
|
30
|
+
'extendoutput' => '1',
|
31
|
+
'filter' => {
|
32
|
+
'hostid' => host_id
|
33
|
+
}
|
34
|
+
}
|
35
|
+
}
|
36
|
+
|
37
|
+
responce = send_request(message)
|
38
|
+
|
39
|
+
unless ( responce.empty? ) then
|
40
|
+
result = {}
|
41
|
+
|
42
|
+
responce.each() do |graph|
|
43
|
+
graph_id = graph['graphid']
|
44
|
+
graph_name = graph['name']
|
45
|
+
|
46
|
+
result[graph_id] = graph_name
|
47
|
+
end
|
48
|
+
else
|
49
|
+
result = nil
|
50
|
+
end
|
51
|
+
|
52
|
+
return result
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
data/lib/group.rb
ADDED
@@ -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_groups_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
|
data/lib/host.rb
ADDED
@@ -0,0 +1,96 @@
|
|
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
|
+
# - useipmi - Use or not ipmi. Default: 0 (don't use ipmi);
|
24
|
+
# - ipmi_ip - Default: '';
|
25
|
+
# - ipmi_port - Default: 623;
|
26
|
+
# - ipmi_authtype - Default: 0;
|
27
|
+
# - ipmi_privilege - Default: 0;
|
28
|
+
# - ipmi_username - Default: '';
|
29
|
+
# - ipmi_password - Default: '';
|
30
|
+
def add_host(host_options)
|
31
|
+
|
32
|
+
host_default = {
|
33
|
+
'host' => nil,
|
34
|
+
'port' => 10050,
|
35
|
+
'status' => 0,
|
36
|
+
'useip' => 0,
|
37
|
+
'dns' => '',
|
38
|
+
'ip' => '0.0.0.0',
|
39
|
+
'proxy_hostid' => 0,
|
40
|
+
'groups' => [],
|
41
|
+
'useipmi' => 0,
|
42
|
+
'ipmi_ip' => '',
|
43
|
+
'ipmi_port' => 623,
|
44
|
+
'ipmi_authtype' => 0,
|
45
|
+
'ipmi_privilege' => 0,
|
46
|
+
'ipmi_username' => '',
|
47
|
+
'ipmi_password' => ''
|
48
|
+
}
|
49
|
+
|
50
|
+
host_options['groups'].map! { |group_id| {'groupid' => group_id} }
|
51
|
+
|
52
|
+
host = merge_opt(host_default, host_options)
|
53
|
+
|
54
|
+
message = {
|
55
|
+
'method' => 'host.create',
|
56
|
+
'params' => host
|
57
|
+
}
|
58
|
+
|
59
|
+
responce = send_request(message)
|
60
|
+
|
61
|
+
if not ( responce.empty? ) then
|
62
|
+
result = responce['hostids'][0].to_i
|
63
|
+
else
|
64
|
+
result = nil
|
65
|
+
end
|
66
|
+
|
67
|
+
return result
|
68
|
+
end
|
69
|
+
|
70
|
+
# Method for retrieving host id from zabbix by hostname.
|
71
|
+
# * Non optional input parameters:
|
72
|
+
# - hostname - Type: String.
|
73
|
+
# * Return:
|
74
|
+
# - host_id - Return finded host_id for passed hostname. If host not found in zabbix - return nil
|
75
|
+
def get_host_id(hostname)
|
76
|
+
|
77
|
+
message = {
|
78
|
+
'method' => 'host.get',
|
79
|
+
'params' => {
|
80
|
+
'filter' => {
|
81
|
+
'host' => hostname
|
82
|
+
}
|
83
|
+
}
|
84
|
+
}
|
85
|
+
|
86
|
+
responce = send_request(message)
|
87
|
+
|
88
|
+
if not ( responce.empty? ) then
|
89
|
+
result = responce[0]['hostid'].to_i
|
90
|
+
else
|
91
|
+
result = nil
|
92
|
+
end
|
93
|
+
|
94
|
+
return result
|
95
|
+
end
|
96
|
+
end
|
data/lib/item.rb
ADDED
@@ -0,0 +1,127 @@
|
|
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
|
+
responce = send_request(message)
|
51
|
+
|
52
|
+
unless ( responce.empty? ) then
|
53
|
+
result = responce['itemids'][0]
|
54
|
+
else
|
55
|
+
result = nil
|
56
|
+
end
|
57
|
+
|
58
|
+
return result
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
def get_webitem_id(host_id, item_name)
|
63
|
+
message = {
|
64
|
+
'method' => 'item.get',
|
65
|
+
'params' => {
|
66
|
+
'filter' => {
|
67
|
+
'hostid' => host_id,
|
68
|
+
'type' => 9,
|
69
|
+
'key_' => "web.test.time[eva.ru,Get main page,resp]"
|
70
|
+
},
|
71
|
+
'webitems' => 1
|
72
|
+
}
|
73
|
+
}
|
74
|
+
|
75
|
+
responce = send_request(message)
|
76
|
+
|
77
|
+
p responce
|
78
|
+
|
79
|
+
unless ( responce.empty? ) then
|
80
|
+
result = responce[0]['itemid']
|
81
|
+
else
|
82
|
+
result = nil
|
83
|
+
end
|
84
|
+
|
85
|
+
return result
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
def get_item_id(host_id, item_name)
|
90
|
+
message = {
|
91
|
+
'method' => 'item.get',
|
92
|
+
'params' => {
|
93
|
+
'filter' => {
|
94
|
+
'hostid' => host_id,
|
95
|
+
'description' => item_name
|
96
|
+
}
|
97
|
+
}
|
98
|
+
}
|
99
|
+
|
100
|
+
responce = send_request(message)
|
101
|
+
|
102
|
+
unless ( responce.empty? ) then
|
103
|
+
result = responce[0]['itemid']
|
104
|
+
else
|
105
|
+
result = nil
|
106
|
+
end
|
107
|
+
|
108
|
+
return result
|
109
|
+
|
110
|
+
end
|
111
|
+
|
112
|
+
def update_item(item_id)
|
113
|
+
|
114
|
+
message = {
|
115
|
+
'method' => 'item.update',
|
116
|
+
'params' => {
|
117
|
+
'itemid' => item_id,
|
118
|
+
'status' => 0
|
119
|
+
}
|
120
|
+
}
|
121
|
+
|
122
|
+
responce = send_request(message)
|
123
|
+
|
124
|
+
p responce
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
data/lib/screen.rb
ADDED
@@ -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
|
data/lib/template.rb
ADDED
@@ -0,0 +1,162 @@
|
|
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 = responce.keys[0]
|
99
|
+
else
|
100
|
+
result = nil
|
101
|
+
end
|
102
|
+
|
103
|
+
return result
|
104
|
+
|
105
|
+
end
|
106
|
+
|
107
|
+
def link_templates_with_hosts(templates_id, hosts_id)
|
108
|
+
|
109
|
+
if ( templates_id.class == Array ) then
|
110
|
+
message_templates_id = templates_id
|
111
|
+
else
|
112
|
+
message_templates_id = [ templates_id ]
|
113
|
+
end
|
114
|
+
|
115
|
+
if ( hosts_id == Array ) then
|
116
|
+
message_hosts_id = hosts_id
|
117
|
+
else
|
118
|
+
message_hosts_id = [ hosts_id ]
|
119
|
+
end
|
120
|
+
|
121
|
+
message = {
|
122
|
+
'method' => 'template.massAdd',
|
123
|
+
'params' => {
|
124
|
+
'hosts' => message_hosts_id,
|
125
|
+
'templates' => message_templates_id
|
126
|
+
}
|
127
|
+
}
|
128
|
+
|
129
|
+
responce = send_request(message)
|
130
|
+
|
131
|
+
return responce
|
132
|
+
end
|
133
|
+
|
134
|
+
def unlink_templates_from_hosts(templates_id, hosts_id)
|
135
|
+
|
136
|
+
if ( templates_id.class == Array ) then
|
137
|
+
message_templates_id = templates_id
|
138
|
+
else
|
139
|
+
message_templates_id = [ templates_id ]
|
140
|
+
end
|
141
|
+
|
142
|
+
if ( hosts_id == Array ) then
|
143
|
+
message_hosts_id = hosts_id
|
144
|
+
else
|
145
|
+
message_hosts_id = [ hosts_id ]
|
146
|
+
end
|
147
|
+
|
148
|
+
message = {
|
149
|
+
'method' => 'template.massRemove',
|
150
|
+
'params' => {
|
151
|
+
'hosts' => message_hosts_id,
|
152
|
+
'templates' => message_templates_id,
|
153
|
+
'force' => '1'
|
154
|
+
}
|
155
|
+
}
|
156
|
+
|
157
|
+
responce = send_request(message)
|
158
|
+
|
159
|
+
return responce
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
data/lib/trigger.rb
ADDED
@@ -0,0 +1,94 @@
|
|
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
|
+
responce = send_request(message)
|
12
|
+
|
13
|
+
unless ( responce.empty? ) then
|
14
|
+
result = responce['triggerids'][0]
|
15
|
+
else
|
16
|
+
result = nil
|
17
|
+
end
|
18
|
+
|
19
|
+
return result
|
20
|
+
end
|
21
|
+
|
22
|
+
def get_trigger_id(host_id, trigger_name)
|
23
|
+
|
24
|
+
message = {
|
25
|
+
'method' => 'trigger.get',
|
26
|
+
'params' => {
|
27
|
+
'filter' => {
|
28
|
+
'hostid' => host_id,
|
29
|
+
'description' => trigger_name
|
30
|
+
}
|
31
|
+
}
|
32
|
+
}
|
33
|
+
|
34
|
+
responce = send_request(message)
|
35
|
+
|
36
|
+
unless ( responce.empty? ) then
|
37
|
+
result = responce[0]['triggerid']
|
38
|
+
else
|
39
|
+
result = nil
|
40
|
+
end
|
41
|
+
|
42
|
+
return result
|
43
|
+
end
|
44
|
+
|
45
|
+
def get_triggers_by_host(host_id)
|
46
|
+
|
47
|
+
message = {
|
48
|
+
'method' => 'trigger.get',
|
49
|
+
'params' => {
|
50
|
+
'filter' => {
|
51
|
+
'hostid' => host_id,
|
52
|
+
},
|
53
|
+
'extendoutput' => '1'
|
54
|
+
}
|
55
|
+
}
|
56
|
+
|
57
|
+
responce = send_request(message)
|
58
|
+
|
59
|
+
unless ( responce.empty? ) then
|
60
|
+
result = {}
|
61
|
+
responce.each do |trigger|
|
62
|
+
trigger_id = trigger['triggerid']
|
63
|
+
description = trigger['description']
|
64
|
+
result[trigger_id] = description
|
65
|
+
end
|
66
|
+
else
|
67
|
+
result = {}
|
68
|
+
end
|
69
|
+
|
70
|
+
return result
|
71
|
+
end
|
72
|
+
|
73
|
+
def update_trigger_status(trigger_id, status)
|
74
|
+
|
75
|
+
message = {
|
76
|
+
'method' => 'trigger.update_status',
|
77
|
+
'params' => {
|
78
|
+
'triggerid' => trigger_id,
|
79
|
+
'status' => status
|
80
|
+
}
|
81
|
+
}
|
82
|
+
|
83
|
+
responce = send_request(message)
|
84
|
+
|
85
|
+
unless ( responce.empty? ) then
|
86
|
+
result = responce['triggerids'][0]
|
87
|
+
else
|
88
|
+
result = nil
|
89
|
+
end
|
90
|
+
|
91
|
+
return result
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
data/lib/usermacro.rb
ADDED
@@ -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
|
data/lib/zabbixapi.rb
ADDED
data/zabbixapi.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'echoe'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
|
7
|
+
spec.version = '0.1.1'
|
8
|
+
spec.name = 'zabbixapi'
|
9
|
+
spec.summary = 'Ruby module for work with zabbix api.'
|
10
|
+
|
11
|
+
spec.email = 'verm666@gmail.com'
|
12
|
+
spec.author = 'Eduard Snesarev'
|
13
|
+
spec.homepage = 'http://github.com/verm666/zabbixapi'
|
14
|
+
spec.description = 'Ruby module for work with zabbix api. '
|
15
|
+
|
16
|
+
spec.has_rdoc = true
|
17
|
+
spec.extra_rdoc_files = 'README.rdoc'
|
18
|
+
|
19
|
+
|
20
|
+
spec.files = FileList["lib/*.rb", "bin/*", "spec/*", 'zabbixapi.gemspec', 'README.rdoc'].to_a
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: zabbixapi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 25
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Eduard Snesarev
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-08-09 00:00:00 +04:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: "Ruby module for work with zabbix api. "
|
23
|
+
email: verm666@gmail.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files:
|
29
|
+
- README.rdoc
|
30
|
+
files:
|
31
|
+
- lib/trigger.rb
|
32
|
+
- lib/screen.rb
|
33
|
+
- lib/1.rb
|
34
|
+
- lib/host.rb
|
35
|
+
- lib/usermacro.rb
|
36
|
+
- lib/template.rb
|
37
|
+
- lib/item.rb
|
38
|
+
- lib/zabbixapi.rb
|
39
|
+
- lib/base.rb
|
40
|
+
- lib/group.rb
|
41
|
+
- lib/graph.rb
|
42
|
+
- zabbixapi.gemspec
|
43
|
+
- README.rdoc
|
44
|
+
has_rdoc: true
|
45
|
+
homepage: http://github.com/verm666/zabbixapi
|
46
|
+
licenses: []
|
47
|
+
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
hash: 3
|
59
|
+
segments:
|
60
|
+
- 0
|
61
|
+
version: "0"
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
hash: 3
|
68
|
+
segments:
|
69
|
+
- 0
|
70
|
+
version: "0"
|
71
|
+
requirements: []
|
72
|
+
|
73
|
+
rubyforge_project:
|
74
|
+
rubygems_version: 1.3.7
|
75
|
+
signing_key:
|
76
|
+
specification_version: 3
|
77
|
+
summary: Ruby module for work with zabbix api.
|
78
|
+
test_files: []
|
79
|
+
|