How to check available disk space and email a report in Ruby

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
# gem install sys-filesystem
# gem install gmail
require 'sys/filesystem'
require 'gmail'
threshold = 9000
stat = Sys::Filesystem.stat("/")
mb_available = stat.block_size * stat.blocks_available / 1024 / 1024
mb_s = mb_available.to_s
gb_s = (mb_available / 1024).to_s
puts 'Available space: ' + mb_s + ' MB / ' + gb_s + ' GB'
if mb_available < threshold
puts 'Sending email ... '
user = "YOUR GMAIL"
pass = "PASSWORD"
gmail = Gmail.connect(user, pass)
email = gmail.compose do
subject "Your available disk space"
body "Space available: " + mb_s + "MB / " + gb_s + "GB"
end
email.deliver!
end

You may want to add it into cron job list.

Share