Created
April 26, 2009 00:31
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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