zabbixapi 0.1.6 → 0.1.6.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +20 -6
- data/examples/zabbix_filesystem +1 -1
- data/lib/zabbixapi/application.rb +14 -31
- data/lib/zabbixapi/base.rb +24 -25
- data/lib/zabbixapi/graph.rb +19 -33
- data/lib/zabbixapi/group.rb +31 -51
- data/lib/zabbixapi/host.rb +43 -61
- data/lib/zabbixapi/item.rb +56 -97
- data/lib/zabbixapi/screen.rb +65 -112
- data/lib/zabbixapi/template.rb +42 -98
- data/lib/zabbixapi/trigger.rb +28 -59
- data/lib/zabbixapi/usermacro.rb +27 -42
- data/spec/item.rb +38 -42
- data/zabbixapi.gemspec +1 -1
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -3,21 +3,35 @@
|
|
3
3
|
Simple and lightweight ruby module for work with zabbix api version 1.8.x
|
4
4
|
|
5
5
|
You can:
|
6
|
-
* Create host/template/application/items/triggers and screens;
|
6
|
+
* Create and delete host/template/application/items/triggers and screens;
|
7
7
|
* Get info about all zabbix essences;
|
8
8
|
|
9
9
|
== Installation
|
10
10
|
|
11
11
|
gem install zabbixapi
|
12
12
|
|
13
|
-
== Get Start
|
13
|
+
== Get Start
|
14
14
|
|
15
|
-
*
|
15
|
+
* Create host and get hostid from zabbix api:
|
16
16
|
|
17
|
-
zbx
|
18
|
-
|
17
|
+
zbx = Zabbix::ZabbixApi.new('https://zabbix.example.com', 'login', 'password')
|
18
|
+
host_options = {
|
19
|
+
"ip" => '127.0.0.1',
|
20
|
+
"dns" => 'my.example.com',
|
21
|
+
"host" => 'my.example.com',
|
22
|
+
"useip" => 1,
|
23
|
+
"groups" => ['some_group']
|
24
|
+
}
|
25
|
+
if zbx.create_host(host_options)
|
26
|
+
puts zbx.get_host_id('my.example.com')
|
27
|
+
end
|
19
28
|
|
20
|
-
|
29
|
+
* Create hostgroup and get hostgroupid from zabbix api:
|
30
|
+
|
31
|
+
zbx = Zabbix::ZabbixApi.new('https://zabbix.example.com', 'login', 'password')
|
32
|
+
if zbx.add_group('some_group')
|
33
|
+
puts zbx.get_group_id('some_group')
|
34
|
+
end
|
21
35
|
|
22
36
|
== Dependencies
|
23
37
|
|
data/examples/zabbix_filesystem
CHANGED
@@ -1,50 +1,33 @@
|
|
1
1
|
module Zabbix
|
2
|
+
|
2
3
|
class ZabbixApi
|
3
4
|
def add_application(app_options)
|
4
|
-
|
5
5
|
app_options_default = {
|
6
|
-
|
7
|
-
|
6
|
+
'hostid' => nil,
|
7
|
+
'name' => nil
|
8
8
|
}
|
9
|
-
|
10
9
|
application = merge_opt(app_options_default, app_options)
|
11
10
|
message = {
|
12
|
-
|
13
|
-
|
11
|
+
'method' => 'application.create',
|
12
|
+
'params' => application
|
14
13
|
}
|
15
|
-
|
16
14
|
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
|
15
|
+
responce.empty? ? nil : responce['applicationids'][0].to_i
|
25
16
|
end
|
26
17
|
end
|
27
18
|
|
28
19
|
def get_app_id(host_id, app_name)
|
29
|
-
|
30
20
|
message = {
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
21
|
+
'method' => 'application.get',
|
22
|
+
'params' => {
|
23
|
+
'filter' => {
|
24
|
+
'name' => app_name,
|
25
|
+
'hostid' => host_id
|
26
|
+
}
|
36
27
|
}
|
37
|
-
}
|
38
28
|
}
|
39
|
-
|
40
29
|
responce = send_request(message)
|
30
|
+
responce.empty? ? nil : responce[0]['applicationid']
|
31
|
+
end
|
41
32
|
|
42
|
-
unless responce.empty? then
|
43
|
-
result = responce[0]['applicationid']
|
44
|
-
else
|
45
|
-
result = nil
|
46
|
-
end
|
47
|
-
|
48
|
-
return result
|
49
|
-
end
|
50
33
|
end
|
data/lib/zabbixapi/base.rb
CHANGED
@@ -25,7 +25,7 @@ module Zabbix
|
|
25
25
|
|
26
26
|
attr_accessor :debug
|
27
27
|
|
28
|
-
def initialize (
|
28
|
+
def initialize (api_url, api_user, api_password)
|
29
29
|
@api_url = api_url
|
30
30
|
@api_user = api_user
|
31
31
|
@api_password = api_password
|
@@ -45,7 +45,7 @@ module Zabbix
|
|
45
45
|
uri = URI.parse(@api_url)
|
46
46
|
http = Net::HTTP.new(uri.host, uri.port)
|
47
47
|
|
48
|
-
if
|
48
|
+
if uri.scheme == "https"
|
49
49
|
http.use_ssl = true
|
50
50
|
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
51
51
|
end
|
@@ -74,6 +74,11 @@ module Zabbix
|
|
74
74
|
|
75
75
|
response_body_hash = JSON.parse(response.body)
|
76
76
|
|
77
|
+
if @debug
|
78
|
+
puts "[ZBXAPI] : INFO : Response body"
|
79
|
+
puts response_body_hash.inspect
|
80
|
+
end
|
81
|
+
|
77
82
|
if error = response_body_hash['error']
|
78
83
|
error_message = error['message']
|
79
84
|
error_data = error['data']
|
@@ -82,17 +87,15 @@ module Zabbix
|
|
82
87
|
e_message = "Code: [" + error_code.to_s + "]. Message: [" + error_message +\
|
83
88
|
"]. Data: [" + error_data + "]."
|
84
89
|
|
85
|
-
case error_code.to_s
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
+
case error_code.to_s
|
91
|
+
when '-32602'
|
92
|
+
raise Zabbix::AlreadyExist.new(e_message)
|
93
|
+
else
|
94
|
+
raise Zabbix::ResponseError.new(e_message)
|
90
95
|
end
|
91
96
|
end
|
92
97
|
|
93
|
-
|
94
|
-
|
95
|
-
return result
|
98
|
+
return response_body_hash['result']
|
96
99
|
end
|
97
100
|
|
98
101
|
def send_request(message)
|
@@ -101,33 +104,29 @@ module Zabbix
|
|
101
104
|
end
|
102
105
|
|
103
106
|
def auth()
|
104
|
-
|
105
107
|
auth_message = {
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
108
|
+
'auth' => nil,
|
109
|
+
'method' => 'user.authenticate',
|
110
|
+
'params' => {
|
111
|
+
'user' => @api_user,
|
112
|
+
'password' => @api_password,
|
113
|
+
'0' => '0'
|
114
|
+
}
|
113
115
|
}
|
114
|
-
|
115
|
-
auth_id = do_request(auth_message)
|
116
|
-
|
117
|
-
return auth_id
|
116
|
+
do_request(auth_message)
|
118
117
|
end
|
119
118
|
|
120
119
|
# Utils.
|
120
|
+
|
121
121
|
def merge_opt(a, b)
|
122
122
|
c = {}
|
123
|
-
|
124
123
|
b.each_pair do |key, value|
|
125
124
|
if a.has_key?(key) then
|
126
125
|
c[key] = value
|
127
126
|
end
|
128
|
-
|
129
|
-
|
127
|
+
end
|
130
128
|
return a.merge(c)
|
131
129
|
end
|
130
|
+
|
132
131
|
end
|
133
132
|
end
|
data/lib/zabbixapi/graph.rb
CHANGED
@@ -3,64 +3,50 @@ module Zabbix
|
|
3
3
|
|
4
4
|
def add_graph(graph)
|
5
5
|
message = {
|
6
|
-
|
7
|
-
|
6
|
+
'method' => 'graph.create',
|
7
|
+
'params' => graph
|
8
8
|
}
|
9
|
-
|
10
9
|
response = send_request(message)
|
11
|
-
|
12
10
|
return 0
|
13
11
|
end
|
14
12
|
|
15
13
|
def get_graph_id(host_id, graph_name)
|
16
|
-
|
17
14
|
message = {
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
15
|
+
'method' => 'graph.get',
|
16
|
+
'params' => {
|
17
|
+
'filter' => {
|
18
|
+
'name' => graph_name,
|
19
|
+
'hostid' => host_id
|
20
|
+
}
|
23
21
|
}
|
24
|
-
}
|
25
22
|
}
|
26
|
-
|
27
23
|
response = send_request(message)
|
28
|
-
|
29
|
-
unless ( response.empty? ) then
|
30
|
-
result = response[0]['graphid']
|
31
|
-
else
|
32
|
-
result = nil
|
33
|
-
end
|
24
|
+
response.empty? ? nil : response[0]['graphid']
|
34
25
|
end
|
35
26
|
|
36
27
|
def get_graphs(host_id)
|
37
|
-
|
38
28
|
message = {
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
29
|
+
'method' => 'graph.get',
|
30
|
+
'params' => {
|
31
|
+
'extendoutput' => '1',
|
32
|
+
'filter' => {
|
33
|
+
'hostid' => host_id
|
34
|
+
}
|
44
35
|
}
|
45
|
-
}
|
46
36
|
}
|
47
|
-
|
48
37
|
response = send_request(message)
|
49
|
-
|
50
|
-
|
38
|
+
if response.empty?
|
39
|
+
result = nil
|
40
|
+
else
|
51
41
|
result = {}
|
52
|
-
|
53
42
|
response.each() do |graph|
|
54
43
|
graph_id = graph['graphid']
|
55
44
|
graph_name = graph['name']
|
56
|
-
|
57
45
|
result[graph_id] = graph_name
|
58
46
|
end
|
59
|
-
else
|
60
|
-
result = nil
|
61
47
|
end
|
62
|
-
|
63
48
|
return result
|
64
49
|
end
|
50
|
+
|
65
51
|
end
|
66
52
|
end
|
data/lib/zabbixapi/group.rb
CHANGED
@@ -1,78 +1,58 @@
|
|
1
1
|
module Zabbix
|
2
|
-
|
3
2
|
class ZabbixApi
|
4
|
-
def get_group_id(pattern)
|
5
3
|
|
4
|
+
def get_group_id(pattern)
|
6
5
|
message = {
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
6
|
+
'method' => 'hostgroup.get',
|
7
|
+
'params' => {
|
8
|
+
'filter' => {
|
9
|
+
'name' => pattern
|
10
|
+
}
|
11
11
|
}
|
12
|
-
}
|
13
12
|
}
|
14
|
-
|
15
13
|
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
|
14
|
+
response.empty? ? nil : response[0]['groupid']
|
24
15
|
end
|
25
16
|
|
26
17
|
def group_exist?(pattern)
|
27
|
-
|
28
18
|
group_id = get_groups_id(pattern)
|
29
|
-
|
30
|
-
if ( group_id ) then
|
31
|
-
return true
|
32
|
-
else
|
33
|
-
return false
|
34
|
-
end
|
19
|
+
group_id ? true : false
|
35
20
|
end
|
36
21
|
|
37
22
|
def add_group(groupname)
|
38
|
-
|
39
23
|
message = {
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
24
|
+
'method' => 'hostgroup.create',
|
25
|
+
'params' => {
|
26
|
+
'name' => groupname
|
27
|
+
}
|
44
28
|
}
|
45
|
-
|
46
29
|
response = send_request(message)
|
30
|
+
response ? response['groupids'][0].to_i : nil
|
31
|
+
end
|
47
32
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
33
|
+
def delete_group(groupname)
|
34
|
+
group_id = get_group_id(groupname)
|
35
|
+
message = {
|
36
|
+
'method' => 'hostgroup.delete',
|
37
|
+
'params' => {
|
38
|
+
'groupid' => group_id
|
39
|
+
}
|
40
|
+
}
|
41
|
+
response = send_request(message)
|
42
|
+
response ? response['groupids'][0].to_i : nil
|
55
43
|
end
|
56
44
|
|
57
45
|
def add_host_to_group(host_id, group_id)
|
58
|
-
|
59
46
|
message = {
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
47
|
+
'method' => 'hostgroup.massAdd',
|
48
|
+
'params' => {
|
49
|
+
'groups' => [group_id],
|
50
|
+
'hosts' => [host_id]
|
51
|
+
}
|
65
52
|
}
|
66
|
-
|
67
53
|
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
|
54
|
+
response.empty? ? false : true
|
76
55
|
end
|
56
|
+
|
77
57
|
end
|
78
58
|
end
|
data/lib/zabbixapi/host.rb
CHANGED
@@ -1,86 +1,68 @@
|
|
1
1
|
module Zabbix
|
2
|
-
|
3
2
|
class ZabbixApi
|
3
|
+
|
4
4
|
def update_host(host_id, host_options)
|
5
5
|
host = host_options
|
6
6
|
host['hostid'] = host_id
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
}
|
12
|
-
|
7
|
+
message = {
|
8
|
+
'method' => 'host.update',
|
9
|
+
'params' => host
|
10
|
+
}
|
13
11
|
responce = send_request(message)
|
14
|
-
|
15
|
-
|
16
|
-
result = responce['hostids'][0].to_i
|
17
|
-
else
|
18
|
-
result = nil
|
19
|
-
end
|
20
|
-
|
21
|
-
return result
|
22
|
-
end
|
12
|
+
responce.empty? ? nil : responce['hostids'][0].to_i
|
13
|
+
end
|
23
14
|
|
24
15
|
def add_host(host_options)
|
25
|
-
|
26
16
|
host_default = {
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
17
|
+
'host' => nil,
|
18
|
+
'port' => 10050,
|
19
|
+
'status' => 0,
|
20
|
+
'useip' => 0,
|
21
|
+
'dns' => '',
|
22
|
+
'ip' => '0.0.0.0',
|
23
|
+
'proxy_hostid' => 0,
|
24
|
+
'groups' => [],
|
25
|
+
'useipmi' => 0,
|
26
|
+
'ipmi_ip' => '',
|
27
|
+
'ipmi_port' => 623,
|
28
|
+
'ipmi_authtype' => 0,
|
29
|
+
'ipmi_privilege' => 0,
|
30
|
+
'ipmi_username' => '',
|
31
|
+
'ipmi_password' => ''
|
42
32
|
}
|
43
|
-
|
44
|
-
host_options['groups'].map! { |group_id| {'groupid' => group_id} }
|
45
|
-
|
33
|
+
host_options['groups'].nil? || host_options['groups'].map! { |group_id| {'groupid' => get_group_id(group_id)} }
|
46
34
|
host = merge_opt(host_default, host_options)
|
47
|
-
|
48
35
|
message = {
|
49
|
-
|
50
|
-
|
36
|
+
'method' => 'host.create',
|
37
|
+
'params' => host
|
51
38
|
}
|
52
|
-
|
53
39
|
response = send_request(message)
|
54
|
-
|
55
|
-
unless response.empty? then
|
56
|
-
result = response['hostids'][0].to_i
|
57
|
-
else
|
58
|
-
result = nil
|
59
|
-
end
|
60
|
-
|
61
|
-
return result
|
40
|
+
response.empty? ? nil : response['hostids'][0].to_i
|
62
41
|
end
|
63
42
|
|
64
43
|
def get_host_id(hostname)
|
65
|
-
|
66
44
|
message = {
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
45
|
+
'method' => 'host.get',
|
46
|
+
'params' => {
|
47
|
+
'filter' => {
|
48
|
+
'host' => hostname
|
49
|
+
}
|
71
50
|
}
|
72
|
-
}
|
73
51
|
}
|
74
|
-
|
75
52
|
response = send_request(message)
|
53
|
+
response.empty? ? nil : response[0]['hostid'].to_i
|
54
|
+
end
|
76
55
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
56
|
+
def delete_host(hostname)
|
57
|
+
host_id = get_host_id(hostname)
|
58
|
+
message = {
|
59
|
+
'method' => 'host.delete',
|
60
|
+
'params' => {
|
61
|
+
'hostid' => host_id
|
62
|
+
}
|
63
|
+
}
|
64
|
+
send_request(message)
|
84
65
|
end
|
66
|
+
|
85
67
|
end
|
86
68
|
end
|