capistrano2-zabbix 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,40 @@
1
+ require 'capistrano'
2
+ require_relative '../zabbix_maintenance'
3
+
4
+ Capistrano::Configuration.instance.load do
5
+ def set_default(pname, *args, &bl)
6
+ set(pname, *args, &bl) unless exists? pname
7
+ end
8
+
9
+ # Defaults
10
+ set_default(:zabbix_url) { 'http://192.168.56.2/zabbix/' }
11
+ set_default(:zabbix_user) { 'Admin' }
12
+ set_default(:zabbix_password) { 'zabbix' }
13
+ set_default(:zabbix_period) { 60 * 60 * 10 } # 10 hours
14
+ set_default(:zabbix_groupid) { 2 }
15
+ set_default(:zabbix_auto_trigger) { false }
16
+
17
+ namespace :zabbix do
18
+ desc 'Create maintenance in Zabbix'
19
+ task :create do
20
+ zm_api.create_or_replace [zabbix_groupid], period: zabbix_period
21
+ end
22
+
23
+ desc 'Delete maintenance in Zabbix'
24
+ task :delete do
25
+ zm_api.delete(id: zm_api.maint_id)
26
+ end
27
+ end
28
+
29
+ if zabbix_auto_trigger
30
+ after 'deploy:setup', 'zabbix:create'
31
+ after 'deploy:restart', 'zabbix:delete'
32
+ end
33
+
34
+ def zm_api
35
+ zbx = ZabbixMaintenance.new("#{zabbix_url}/api_jsonrpc.php",
36
+ zabbix_user,
37
+ zabbix_password)
38
+ set(:zbx_handle, zbx)
39
+ end
40
+ end
@@ -0,0 +1 @@
1
+ require_relative './capistrano/tasks'
@@ -0,0 +1,64 @@
1
+ require 'zabbixapi'
2
+
3
+ # Communicates with Zabbix through API and performs actions on
4
+ # maintenance objects.
5
+ class ZabbixMaintenance
6
+ attr_reader :id, :maint_title, :zbx
7
+
8
+ def initialize(url, user, password, title: 'capistrano auto maintenance')
9
+ begin
10
+ @zbx = ZabbixApi.connect(url: url, user: user, password: password)
11
+ rescue RuntimeError
12
+ @zbx = nil
13
+ end
14
+
15
+ @maint_title = title
16
+ @id = nil
17
+ end
18
+
19
+ def maint_id
20
+ ret = @zbx.query method: 'maintenance.get', params: {
21
+ 'search': {
22
+ 'name': @maint_title
23
+ }
24
+ }
25
+ begin
26
+ ret.first['maintenanceid'].to_i
27
+ rescue KeyError
28
+ nil
29
+ end
30
+ end
31
+
32
+ def create_or_replace(*args)
33
+ delete(id: maint_id) if exists?
34
+ create(*args)
35
+ end
36
+
37
+ def create(groupids, period: 3600)
38
+ maint_params = {
39
+ name: @maint_title,
40
+ active_since: Time.now.to_i - 200,
41
+ active_till: Time.now.to_i + 866_00,
42
+ groupids: groupids,
43
+ timeperiods: [{ period: period }]
44
+ }
45
+ ret = @zbx.query method: 'maintenance.create', params: maint_params
46
+ @id = ret['maintenanceids'].first.to_i
47
+ end
48
+
49
+ def delete(id: @id)
50
+ ret = @zbx.query method: 'maintenance.delete', params: [id]
51
+ unless ret['maintenanceids'] == [id]
52
+ fail "Maintenance id:#{id} has not been deleted"
53
+ end
54
+ true
55
+ end
56
+
57
+ def exists?
58
+ @zbx.query method: 'maintenance.exists', params: { name: @maint_title }
59
+ end
60
+
61
+ def authenticated?
62
+ !(@zbx.nil? || @zbx.client.auth.nil?)
63
+ end
64
+ end
@@ -0,0 +1,4 @@
1
+ require_relative './spec_helper'
2
+
3
+ describe 'Capistrano2Zabbix' do
4
+ end
@@ -0,0 +1,108 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: http://192.168.56.2/zabbix/api_jsonrpc.php
6
+ body:
7
+ encoding: UTF-8
8
+ string: '{"method":"user.authenticate","params":{"user":"Admin","password":"zabbix"},"auth":null,"id":90443,"jsonrpc":"2.0"}'
9
+ headers:
10
+ Accept-Encoding:
11
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
12
+ Accept:
13
+ - "*/*"
14
+ User-Agent:
15
+ - Ruby
16
+ Content-Type:
17
+ - application/json-rpc
18
+ response:
19
+ status:
20
+ code: 200
21
+ message: OK
22
+ headers:
23
+ Date:
24
+ - Thu, 14 May 2015 04:45:14 GMT
25
+ Server:
26
+ - Apache/2.2.22 (Linux/SUSE)
27
+ X-Powered-By:
28
+ - PHP/5.3.17
29
+ Content-Length:
30
+ - '72'
31
+ Content-Type:
32
+ - application/json
33
+ body:
34
+ encoding: UTF-8
35
+ string: '{"jsonrpc":"2.0","result":"9cea11e673f8859e7c09085362dc99c1","id":90443}'
36
+ http_version:
37
+ recorded_at: Thu, 14 May 2015 12:16:34 GMT
38
+ - request:
39
+ method: post
40
+ uri: http://192.168.56.2/zabbix/api_jsonrpc.php
41
+ body:
42
+ encoding: UTF-8
43
+ string: '{"method":"apiinfo.version","params":{},"auth":"9cea11e673f8859e7c09085362dc99c1","id":36215,"jsonrpc":"2.0"}'
44
+ headers:
45
+ Accept-Encoding:
46
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
47
+ Accept:
48
+ - "*/*"
49
+ User-Agent:
50
+ - Ruby
51
+ Content-Type:
52
+ - application/json-rpc
53
+ response:
54
+ status:
55
+ code: 200
56
+ message: OK
57
+ headers:
58
+ Date:
59
+ - Thu, 14 May 2015 04:45:14 GMT
60
+ Server:
61
+ - Apache/2.2.22 (Linux/SUSE)
62
+ X-Powered-By:
63
+ - PHP/5.3.17
64
+ Content-Length:
65
+ - '45'
66
+ Content-Type:
67
+ - application/json
68
+ body:
69
+ encoding: UTF-8
70
+ string: '{"jsonrpc":"2.0","result":"2.2.2","id":36215}'
71
+ http_version:
72
+ recorded_at: Thu, 14 May 2015 12:16:34 GMT
73
+ - request:
74
+ method: post
75
+ uri: http://192.168.56.2/zabbix/api_jsonrpc.php
76
+ body:
77
+ encoding: UTF-8
78
+ string: '{"method":"user.authenticate","params":{"user":"Admin","password":"zabbix"},"auth":"9cea11e673f8859e7c09085362dc99c1","id":89508,"jsonrpc":"2.0"}'
79
+ headers:
80
+ Accept-Encoding:
81
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
82
+ Accept:
83
+ - "*/*"
84
+ User-Agent:
85
+ - Ruby
86
+ Content-Type:
87
+ - application/json-rpc
88
+ response:
89
+ status:
90
+ code: 200
91
+ message: OK
92
+ headers:
93
+ Date:
94
+ - Thu, 14 May 2015 04:45:14 GMT
95
+ Server:
96
+ - Apache/2.2.22 (Linux/SUSE)
97
+ X-Powered-By:
98
+ - PHP/5.3.17
99
+ Content-Length:
100
+ - '72'
101
+ Content-Type:
102
+ - application/json
103
+ body:
104
+ encoding: UTF-8
105
+ string: '{"jsonrpc":"2.0","result":"fdab665e94c7e4632823694c6b88f2f2","id":89508}'
106
+ http_version:
107
+ recorded_at: Thu, 14 May 2015 12:16:34 GMT
108
+ recorded_with: VCR 2.9.3
@@ -0,0 +1,39 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: http://192.168.56.2/zabbix/api_jsonrpc.php
6
+ body:
7
+ encoding: UTF-8
8
+ string: '{"method":"user.authenticate","params":{"user":"wronglogin","password":"wrongpw"},"auth":null,"id":63859,"jsonrpc":"2.0"}'
9
+ headers:
10
+ Accept-Encoding:
11
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
12
+ Accept:
13
+ - "*/*"
14
+ User-Agent:
15
+ - Ruby
16
+ Content-Type:
17
+ - application/json-rpc
18
+ response:
19
+ status:
20
+ code: 200
21
+ message: OK
22
+ headers:
23
+ Date:
24
+ - Thu, 14 May 2015 04:47:04 GMT
25
+ Server:
26
+ - Apache/2.2.22 (Linux/SUSE)
27
+ X-Powered-By:
28
+ - PHP/5.3.17
29
+ Content-Length:
30
+ - '126'
31
+ Content-Type:
32
+ - application/json
33
+ body:
34
+ encoding: UTF-8
35
+ string: '{"jsonrpc":"2.0","error":{"code":-32602,"message":"Invalid params.","data":"Login
36
+ name or password is incorrect."},"id":63859}'
37
+ http_version:
38
+ recorded_at: Thu, 14 May 2015 12:18:23 GMT
39
+ recorded_with: VCR 2.9.3
@@ -0,0 +1,143 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: http://192.168.56.2/zabbix/api_jsonrpc.php
6
+ body:
7
+ encoding: UTF-8
8
+ string: '{"method":"user.authenticate","params":{"user":"Admin","password":"zabbix"},"auth":null,"id":12876,"jsonrpc":"2.0"}'
9
+ headers:
10
+ Accept-Encoding:
11
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
12
+ Accept:
13
+ - "*/*"
14
+ User-Agent:
15
+ - Ruby
16
+ Content-Type:
17
+ - application/json-rpc
18
+ response:
19
+ status:
20
+ code: 200
21
+ message: OK
22
+ headers:
23
+ Date:
24
+ - Thu, 14 May 2015 04:05:39 GMT
25
+ Server:
26
+ - Apache/2.2.22 (Linux/SUSE)
27
+ X-Powered-By:
28
+ - PHP/5.3.17
29
+ Content-Length:
30
+ - '72'
31
+ Content-Type:
32
+ - application/json
33
+ body:
34
+ encoding: UTF-8
35
+ string: '{"jsonrpc":"2.0","result":"d0762dbd317f9340c2f50e3740fb32b4","id":12876}'
36
+ http_version:
37
+ recorded_at: Thu, 14 May 2015 11:36:59 GMT
38
+ - request:
39
+ method: post
40
+ uri: http://192.168.56.2/zabbix/api_jsonrpc.php
41
+ body:
42
+ encoding: UTF-8
43
+ string: '{"method":"apiinfo.version","params":{},"auth":"d0762dbd317f9340c2f50e3740fb32b4","id":92137,"jsonrpc":"2.0"}'
44
+ headers:
45
+ Accept-Encoding:
46
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
47
+ Accept:
48
+ - "*/*"
49
+ User-Agent:
50
+ - Ruby
51
+ Content-Type:
52
+ - application/json-rpc
53
+ response:
54
+ status:
55
+ code: 200
56
+ message: OK
57
+ headers:
58
+ Date:
59
+ - Thu, 14 May 2015 04:05:40 GMT
60
+ Server:
61
+ - Apache/2.2.22 (Linux/SUSE)
62
+ X-Powered-By:
63
+ - PHP/5.3.17
64
+ Content-Length:
65
+ - '45'
66
+ Content-Type:
67
+ - application/json
68
+ body:
69
+ encoding: UTF-8
70
+ string: '{"jsonrpc":"2.0","result":"2.2.2","id":92137}'
71
+ http_version:
72
+ recorded_at: Thu, 14 May 2015 11:36:59 GMT
73
+ - request:
74
+ method: post
75
+ uri: http://192.168.56.2/zabbix/api_jsonrpc.php
76
+ body:
77
+ encoding: UTF-8
78
+ string: '{"method":"maintenance.create","params":{"name":"capistrano auto maintenance","active_since":1431603219,"active_till":1431690019,"groupids":[2],"timeperiods":[{"period":3200}]},"auth":"d0762dbd317f9340c2f50e3740fb32b4","id":11855,"jsonrpc":"2.0"}'
79
+ headers:
80
+ Accept-Encoding:
81
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
82
+ Accept:
83
+ - "*/*"
84
+ User-Agent:
85
+ - Ruby
86
+ Content-Type:
87
+ - application/json-rpc
88
+ response:
89
+ status:
90
+ code: 200
91
+ message: OK
92
+ headers:
93
+ Date:
94
+ - Thu, 14 May 2015 04:05:40 GMT
95
+ Server:
96
+ - Apache/2.2.22 (Linux/SUSE)
97
+ X-Powered-By:
98
+ - PHP/5.3.17
99
+ Content-Length:
100
+ - '63'
101
+ Content-Type:
102
+ - application/json
103
+ body:
104
+ encoding: UTF-8
105
+ string: '{"jsonrpc":"2.0","result":{"maintenanceids":["13"]},"id":11855}'
106
+ http_version:
107
+ recorded_at: Thu, 14 May 2015 11:36:59 GMT
108
+ - request:
109
+ method: post
110
+ uri: http://192.168.56.2/zabbix/api_jsonrpc.php
111
+ body:
112
+ encoding: UTF-8
113
+ string: '{"method":"maintenance.delete","params":[13],"auth":"d0762dbd317f9340c2f50e3740fb32b4","id":93840,"jsonrpc":"2.0"}'
114
+ headers:
115
+ Accept-Encoding:
116
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
117
+ Accept:
118
+ - "*/*"
119
+ User-Agent:
120
+ - Ruby
121
+ Content-Type:
122
+ - application/json-rpc
123
+ response:
124
+ status:
125
+ code: 200
126
+ message: OK
127
+ headers:
128
+ Date:
129
+ - Thu, 14 May 2015 04:05:40 GMT
130
+ Server:
131
+ - Apache/2.2.22 (Linux/SUSE)
132
+ X-Powered-By:
133
+ - PHP/5.3.17
134
+ Content-Length:
135
+ - '61'
136
+ Content-Type:
137
+ - application/json
138
+ body:
139
+ encoding: UTF-8
140
+ string: '{"jsonrpc":"2.0","result":{"maintenanceids":[13]},"id":93840}'
141
+ http_version:
142
+ recorded_at: Thu, 14 May 2015 11:37:00 GMT
143
+ recorded_with: VCR 2.9.3
@@ -0,0 +1,213 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: http://192.168.56.2/zabbix/api_jsonrpc.php
6
+ body:
7
+ encoding: UTF-8
8
+ string: '{"method":"user.authenticate","params":{"user":"Admin","password":"zabbix"},"auth":null,"id":76622,"jsonrpc":"2.0"}'
9
+ headers:
10
+ Accept-Encoding:
11
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
12
+ Accept:
13
+ - "*/*"
14
+ User-Agent:
15
+ - Ruby
16
+ Content-Type:
17
+ - application/json-rpc
18
+ response:
19
+ status:
20
+ code: 200
21
+ message: OK
22
+ headers:
23
+ Date:
24
+ - Thu, 14 May 2015 04:34:45 GMT
25
+ Server:
26
+ - Apache/2.2.22 (Linux/SUSE)
27
+ X-Powered-By:
28
+ - PHP/5.3.17
29
+ Content-Length:
30
+ - '72'
31
+ Content-Type:
32
+ - application/json
33
+ body:
34
+ encoding: UTF-8
35
+ string: '{"jsonrpc":"2.0","result":"aebd33c25d82d0bab6c1477705762484","id":76622}'
36
+ http_version:
37
+ recorded_at: Thu, 14 May 2015 12:06:05 GMT
38
+ - request:
39
+ method: post
40
+ uri: http://192.168.56.2/zabbix/api_jsonrpc.php
41
+ body:
42
+ encoding: UTF-8
43
+ string: '{"method":"apiinfo.version","params":{},"auth":"aebd33c25d82d0bab6c1477705762484","id":77881,"jsonrpc":"2.0"}'
44
+ headers:
45
+ Accept-Encoding:
46
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
47
+ Accept:
48
+ - "*/*"
49
+ User-Agent:
50
+ - Ruby
51
+ Content-Type:
52
+ - application/json-rpc
53
+ response:
54
+ status:
55
+ code: 200
56
+ message: OK
57
+ headers:
58
+ Date:
59
+ - Thu, 14 May 2015 04:34:45 GMT
60
+ Server:
61
+ - Apache/2.2.22 (Linux/SUSE)
62
+ X-Powered-By:
63
+ - PHP/5.3.17
64
+ Content-Length:
65
+ - '45'
66
+ Content-Type:
67
+ - application/json
68
+ body:
69
+ encoding: UTF-8
70
+ string: '{"jsonrpc":"2.0","result":"2.2.2","id":77881}'
71
+ http_version:
72
+ recorded_at: Thu, 14 May 2015 12:06:05 GMT
73
+ - request:
74
+ method: post
75
+ uri: http://192.168.56.2/zabbix/api_jsonrpc.php
76
+ body:
77
+ encoding: UTF-8
78
+ string: '{"method":"maintenance.create","params":{"name":"capistrano auto maintenance","active_since":1431604965,"active_till":1431691765,"groupids":[2],"timeperiods":[{"period":3600}]},"auth":"aebd33c25d82d0bab6c1477705762484","id":74046,"jsonrpc":"2.0"}'
79
+ headers:
80
+ Accept-Encoding:
81
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
82
+ Accept:
83
+ - "*/*"
84
+ User-Agent:
85
+ - Ruby
86
+ Content-Type:
87
+ - application/json-rpc
88
+ response:
89
+ status:
90
+ code: 200
91
+ message: OK
92
+ headers:
93
+ Date:
94
+ - Thu, 14 May 2015 04:34:45 GMT
95
+ Server:
96
+ - Apache/2.2.22 (Linux/SUSE)
97
+ X-Powered-By:
98
+ - PHP/5.3.17
99
+ Content-Length:
100
+ - '63'
101
+ Content-Type:
102
+ - application/json
103
+ body:
104
+ encoding: UTF-8
105
+ string: '{"jsonrpc":"2.0","result":{"maintenanceids":["14"]},"id":74046}'
106
+ http_version:
107
+ recorded_at: Thu, 14 May 2015 12:06:05 GMT
108
+ - request:
109
+ method: post
110
+ uri: http://192.168.56.2/zabbix/api_jsonrpc.php
111
+ body:
112
+ encoding: UTF-8
113
+ string: '{"method":"maintenance.exists","params":{"name":"capistrano auto maintenance"},"auth":"aebd33c25d82d0bab6c1477705762484","id":93394,"jsonrpc":"2.0"}'
114
+ headers:
115
+ Accept-Encoding:
116
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
117
+ Accept:
118
+ - "*/*"
119
+ User-Agent:
120
+ - Ruby
121
+ Content-Type:
122
+ - application/json-rpc
123
+ response:
124
+ status:
125
+ code: 200
126
+ message: OK
127
+ headers:
128
+ Date:
129
+ - Thu, 14 May 2015 04:34:46 GMT
130
+ Server:
131
+ - Apache/2.2.22 (Linux/SUSE)
132
+ X-Powered-By:
133
+ - PHP/5.3.17
134
+ Content-Length:
135
+ - '42'
136
+ Content-Type:
137
+ - application/json
138
+ body:
139
+ encoding: UTF-8
140
+ string: '{"jsonrpc":"2.0","result":true,"id":93394}'
141
+ http_version:
142
+ recorded_at: Thu, 14 May 2015 12:06:05 GMT
143
+ - request:
144
+ method: post
145
+ uri: http://192.168.56.2/zabbix/api_jsonrpc.php
146
+ body:
147
+ encoding: UTF-8
148
+ string: '{"method":"maintenance.delete","params":[14],"auth":"aebd33c25d82d0bab6c1477705762484","id":15338,"jsonrpc":"2.0"}'
149
+ headers:
150
+ Accept-Encoding:
151
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
152
+ Accept:
153
+ - "*/*"
154
+ User-Agent:
155
+ - Ruby
156
+ Content-Type:
157
+ - application/json-rpc
158
+ response:
159
+ status:
160
+ code: 200
161
+ message: OK
162
+ headers:
163
+ Date:
164
+ - Thu, 14 May 2015 04:34:46 GMT
165
+ Server:
166
+ - Apache/2.2.22 (Linux/SUSE)
167
+ X-Powered-By:
168
+ - PHP/5.3.17
169
+ Content-Length:
170
+ - '61'
171
+ Content-Type:
172
+ - application/json
173
+ body:
174
+ encoding: UTF-8
175
+ string: '{"jsonrpc":"2.0","result":{"maintenanceids":[14]},"id":15338}'
176
+ http_version:
177
+ recorded_at: Thu, 14 May 2015 12:06:05 GMT
178
+ - request:
179
+ method: post
180
+ uri: http://192.168.56.2/zabbix/api_jsonrpc.php
181
+ body:
182
+ encoding: UTF-8
183
+ string: '{"method":"maintenance.exists","params":{"name":"capistrano auto maintenance"},"auth":"aebd33c25d82d0bab6c1477705762484","id":600,"jsonrpc":"2.0"}'
184
+ headers:
185
+ Accept-Encoding:
186
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
187
+ Accept:
188
+ - "*/*"
189
+ User-Agent:
190
+ - Ruby
191
+ Content-Type:
192
+ - application/json-rpc
193
+ response:
194
+ status:
195
+ code: 200
196
+ message: OK
197
+ headers:
198
+ Date:
199
+ - Thu, 14 May 2015 04:34:46 GMT
200
+ Server:
201
+ - Apache/2.2.22 (Linux/SUSE)
202
+ X-Powered-By:
203
+ - PHP/5.3.17
204
+ Content-Length:
205
+ - '41'
206
+ Content-Type:
207
+ - application/json
208
+ body:
209
+ encoding: UTF-8
210
+ string: '{"jsonrpc":"2.0","result":false,"id":600}'
211
+ http_version:
212
+ recorded_at: Thu, 14 May 2015 12:06:05 GMT
213
+ recorded_with: VCR 2.9.3