zabbixapi 0.1.4 → 0.1.5a1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,276 @@
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:")
9
+
10
+ group_name = opt["g"]
11
+ zabbix_env = opt["E"]
12
+
13
+ template_name = "TMPL_Memory"
14
+
15
+ # read config
16
+ config = YAML::load(open('./config.yml'))
17
+
18
+ api_url = config[zabbix_env]["api_url"]
19
+ api_login = config[zabbix_env]["api_login"]
20
+ api_password = config[zabbix_env]["api_password"]
21
+
22
+ # Esablish new connection
23
+ zbx = Zabbix::ZabbixApi.new(api_url, api_login, api_password)
24
+
25
+ # Create new template
26
+ p " * Creating template #{template_name}."
27
+ g_id = zbx.get_group_id(group_name)
28
+
29
+ options = {
30
+ 'groups' => [ g_id.to_i ],
31
+ 'host' => template_name
32
+ }
33
+
34
+ t_id = zbx.add_template(options)
35
+
36
+ # Create application #{app_name}
37
+ app_name = "Memory"
38
+ p " ** Create application #{app_name}."
39
+ application = {
40
+ 'hostid' => t_id.to_i,
41
+ 'name' => app_name
42
+ }
43
+ a_id = zbx.add_application(application)
44
+
45
+ # 'Buffers memory'
46
+ options = {
47
+ 'description' => "Buffers memory",
48
+ 'key_' => "vm.memory.size[buffers]",
49
+ 'hostid' => t_id.to_i,
50
+ 'applications' => [ a_id.to_i ],
51
+ 'units' => 'B',
52
+ 'history' => 7,
53
+ 'trends' => 30,
54
+ 'value_type' => 0
55
+ }
56
+ p " ** Add 'Buffers memory' to #{template_name}."
57
+
58
+ buffers_in_b_item_id = zbx.add_item(options)
59
+
60
+ # 'Cached memory'
61
+ options = {
62
+ 'description' => "Cached memory",
63
+ 'key_' => "vm.memory.size[cached]",
64
+ 'hostid' => t_id.to_i,
65
+ 'applications' => [ a_id.to_i ],
66
+ 'units' => 'B',
67
+ 'history' => 7,
68
+ 'trends' => 30,
69
+ 'value_type' => 0
70
+ }
71
+ p " ** Add 'Cached memory' to #{template_name}."
72
+
73
+ cached_in_b_item_id = zbx.add_item(options)
74
+
75
+ # 'Total memory'
76
+ options = {
77
+ 'description' => "Total memory",
78
+ 'key_' => "vm.memory.size[total]",
79
+ 'hostid' => t_id.to_i,
80
+ 'applications' => [ a_id.to_i ],
81
+ 'delay' => '300',
82
+ 'units' => 'B',
83
+ 'history' => 7,
84
+ 'trends' => 30,
85
+ 'value_type' => 0
86
+ }
87
+ p " ** Add 'Total memory' to #{template_name}."
88
+
89
+ total_in_b_item_id = zbx.add_item(options)
90
+
91
+ # 'Used memory with buffers and cache'
92
+ options = {
93
+ 'description' => "Used memory",
94
+ 'key_' => "vm.memory.size[used]",
95
+ 'hostid' => t_id.to_i,
96
+ 'applications' => [ a_id.to_i ],
97
+ 'delay' => '300',
98
+ 'units' => 'B',
99
+ 'history' => 7,
100
+ 'trends' => 30,
101
+ 'value_type' => 0
102
+ }
103
+ p " ** Add 'Used memory' to #{template_name}."
104
+
105
+ used_in_b_item_id = zbx.add_item(options)
106
+
107
+ # 'Free memory'
108
+ options = {
109
+ 'description' => "Free memory",
110
+ 'key_' => "vm.memory.size[free]",
111
+ 'hostid' => t_id.to_i,
112
+ 'applications' => [ a_id.to_i ],
113
+ 'units' => 'B',
114
+ 'history' => 7,
115
+ 'trends' => 30,
116
+ 'value_type' => 0
117
+ }
118
+ p " ** Add 'Free memory' to #{template_name}."
119
+
120
+ i_id = zbx.add_item(options)
121
+
122
+ # 'Free mmeory with buffers and cache'
123
+ options = {
124
+ 'description' => "Free memory with buffers and cache",
125
+ 'key_' => "vm.memory.size[free_with_buffer_cache]",
126
+ 'params' => "last(\"vm.memory.size[free]\") + last(\"vm.memory.size[cached]\") + last(\"vm.memory.size[buffers]\")",
127
+ 'hostid' => t_id.to_i,
128
+ 'type' => '15', #calculated
129
+ 'applications' => [ a_id.to_i ],
130
+ 'units' => 'B',
131
+ 'history' => 7,
132
+ 'trends' => 30,
133
+ 'value_type' => 0
134
+ }
135
+ p " ** Add 'Free memory with buffers and cache' to #{template_name}."
136
+
137
+ i_id = zbx.add_item(options)
138
+
139
+ # 'Swap in all'
140
+ options = {
141
+ 'description' => "Swap in all",
142
+ 'key_' => "system.swap.in[]",
143
+ 'hostid' => t_id.to_i,
144
+ 'applications' => [ a_id.to_i ],
145
+ 'multiplier' => 1,
146
+ 'formula' => 512,
147
+ 'units' => 'B',
148
+ 'history' => 7,
149
+ 'trends' => 30,
150
+ 'value_type' => 0
151
+ }
152
+ p " ** Add 'Swap in all' to #{template_name}."
153
+
154
+ i_id = zbx.add_item(options)
155
+
156
+ # 'Swap out all'
157
+ options = {
158
+ 'description' => "Swap out all",
159
+ 'key_' => "system.swap.out[]",
160
+ 'hostid' => t_id.to_i,
161
+ 'applications' => [ a_id.to_i ],
162
+ 'multiplier' => 1,
163
+ 'formula' => 512,
164
+ 'units' => 'B',
165
+ 'history' => 7,
166
+ 'trends' => 30,
167
+ 'value_type' => 0
168
+ }
169
+ p " ** Add 'Swap out all' to #{template_name}."
170
+
171
+ i_id = zbx.add_item(options)
172
+
173
+ # TRIGGERS
174
+ options = {
175
+ 'description' => "Free memory without cache, %",
176
+ 'expression' => "{#{template_name}:vm.memory.size[free].last(0)}<15",
177
+ 'priority' => 2, # warning
178
+ 'templateid' => 0,
179
+ 'comments' => "Free memory without cache, % warning trigger",
180
+ 'type' => 0,
181
+ 'status' => '0'
182
+ }
183
+
184
+ p " ** Add 'Free memory without cache, %'"
185
+ tr_id = zbx.add_trigger(options)
186
+
187
+ options = {
188
+ 'description' => "Free memory without cache, %",
189
+ 'expression' => "{#{template_name}:vm.memory.size[free].last(0)}<10",
190
+ 'priority' => 5, # disaster
191
+ 'templateid' => 0,
192
+ 'commenty' => "Free memory without cache, %",
193
+ 'type' => 0,
194
+ 'status' => '0'
195
+ }
196
+
197
+ p " ** Add 'Free memory without cache, % disaster trigger'"
198
+ tr_id = zbx.add_trigger(options)
199
+
200
+ options = {
201
+ 'description' => "Swap usage in, B",
202
+ 'expression' => "{#{template_name}:system.swap.in[].last(0)}#0",
203
+ 'priority' => 2, # warning
204
+ 'templateid' => 0,
205
+ 'comments' => "Swap usage in, B",
206
+ 'type' => 0,
207
+ 'status' => '0'
208
+ }
209
+
210
+ p " ** Add 'Swap usage in, B warning trigger'"
211
+ tr_id = zbx.add_trigger(options)
212
+
213
+ options = {
214
+ 'description' => "Swap usage out, B",
215
+ 'expression' => "{#{template_name}:system.swap.out[].last(0)}#0",
216
+ 'priority' => 2, # warning
217
+ 'templateid' => 0,
218
+ 'comments' => "Swap usage out, B",
219
+ 'type' => 0,
220
+ 'status' => '0'
221
+ }
222
+
223
+ p " ** Add 'Swap usage out, B warning trigger'"
224
+ tr_id = zbx.add_trigger(options)
225
+
226
+ options = {
227
+ 'gitems' => [
228
+ {
229
+ "itemid" => total_in_b_item_id,
230
+ "drawtype" => "0",
231
+ "sortorder" => "0",
232
+ "color" => "AA0000",
233
+ "yaxisside" => "0",
234
+ "calc_fnc" => "2",
235
+ "type" => "0",
236
+ "periods_cnt" => "5"
237
+ },
238
+ {
239
+ "itemid" => used_in_b_item_id,
240
+ "drawtype" => "0",
241
+ "sortorder" => "0",
242
+ "color" => "009900",
243
+ "yaxisside" => "0",
244
+ "calc_fnc" => "2",
245
+ "type" => "0",
246
+ "periods_cnt" => "5"
247
+ },
248
+ {
249
+ "itemid" => cached_in_b_item_id,
250
+ "drawtype" => "0",
251
+ "sortorder" => "0",
252
+ "color" => "0000AA",
253
+ "yaxisside" => "0",
254
+ "calc_fnc" => "2",
255
+ "type" => "0",
256
+ "periods_cnt" => "5"
257
+ },
258
+ {
259
+ "itemid" => buffers_in_b_item_id,
260
+ "drawtype" => "0",
261
+ "sortorder" => "0",
262
+ "color" => "AA00AA",
263
+ "yaxisside" => "0",
264
+ "calc_fnc" => "2",
265
+ "type" => "0",
266
+ "periods_cnt" => "5"
267
+ }
268
+ ],
269
+ "show_triggers" => "1",
270
+ "name" => "Memory usage",
271
+ "width" => "900",
272
+ "height" => "200",
273
+ "templateid" => "0"
274
+ }
275
+
276
+ g_id = zbx.add_graph(options)
@@ -0,0 +1,82 @@
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
+ interface_name = opt["i"]
12
+ zabbix_env = opt["E"]
13
+
14
+ template_name = "TMPL_Net_#{interface_name}"
15
+ app_name = "Net_#{interface_name}"
16
+
17
+
18
+ # read config
19
+ config = YAML::load(open('./config.yml'))
20
+
21
+ api_url = config[zabbix_env]["api_url"]
22
+ api_login = config[zabbix_env]["api_login"]
23
+ api_password = config[zabbix_env]["api_password"]
24
+
25
+ # Esablish new connection
26
+ zbx = Zabbix::ZabbixApi.new(api_url, api_login, api_password)
27
+
28
+ # Create new template
29
+ p " * Creating template #{template_name}."
30
+ g_id = zbx.get_group_id(group_name)
31
+
32
+ options = {
33
+ 'groups' => [ g_id.to_i ],
34
+ 'host' => template_name
35
+ }
36
+
37
+ t_id = zbx.add_template(options)
38
+
39
+ # Create application #{app_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_application(application)
46
+
47
+ # 'Net #{interface_name} incoming traffic'
48
+ options = {
49
+ 'description' => "Net #{interface_name} incoming, B",
50
+ 'key_' => "net.if.in[#{interface_name}, bytes]",
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
+ 'units' => 'Bps',
58
+ 'value_type' => '3', # Numeric (unsigned)
59
+ 'delta' => 1 # Store as delta (Speed per second)
60
+ }
61
+ p " ** Add 'Net #{interface_name} incoming, B' to #{template_name}."
62
+
63
+ i_id = zbx.add_item(options)
64
+
65
+ # 'Net #{interface_name} outgoing traffic'
66
+ options = {
67
+ 'description' => "Net #{interface_name} outgoing, B",
68
+ 'key_' => "net.if.out[#{interface_name}, bytes]",
69
+ 'hostid' => t_id.to_i,
70
+ 'applications' => [ a_id.to_i ],
71
+ 'history' => 7,
72
+ 'trends' => 30,
73
+ 'delay' => 60,
74
+ 'value_type' => 0,
75
+ 'units' => 'Bps',
76
+ 'value_type' => '3', # Numeric (unsigned)
77
+ 'delta' => 1 # Store as delta (Speed per second)
78
+
79
+ }
80
+ p " ** Add 'Net #{interface_name} outgoing, B' to #{template_name}."
81
+
82
+ i_id = zbx.add_item(options)
@@ -1,5 +1,3 @@
1
- require 'base'
2
-
3
1
  module Zabbix
4
2
  class ZabbixApi
5
3
  def add_application(app_options)
@@ -17,13 +15,36 @@ module Zabbix
17
15
 
18
16
  responce = send_request(message)
19
17
 
20
- if not ( responce.empty? ) then
18
+ unless responce.empty? then
21
19
  result = responce['applicationids'][0].to_i
22
20
  else
23
- result = nil
21
+ result = nil
24
22
  end
25
23
 
26
24
  return result
27
25
  end
28
26
  end
27
+
28
+ def get_app_id(host_id, app_name)
29
+
30
+ message = {
31
+ 'method' => 'application.get',
32
+ 'params' => {
33
+ 'filter' => {
34
+ 'name' => app_name,
35
+ 'hostid' => host_id
36
+ }
37
+ }
38
+ }
39
+
40
+ responce = send_request(message)
41
+
42
+ unless responce.empty? then
43
+ result = responce[0]['applicationid']
44
+ else
45
+ result = nil
46
+ end
47
+
48
+ return result
49
+ end
29
50
  end
@@ -0,0 +1,133 @@
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
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
+ end
35
+
36
+ def do_request(message)
37
+
38
+ id = rand 100_000
39
+
40
+ message['id'] = id
41
+ message['jsonrpc'] = '2.0'
42
+
43
+ message_json = JSON.generate(message)
44
+
45
+ uri = URI.parse(@api_url)
46
+ http = Net::HTTP.new(uri.host, uri.port)
47
+
48
+ if ( uri.scheme == "https" ) then
49
+ http.use_ssl = true
50
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
51
+ end
52
+
53
+ request = Net::HTTP::Post.new(uri.request_uri)
54
+ request.add_field('Content-Type', 'application/json-rpc')
55
+ request.body=(message_json)
56
+
57
+ begin
58
+ puts "[ZBXAPI] : INFO : Do request. Body => #{request.body}" if @debug
59
+ response = http.request(request)
60
+ rescue ::SocketError => e
61
+ puts "[ZBXAPI] : ERROR : SocketError => #{e.message}" if @debug
62
+ raise Zabbix::SocketError.new(e.message)
63
+ end
64
+
65
+ if @debug
66
+ puts "[ZBXAPI] : INFO : Response start"
67
+ puts response
68
+ puts "[ZBXAPI] : INFO : Response end"
69
+ end
70
+
71
+ if response.code != "200"
72
+ raise Zabbix::ResponseCodeError.new("Responce code from [" + @api_url + "] is #{response.code}")
73
+ end
74
+
75
+ response_body_hash = JSON.parse(response.body)
76
+
77
+ if error = response_body_hash['error']
78
+ error_message = error['message']
79
+ error_data = error['data']
80
+ error_code = error['code']
81
+
82
+ e_message = "Code: [" + error_code.to_s + "]. Message: [" + error_message +\
83
+ "]. Data: [" + error_data + "]."
84
+
85
+ case error_code.to_s
86
+ when '-32602'
87
+ raise Zabbix::AlreadyExist.new(e_message)
88
+ else
89
+ raise Zabbix::ResponseError.new(e_message)
90
+ end
91
+ end
92
+
93
+ result = response_body_hash['result']
94
+
95
+ return result
96
+ end
97
+
98
+ def send_request(message)
99
+ message['auth'] = auth()
100
+ do_request(message)
101
+ end
102
+
103
+ def auth()
104
+
105
+ auth_message = {
106
+ 'auth' => nil,
107
+ 'method' => 'user.authenticate',
108
+ 'params' => {
109
+ 'user' => @api_user,
110
+ 'password' => @api_password,
111
+ '0' => '0'
112
+ }
113
+ }
114
+
115
+ auth_id = do_request(auth_message)
116
+
117
+ return auth_id
118
+ end
119
+
120
+ # Utils.
121
+ def merge_opt(a, b)
122
+ c = {}
123
+
124
+ b.each_pair do |key, value|
125
+ if a.has_key?(key) then
126
+ c[key] = value
127
+ end
128
+ end
129
+
130
+ return a.merge(c)
131
+ end
132
+ end
133
+ end
@@ -7,15 +7,13 @@ module Zabbix
7
7
  'params' => graph
8
8
  }
9
9
 
10
- responce = send_request(message)
11
-
12
- puts "DEBUG: #{responce.inspect}"
10
+ response = send_request(message)
13
11
 
14
12
  return 0
15
13
  end
16
14
 
17
15
  def get_graph_id(host_id, graph_name)
18
-
16
+
19
17
  message = {
20
18
  'method' => 'graph.get',
21
19
  'params' => {
@@ -26,10 +24,10 @@ module Zabbix
26
24
  }
27
25
  }
28
26
 
29
- responce = send_request(message)
27
+ response = send_request(message)
30
28
 
31
- unless ( responce.empty? ) then
32
- result = responce[0]['graphid']
29
+ unless ( response.empty? ) then
30
+ result = response[0]['graphid']
33
31
  else
34
32
  result = nil
35
33
  end
@@ -47,12 +45,12 @@ module Zabbix
47
45
  }
48
46
  }
49
47
 
50
- responce = send_request(message)
48
+ response = send_request(message)
51
49
 
52
- unless ( responce.empty? ) then
50
+ unless ( response.empty? ) then
53
51
  result = {}
54
52
 
55
- responce.each() do |graph|
53
+ response.each() do |graph|
56
54
  graph_id = graph['graphid']
57
55
  graph_name = graph['name']
58
56
 
@@ -0,0 +1,78 @@
1
+ module Zabbix
2
+
3
+ class ZabbixApi
4
+ def get_group_id(pattern)
5
+
6
+ message = {
7
+ 'method' => 'hostgroup.get',
8
+ 'params' => {
9
+ 'filter' => {
10
+ 'name' => pattern
11
+ }
12
+ }
13
+ }
14
+
15
+ 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
24
+ end
25
+
26
+ def group_exist?(pattern)
27
+
28
+ group_id = get_groups_id(pattern)
29
+
30
+ if ( group_id ) then
31
+ return true
32
+ else
33
+ return false
34
+ end
35
+ end
36
+
37
+ def add_group(groupname)
38
+
39
+ message = {
40
+ 'method' => 'hostgroup.create',
41
+ 'params' => {
42
+ 'name' => groupname
43
+ }
44
+ }
45
+
46
+ response = send_request(message)
47
+
48
+ if ( response ) then
49
+ result = response['groupids'][0].to_i
50
+ else
51
+ result = nil
52
+ end
53
+
54
+ return result
55
+ end
56
+
57
+ def add_host_to_group(host_id, group_id)
58
+
59
+ message = {
60
+ 'method' => 'hostgroup.massAdd',
61
+ 'params' => {
62
+ 'groups' => [ group_id ],
63
+ 'hosts' => [ host_id ]
64
+ }
65
+ }
66
+
67
+ 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
76
+ end
77
+ end
78
+ end