iteh-zabbixapi 0.2.2
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/.gitignore +5 -0
- data/Gemfile +4 -0
- data/LICENSE +13 -0
- data/README.rdoc +41 -0
- data/Rakefile +8 -0
- data/examples/config.yml +4 -0
- data/examples/zabbix_availability +73 -0
- data/examples/zabbix_disk_io +152 -0
- data/examples/zabbix_filesystem +249 -0
- data/examples/zabbix_la +101 -0
- data/examples/zabbix_memory +276 -0
- data/examples/zabbix_net +82 -0
- data/lib/zabbixapi/application.rb +50 -0
- data/lib/zabbixapi/base.rb +139 -0
- data/lib/zabbixapi/graph.rb +66 -0
- data/lib/zabbixapi/group.rb +78 -0
- data/lib/zabbixapi/host.rb +85 -0
- data/lib/zabbixapi/item.rb +145 -0
- data/lib/zabbixapi/screen.rb +166 -0
- data/lib/zabbixapi/template.rb +158 -0
- data/lib/zabbixapi/trigger.rb +95 -0
- data/lib/zabbixapi/usermacro.rb +74 -0
- data/lib/zabbixapi/version.rb +20 -0
- data/lib/zabbixapi.rb +9 -0
- data/spec/fixtures/failed_auth_wrong_password_user.authenticate.txt +9 -0
- data/spec/fixtures/get_existing_host_host.get.txt +9 -0
- data/spec/fixtures/get_nil_for_unknown_host_host.get.txt +9 -0
- data/spec/fixtures/login_success_user.authenticate.txt +10 -0
- data/spec/record_http.rb +90 -0
- data/spec/spec_helper.rb +41 -0
- data/spec/zabbixapi_spec.rb +97 -0
- data/zabbixapi.gemspec +39 -0
- metadata +178 -0
@@ -0,0 +1,50 @@
|
|
1
|
+
module Zabbix
|
2
|
+
class ZabbixApi
|
3
|
+
def add_application(app_options)
|
4
|
+
|
5
|
+
app_options_default = {
|
6
|
+
'hostid' => nil,
|
7
|
+
'name' => nil
|
8
|
+
}
|
9
|
+
|
10
|
+
application = merge_opt(app_options_default, app_options)
|
11
|
+
message = {
|
12
|
+
'method' => 'application.create',
|
13
|
+
'params' => application
|
14
|
+
}
|
15
|
+
|
16
|
+
responce = send_request(message)
|
17
|
+
|
18
|
+
unless responce.empty? then
|
19
|
+
result = responce['applicationids'][0].to_i
|
20
|
+
else
|
21
|
+
result = nil
|
22
|
+
end
|
23
|
+
|
24
|
+
return result
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def get_app_id(host_id, app_name)
|
29
|
+
|
30
|
+
message = {
|
31
|
+
'method' => 'application.get',
|
32
|
+
'params' => {
|
33
|
+
'filter' => {
|
34
|
+
'name' => app_name,
|
35
|
+
'hostid' => host_id
|
36
|
+
}
|
37
|
+
}
|
38
|
+
}
|
39
|
+
|
40
|
+
responce = send_request(message)
|
41
|
+
|
42
|
+
unless responce.empty? then
|
43
|
+
result = responce[0]['applicationid']
|
44
|
+
else
|
45
|
+
result = nil
|
46
|
+
end
|
47
|
+
|
48
|
+
return result
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,139 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
require 'net/http'
|
5
|
+
require 'net/https'
|
6
|
+
|
7
|
+
module Zabbix
|
8
|
+
|
9
|
+
class SocketError < RuntimeError
|
10
|
+
end
|
11
|
+
|
12
|
+
class ResponseCodeError < RuntimeError
|
13
|
+
end
|
14
|
+
|
15
|
+
class ResponseError < RuntimeError
|
16
|
+
end
|
17
|
+
|
18
|
+
class AlreadyExist < RuntimeError
|
19
|
+
end
|
20
|
+
|
21
|
+
class AuthError < RuntimeError
|
22
|
+
end
|
23
|
+
|
24
|
+
class ZabbixApi
|
25
|
+
|
26
|
+
attr_accessor :debug
|
27
|
+
|
28
|
+
def initialize ( api_url, api_user, api_password )
|
29
|
+
@api_url = api_url
|
30
|
+
@api_user = api_user
|
31
|
+
@api_password = api_password
|
32
|
+
@auth_id = nil
|
33
|
+
|
34
|
+
@debug = false # Disable debug by default
|
35
|
+
end
|
36
|
+
|
37
|
+
def do_request(message)
|
38
|
+
|
39
|
+
id = rand 100_000
|
40
|
+
|
41
|
+
message['id'] = id
|
42
|
+
message['jsonrpc'] = '2.0'
|
43
|
+
|
44
|
+
message_json = JSON.generate(message)
|
45
|
+
|
46
|
+
uri = URI.parse(@api_url)
|
47
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
48
|
+
|
49
|
+
if ( uri.scheme == "https" ) then
|
50
|
+
http.use_ssl = true
|
51
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
52
|
+
end
|
53
|
+
|
54
|
+
request = Net::HTTP::Post.new(uri.request_uri)
|
55
|
+
request.add_field('Content-Type', 'application/json-rpc')
|
56
|
+
request.body=(message_json)
|
57
|
+
|
58
|
+
begin
|
59
|
+
puts "[ZBXAPI] : INFO : Do request. Body => #{request.body}" if @debug
|
60
|
+
response = http.request(request)
|
61
|
+
rescue ::SocketError => e
|
62
|
+
puts "[ZBXAPI] : ERROR : SocketError => #{e.message}" if @debug
|
63
|
+
raise Zabbix::SocketError.new(e.message)
|
64
|
+
end
|
65
|
+
|
66
|
+
if @debug
|
67
|
+
puts "[ZBXAPI] : INFO : Response start"
|
68
|
+
response.each_header do |key,value|
|
69
|
+
puts "#{key}:#{value}"
|
70
|
+
end
|
71
|
+
puts response
|
72
|
+
puts "[ZBXAPI] : INFO : Response end"
|
73
|
+
end
|
74
|
+
|
75
|
+
if response.code != "200"
|
76
|
+
raise Zabbix::ResponseCodeError.new("Responce code from [" + @api_url + "] is #{response.code}")
|
77
|
+
end
|
78
|
+
|
79
|
+
response_body_hash = JSON.parse(response.body)
|
80
|
+
|
81
|
+
if error = response_body_hash['error']
|
82
|
+
error_message = error['message']
|
83
|
+
error_data = error['data']
|
84
|
+
error_code = error['code']
|
85
|
+
|
86
|
+
e_message = "Code: [" + error_code.to_s + "]. Message: [" + error_message + "]. Data: [" + error_data + "]."
|
87
|
+
|
88
|
+
case error_code.to_s
|
89
|
+
when '-32602'
|
90
|
+
raise Zabbix::AlreadyExist.new(e_message)
|
91
|
+
else
|
92
|
+
raise Zabbix::ResponseError.new(e_message)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
result = response_body_hash['result']
|
97
|
+
result
|
98
|
+
end
|
99
|
+
|
100
|
+
def send_request(message)
|
101
|
+
message['auth'] = auth()
|
102
|
+
do_request(message)
|
103
|
+
end
|
104
|
+
|
105
|
+
def auth()
|
106
|
+
|
107
|
+
return @auth_id if @auth_id
|
108
|
+
|
109
|
+
auth_message = {
|
110
|
+
'auth' => nil,
|
111
|
+
'method' => 'user.authenticate',
|
112
|
+
'params' => {
|
113
|
+
'user' => @api_user,
|
114
|
+
'password' => @api_password,
|
115
|
+
'0' => '0'
|
116
|
+
}
|
117
|
+
}
|
118
|
+
|
119
|
+
begin
|
120
|
+
@auth_id = do_request(auth_message)
|
121
|
+
rescue RuntimeError => e
|
122
|
+
raise AuthError.new(e.message)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
# Utils.
|
127
|
+
def merge_opt(a, b)
|
128
|
+
c = {}
|
129
|
+
|
130
|
+
b.each_pair do |key, value|
|
131
|
+
if a.has_key?(key) then
|
132
|
+
c[key] = value
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
return a.merge(c)
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
module Zabbix
|
2
|
+
class ZabbixApi
|
3
|
+
|
4
|
+
def add_graph(graph)
|
5
|
+
message = {
|
6
|
+
'method' => 'graph.create',
|
7
|
+
'params' => graph
|
8
|
+
}
|
9
|
+
|
10
|
+
response = send_request(message)
|
11
|
+
|
12
|
+
return 0
|
13
|
+
end
|
14
|
+
|
15
|
+
def get_graph_id(host_id, graph_name)
|
16
|
+
|
17
|
+
message = {
|
18
|
+
'method' => 'graph.get',
|
19
|
+
'params' => {
|
20
|
+
'filter' => {
|
21
|
+
'name' => graph_name,
|
22
|
+
'hostid' => host_id
|
23
|
+
}
|
24
|
+
}
|
25
|
+
}
|
26
|
+
|
27
|
+
response = send_request(message)
|
28
|
+
|
29
|
+
unless ( response.empty? ) then
|
30
|
+
result = response[0]['graphid']
|
31
|
+
else
|
32
|
+
result = nil
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def get_graphs(host_id)
|
37
|
+
|
38
|
+
message = {
|
39
|
+
'method' => 'graph.get',
|
40
|
+
'params' => {
|
41
|
+
'extendoutput' => '1',
|
42
|
+
'filter' => {
|
43
|
+
'hostid' => host_id
|
44
|
+
}
|
45
|
+
}
|
46
|
+
}
|
47
|
+
|
48
|
+
response = send_request(message)
|
49
|
+
|
50
|
+
unless ( response.empty? ) then
|
51
|
+
result = {}
|
52
|
+
|
53
|
+
response.each() do |graph|
|
54
|
+
graph_id = graph['graphid']
|
55
|
+
graph_name = graph['name']
|
56
|
+
|
57
|
+
result[graph_id] = graph_name
|
58
|
+
end
|
59
|
+
else
|
60
|
+
result = nil
|
61
|
+
end
|
62
|
+
|
63
|
+
return result
|
64
|
+
end
|
65
|
+
end
|
66
|
+
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
|
+
response = send_request(message)
|
16
|
+
|
17
|
+
if not ( response.empty? ) then
|
18
|
+
result = response[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
|
+
response = send_request(message)
|
47
|
+
|
48
|
+
if ( response ) then
|
49
|
+
result = response['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
|
+
response = send_request(message)
|
68
|
+
|
69
|
+
if not ( response.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,85 @@
|
|
1
|
+
module Zabbix
|
2
|
+
|
3
|
+
class ZabbixApi
|
4
|
+
def add_host(host_options)
|
5
|
+
|
6
|
+
host_default = {
|
7
|
+
'host' => nil,
|
8
|
+
'port' => 10050,
|
9
|
+
'status' => 0,
|
10
|
+
'useip' => 0,
|
11
|
+
'dns' => '',
|
12
|
+
'ip' => '0.0.0.0',
|
13
|
+
'proxy_hostid' => 0,
|
14
|
+
'groups' => [],
|
15
|
+
'useipmi' => 0,
|
16
|
+
'ipmi_ip' => '',
|
17
|
+
'ipmi_port' => 623,
|
18
|
+
'ipmi_authtype' => 0,
|
19
|
+
'ipmi_privilege' => 0,
|
20
|
+
'ipmi_username' => '',
|
21
|
+
'ipmi_password' => ''
|
22
|
+
}
|
23
|
+
|
24
|
+
host_options['groups'].map! { |group_id| {'groupid' => group_id} }
|
25
|
+
|
26
|
+
host = merge_opt(host_default, host_options)
|
27
|
+
|
28
|
+
message = {
|
29
|
+
'method' => 'host.create',
|
30
|
+
'params' => host
|
31
|
+
}
|
32
|
+
|
33
|
+
response = send_request(message)
|
34
|
+
|
35
|
+
unless response.empty? then
|
36
|
+
result = response['hostids'][0].to_i
|
37
|
+
else
|
38
|
+
result = nil
|
39
|
+
end
|
40
|
+
|
41
|
+
result
|
42
|
+
end
|
43
|
+
|
44
|
+
def get_host_id(hostname)
|
45
|
+
|
46
|
+
message = {
|
47
|
+
'method' => 'host.get',
|
48
|
+
'params' => {
|
49
|
+
'filter' => {
|
50
|
+
'host' => hostname
|
51
|
+
}
|
52
|
+
}
|
53
|
+
}
|
54
|
+
|
55
|
+
response = send_request(message)
|
56
|
+
|
57
|
+
unless response.empty? then
|
58
|
+
result = response[0]['hostid'].to_i
|
59
|
+
else
|
60
|
+
result = nil
|
61
|
+
end
|
62
|
+
|
63
|
+
result
|
64
|
+
end
|
65
|
+
|
66
|
+
def get_host(params)
|
67
|
+
|
68
|
+
message = {
|
69
|
+
'method' => 'host.get',
|
70
|
+
'params' => params
|
71
|
+
}
|
72
|
+
|
73
|
+
response = send_request(message)
|
74
|
+
|
75
|
+
unless response.empty? then
|
76
|
+
result = response[0]
|
77
|
+
else
|
78
|
+
result = nil
|
79
|
+
end
|
80
|
+
|
81
|
+
result
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,145 @@
|
|
1
|
+
module Zabbix
|
2
|
+
## Item types (see ./frontends/php/include/defines.inc.php in zabbix source)
|
3
|
+
ITEM_TYPE_ZABBIX = 0
|
4
|
+
ITEM_TYPE_SNMPV1 = 1
|
5
|
+
ITEM_TYPE_TRAPPER = 2
|
6
|
+
ITEM_TYPE_SIMPLE = 3
|
7
|
+
ITEM_TYPE_SNMPV2C = 4
|
8
|
+
ITEM_TYPE_INTERNAL = 5
|
9
|
+
ITEM_TYPE_SNMPV3 = 6
|
10
|
+
ITEM_TYPE_ZABBIX_ACTIVE = 7
|
11
|
+
ITEM_TYPE_AGGREGATE = 8
|
12
|
+
ITEM_TYPE_HTTPTEST = 9
|
13
|
+
ITEM_TYPE_EXTERNAL = 10
|
14
|
+
ITEM_TYPE_DB_MONITOR = 11
|
15
|
+
ITEM_TYPE_IPMI = 12
|
16
|
+
ITEM_TYPE_SSH = 13
|
17
|
+
ITEM_TYPE_TELNET = 14
|
18
|
+
ITEM_TYPE_CALCULATED = 15
|
19
|
+
|
20
|
+
class ZabbixApi
|
21
|
+
|
22
|
+
def add_item(item)
|
23
|
+
|
24
|
+
# Default item options
|
25
|
+
# See: http://www.zabbix.com/documentation/1.8/api/item
|
26
|
+
|
27
|
+
item_options = {
|
28
|
+
'description' => nil,
|
29
|
+
'key_' => nil,
|
30
|
+
'hostid' => nil,
|
31
|
+
'delay' => 60,
|
32
|
+
'history' => 60,
|
33
|
+
'status' => 0,
|
34
|
+
'type' => 7,
|
35
|
+
'snmp_community' => '',
|
36
|
+
'snmp_oid' => '',
|
37
|
+
'value_type' => 3,
|
38
|
+
'data_type' => 0,
|
39
|
+
'trapper_hosts' => 'localhost',
|
40
|
+
'snmp_port' => 161,
|
41
|
+
'units' => '',
|
42
|
+
'multiplier' => 0,
|
43
|
+
'delta' => 0,
|
44
|
+
'snmpv3_securityname' => '',
|
45
|
+
'snmpv3_securitylevel' => 0,
|
46
|
+
'snmpv3_authpassphrase' => '',
|
47
|
+
'snmpv3_privpassphrase' => '',
|
48
|
+
'formula' => 0,
|
49
|
+
'trends' => 365,
|
50
|
+
'logtimefmt' => '',
|
51
|
+
'valuemapid' => 0,
|
52
|
+
'delay_flex' => '',
|
53
|
+
'authtype' => 0,
|
54
|
+
'username' => '',
|
55
|
+
'password' => '',
|
56
|
+
'publickey' => '',
|
57
|
+
'privatekey' => '',
|
58
|
+
'params' => '',
|
59
|
+
'ipmi_sensor' => '',
|
60
|
+
'applications' => '',
|
61
|
+
'templateid' => 0
|
62
|
+
}
|
63
|
+
|
64
|
+
|
65
|
+
item_options.merge!(item)
|
66
|
+
|
67
|
+
message = {
|
68
|
+
'method' => 'item.create',
|
69
|
+
'params' => [ item_options ]
|
70
|
+
}
|
71
|
+
|
72
|
+
response = send_request(message)
|
73
|
+
|
74
|
+
unless response.empty? then
|
75
|
+
result = response['itemids'][0]
|
76
|
+
else
|
77
|
+
result = nil
|
78
|
+
end
|
79
|
+
|
80
|
+
return result
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
def get_webitem_id(host_id, item_name)
|
85
|
+
message = {
|
86
|
+
'method' => 'item.get',
|
87
|
+
'params' => {
|
88
|
+
'filter' => {
|
89
|
+
'hostid' => host_id,
|
90
|
+
'type' => 9,
|
91
|
+
'key_' => "web.test.time[eva.ru,Get main page,resp]"
|
92
|
+
},
|
93
|
+
'webitems' => 1
|
94
|
+
}
|
95
|
+
}
|
96
|
+
|
97
|
+
response = send_request(message)
|
98
|
+
|
99
|
+
unless ( response.empty? ) then
|
100
|
+
result = response[0]['itemid']
|
101
|
+
else
|
102
|
+
result = nil
|
103
|
+
end
|
104
|
+
|
105
|
+
return result
|
106
|
+
end
|
107
|
+
|
108
|
+
def get_item_id(host_id, item_name)
|
109
|
+
message = {
|
110
|
+
'method' => 'item.get',
|
111
|
+
'params' => {
|
112
|
+
'filter' => {
|
113
|
+
'hostid' => host_id,
|
114
|
+
'description' => item_name
|
115
|
+
}
|
116
|
+
}
|
117
|
+
}
|
118
|
+
|
119
|
+
response = send_request(message)
|
120
|
+
|
121
|
+
unless ( response.empty? ) then
|
122
|
+
result = response[0]['itemid']
|
123
|
+
else
|
124
|
+
result = nil
|
125
|
+
end
|
126
|
+
|
127
|
+
return result
|
128
|
+
|
129
|
+
end
|
130
|
+
|
131
|
+
def update_item(item_id)
|
132
|
+
|
133
|
+
message = {
|
134
|
+
'method' => 'item.update',
|
135
|
+
'params' => {
|
136
|
+
'itemid' => item_id,
|
137
|
+
'status' => 0
|
138
|
+
}
|
139
|
+
}
|
140
|
+
|
141
|
+
response = send_request(message)
|
142
|
+
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|