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
@@ -1,94 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'rubygems'
4
- require 'yaml'
5
- require 'getopt/std'
6
- require 'zabbixapi'
7
-
8
- opt = Getopt::Std.getopts("g:E:n:t:")
9
-
10
- group_name = opt["g"]
11
- zabbix_env = opt["E"]
12
- disk_name = opt["n"]
13
- parameter_name = opt["t"]
14
-
15
- template_name = "TMPL_#{parameter_name}_#{disk_name}"
16
-
17
- # read config
18
- config = YAML::load(open('./config.yml'))
19
-
20
- api_url = config[zabbix_env]["api_url"]
21
- api_login = config[zabbix_env]["api_login"]
22
- api_password = config[zabbix_env]["api_password"]
23
-
24
- # Esablish new connection
25
- zbx = Zabbix::ZabbixApi.new(api_url, api_login, api_password)
26
-
27
- # Create new template
28
- p " * Creating template #{template_name}."
29
- g_id = zbx.add_or_get_group(group_name)
30
-
31
- options = {
32
- 'groups' => [ g_id.to_i ],
33
- 'host' => template_name
34
- }
35
-
36
- t_id = zbx.add_or_get_template(options)
37
-
38
- # Create application #{app_name}
39
- app_name = "#{parameter_name}_#{disk_name}"
40
- p " ** Create application #{app_name}."
41
- application = {
42
- 'hostid' => t_id.to_i,
43
- 'name' => app_name
44
- }
45
- a_id = zbx.add_or_get_application(t_id, application)
46
-
47
- # '#{parameter_name} for #{disk_name}'
48
- options = {
49
- 'description' => "#{parameter_name} for #{disk_name}",
50
- 'key_' => "smart.state[#{disk_name},#{parameter_name}]",
51
- 'hostid' => t_id.to_i,
52
- 'applications' => [ a_id.to_i ],
53
- 'history' => 7,
54
- 'trends' => 30,
55
- 'delay' => 60,
56
- 'value_type' => 0
57
- }
58
- p " ** Add '#{parameter_name} for #{disk_name}' to #{template_name}."
59
- param_id = zbx.add_or_get_item(t_id, options)
60
-
61
- # TRIGGERS
62
- options = {
63
- 'description' => "Change of #{parameter_name} on #{disk_name} is too big",
64
- 'expression' => "{#{template_name}:smart.state[#{disk_name},#{parameter_name}].abschange(0)}>10",
65
- 'priority' => 2, # warning
66
- 'templateid' => 0,
67
- 'comments' => "too many reads on #{disk_name} (warning)",
68
- 'type' => 0,
69
- 'status' => '0'
70
- }
71
- p " ** Add '#{parameter_name} on #{disk_name}' trigger'"
72
- tr_id = zbx.add_or_get_trigger(t_id, options)
73
-
74
- # GRAPHS
75
- options = {
76
- 'gitems' => [
77
- "itemid" => param_id,
78
- "drawtype" => "0",
79
- "sortorder" => "0",
80
- "color" => "AA0000",
81
- "yaxisside" => "0",
82
- "calc_fnc" => "2",
83
- "type" => "0",
84
- "periods_cnt" => "5"
85
- ],
86
- "show_triggers" => "1",
87
- "name" => "#{parameter_name} graph",
88
- "width" => "900",
89
- "height" => "200",
90
- "templateid" => "0"
91
- }
92
-
93
- p " ** Add '#{parameter_name} graph'"
94
- g_id = zbx.add_or_get_graph(t_id, options)
data/examples/zabbix_ssh DELETED
@@ -1,71 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'rubygems'
4
- require 'getopt/std'
5
- require 'yaml'
6
- require 'zabbixapi'
7
-
8
- opt = Getopt::Std.getopts("g:i:E:")
9
-
10
- group_name = opt["g"]
11
- zabbix_env = opt["E"]
12
-
13
- template_name = "TMPL_ssh"
14
- app_name = "SSH"
15
-
16
- # read config
17
- config = YAML::load(open('./config.yml'))
18
-
19
- api_url = config[zabbix_env]["api_url"]
20
- api_login = config[zabbix_env]["api_login"]
21
- api_password = config[zabbix_env]["api_password"]
22
-
23
- # Esablish new connection
24
- zbx = Zabbix::ZabbixApi.new(api_url, api_login, api_password)
25
-
26
- # Create new template
27
- p " * Creating template #{template_name}."
28
- g_id = zbx.add_or_get_group(group_name)
29
-
30
- options = {
31
- 'groups' => [ g_id.to_i ],
32
- 'host' => template_name
33
- }
34
-
35
- t_id = zbx.add_or_get_template(options)
36
-
37
- # Create application #{app_name}
38
- p " ** Create application #{app_name}."
39
- application = {
40
- 'hostid' => t_id.to_i,
41
- 'name' => app_name
42
- }
43
- a_id = zbx.add_or_get_application(t_id, application)
44
-
45
- # 'sshd process exists'
46
- options = {
47
- 'description' => "sshd process exists",
48
- 'key_' => "proc.num[sshd]",
49
- 'hostid' => t_id.to_i,
50
- 'applications' => [ a_id.to_i ],
51
- 'history' => 7,
52
- 'trends' => 30,
53
- 'delay' => 120,
54
- 'value_type' => 0
55
- }
56
- p " ** Add 'sshd process exists' to #{template_name}."
57
- item_id = zbx.add_or_get_item(t_id, options)
58
-
59
- # TRIGGERS
60
- options = {
61
- 'description' => "sshd process exists",
62
- 'expression' => "{#{template_name}:proc.num[sshd].last(0)}<1",
63
- 'priority' => 5, # disaster
64
- 'templateid' => 0,
65
- 'comments' => "sshd process exists (disaster)",
66
- 'type' => 0,
67
- 'status' => '0'
68
- }
69
-
70
- p " ** Add 'sshd process exists' trigger"
71
- tr_id = zbx.add_or_get_trigger(t_id, options)
@@ -1,71 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'rubygems'
4
- require 'getopt/std'
5
- require 'yaml'
6
- require 'zabbixapi'
7
-
8
- opt = Getopt::Std.getopts("g:E:")
9
-
10
- group_name = opt["g"]
11
- zabbix_env = opt["E"]
12
-
13
- template_name = "TMPL_varnish"
14
- app_name = "Varnish"
15
-
16
- # read config
17
- config = YAML::load(open('./config.yml'))
18
-
19
- api_url = config[zabbix_env]["api_url"]
20
- api_login = config[zabbix_env]["api_login"]
21
- api_password = config[zabbix_env]["api_password"]
22
-
23
- # Esablish new connection
24
- zbx = Zabbix::ZabbixApi.new(api_url, api_login, api_password)
25
-
26
- # Create new template
27
- p " * Creating template #{template_name}."
28
- g_id = zbx.add_or_get_group(group_name)
29
-
30
- options = {
31
- 'groups' => [ g_id.to_i ],
32
- 'host' => template_name
33
- }
34
-
35
- t_id = zbx.add_or_get_template(options)
36
-
37
- # Create application #{app_name}
38
- p " ** Create application #{app_name}."
39
- application = {
40
- 'hostid' => t_id.to_i,
41
- 'name' => app_name
42
- }
43
- a_id = zbx.add_or_get_application(t_id, application)
44
-
45
- # 'varnish service status'
46
- options = {
47
- 'description' => "varnish service status",
48
- 'key_' => "runit_service[varnish]",
49
- 'hostid' => t_id.to_i,
50
- 'applications' => [ a_id.to_i ],
51
- 'history' => 7,
52
- 'trends' => 30,
53
- 'delay' => 60,
54
- 'value_type' => 0
55
- }
56
- p " ** Add 'varnish service status' to #{template_name}."
57
- item_id = zbx.add_or_get_item(t_id, options)
58
-
59
- # TRIGGERS
60
- options = {
61
- 'description' => "varnish service status",
62
- 'expression' => "{#{template_name}:runit_service[varnish].last(0)}#0",
63
- 'priority' => 5, # disaster
64
- 'templateid' => 0,
65
- 'comments' => "varnish service status (disaster)",
66
- 'type' => 0,
67
- 'status' => '0'
68
- }
69
-
70
- p " ** Add 'varnish service status' trigger"
71
- tr_id = zbx.add_or_get_trigger(t_id, options)
@@ -1,40 +0,0 @@
1
- module Zabbix
2
- class ZabbixApi
3
-
4
- def add_application(app_options)
5
- app_options_default = {
6
- 'hostid' => nil,
7
- 'name' => nil
8
- }
9
- application = merge_opt(app_options_default, app_options)
10
- message = {
11
- 'method' => 'application.create',
12
- 'params' => application
13
- }
14
- response = send_request(message)
15
- response.empty? ? nil : response['applicationids'][0].to_i
16
- end
17
-
18
- def add_or_get_application(host_id, app_options)
19
- unless a_id = get_app_id(host_id, app_options['name'])
20
- a_id = add_application(app_options)
21
- end
22
- return a_id
23
- end
24
-
25
- def get_app_id(host_id, app_name)
26
- message = {
27
- 'method' => 'application.get',
28
- 'params' => {
29
- 'filter' => {
30
- 'name' => app_name,
31
- 'hostid' => host_id
32
- }
33
- }
34
- }
35
- responce = send_request(message)
36
- responce.empty? ? nil : responce[0]['applicationid']
37
- end
38
-
39
- end
40
- end
@@ -1,158 +0,0 @@
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 ArgumentError < RuntimeError
22
- end
23
-
24
- class ZabbixApi
25
-
26
- attr_accessor :debug, :basic_auth
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
-
33
- @debug = false # Disable debug by default
34
- @basic_auth = false #Disable basic_auth by default
35
-
36
- @proxy_uri = nil
37
-
38
- unless ENV['http_proxy'].nil?
39
- @proxy_uri = URI.parse(ENV['http_proxy'])
40
- @proxy_host = @proxy_uri.host
41
- @proxy_port = @proxy_uri.port
42
- @proxy_user, @proxy_pass = @proxy_uri.userinfo.split(/:/) if @proxy_uri.userinfo
43
- end
44
- end
45
-
46
- def do_request(message)
47
-
48
- id = rand 100_000
49
-
50
- message['id'] = id
51
- message['jsonrpc'] = '2.0'
52
-
53
- message_json = JSON.generate(message)
54
-
55
- uri = URI.parse(@api_url)
56
-
57
- if @proxy_uri.nil?
58
- http = Net::HTTP.new(uri.host, uri.port)
59
- else
60
- http = Net::HTTP.Proxy(@proxy_host, @proxy_port, @proxy_user, @proxy_pass).new(uri.host, uri.port)
61
- end
62
-
63
- if uri.scheme == "https"
64
- http.use_ssl = true
65
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
66
- end
67
-
68
- request = Net::HTTP::Post.new(uri.request_uri)
69
- request.add_field('Content-Type', 'application/json-rpc')
70
- request.body=(message_json)
71
-
72
- if @basic_auth
73
- request.basic_auth @api_user, @api_password
74
- end
75
-
76
- begin
77
- puts "[ZBXAPI] : #{Time.now()} : INFO : Do request. Body => #{request.body}" if @debug
78
- response = http.request(request)
79
- rescue ::SocketError => e
80
- puts "[ZBXAPI] : #{Time.now()} : ERROR : SocketError => #{e.message}" if @debug
81
- raise Zabbix::SocketError.new(e.message)
82
- end
83
-
84
- if @debug
85
- puts "[ZBXAPI] : #{Time.now()} : INFO : Response start"
86
- puts "[ZBXAPI] : #{Time.now()} : INFO : Response body: #{response.inspect}"
87
- puts "[ZBXAPI] : #{Time.now()} : INFO : Response end"
88
- end
89
-
90
- if response.code != "200"
91
- raise Zabbix::ResponseCodeError.new("Responce code from [" + @api_url + "] is #{response.code}")
92
- end
93
-
94
- response_body_hash = JSON.parse(response.body)
95
-
96
- if @debug
97
- puts "[ZBXAPI] : #{Time.now()} : INFO : Response body"
98
- puts response_body_hash.inspect
99
- end
100
-
101
- if error = response_body_hash['error']
102
- error_message = error['message']
103
- error_data = error['data']
104
- error_code = error['code']
105
-
106
- e_message = "Code: [" + error_code.to_s + "]. Message: [" + error_message +\
107
- "]. Data: [" + error_data + "]."
108
-
109
- case error_code.to_s
110
- when '-32602'
111
- raise Zabbix::AlreadyExist.new(e_message)
112
- when '-32500'
113
- raise Zabbix::ArgumentError.new(e_message)
114
- else
115
- raise Zabbix::ResponseError.new(e_message)
116
- end
117
- end
118
-
119
- if response_body_hash['result'].class == TrueClass || response_body_hash['result'].class == FalseClass
120
- return ""
121
- else
122
- return response_body_hash['result']
123
- end
124
-
125
- end
126
-
127
- def send_request(message)
128
- message['auth'] = auth()
129
- do_request(message)
130
- end
131
-
132
- def auth()
133
- auth_message = {
134
- 'auth' => nil,
135
- 'method' => 'user.authenticate',
136
- 'params' => {
137
- 'user' => @api_user,
138
- 'password' => @api_password,
139
- '0' => '0'
140
- }
141
- }
142
- do_request(auth_message)
143
- end
144
-
145
- # Utils.
146
-
147
- def merge_opt(a, b)
148
- c = {}
149
- b.each_pair do |key, value|
150
- if a.has_key?(key) then
151
- c[key] = value
152
- end
153
- end
154
- return a.merge(c)
155
- end
156
-
157
- end
158
- end
@@ -1,59 +0,0 @@
1
- module Zabbix
2
- class ZabbixApi
3
-
4
- def add_graph(graph)
5
- message = {
6
- 'method' => 'graph.create',
7
- 'params' => graph
8
- }
9
- response = send_request(message)
10
- response.empty? ? nil : response['graphids'][0]
11
- end
12
-
13
- def get_graph_id(host_id, graph_name)
14
- message = {
15
- 'method' => 'graph.get',
16
- 'params' => {
17
- 'filter' => {
18
- 'name' => graph_name,
19
- 'hostid' => host_id
20
- }
21
- }
22
- }
23
- response = send_request(message)
24
- response.empty? ? nil : response[0]['graphid']
25
- end
26
-
27
- def add_or_get_graph(host_id, graph)
28
- unless g_id = get_graph_id(host_id, graph['name'])
29
- g_id = add_graph(graph)
30
- end
31
- return g_id
32
- end
33
-
34
- def get_graphs(host_id)
35
- message = {
36
- 'method' => 'graph.get',
37
- 'params' => {
38
- 'extendoutput' => '1',
39
- 'filter' => {
40
- 'hostid' => host_id
41
- }
42
- }
43
- }
44
- response = send_request(message)
45
- if response.empty?
46
- result = nil
47
- else
48
- result = {}
49
- response.each() do |graph|
50
- graph_id = graph['graphid']
51
- graph_name = graph['name']
52
- result[graph_id] = graph_name
53
- end
54
- end
55
- return result
56
- end
57
-
58
- end
59
- end
@@ -1,66 +0,0 @@
1
- module Zabbix
2
- class ZabbixApi
3
-
4
- def get_group_id(pattern)
5
- message = {
6
- 'method' => 'hostgroup.get',
7
- 'params' => {
8
- 'filter' => {
9
- 'name' => pattern
10
- }
11
- }
12
- }
13
- response = send_request(message)
14
- response.empty? ? nil : response[0]['groupid'].to_i
15
- end
16
-
17
- def group_exist?(pattern)
18
- group_id = get_group_id(pattern)
19
- group_id ? true : false
20
- end
21
-
22
- def add_group(groupname)
23
- message = {
24
- 'method' => 'hostgroup.create',
25
- 'params' => {
26
- 'name' => groupname
27
- }
28
- }
29
- response = send_request(message)
30
- response ? response['groupids'][0].to_i : nil
31
- end
32
-
33
- def add_or_get_group(groupname)
34
- unless g_id = get_group_id([groupname])
35
- g_id = add_group(groupname)
36
- end
37
- return g_id
38
- end
39
-
40
- def delete_group(groupname)
41
- if group_id = get_group_id(groupname)
42
- message = {
43
- 'method' => 'hostgroup.delete',
44
- 'params' => {
45
- 'groupid' => group_id
46
- }
47
- }
48
- response = send_request(message)
49
- response ? response['groupids'][0].to_i : nil
50
- end
51
- end
52
-
53
- def add_host_to_group(host_id, group_id)
54
- message = {
55
- 'method' => 'hostgroup.massAdd',
56
- 'params' => {
57
- 'groups' => [group_id],
58
- 'hosts' => [host_id]
59
- }
60
- }
61
- response = send_request(message)
62
- response ? response[0]['groupid'].to_i : nil
63
- end
64
-
65
- end
66
- end
@@ -1,70 +0,0 @@
1
- module Zabbix
2
- class ZabbixApi
3
-
4
- def update_host(host_id, host_options)
5
- host = host_options
6
- host['hostid'] = host_id
7
- message = {
8
- 'method' => 'host.update',
9
- 'params' => host
10
- }
11
- response = send_request(message)
12
- response.empty? ? nil : response['hostids'][0].to_i
13
- end
14
-
15
- def add_host(host_options)
16
- host_default = {
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' => ''
32
- }
33
- host_options['groups'].nil? || host_options['groups'].map! { |group_id| {'groupid' => group_id} }
34
- host = merge_opt(host_default, host_options)
35
- message = {
36
- 'method' => 'host.create',
37
- 'params' => host
38
- }
39
- response = send_request(message)
40
- response.empty? ? nil : response['hostids'][0].to_i
41
- end
42
-
43
- def get_host_id(hostname)
44
- message = {
45
- 'method' => 'host.get',
46
- 'params' => {
47
- 'filter' => {
48
- 'host' => hostname
49
- }
50
- }
51
- }
52
- response = send_request(message)
53
- response.empty? ? nil : response[0]['hostid'].to_i
54
- end
55
-
56
- def delete_host(hostname)
57
- if host_id = get_host_id(hostname)
58
- message = {
59
- 'method' => 'host.delete',
60
- 'params' => {
61
- 'hostid' => host_id
62
- }
63
- }
64
- response = send_request(message)
65
- response.empty? ? nil : response['hostids'][0].to_i
66
- end
67
- end
68
-
69
- end
70
- end