Skip to content

Instantly share code, notes, and snippets.

@anderscarling
Created April 26, 2009 00:31
Show Gist options
  • Save anderscarling/101819 to your computer and use it in GitHub Desktop.
Save anderscarling/101819 to your computer and use it in GitHub Desktop.
Will send mail to root@localhost if disk usage on one or more file systems is above 80 percent
#! /usr/bin/ruby -w
require 'net/smtp'
# Configuration
SMTP_SERVER = "localhost"
FROM_ADDRESS = "root@localhost"
TO_ADDRESS = "root@localhost"
DEFAULT_LIMIT = 80
# Warnlimit
WARN_LIMIT = ARGV[0].to_i > 0 ? ARGV[0].to_i : DEFAULT_LIMIT
# Parse data, making sure blocksize is 1024
df = `df -k`
# Report variable
report = []
# first line from report
df.each do |line|
if line =~ /^([\w\/]+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+%)\s+(.+$)/
device, blocks, used, avail, percentage, mountpoint = $1, $2, $3, $4, $5, $6
if percentage.to_i > WARN_LIMIT
report << {
:device => device,
:mountpoint => mountpoint,
:percentage => percentage.to_i,
:free_space => avail.to_i / 1024
}
end
end
end
exit 0 if report.empty?
# Create report
subject = "[Warning] Running low on disk space: #{report.map { |hash| hash[:device] }.join(" ")}"
report.map! do |hash|
"Device #{hash[:device]}, mounted on #{hash[:mountpoint]}: " +\
"#{hash[:percentage]}% (#{hash[:free_space]}MB free)"
end
report = report.join("\n")
message = <<EOT
From: #{FROM_ADDRESS}
To: #{TO_ADDRESS}
Subject: #{subject}
Warning, we're currently running low on disk space on the following devices:
#{report}
Please take action as soon as possible to avoid potential data loss.
EOT
Net::SMTP.start(SMTP_SERVER) do |smtp|
smtp.send_message message, FROM_ADDRESS, [TO_ADDRESS]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment