zabbixapi 0.5.0b2 → 0.5.0b3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +3 -2
- data/lib/zabbixapi.rb +50 -29
- data/lib/zabbixapi/applications.rb +10 -31
- data/lib/zabbixapi/basic.rb +109 -0
- data/lib/zabbixapi/client.rb +3 -3
- data/lib/zabbixapi/graphs.rb +9 -14
- data/lib/zabbixapi/hostgroups.rb +12 -46
- data/lib/zabbixapi/hosts.rb +13 -31
- data/lib/zabbixapi/items.rb +14 -33
- data/lib/zabbixapi/mediatypes.rb +15 -88
- data/lib/zabbixapi/screens.rb +9 -60
- data/lib/zabbixapi/server.rb +3 -3
- data/lib/zabbixapi/templates.rb +10 -63
- data/lib/zabbixapi/triggers.rb +10 -34
- data/lib/zabbixapi/usergroups.rb +8 -90
- data/lib/zabbixapi/users.rb +7 -43
- data/lib/zabbixapi/version.rb +1 -1
- data/spec/localhost.rb +102 -75
- data/spec/run.rb +102 -75
- metadata +4 -3
data/README.md
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
Simple and lightweight ruby module for work with zabbix api
|
4
4
|
|
5
5
|
[![Build Status](https://travis-ci.org/vadv/zabbixapi.png)](https://travis-ci.org/vadv/zabbixapi)
|
6
|
+
[![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/vadv/zabbixapi)
|
6
7
|
|
7
8
|
#####Now worked with zabbix
|
8
9
|
* 1.8.2 (api version 1.2)
|
@@ -236,7 +237,7 @@ zbx.usergroups.set_perm(
|
|
236
237
|
```ruby
|
237
238
|
zbx.mediatypes.create_or_update(
|
238
239
|
:description => "mediatype",
|
239
|
-
:type => 0,
|
240
|
+
:type => 0, # 0 - Email, 1 - External script, 2 - SMS, 3 - Jabber, 100 - EzTexting,
|
240
241
|
:smtp_server => "127.0.0.1",
|
241
242
|
:smtp_email => "zabbix@test.com"
|
242
243
|
)
|
@@ -247,7 +248,7 @@ zbx.users.add_medias(
|
|
247
248
|
:mediatypeid => zbx.mediatypes.get_id(:description => "mediatype"),
|
248
249
|
:sendto => "test@test",
|
249
250
|
:active => 0,
|
250
|
-
:period => "1-7,00:00-24:00",
|
251
|
+
:period => "1-7,00:00-24:00", # 1-7 days and 00:00-24:00 hours
|
251
252
|
:severity => "56"
|
252
253
|
}
|
253
254
|
]
|
data/lib/zabbixapi.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
-
require "zabbixapi/
|
1
|
+
require "zabbixapi/basic"
|
2
2
|
require "zabbixapi/client"
|
3
|
+
require "zabbixapi/version"
|
3
4
|
require "zabbixapi/server"
|
4
5
|
require "zabbixapi/applications"
|
5
6
|
require "zabbixapi/templates"
|
@@ -16,45 +17,65 @@ require "zabbixapi/mediatypes"
|
|
16
17
|
class ZabbixApi
|
17
18
|
|
18
19
|
attr :client
|
19
|
-
attr :server
|
20
|
-
attr :users
|
21
|
-
attr :items
|
22
|
-
attr :applications
|
23
|
-
attr :templates
|
24
|
-
attr :hostgroups
|
25
|
-
attr :hosts
|
26
|
-
attr :triggers
|
27
|
-
attr :graphs
|
28
|
-
attr :screens
|
29
|
-
attr :usergroups
|
30
|
-
attr :mediatypes
|
31
20
|
|
32
21
|
def self.connect(options = {})
|
33
22
|
new(options)
|
34
23
|
end
|
35
24
|
|
36
|
-
def self.current
|
37
|
-
@current ||= ZabbixApi.new
|
38
|
-
end
|
39
|
-
|
40
25
|
def query(data)
|
41
26
|
@client.api_request(:method => data[:method], :params => data[:params])
|
42
27
|
end
|
43
28
|
|
44
29
|
def initialize(options = {})
|
45
30
|
@client = Client.new(options)
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
@
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
@
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
@
|
31
|
+
end
|
32
|
+
|
33
|
+
def server
|
34
|
+
@server ||= Server.new(@client)
|
35
|
+
end
|
36
|
+
|
37
|
+
def users
|
38
|
+
@users ||= Users.new(@client)
|
39
|
+
end
|
40
|
+
|
41
|
+
def items
|
42
|
+
@items ||= Items.new(@client)
|
43
|
+
end
|
44
|
+
|
45
|
+
def hosts
|
46
|
+
@hosts ||= Hosts.new(@client)
|
47
|
+
end
|
48
|
+
|
49
|
+
def applications
|
50
|
+
@applications ||= Applications.new(@client)
|
51
|
+
end
|
52
|
+
|
53
|
+
def templates
|
54
|
+
@templates ||= Templates.new(@client)
|
55
|
+
end
|
56
|
+
|
57
|
+
def hostgroups
|
58
|
+
@hostgroups ||= HostGroups.new(@client)
|
59
|
+
end
|
60
|
+
|
61
|
+
def triggers
|
62
|
+
@triggers ||= Triggers.new(@client)
|
63
|
+
end
|
64
|
+
|
65
|
+
def graphs
|
66
|
+
@graphs ||= Graphs.new(@client)
|
67
|
+
end
|
68
|
+
|
69
|
+
def screens
|
70
|
+
@screens ||= Screens.new(@client)
|
71
|
+
end
|
72
|
+
|
73
|
+
def usergroups
|
74
|
+
@usergroups ||= Usergroups.new(@client)
|
75
|
+
end
|
76
|
+
|
77
|
+
def mediatypes
|
78
|
+
@mediatypes ||= Mediatypes.new(@client)
|
58
79
|
end
|
59
80
|
|
60
81
|
end
|
@@ -1,46 +1,25 @@
|
|
1
1
|
class ZabbixApi
|
2
|
-
class Applications
|
2
|
+
class Applications < Basic
|
3
3
|
|
4
|
-
def
|
5
|
-
|
6
|
-
@options = options
|
4
|
+
def api_method_name
|
5
|
+
"application"
|
7
6
|
end
|
8
7
|
|
9
|
-
def
|
10
|
-
|
11
|
-
result.empty? ? nil : result['applicationids'][0].to_i
|
8
|
+
def api_identify
|
9
|
+
"name"
|
12
10
|
end
|
13
11
|
|
14
|
-
def
|
15
|
-
|
12
|
+
def create(data)
|
13
|
+
create_array(data)
|
16
14
|
end
|
17
15
|
|
18
16
|
def delete(data)
|
19
|
-
|
20
|
-
result.empty? ? nil : result['applicationids'][0].to_i
|
21
|
-
end
|
22
|
-
|
23
|
-
def get_or_create(data)
|
24
|
-
unless appid = get_id(data)
|
25
|
-
appid = create(data)
|
26
|
-
end
|
27
|
-
appid
|
28
|
-
end
|
29
|
-
|
30
|
-
def destroy(data)
|
31
|
-
delete(data)
|
17
|
+
delete_array(data)
|
32
18
|
end
|
33
19
|
|
34
20
|
def get_full_data(data)
|
35
|
-
|
36
|
-
end
|
37
|
-
|
38
|
-
def get_id(data)
|
39
|
-
result = get_full_data(data)
|
40
|
-
applicationid = nil
|
41
|
-
result.each { |app| applicationid = app['applicationid'].to_i if app['name'] == data[:name] }
|
42
|
-
applicationid
|
21
|
+
get_full_data_filter(data)
|
43
22
|
end
|
44
23
|
|
45
24
|
end
|
46
|
-
end
|
25
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
class ZabbixApi
|
2
|
+
class Basic
|
3
|
+
|
4
|
+
def initialize(client)
|
5
|
+
@client = client
|
6
|
+
end
|
7
|
+
|
8
|
+
####### Synonyms #########
|
9
|
+
def add(data)
|
10
|
+
create(data)
|
11
|
+
end
|
12
|
+
|
13
|
+
def destroy(data)
|
14
|
+
delete(data)
|
15
|
+
end
|
16
|
+
|
17
|
+
def dump(data)
|
18
|
+
get_full_data(data)
|
19
|
+
end
|
20
|
+
|
21
|
+
##########################
|
22
|
+
|
23
|
+
####### Methods ##########
|
24
|
+
def create_array(data)
|
25
|
+
result = @client.api_request(:method => "#{api_method_name}.create", :params => [data])
|
26
|
+
result.empty? ? nil : result[api_keys][0].to_i
|
27
|
+
end
|
28
|
+
|
29
|
+
def create(data)
|
30
|
+
result = @client.api_request(:method => "#{api_method_name}.create", :params => data)
|
31
|
+
result.empty? ? nil : result[api_keys][0].to_i
|
32
|
+
end
|
33
|
+
|
34
|
+
def delete_array(data)
|
35
|
+
result = @client.api_request(:method => "#{api_method_name}.delete", :params => [data])
|
36
|
+
result.empty? ? nil : result[api_keys][0].to_i
|
37
|
+
end
|
38
|
+
|
39
|
+
def delete_array_sym(data)
|
40
|
+
result = @client.api_request(:method => "#{api_method_name}.delete", :params => [api_key.to_sym => data])
|
41
|
+
result.empty? ? nil : result[api_keys][0].to_i
|
42
|
+
end
|
43
|
+
|
44
|
+
def delete(data)
|
45
|
+
result = @client.api_request(:method => "#{api_method_name}.delete", :params => data)
|
46
|
+
result.empty? ? nil : result[api_keys][0].to_i
|
47
|
+
end
|
48
|
+
|
49
|
+
def update(data)
|
50
|
+
result = @client.api_request(:method => "#{api_method_name}.update", :params => data)
|
51
|
+
result.empty? ? nil : result[api_keys][0].to_i
|
52
|
+
end
|
53
|
+
|
54
|
+
def get_full_data_filter(data)
|
55
|
+
@client.api_request(:method => "#{api_method_name}.get", :params => {:filter => data, :output => "extend"})
|
56
|
+
end
|
57
|
+
|
58
|
+
def get_full_data_filter_array(data)
|
59
|
+
@client.api_request(:method => "#{api_method_name}.get", :params => {:filter => [data[api_identify.to_sym]], :output => "extend"})
|
60
|
+
end
|
61
|
+
|
62
|
+
def get_id(data)
|
63
|
+
result = get_full_data(data)
|
64
|
+
id = nil
|
65
|
+
result.each { |tmpl| id = tmpl[api_key].to_i if tmpl[api_identify] == data[api_identify.to_sym] }
|
66
|
+
id
|
67
|
+
end
|
68
|
+
|
69
|
+
def get_or_create(data)
|
70
|
+
id = get_id(data)
|
71
|
+
if id.nil?
|
72
|
+
id = create(data)
|
73
|
+
end
|
74
|
+
id
|
75
|
+
end
|
76
|
+
|
77
|
+
def create_or_update(data)
|
78
|
+
id = get_id(api_identify.to_sym => data[api_identify.to_sym], :templateid => data[:templateid])
|
79
|
+
id ? update(data.merge(api_key.to_sym => id)) : create(data)
|
80
|
+
end
|
81
|
+
|
82
|
+
def all
|
83
|
+
result = {}
|
84
|
+
@client.api_request(:method => "#{api_method_name}.get", :params => {:output => "extend"}).each do |tmpl|
|
85
|
+
result[tmpl[api_identify]] = tmpl[api_key]
|
86
|
+
end
|
87
|
+
result
|
88
|
+
end
|
89
|
+
|
90
|
+
##########################
|
91
|
+
|
92
|
+
def api_method_name
|
93
|
+
raise "Can't call here api_method_name"
|
94
|
+
end
|
95
|
+
|
96
|
+
def api_identify
|
97
|
+
raise "Can't call here api_identify"
|
98
|
+
end
|
99
|
+
|
100
|
+
def api_keys
|
101
|
+
api_key + "s"
|
102
|
+
end
|
103
|
+
|
104
|
+
def api_key
|
105
|
+
api_method_name + "id"
|
106
|
+
end
|
107
|
+
|
108
|
+
end
|
109
|
+
end
|
data/lib/zabbixapi/client.rb
CHANGED
@@ -46,10 +46,10 @@ class ZabbixApi
|
|
46
46
|
|
47
47
|
def http_request(body)
|
48
48
|
uri = URI.parse(@options[:url])
|
49
|
-
|
50
|
-
http = Net::HTTP.new(uri.host, uri.port)
|
51
|
-
else
|
49
|
+
if @proxy_uri.nil?
|
52
50
|
http = Net::HTTP.Proxy(@proxy_host, @proxy_port, @proxy_user, @proxy_pass).new(uri.host, uri.port)
|
51
|
+
else
|
52
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
53
53
|
end
|
54
54
|
request = Net::HTTP::Post.new(uri.request_uri)
|
55
55
|
request.add_field('Content-Type', 'application/json-rpc')
|
data/lib/zabbixapi/graphs.rb
CHANGED
@@ -1,18 +1,17 @@
|
|
1
1
|
class ZabbixApi
|
2
|
-
class Graphs
|
2
|
+
class Graphs < Basic
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
|
5
|
+
def api_method_name
|
6
|
+
"graph"
|
7
7
|
end
|
8
8
|
|
9
|
-
def
|
10
|
-
|
11
|
-
result.empty? ? nil : result['graphids'][0].to_i
|
9
|
+
def api_identify
|
10
|
+
"name"
|
12
11
|
end
|
13
12
|
|
14
|
-
def
|
15
|
-
|
13
|
+
def create(data)
|
14
|
+
create_array(data)
|
16
15
|
end
|
17
16
|
|
18
17
|
def delete(data)
|
@@ -25,12 +24,8 @@ class ZabbixApi
|
|
25
24
|
end
|
26
25
|
end
|
27
26
|
|
28
|
-
def destroy(data)
|
29
|
-
delete(data)
|
30
|
-
end
|
31
|
-
|
32
27
|
def get_full_data(data)
|
33
|
-
|
28
|
+
get_full_data_filter()
|
34
29
|
end
|
35
30
|
|
36
31
|
def get_ids_by_host(data)
|
data/lib/zabbixapi/hostgroups.rb
CHANGED
@@ -1,62 +1,28 @@
|
|
1
1
|
class ZabbixApi
|
2
|
-
class HostGroups
|
2
|
+
class HostGroups < Basic
|
3
3
|
|
4
|
-
def
|
5
|
-
|
6
|
-
@options = options
|
4
|
+
def api_method_name
|
5
|
+
"hostgroup"
|
7
6
|
end
|
8
7
|
|
9
|
-
def
|
10
|
-
|
11
|
-
result.empty? ? nil : result['groupids'][0].to_i
|
8
|
+
def api_identify
|
9
|
+
"name"
|
12
10
|
end
|
13
11
|
|
14
|
-
def
|
15
|
-
|
12
|
+
def api_key
|
13
|
+
"groupid"
|
16
14
|
end
|
17
15
|
|
18
|
-
def
|
19
|
-
|
20
|
-
result.empty? ? nil : result['groupids'][0].to_i
|
21
|
-
end
|
22
|
-
|
23
|
-
def destroy(data)
|
24
|
-
delete(data)
|
16
|
+
def create(data)
|
17
|
+
create_array(data)
|
25
18
|
end
|
26
19
|
|
27
|
-
def
|
28
|
-
|
29
|
-
hostgroupid = update(data)
|
30
|
-
end
|
31
|
-
hostgroupid
|
20
|
+
def delete(data)
|
21
|
+
delete_array_sym(data)
|
32
22
|
end
|
33
23
|
|
34
24
|
def get_full_data(data)
|
35
|
-
|
36
|
-
when "1.2"
|
37
|
-
@client.api_request(:method => "hostgroup.get", :params => {:filter => data, :output => "extend"})
|
38
|
-
else
|
39
|
-
@client.api_request(:method => "hostgroup.get", :params => {:filter => data, :output => "extend"})
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
# Return all hostgroups
|
44
|
-
#
|
45
|
-
# * *Returns* :
|
46
|
-
# - Hash with {"Hostgroup1" => "id1", "Hostgroup2" => "id2"}
|
47
|
-
def all
|
48
|
-
result = {}
|
49
|
-
@client.api_request(:method => "hostgroup.get", :params => {:output => "extend"}).each do |hostgrp|
|
50
|
-
result[hostgrp['name']] = hostgrp['groupid']
|
51
|
-
end
|
52
|
-
result
|
53
|
-
end
|
54
|
-
|
55
|
-
def get_id(data)
|
56
|
-
result = get_full_data(data)
|
57
|
-
hostgroupid = nil
|
58
|
-
result.each { |hgroup| hostgroupid = hgroup['groupid'].to_i if hgroup['name'] == data[:name] }
|
59
|
-
hostgroupid
|
25
|
+
get_full_data_filter(data)
|
60
26
|
end
|
61
27
|
|
62
28
|
end
|
data/lib/zabbixapi/hosts.rb
CHANGED
@@ -1,9 +1,16 @@
|
|
1
1
|
class ZabbixApi
|
2
|
-
class Hosts
|
2
|
+
class Hosts < Basic
|
3
3
|
|
4
|
-
def
|
5
|
-
|
6
|
-
|
4
|
+
def api_method_name
|
5
|
+
"host"
|
6
|
+
end
|
7
|
+
|
8
|
+
def api_identify
|
9
|
+
"host"
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize(client)
|
13
|
+
@client = client
|
7
14
|
@host_default_options = {
|
8
15
|
:host => nil,
|
9
16
|
:port => 10050,
|
@@ -33,10 +40,6 @@ class ZabbixApi
|
|
33
40
|
result.empty? ? nil : result['hostids'][0].to_i
|
34
41
|
end
|
35
42
|
|
36
|
-
def add(data)
|
37
|
-
create(data)
|
38
|
-
end
|
39
|
-
|
40
43
|
def unlink_templates(data)
|
41
44
|
result = @client.api_request(
|
42
45
|
:method => "host.massRemove",
|
@@ -54,33 +57,12 @@ class ZabbixApi
|
|
54
57
|
end
|
55
58
|
|
56
59
|
def delete(data)
|
57
|
-
|
58
|
-
result.empty? ? nil : result['hostids'][0].to_i
|
60
|
+
delete_array_sym(data)
|
59
61
|
end
|
60
62
|
|
61
|
-
def destroy(data)
|
62
|
-
delete(data)
|
63
|
-
end
|
64
|
-
|
65
|
-
def update(data)
|
66
|
-
result = @client.api_request(:method => "host.update", :params => data)
|
67
|
-
result.empty? ? nil : result['hostids'][0].to_i
|
68
|
-
end
|
69
63
|
|
70
64
|
def get_full_data(data)
|
71
|
-
|
72
|
-
end
|
73
|
-
|
74
|
-
def create_or_update(data)
|
75
|
-
hostid = get_id(:host => data[:host])
|
76
|
-
hostid ? update(data.merge(:hostid => hostid)) : create(data)
|
77
|
-
end
|
78
|
-
|
79
|
-
def get_id(data)
|
80
|
-
result = get_full_data(data)
|
81
|
-
hostid = nil
|
82
|
-
result.each { |host| hostid = host['hostid'].to_i if host['host'] == data[:host] }
|
83
|
-
hostid
|
65
|
+
get_full_data_filter(data)
|
84
66
|
end
|
85
67
|
|
86
68
|
end
|