Created
December 24, 2012 01:50
-
-
Save masao/4367077 to your computer and use it in GitHub Desktop.
Check whether an IMAP Mailbox reaches for the quota.
Should setup ~/.check-imap-quota.yml as follows:
```
host: localhost
port: 143
user: masao
password: XXXXXXXX
```
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/env ruby | |
require "net/imap" | |
require "yaml" | |
CONFIG = File.join( ENV["HOME"], ".check-imap-quota.yml" ) | |
if $0 == __FILE__ | |
config = YAML.load( open( CONFIG ){|i| i.read } ) | |
config[ "port" ] = config[ "port" ].to_i | |
config[ "port" ] = 143 if config[ "port" ] == 0 | |
# If quota usage reaches this ratio, an alert is printed: | |
config[ "alert" ] ||= 0.9 | |
# Net::IMAP.debug = true | |
imap = Net::IMAP.new( config[ "host" ], config[ "port" ] ) | |
imap.login( config[ "user" ], config[ "password" ] ) | |
quota = imap.getquota( "ROOT" ).first | |
#p quota | |
ratio = quota.usage.to_i / quota.quota.to_f | |
if ratio > config[ "alert" ] | |
#puts "ALERT: Mailbox for #{ config["user"] }@#{ config["host"] }: reaches quota (#{ "%.1f%%" % (ratio*100) } = #{ quota.usage }/#{ quota.quota})" | |
open( "|Mail -s 'ALERT: Mailbox for #{ config["user"] }@#{ config["host"] }' #{ config[ "email" ] }", "w" ) do |io| | |
io.puts "ALERT: Mailbox for #{ config["user"] }@#{ config["host"] }: reaches quota (#{ "%.1f%%" % (ratio*100) } = #{ quota.usage }/#{ quota.quota})" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment