forked from alibby/nagios-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_mysql_dumps
executable file
·63 lines (54 loc) · 1.65 KB
/
check_mysql_dumps
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
#!/usr/bin/env ruby
#
# Checks the status of mysql dumps.
class DB_Dump
@@max_age=60*60*30 # 30 hrs
@@user='backup'
@@pass=`echo 'cbdlvq!tfdsfu' | tr '[b-za]' '[a-z]'`.strip
@@backup_root='/var/mysql_backups'
attr_accessor :name, :file
def initialize(dbname)
@name = dbname
begin
@file = File.new(dump_file)
rescue Errno::EACCES => e
@file = nil
rescue Errno::ENOENT => e
@file = nil
end
end
def self.each_database
cmd=%Q{echo "SHOW DATABASES"|mysql -u #{@@user} --password='#{@@pass}' | sed -n '2,\$p'}
IO.popen(cmd,'r') { |io| io.each_line { |line| yield(self.new(line.strip)) } }
end
def dump_file; "#{@@backup_root}/#{name}.sql"; end
def too_old?; file && (file.mtime < Time.now - @@max_age); end
def zero_size?; File.zero?(dump_file); end
def bad_dump?; file.nil? || too_old? || zero_size?; end
def reason
return "#{dump_file} does not exist" unless file
return "#{dump_file} older than #{@@max_age.to_f/(60*60*24)} days" if too_old?
return "#{dump_file} contains no data" if zero_size?
end
end
@bad_databases = []
@db_count = 0
DB_Dump.each_database do |db|
@db_count+=1
begin
@bad_databases << db.reason if db.bad_dump?
rescue Exception => e
@bad_databases << e.message
end
end
if @bad_databases.empty?
puts "OK - database dumps are current"
exit(0)
elsif(@bad_databases.length < (@db_count/2))
puts "WARNING - #{@bad_databases.length} of #{@db_count} dumps missing, empty or old"
exit(1)
else
puts "CRITICAL - #{@bad_databases.length} of #{@db_count} dumps missing, empty or old"
exit(2)
end
__END__