forked from hamann/check-barman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-barman.rb
executable file
·216 lines (189 loc) · 6.12 KB
/
check-barman.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env ruby
# Copyright (c) 2013 Holger Amann <[email protected]>
#
# MIT License
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
require 'optparse'
require 'rbarman'
include RBarman
def nagios_return_value(value, w, c)
ret_val = 0
if value >= c.to_i
ret_val = 2
elsif value >= w.to_i
ret_val = 1
else
ret_val = 0
end
ret_val
end
def check_ssh(server)
return_code = 0
ssh_ok = Server.by_name(server).ssh_check_ok
if ssh_ok
puts "SSH connection ok for #{server}"
else
puts "SSH connection failed for #{server}!"
return_code = 2
end
return_code
end
def check_pg(server)
return_code = 0
pg_ok = Server.by_name(server).pg_conn_ok
if pg_ok
puts "PG connection ok for #{server}"
else
puts "PG connection failed for #{server}!"
return_code = 2
end
return_code
end
def check_backups_available(server, warning, critical)
return_code = 0
count = Backups.all(server).count
if count == 0
p "No backups available! for #{server}"
else
p "#{count} backups available for #{server}"
nagios_return_value(count, warning, critical)
end
end
def check_last_wal_received(server, warning, critical)
latest = Backups.all(server, { :with_wal_files => true }).latest
if latest.status == :started
p "New backup started for #{server}"
return 0
else
last = latest.wal_files.last
diff = (Time.now - last.created).to_i
p "Last wal was received #{diff} seconds ago (#{last}) for #{server}"
nagios_return_value(diff, warning, critical)
end
end
def check_failed_backups(server, warning, critical)
backups = Backups.all(server)
count = 0
backups.each do |backup|
count = count + 1 if backup.status == :failed
end
p "#{count} backup(s) failed for #{server}"
nagios_return_value(count, warning, critical)
end
def check_missing_wals(server)
latest = Backups.all(server, { :with_wal_files => true }).latest
if latest.status == :started
p "New backup started for #{server}"
return 0
end
missing = latest.missing_wal_files
if missing.count == 0
puts "There are no missing wal files in the latest backup for #{server}"
return 0
else
lines = Array.new
missing.each { |m| lines << m.to_s }
puts "There are #{missing.count} missing wal files for #{server} in the latest backup for #{server}:\n #{ lines.join("\n") }"
return 2
end
end
def validate_params(params)
params.each do |k,v|
if v.nil?
raise OptionParser::MissingArgument.new("\'#{k.upcase}\' must be defined!")
end
end
end
options = {}
optparse = OptionParser.new do |opts|
opts.banner = "Usage check-barman.rb [options]"
options[:action] = nil
opts.on('-a', '--action ACTION',
[
:ssh,
:pg,
:backups_available,
:last_wal_received,
:failed_backups,
:missing_wals
] ,'The name of the check to be executed') do |action|
options[:action] = action
end
options[:server] = nil
opts.on('-s', '--server SERVER ', String, 'The \'server\' in barman terms') do |server|
options[:server] = server
end
options[:warning] = nil
opts.on('-w', '--warning WARNING', 'The warning level') do |warn|
options[:warning] = warn
end
options[:critical] = nil
opts.on('-c', '--critical CRITICAL', 'The critical level') do |critical|
options[:critical] = critical
end
options[:barman_home] = "/var/lib/barman"
opts.on('-h', '--barman-home PATH', String, "path to barman's Home directory, default /var/lib/barman") do |h|
options[:barman_home] = h
end
options[:barman_binary] = "/usr/bin/barman"
opts.on('-b', '--barman-binary PATH', String, "path to barman binary, default /usr/bin/barman") do |b|
options[:barman_binary] = b
end
end
if ARGV.count == 0
puts optparse
exit 1
end
optparse.parse!
server = options[:server]
warning = options[:warning]
critical = options[:critical]
action = options[:action]
return_code = 1
Configuration.instance.binary = options[:barman_binary]
Configuration.instance.barman_home = options[:barman_home]
begin
return_code = case action
when :ssh
validate_params({:server => server})
check_ssh(server)
when :pg
validate_params({:server => server})
check_pg(server)
when :backups_available
validate_params({:server => server, :warning => warning, :critical => critical})
check_backups_available(server, warning, critical)
when :last_wal_received
validate_params({:server => server, :warning => warning, :critical => critical})
check_last_wal_received(server, warning, critical)
when :failed_backups
validate_params({:server => server, :warning => warning, :critical => critical})
check_failed_backups(server, warning, critical)
when :missing_wals
validate_params({:server => server})
check_missing_wals(server)
end
rescue OptionParser::MissingArgument
puts $!.to_s
puts optparse
exit 1
end
exit return_code