Skip to content

Instantly share code, notes, and snippets.

@iDiogenes
Created December 10, 2010 19:05
Show Gist options
  • Save iDiogenes/736626 to your computer and use it in GitHub Desktop.
Save iDiogenes/736626 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
$:.unshift File.join(File.dirname(__FILE__), *%w[ ../lib])
require 'ifs_notify'
require 'sequel'
# method for converting isilon string to int
def quota_compare(size)
prefix = size.slice(/[GMTK]/) # remove the letter value for later use
size = sprintf('%0.1f',(size.to_f)).to_f # Isilon can only compute float to the thenth's place
if size > 9
size = sprintf('%0.0f',(size.to_f)).to_i # Isilon rounds after 9
end
size = size * 10 if prefix == "M"
size = size * 10000 if prefix == "G"
size = size * 10000000 if prefix == "T"
size = 0 unless prefix =~ (/[GMTK]/)
return size unless size == nil
end
#
# Main Code
#
# setup the sqlite environment
DB = Sequel.sqlite('../var/ifs_notify.db') # set db path
DB.run("create table users(username TEXT primary key, directory TEXT, emailaddr TEXT, hardquota FLOAT, advquota FLOAT, actualsize FLOAT, lstemail DATETIME);") unless File.exists? "../var/ifs_notify.db"
# set time range for email - when script loops through between 3:57PM and 4:03PM it clean the DB and send an email to sysadm
runtime = 1557..1602
# Run the program
loop do
users = DB[:users] # select table to work with
viol = IfsNotify::QuotaCheck.new.run # create an array of quota violators
viol.each do |h|
# Gather users info from Active Directory
ldap = IfsNotify::LoniLDAP.new(h[:username]) # start connection to LDAP server h["username"]
ad = {
:gname => ldap.ldap_firstname, # get firstname from ldap
:name => ldap.ldap_username, # get full name from ldap
:mail => ldap.ldap_mail # get email from ldap
}
h.merge!(ad)
usercheck = users.first(:username => h[:username]) # check if user is in database
if usercheck == nil
IfsNotify::Email.new.send_advquota(h) if quota_compare(h[:hs]) > quota_compare(h[:as])
IfsNotify::Email.new.send_hardquota(h) if quota_compare(h[:hs]) < quota_compare(h[:as])
users.insert(:username => h[:username], :directory => h[:home], :emailaddr => h[:mail], :hardquota =>h[:hs], :advquota => h[:qs], :actualsize =>h[:as], :lstemail => Time.now+86400 )
else
lastemail = users.where(:username=> h[:username]).limit(1).first[:lstemail] # get value for last email
IfsNotify::Email.new.send_advquota(h) if (quota_compare(h[:hs]) > quota_compare(h[:as])) && lastemail <= Time.now # email advisory quota if they have not been emailed in the past 24 hours (86400 seconds)
IfsNotify::Email.new.send_hardquota(h) if (quota_compare(h[:hs]) < quota_compare(h[:as])) && lastemail <= Time.now # email hardquota if they have not been emailed in the past 24 hours (86400 seconds)
users.filter(:username => h[:username]).update(:lstemail => Time.now+86400 ) # update to time till next email
end
end
if runtime.include? Time.now.strftime("%H%M").to_i
users.filter(:lstemail < Time.now ).delete # clean old records from DB
db_dump = users.select(:username, :directory, :hardquota, :advquota, :actualsize, :lstemail).order(:username).all
IfsNotify::Email.new.daily_report(db_dump) # send an email of all users inviolation of quota
end
sleep(360) # loop through program every 6 min
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment