zabbixapi 0.3.0 → 0.4.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.
Files changed (68) hide show
  1. data/.travis.yml +9 -0
  2. data/Gemfile +4 -0
  3. data/Gemfile.lock +20 -0
  4. data/README.md +123 -0
  5. data/Rakefile +1 -0
  6. data/lib/zabbixapi/applications.rb +37 -0
  7. data/lib/zabbixapi/client.rb +65 -0
  8. data/lib/zabbixapi/errors.rb +12 -0
  9. data/lib/zabbixapi/graphs.rb +47 -0
  10. data/lib/zabbixapi/hostgroups.rb +42 -0
  11. data/lib/zabbixapi/hosts.rb +80 -0
  12. data/lib/zabbixapi/items.rb +81 -0
  13. data/lib/zabbixapi/server.rb +12 -0
  14. data/lib/zabbixapi/templates.rb +78 -0
  15. data/lib/zabbixapi/triggers.rb +42 -0
  16. data/lib/zabbixapi/users.rb +59 -0
  17. data/lib/zabbixapi/version.rb +3 -0
  18. data/lib/zabbixapi.rb +51 -12
  19. data/spec/localhost.rb +227 -75
  20. data/spec/run.rb +265 -0
  21. data/zabbixapi.gemspec +15 -15
  22. metadata +35 -57
  23. data/README.rdoc +0 -65
  24. data/examples/basic_auth.rb +0 -30
  25. data/examples/config.yml +0 -4
  26. data/examples/maintenance-example.rb +0 -55
  27. data/examples/populate_new_zabbix.sh +0 -84
  28. data/examples/zabbix_availability +0 -73
  29. data/examples/zabbix_clear_default +0 -36
  30. data/examples/zabbix_cpufan-sersor +0 -114
  31. data/examples/zabbix_cputemp-sersor +0 -112
  32. data/examples/zabbix_disk_io +0 -152
  33. data/examples/zabbix_filesystem +0 -249
  34. data/examples/zabbix_group +0 -25
  35. data/examples/zabbix_hlsp +0 -100
  36. data/examples/zabbix_host +0 -37
  37. data/examples/zabbix_iops +0 -131
  38. data/examples/zabbix_ipmi-cpufan-sersor +0 -101
  39. data/examples/zabbix_ipmi_systemp +0 -110
  40. data/examples/zabbix_la +0 -136
  41. data/examples/zabbix_mailer +0 -125
  42. data/examples/zabbix_mdadm +0 -72
  43. data/examples/zabbix_megaraid +0 -71
  44. data/examples/zabbix_memory +0 -290
  45. data/examples/zabbix_named +0 -71
  46. data/examples/zabbix_net +0 -218
  47. data/examples/zabbix_nginx +0 -167
  48. data/examples/zabbix_nginx_500 +0 -112
  49. data/examples/zabbix_ntp +0 -71
  50. data/examples/zabbix_nv-gputemp +0 -111
  51. data/examples/zabbix_pgsql +0 -125
  52. data/examples/zabbix_server_process +0 -71
  53. data/examples/zabbix_smart_blade +0 -94
  54. data/examples/zabbix_ssh +0 -71
  55. data/examples/zabbix_varnish +0 -71
  56. data/lib/zabbixapi/application.rb +0 -40
  57. data/lib/zabbixapi/base.rb +0 -158
  58. data/lib/zabbixapi/graph.rb +0 -59
  59. data/lib/zabbixapi/group.rb +0 -66
  60. data/lib/zabbixapi/host.rb +0 -70
  61. data/lib/zabbixapi/item.rb +0 -130
  62. data/lib/zabbixapi/maintenance.rb +0 -78
  63. data/lib/zabbixapi/mediatype.rb +0 -53
  64. data/lib/zabbixapi/screen.rb +0 -119
  65. data/lib/zabbixapi/template.rb +0 -128
  66. data/lib/zabbixapi/trigger.rb +0 -71
  67. data/lib/zabbixapi/user.rb +0 -0
  68. data/lib/zabbixapi/usermacro.rb +0 -46
@@ -0,0 +1,59 @@
1
+ class ZabbixApi
2
+ class Users
3
+
4
+ def initialize(options = {})
5
+ @client = Client.new(options)
6
+ @options = options
7
+ end
8
+
9
+ def create(data)
10
+ result = @client.api_request(:method => "user.create", :params => data)
11
+ result ? result['userids'][0].to_i : nil
12
+ end
13
+
14
+ def add(data)
15
+ create(data)
16
+ end
17
+
18
+ def delete(data)
19
+ result = @client.api_request(:method => "user.delete", :params => [:userid => data])
20
+ result ? result['userids'][0].to_i : nil
21
+ end
22
+
23
+ def get_full_data(data)
24
+ @client.api_request(
25
+ :method => "user.get",
26
+ :params => {
27
+ :filter => {
28
+ :name => data[:name]
29
+ },
30
+ :output => "extend"
31
+ }
32
+ )
33
+ end
34
+
35
+ def get(data)
36
+ get_full_data(data)
37
+ end
38
+
39
+ def get_id(data)
40
+ result = get_full_data(data)
41
+ userid = -1
42
+ case @client.api_version
43
+ when "1.2"
44
+ result.each do |usr|
45
+ userid = usr['userid'] if usr['name'] == data[:name]
46
+ end
47
+ userid
48
+ else
49
+ result.empty? ? nil : result[0]['userid']
50
+ end
51
+ end
52
+
53
+ def update(data)
54
+ result = @client.api_request(:method => "user.update", :params => data)
55
+ result ? result['userids'][0].to_i : nil
56
+ end
57
+
58
+ end
59
+ end
@@ -0,0 +1,3 @@
1
+ class ZabbixApi
2
+ VERSION = "0.4.1"
3
+ end
data/lib/zabbixapi.rb CHANGED
@@ -1,12 +1,51 @@
1
- require 'zabbixapi/application'
2
- require 'zabbixapi/base'
3
- require 'zabbixapi/graph'
4
- require 'zabbixapi/group'
5
- require 'zabbixapi/host'
6
- require 'zabbixapi/item'
7
- require 'zabbixapi/screen'
8
- require 'zabbixapi/template'
9
- require 'zabbixapi/trigger'
10
- require 'zabbixapi/usermacro'
11
- require 'zabbixapi/mediatype'
12
- require 'zabbixapi/maintenance'
1
+ require "zabbixapi/version"
2
+ require "zabbixapi/client"
3
+ require "zabbixapi/server"
4
+ require "zabbixapi/applications"
5
+ require "zabbixapi/templates"
6
+ require "zabbixapi/hostgroups"
7
+ require "zabbixapi/users"
8
+ require "zabbixapi/hosts"
9
+ require "zabbixapi/triggers"
10
+ require "zabbixapi/items"
11
+ require "zabbixapi/graphs"
12
+
13
+ class ZabbixApi
14
+
15
+ attr :client
16
+ attr :server
17
+ attr :users
18
+ attr :items
19
+ attr :applications
20
+ attr :templates
21
+ attr :hostgroups
22
+ attr :hosts
23
+ attr :triggers
24
+ attr :graphs
25
+
26
+ def self.connect(options = {})
27
+ new(options)
28
+ end
29
+
30
+ def self.current
31
+ @current ||= ZabbixApi.new
32
+ end
33
+
34
+ def query(data)
35
+ @client.api_request(:method => data[:method], :params => data[:params])
36
+ end
37
+
38
+ def initialize(options = {})
39
+ @client = Client.new(options)
40
+ @server = Server.new(options)
41
+ @users = Users.new(options)
42
+ @items = Items.new(options)
43
+ @hosts = Hosts.new(options)
44
+ @applications = Applications.new(options)
45
+ @templates = Templates.new(options)
46
+ @hostgroups = HostGroups.new(options)
47
+ @triggers = Triggers.new(options)
48
+ @graphs = Graphs.new(options)
49
+ end
50
+
51
+ end
data/spec/localhost.rb CHANGED
@@ -1,112 +1,264 @@
1
1
  require 'zabbixapi'
2
- require 'json'
3
2
 
4
3
  # settings
5
- api_url = 'http://localhost/zabbix/api_jsonrpc.php'
4
+ api_url = 'http://localhost/api_jsonrpc.php'
6
5
  api_login = 'Admin'
7
6
  api_password = 'zabbix'
8
7
 
8
+ zbx = ZabbixApi.connect(
9
+ :url => api_url,
10
+ :user => api_login,
11
+ :password => api_password,
12
+ :debug => false
13
+ )
9
14
 
10
- zbx = Zabbix::ZabbixApi.new(api_url, api_login, api_password)
11
- #zbx.debug = true
15
+ hostgroup = "hostgroup______1"
16
+ template = "template______1"
17
+ application = "application_____1"
18
+ item = "item_____1"
19
+ host = "hostname____1"
20
+ trigger = "trigger____1"
21
+ user = "user____1"
22
+ user2 = "user____2"
23
+ graph = "graph___a"
12
24
 
13
- describe Zabbix::ZabbixApi, "test_api" do
25
+ describe ZabbixApi, "test_api" do
14
26
 
15
- # 01. Create group
16
- it "Create some group" do
17
- result = zbx.add_group('some_group')
18
- result.should be_kind_of(Integer)
27
+ it "SERVER: Get version api" do
28
+ zbx.server.version.should be_kind_of(String)
19
29
  end
20
30
 
21
- # 02. Get group_id
22
- it "Get group_id" do
23
- result = zbx.get_group_id('some_group')
24
- result.should be_kind_of(Integer)
31
+ it "HOSTGROUP: Create" do
32
+ zbx.hostgroups.create(:name => hostgroup).should be_kind_of(Integer)
25
33
  end
26
34
 
27
- # 03. Get unknown group_id
28
- it "Get unknown group" do
29
- result = zbx.get_group_id('___some_group')
30
- result.should be_nil
35
+ it "HOSTGROUP: Find" do
36
+ zbx.hostgroups.get_id(:name => hostgroup).should be_kind_of(Integer)
31
37
  end
32
38
 
33
- # 04. Create host
34
- host_options = {
35
- "ip" => '127.0.0.1',
36
- "dns" => 'my.example.com',
37
- "host" => 'my.example.com',
38
- "useip" => 1,
39
- "groups" => [1]
40
- }
41
- it "Create host" do
42
- result = zbx.add_host(host_options)
43
- result.should be_kind_of(Integer)
39
+ it "HOSTGROUP: Find unknown" do
40
+ zbx.hostgroups.get_id(:name => "#{hostgroup}______").should be_kind_of(NilClass)
44
41
  end
45
42
 
46
- # 05. Get host
47
- it "Get host by name" do
48
- result = zbx.get_host_id('my.example.com')
49
- result.should be_kind_of(Integer)
43
+ it "TEMPLATE: Create" do
44
+ zbx.templates.create(
45
+ :host => template,
46
+ :groups => [:groupid => zbx.hostgroups.get_id(:name => hostgroup)]
47
+ ).should be_kind_of(Integer)
50
48
  end
51
49
 
52
- # 06. Get unknown host
53
- it "Get unknown host by name" do
54
- result = zbx.get_host_id('___my.example.com')
55
- result.should be_nil
50
+ it "TEMPLATE: Check full data" do
51
+ zbx.templates.get_full_data(:host => template)[0]['host'].should be_kind_of(String)
56
52
  end
57
53
 
58
- # 07. Delete host
59
- it "Delete host" do
60
- result = zbx.delete_host('my.example.com')
61
- result.should be_kind_of(Integer)
54
+ it "TEMPLATE: Find" do
55
+ zbx.templates.get_id(:host => template).should be_kind_of(Integer)
62
56
  end
63
57
 
64
- # 08. Delete unknown host
65
- it "Delete unknown host" do
66
- result = zbx.delete_host('__my.example.com')
67
- result.should be_nil
58
+ it "TEMPLATE: Find unknown" do
59
+ zbx.templates.get_id(:host => "#{template}_____").should be_kind_of(NilClass)
68
60
  end
69
61
 
70
- # 09. Delete group
71
- it "Delete some group" do
72
- result = zbx.delete_group('some_group')
73
- result.should be_kind_of(Integer)
62
+ it "APPLICATION: Create" do
63
+ zbx.applications.create(
64
+ :name => application,
65
+ :hostid => zbx.templates.get_id(:host => template)
66
+ )
74
67
  end
75
68
 
76
- # 10. Delete unknown group
77
- it "Delete unknown group" do
78
- result = zbx.delete_group('___some_group')
79
- result.should be_nil
69
+ it "APPLICATION: Full info check" do
70
+ zbx.applications.get_full_data(:name => application)[0]['applicationid'].should be_kind_of(String)
80
71
  end
81
72
 
82
- # 11. Mediatype create
83
- mediatype_options = {
84
- 'type' => '0', #email
85
- 'description' => 'example_mediatype',
86
- 'smtp_server' => 'test.company.com',
87
- 'smtp_helo' => 'test.company.com',
88
- 'smtp_email' => 'test@test.company.com',
89
- }
90
- it "Create mediatype" do
91
- result = zbx.add_mediatype(mediatype_options)
92
- result.should be_kind_of(Integer)
73
+ it "APPLICATION: Find" do
74
+ zbx.applications.get_id(:name => application).should be_kind_of(Integer)
93
75
  end
94
- # 12. Mediatype unknown delete
95
- it "Delete unknown mediatype" do
96
- result = zbx.delete_mediatype('__example_mediatype')
97
- result.should be_nil
76
+
77
+ it "APPLICATION: Find unknown" do
78
+ zbx.applications.get_id(:name => "#{application}___").should be_kind_of(NilClass)
98
79
  end
99
80
 
100
- # 13. Mediatype delete
101
- it "Delete mediatype" do
102
- result = zbx.delete_mediatype('example_mediatype')
103
- result.should be_kind_of(Integer)
81
+ it "ITEM: Create" do
82
+ zbx.items.create(
83
+ :description => item,
84
+ :key_ => "proc.num[aaa]",
85
+ :hostid => zbx.templates.get_id(:host => template),
86
+ :applications => [zbx.applications.get_id(:name => application)]
87
+ )
104
88
  end
105
89
 
106
- end
90
+ it "ITEM: Full info check" do
91
+ zbx.items.get_full_data(:description => item)[0]['itemid'].should be_kind_of(String)
92
+ end
93
+
94
+ it "ITEM: Find" do
95
+ zbx.items.get_id(:description => item).should be_kind_of(Integer)
96
+ end
97
+
98
+ it "ITEM: Update" do
99
+ zbx.items.update(
100
+ :itemid => zbx.items.get_id(:description => item),
101
+ :status => 0
102
+ ).should be_kind_of(Integer)
103
+ end
104
+
105
+ it "ITEM: Get unknown" do
106
+ zbx.items.get_id(:description => "#{item}_____")
107
+ end
108
+
109
+ it "HOST: Create" do
110
+ zbx.hosts.create(
111
+ :host => host,
112
+ :ip => "10.20.48.88",
113
+ :groups => [:groupid => zbx.hostgroups.get_id(:name => hostgroup)]
114
+ ).should be_kind_of(Integer)
115
+ end
116
+
117
+ it "HOST: Find unknown" do
118
+ zbx.hosts.get_id(:host => "#{host}___").should be_kind_of(NilClass)
119
+ end
120
+
121
+ it "HOST: Find" do
122
+ zbx.hosts.get_id(:host => host).should be_kind_of(Integer)
123
+ end
107
124
 
108
- describe Zabbix::ZabbixApi, "test_examples" do
109
- it "Test all examples" do
110
- system("examples/populate_new_zabbix.sh development")
125
+ it "HOST: Update" do
126
+ zbx.hosts.update(
127
+ :hostid => zbx.hosts.get_id(:host => host),
128
+ :status => 0
129
+ )
111
130
  end
131
+
132
+ it "TEMPLATE: Get all templates linked with host" do
133
+ zbx.templates.get_ids_by_host(
134
+ :hostids => [zbx.hosts.get_id(:host => host)]
135
+ ).should be_kind_of(Array)
136
+ end
137
+
138
+ it "HOSTS: Linked host with templates" do
139
+ zbx.hosts.unlink_templates(
140
+ :hosts_id => [zbx.hosts.get_id(:host => host)],
141
+ :templates_id => [zbx.templates.get_id(:host => template)]
142
+ ).should be_kind_of(TrueClass)
143
+ end
144
+
145
+ it "TEMPLATE: Unlink host from templates" do
146
+
147
+ end
148
+
149
+ it "TEMPLATE: Get all" do
150
+ zbx.templates.all.should be_kind_of(Hash)
151
+ end
152
+
153
+ it "TRIGGER: Create" do
154
+ zbx.triggers.create(
155
+ :description => trigger,
156
+ :expression => "{#{template}:proc.num[aaa].last(0)}<1",
157
+ :comments => "Bla-bla is faulty (disaster)",
158
+ :priority => 5,
159
+ :status => 0,
160
+ :templateid => 0,
161
+ :type => 0
162
+ ).should be_kind_of(Integer)
163
+ end
164
+
165
+ it "TRIGGER: Find" do
166
+ zbx.triggers.get_id(:description => trigger).should be_kind_of(Integer)
167
+ end
168
+
169
+ it "GRAPH: Create" do
170
+ gitems = {
171
+ :itemid => zbx.items.get_id(:description => item),
172
+ :calc_fnc => "2",
173
+ :type => "0",
174
+ :periods_cnt => "5"
175
+ }
176
+ zbx.graphs.create(
177
+ :gitems => [gitems],
178
+ :show_triggers => "0",
179
+ :name => graph,
180
+ :width => "900",
181
+ :height => "200"
182
+ ).should be_kind_of(Integer)
183
+ #
184
+ end
185
+
186
+ it "GRAPH: Find" do
187
+ zbx.graphs.get_id( :name => graph ).should be_kind_of(Integer)
188
+ end
189
+
190
+ it "GRAPH: Update" do
191
+ zbx.graphs.update(
192
+ :graphid => zbx.graphs.get_id(
193
+ :name => graph,
194
+ :hostid => zbx.hosts.get_id(:host => host)
195
+ ),
196
+ :ymax_type => 1
197
+ ).should be_kind_of(Integer)
198
+ end
199
+
200
+ it "GRAPH: Delete" do
201
+ zbx.graphs.delete(zbx.graphs.get_id(:name => graph)).should be_kind_of(Integer)
202
+ end
203
+
204
+ it "TRIGGER: Delete" do
205
+ zbx.triggers.delete( zbx.triggers.get_id(:description => trigger) ).should be_kind_of(Integer)
206
+ end
207
+
208
+ it "HOST: Delete" do
209
+ zbx.hosts.delete( zbx.hosts.get_id(:host => host) ).should be_kind_of(Integer)
210
+ end
211
+
212
+ it "ITEM: Delete" do
213
+ zbx.items.delete(
214
+ zbx.items.get_id(:description => item)
215
+ ).should be_kind_of(Integer)
216
+ end
217
+
218
+ it "APPLICATION: Delete" do
219
+ zbx.applications.delete( zbx.applications.get_id(:name => application) )
220
+ end
221
+
222
+ it "TEMPLATE: Delete" do
223
+ zbx.templates.delete(zbx.templates.get_id(:host => template))
224
+ end
225
+
226
+ it "HOSTGROUP: Delete" do
227
+ zbx.hostgroups.delete(
228
+ zbx.hostgroups.get_id(:name => hostgroup)
229
+ ).should be_kind_of(Integer)
230
+ end
231
+
232
+ it "USER: Create" do
233
+ zbx.users.create(
234
+ :alias => "Test #{user}",
235
+ :name => user,
236
+ :surname => user,
237
+ :passwd => user
238
+ ).should be_kind_of(Integer)
239
+ end
240
+
241
+ it "USER: Find" do
242
+ zbx.users.get_full_data(:name => user)[0]['name'].should be_kind_of(String)
243
+ end
244
+
245
+ it "USER: Update" do
246
+ zbx.users.update(:userid => zbx.users.get_id(:name => user), :name => user2).should be_kind_of(Integer)
247
+ end
248
+
249
+ it "USER: Find unknown" do
250
+ zbx.users.get_id(:name => "#{user}_____")
251
+ end
252
+
253
+ it "USER: Delete" do
254
+ zbx.users.delete(zbx.users.get_id(:name => user2)).should be_kind_of(Integer)
255
+ end
256
+
257
+ it "QUERY" do
258
+ zbx.query(
259
+ :method => "apiinfo.version",
260
+ :params => {}
261
+ ).should be_kind_of(String)
262
+ end
263
+
112
264
  end