zabbixapi 0.3.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
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
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ language:
2
+ - ruby
3
+ rvm:
4
+ - 1.9.3
5
+ before_script:
6
+ - sudo apt-get install mysql-server zabbix-server-mysql zabbix-frontend-php
7
+ - mysql -u root -e "use zabbix; insert into users_groups(usrgrpid, userid) values(10,1);"
8
+ - echo "$(curl -fsSL https://raw.github.com/gist/8f0df0d1824ea25a3827/zabbix.php)" | sudo tee /usr/share/zabbix/conf/zabbix.conf.php
9
+ script: "bundle exec rspec spec/run.rb"
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source :rubygems
2
+
3
+ gem "rspec"
4
+ gem "rake"
data/Gemfile.lock ADDED
@@ -0,0 +1,20 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.3)
5
+ rake (0.9.2.2)
6
+ rspec (2.12.0)
7
+ rspec-core (~> 2.12.0)
8
+ rspec-expectations (~> 2.12.0)
9
+ rspec-mocks (~> 2.12.0)
10
+ rspec-core (2.12.0)
11
+ rspec-expectations (2.12.0)
12
+ diff-lcs (~> 1.1.3)
13
+ rspec-mocks (2.12.0)
14
+
15
+ PLATFORMS
16
+ ruby
17
+
18
+ DEPENDENCIES
19
+ rake
20
+ rspec
data/README.md ADDED
@@ -0,0 +1,123 @@
1
+ #Ruby Zabbix Api Module
2
+
3
+ Simple and lightweight ruby module for work with zabbix api
4
+
5
+ [![Build Status](https://travis-ci.org/vadv/zabbixapi.png)](https://travis-ci.org/vadv/zabbixapi)
6
+
7
+ #####Now worked with zabbix
8
+ * 1.8.2 (api version 1.2)
9
+ * 1.8.9 (api version 1.3)
10
+
11
+ ## Installation
12
+ ```
13
+ gem install zabbixapi
14
+ ```
15
+
16
+ ## Get Start
17
+
18
+ ### Connect
19
+ ```ruby
20
+ zbx = ZabbixApi.connect(
21
+ :url => 'http://localhost/zabbix/api_jsonrpc.php',
22
+ :user => 'Admin',
23
+ :password => 'zabbix'
24
+ )
25
+ ```
26
+ ### Create Hostgroup
27
+ ```ruby
28
+ zbx.hostgroups.create(:name => "hostgroup")
29
+ ```
30
+
31
+ ### Create Template
32
+ ```ruby
33
+ zbx.templates.create(
34
+ :host => "template",
35
+ :groups => [:groupid => zbx.hostgroups.get_id(:name => "hostgroup")]
36
+ )
37
+ ```
38
+
39
+ ### Create Application
40
+ ```ruby
41
+ zbx.applications.create(
42
+ :name => application,
43
+ :hostid => zbx.templates.get_id(:host => "template")
44
+ )
45
+ ```
46
+
47
+ ### Create Item
48
+ ```ruby
49
+ zbx.items.create(
50
+ :description => "item",
51
+ :key_ => "proc.num[aaa]",
52
+ :hostid => zbx.templates.get_id(:host => "template"),
53
+ :applications => [zbx.applications.get_id(:name => "application")]
54
+ )
55
+ ```
56
+
57
+ ### Create host
58
+ ```ruby
59
+ zbx.hosts.add(
60
+ :host => "hostname",
61
+ :groups => [ :groupid => zbx.hostgroups.get_id(:name => "hostgroup") ]
62
+ )
63
+ ```
64
+
65
+ ### Get all templates linked with host
66
+ ```ruby
67
+ zbx.templates.get_ids_by_host( :hostids => [zbx.hosts.get_id(:host => "hostname")] )
68
+ returned hash:
69
+ {
70
+ "Templatename" => "10",
71
+ "Templatename" => "1021"
72
+ }
73
+ ```
74
+
75
+ ### Link host with templates
76
+ ```ruby
77
+ zbx.hosts.unlink_templates(
78
+ :hosts_id => [zbx.hosts.get_id(:host => "hostname")],
79
+ :templates_id => [111, 214]
80
+ )
81
+ ```
82
+
83
+ ### Create trigger
84
+ ```ruby
85
+ zbx.triggers.create(
86
+ :description => "trigger",
87
+ :expression => "{template:proc.num[aaa].last(0)}<1",
88
+ :comments => "Bla-bla is faulty (disaster)",
89
+ :priority => 5,
90
+ :status => 0,
91
+ :templateid => 0,
92
+ :type => 0
93
+ )
94
+ ````
95
+
96
+ ### Create user
97
+ ```ruby
98
+ zbx.users.create(
99
+ :alias => "Test user",
100
+ :name => "username",
101
+ :surname => "usersername",
102
+ :passwd => "password"
103
+ )
104
+ ```
105
+
106
+ ### Custom queries
107
+ ```ruby
108
+ zbx.query(
109
+ :method => "apiinfo.version",
110
+ :params => {}
111
+ )
112
+ ```
113
+
114
+ ## Dependencies
115
+
116
+ * net/http
117
+ * net/https
118
+ * json
119
+
120
+ ## Zabbix documentation
121
+
122
+ * Zabbix Project Homepage -> http://zabbix.com/
123
+ * Zabbix Api docs -> http://www.zabbix.com/documentation/1.8/api
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,37 @@
1
+ class ZabbixApi
2
+ class Applications
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 => "application.create", :params => [data])
11
+ result.empty? ? nil : result['applicationids'][0].to_i
12
+ end
13
+
14
+ def add(data)
15
+ create(data)
16
+ end
17
+
18
+ def delete(data)
19
+ result = @client.api_request(:method => "application.delete", :params => [data])
20
+ result.empty? ? nil : result['applicationids'][0].to_i
21
+ end
22
+
23
+ def destroy(data)
24
+ delete(data)
25
+ end
26
+
27
+ def get_full_data(data)
28
+ @client.api_request(:method => "application.get", :params => {:filter => data, :output => "extend"})
29
+ end
30
+
31
+ def get_id(data)
32
+ result = get_full_data(data)
33
+ result.empty? ? nil : result[0]['applicationid'].to_i
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,65 @@
1
+ require 'json'
2
+ require 'net/http'
3
+
4
+ class ZabbixApi
5
+ class Client
6
+
7
+ def id
8
+ Random.rand(100000)
9
+ end
10
+
11
+ def api_version
12
+ @version ||= api_request(:method => "apiinfo.version", :params => {})
13
+ end
14
+
15
+ def auth
16
+ api_request(
17
+ :method => 'user.authenticate',
18
+ :params => {
19
+ :user => @options[:user],
20
+ :password => @options[:password],
21
+ }
22
+ )
23
+ end
24
+
25
+ def initialize(options = {})
26
+ @options = options
27
+ @auth_hash = auth
28
+ end
29
+
30
+ def message_json(body)
31
+ message = {
32
+ :method => body[:method],
33
+ :params => body[:params],
34
+ :auth => @auth_hash,
35
+ :id => id,
36
+ :jsonrpc => '2.0'
37
+ }
38
+ JSON.generate(message)
39
+ end
40
+
41
+ def http_request(body)
42
+ uri = URI.parse(@options[:url])
43
+ http = Net::HTTP.new(uri.host, uri.port)
44
+ request = Net::HTTP::Post.new(uri.request_uri)
45
+ request.add_field('Content-Type', 'application/json-rpc')
46
+ request.body = body
47
+ response = http.request(request)
48
+ raise "HTTP Error: #{response.code} on #{@options[:url]}" unless response.code == "200"
49
+ puts "[DEBUG] Get answer: #{response.body}" if @options[:debug]
50
+ response.body
51
+ end
52
+
53
+ def _request(body)
54
+ puts "[DEBUG] Send request: #{body}" if @options[:debug]
55
+ result = JSON.parse(http_request(body))
56
+ raise "Server answer API error: #{result['error'].inspect} on request: #{body}" if result['error']
57
+ result['result']
58
+ end
59
+
60
+ def api_request(body)
61
+ _request message_json(body)
62
+ end
63
+
64
+ end
65
+ end
@@ -0,0 +1,12 @@
1
+ class ZabbixApi
2
+
3
+ class BaseError < RuntimeError
4
+ end
5
+
6
+ class ApiError < BaseError
7
+ end
8
+
9
+ class HttpError < BaseError
10
+ end
11
+
12
+ end
@@ -0,0 +1,47 @@
1
+ class ZabbixApi
2
+ class Graphs
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 => "graph.create", :params => [data])
11
+ result.empty? ? nil : result['graphids'][0].to_i
12
+ end
13
+
14
+ def add(data)
15
+ create(data)
16
+ end
17
+
18
+ def delete(data)
19
+ result = @client.api_request(:method => "graph.delete", :params => [data])
20
+ case @client.api_version
21
+ when "1.3"
22
+ result ? 1 : 0 #return "true" or "false" for this api version
23
+ else
24
+ result.empty? ? nil : result['graphids'][0].to_i
25
+ end
26
+ end
27
+
28
+ def destroy(data)
29
+ delete(data)
30
+ end
31
+
32
+ def get_full_data(data)
33
+ @client.api_request(:method => "graph.get", :params => {:search => {:name => data}, :output => "extend"})
34
+ end
35
+
36
+ def get_id(data)
37
+ result = @client.api_request(:method => "graph.get", :params => {:filter => {:name=> data[:name]}, :output => "extend"})
38
+ result.empty? ? nil : result[0]['graphid'].to_i
39
+ end
40
+
41
+ def update(data)
42
+ result = @client.api_request(:method => "graph.update", :params => data)
43
+ result.empty? ? nil : result['graphids'][0].to_i
44
+ end
45
+
46
+ end
47
+ end
@@ -0,0 +1,42 @@
1
+ class ZabbixApi
2
+ class HostGroups
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 => "hostgroup.create", :params => [data])
11
+ result.empty? ? nil : result['groupids'][0].to_i
12
+ end
13
+
14
+ def add(data)
15
+ create(data)
16
+ end
17
+
18
+ def delete(data)
19
+ result = @client.api_request(:method => "hostgroup.delete", :params => [:groupid => data])
20
+ result.empty? ? nil : result['groupids'][0].to_i
21
+ end
22
+
23
+ def destroy(data)
24
+ delete(data)
25
+ end
26
+
27
+ def get_full_data(data)
28
+ case @client.api_version
29
+ when "1.2"
30
+ @client.api_request(:method => "hostgroup.get", :params => {:filter => data, :output => "extend"})
31
+ else
32
+ @client.api_request(:method => "hostgroup.get", :params => {:filter => data, :output => "extend"})
33
+ end
34
+ end
35
+
36
+ def get_id(data)
37
+ result = get_full_data(data)
38
+ result.empty? ? nil : result[0]['groupid'].to_i
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,80 @@
1
+ class ZabbixApi
2
+ class Hosts
3
+
4
+ def initialize(options = {})
5
+ @client = Client.new(options)
6
+ @options = options
7
+ @host_default_options = {
8
+ :host => nil,
9
+ :port => 10050,
10
+ :status => 1,
11
+ :useip => 1,
12
+ :dns => '',
13
+ :ip => '0.0.0.0',
14
+ :proxy_hostid => 0,
15
+ :groups => [],
16
+ :useipmi => 0,
17
+ :ipmi_ip => '',
18
+ :ipmi_port => 623,
19
+ :ipmi_authtype => 0,
20
+ :ipmi_privilege => 0,
21
+ :ipmi_username => '',
22
+ :ipmi_password => ''
23
+ }
24
+ end
25
+
26
+ def merge_params(params)
27
+ result = JSON.generate(@host_default_options).to_s + "," + JSON.generate(params).to_s
28
+ JSON.parse(result.gsub('},{', ','))
29
+ end
30
+
31
+ def create(data)
32
+ result = @client.api_request(:method => "host.create", :params => [merge_params(data)])
33
+ result.empty? ? nil : result['hostids'][0].to_i
34
+ end
35
+
36
+ def add(data)
37
+ create(data)
38
+ end
39
+
40
+ def unlink_templates(data)
41
+ result = @client.api_request(
42
+ :method => "host.massRemove",
43
+ :params => {
44
+ :hostids => data[:hosts_id],
45
+ :templates => data[:templates_id]
46
+ }
47
+ )
48
+ case @client.api_version
49
+ when "1.2"
50
+ result ? true : false
51
+ else
52
+ result.empty? ? false : true
53
+ end
54
+ end
55
+
56
+ def delete(data)
57
+ result = @client.api_request(:method => "host.delete", :params => [:hostid => data])
58
+ result.empty? ? nil : result['hostids'][0].to_i
59
+ end
60
+
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
+
70
+ def get_full_data(data)
71
+ @client.api_request(:method => "host.get", :params => {:filter => data, :output => "extend"})
72
+ end
73
+
74
+ def get_id(data)
75
+ result = get_full_data(data)
76
+ result.empty? ? nil : result[0]['hostid'].to_i
77
+ end
78
+
79
+ end
80
+ end
@@ -0,0 +1,81 @@
1
+ class ZabbixApi
2
+ class Items
3
+
4
+ def initialize(options = {})
5
+ @client = Client.new(options)
6
+ @options = options
7
+ @item_default_options = {
8
+ :description => nil,
9
+ :key_ => nil,
10
+ :hostid => nil,
11
+ :delay => 60,
12
+ :history => 60,
13
+ :status => 0,
14
+ :type => 7,
15
+ :snmp_community => '',
16
+ :snmp_oid => '',
17
+ :value_type => 3,
18
+ :data_type => 0,
19
+ :trapper_hosts => 'localhost',
20
+ :snmp_port => 161,
21
+ :units => '',
22
+ :multiplier => 0,
23
+ :delta => 0,
24
+ :snmpv3_securityname => '',
25
+ :snmpv3_securitylevel => 0,
26
+ :snmpv3_authpassphrase => '',
27
+ :snmpv3_privpassphrase => '',
28
+ :formula => 0,
29
+ :trends => 365,
30
+ :logtimefmt => '',
31
+ :valuemapid => 0,
32
+ :delay_flex => '',
33
+ :authtype => 0,
34
+ :username => '',
35
+ :password => '',
36
+ :publickey => '',
37
+ :privatekey => '',
38
+ :params => '',
39
+ :ipmi_sensor => ''
40
+ }
41
+ end
42
+
43
+ def merge_params(params)
44
+ result = JSON.generate(@item_default_options).to_s + "," + JSON.generate(params).to_s
45
+ JSON.parse(result.gsub('},{', ','))
46
+ end
47
+
48
+ def create(data)
49
+ result = @client.api_request(:method => "item.create", :params => [merge_params(data)] )
50
+ result.empty? ? nil : result['itemids'][0].to_i
51
+ end
52
+
53
+ def add(data)
54
+ create(data)
55
+ end
56
+
57
+ def get_full_data(data)
58
+ @client.api_request(:method => "item.get", :params => {:filter => data, :output => "extend"})
59
+ end
60
+
61
+ def get_id(data)
62
+ result = get_full_data(data)
63
+ result.empty? ? nil : result[0]['itemid'].to_i
64
+ end
65
+
66
+ def update(data)
67
+ result = @client.api_request(:method => "item.update", :params => data)
68
+ result.empty? ? nil : result['itemids'][0].to_i
69
+ end
70
+
71
+ def delete(data)
72
+ result = @client.api_request(:method => "item.delete", :params => [data])
73
+ result.empty? ? nil : result['itemids'][0].to_i
74
+ end
75
+
76
+ def destroy(data)
77
+ delete(data)
78
+ end
79
+
80
+ end
81
+ end
@@ -0,0 +1,12 @@
1
+ class ZabbixApi
2
+ class Server
3
+
4
+ attr :version
5
+
6
+ def initialize(options = {})
7
+ @client = Client.new(options)
8
+ @version = @client.api_request(:method => "apiinfo.version", :params => {})
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,78 @@
1
+ class ZabbixApi
2
+ class Templates
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 => "template.create", :params => [data])
11
+ result.empty? ? nil : result['templateids'][0].to_i
12
+ end
13
+
14
+ def add(data)
15
+ create(data)
16
+ end
17
+
18
+ def delete(data)
19
+ result = @client.api_request(:method => "template.delete", :params => [:templateid => data])
20
+ result.empty? ? nil : result['templateids'][0].to_i
21
+ end
22
+
23
+ def destroy(data)
24
+ delete(data)
25
+ end
26
+
27
+ def get_ids_by_host(data)
28
+ result = []
29
+ @client.api_request(:method => "template.get", :params => data).each do |tmpl|
30
+ result << tmpl['templateid']
31
+ end
32
+ result
33
+ end
34
+
35
+ def link_with_host(data)
36
+ result = @client.api_request(
37
+ :method => "template.massAdd",
38
+ :params => {
39
+ :hosts => data[:hosts_id].map { |t| {:hostid => t} },
40
+ :templates => data[:templates_id].map { |t| {:templateid => t} }
41
+ }
42
+ )
43
+ result.empty? ? false : true
44
+ end
45
+
46
+ def all
47
+ result = {}
48
+ case @client.api_version
49
+ when "1.2"
50
+ @client.api_request(:method => "template.get", :params => {:output => "extend"}).values.each do |tmpl|
51
+ result[tmpl['host']] = tmpl['hostid']
52
+ end
53
+ else
54
+ @client.api_request(:method => "template.get", :params => {:output => "extend"}).each do |tmpl|
55
+ result[tmpl['host']] = tmpl['hostid']
56
+ end
57
+ end
58
+ result
59
+ end
60
+
61
+ def get_full_data(data)
62
+ case @client.api_version
63
+ when "1.2"
64
+ # in this version returned id=>{...}
65
+ result = @client.api_request(:method => "template.get", :params => {:filter => data, :output => "extend"})
66
+ result.empty? ? [] : result.values
67
+ else
68
+ @client.api_request(:method => "template.get", :params => {:filter => data, :output => "extend"})
69
+ end
70
+ end
71
+
72
+ def get_id(data)
73
+ result = get_full_data(data)
74
+ result.empty? ? nil : result[0]['templateid'].to_i
75
+ end
76
+
77
+ end
78
+ end
@@ -0,0 +1,42 @@
1
+ class ZabbixApi
2
+ class Triggers
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 => "trigger.create", :params => [data])
11
+ result.empty? ? nil : result['triggerids'][0].to_i
12
+ end
13
+
14
+ def add(data)
15
+ create(data)
16
+ end
17
+
18
+ def delete(data)
19
+ result = @client.api_request(:method => "trigger.delete", :params => [data])
20
+ result.empty? ? nil : result['triggerids'][0].to_i
21
+ end
22
+
23
+ def destroy(data)
24
+ delete(data)
25
+ end
26
+
27
+ def update(data)
28
+ result = @client.api_request(:method => "trigger.update", :params => data)
29
+ result.empty? ? nil : result['triggerid'][0].to_i
30
+ end
31
+
32
+ def get_full_data(data)
33
+ @client.api_request(:method => "trigger.get", :params => {:filter => data, :output => "extend"})
34
+ end
35
+
36
+ def get_id(data)
37
+ result = get_full_data(data)
38
+ result.empty? ? nil : result[0]['triggerid'].to_i
39
+ end
40
+
41
+ end
42
+ end