]> git.donarmstrong.com Git - dsa-puppet.git/commitdiff
Fix a couple minor bugs that refactoring introduced, and implement removing old/big...
authorPeter Palfrader <peter@palfrader.org>
Mon, 16 Dec 2013 08:09:30 +0000 (09:09 +0100)
committerPeter Palfrader <peter@palfrader.org>
Mon, 16 Dec 2013 08:09:30 +0000 (09:09 +0100)
modules/porterbox/files/mail-big-homedirs

index b04f5e61539c02ca9b3a20dec129663949e8891d..4aec47f518484574ae1e3908a6ad0716cbde8852 100755 (executable)
@@ -2,7 +2,7 @@
 ## vim:set et ts=2 sw=2 ai:
 # Send email reminders to users having sizable homedirs.
 ##
-# Copyright (c) 2013 Philipp Kern <phil@philkern.de>
+# Copyright (c) 2013 Philipp Kern <pkern@debian.org>
 # Copyright (c) 2013 Peter Palfrader <peter@palfrader.org>
 # Copyright (c) 2013 Luca Filipozzi <lfilipoz@debian.org>
 #
@@ -43,8 +43,13 @@ import StringIO
 # avoid base64 encoding for utf-8
 email.charset.add_charset('utf-8', email.charset.SHORTEST, email.charset.QP)
 
-SENDMAIL = ['/usr/sbin/sendmail', '-t', '-oi']
-#SENDMAIL = ['/bin/cat']
+DRYRUN=False
+
+if DRYRUN: SENDMAIL_COMMAND = ['/bin/cat']
+else:      SENDMAIL_COMMAND = ['/usr/sbin/sendmail', '-t', '-oi']
+
+if DRYRUN: RM_COMMAND = ['/bin/echo', 'Would remove']
+else:      RM_COMMAND = ['/bin/rm', '-rf']
 
 EXPLANATIONS = [
 u"""\
@@ -61,11 +66,11 @@ files that you do not need anymore."""
   ]
 
 CRITERIA = [
-    { 'days':  5, 'size': 10240 },
-    { 'days': 10, 'size':  1024 },
-    { 'days': 30, 'size':   100 },
-    { 'days': 60, 'size':    60 },
-    { 'days': 90, 'size':    10 }
+    { 'size': 10240,  'notifyafter':  5}, #, 'deleteafter': 40 },
+    { 'size':  1024,  'notifyafter': 10}, #, 'deleteafter': 50 },
+    { 'size':   100,  'notifyafter': 30}, #, 'deleteafter': 90 },
+    { 'size':    20,  'notifyafter': 90}, #, 'deleteafter': 150 },
+    { 'size':     5,                     'deleteafter': 450 }
   ]
 EXCLUDED_USERNAMES = ['lost+found']
 MAIL_FROM = 'debian-admin (via Cron) <bulk@admin.debian.org>'
@@ -101,7 +106,7 @@ class SendmailError(Error):
 
 class LastlogTimes(dict):
   LASTLOG_STRUCT = '=L32s256s'
-  
+
   def __init__(self):
     record_size = struct.calcsize(self.LASTLOG_STRUCT)
     with open('/var/log/lastlog', 'r') as fp:
@@ -109,26 +114,32 @@ class LastlogTimes(dict):
       for record in iter(lambda: fp.read(record_size), ''):
         uid += 1 # so keep incrementing uid for each record read
         lastlog_time, _, _ = list(struct.unpack(self.LASTLOG_STRUCT, record))
-        if lastlog_time == 0:
-          continue
         try:
           self[pwd.getpwuid(uid).pw_name] = lastlog_time
         except KeyError:
-          logging.error('could not resolve username from uid')
+          # this is a normal condition.
+          #logging.error('could not resolve username from uid %d', uid)
           continue
 
 class HomedirSizes(dict):
   def __init__(self):
     for direntry in glob.glob('/home/*'):
       username = os.path.basename(direntry)
+
       if username in EXCLUDED_USERNAMES:
         continue
+
       try:
         pwinfo = pwd.getpwnam(username)
       except KeyError:
         if os.path.isdir(direntry):
           logging.warning('directory %s exists on %s but there is no %s user', direntry, platform.node(), username)
         continue
+
+      if pwinfo.pw_dir != direntry:
+        logging.warning('home directory for %s is not %s, but that exists.  confused.', username, direntry)
+        continue
+
       command = ['/usr/bin/du', '-ms', pwinfo.pw_dir]
       p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
       (stdout, stderr) = p.communicate()
@@ -158,23 +169,43 @@ class HomedirReminder(object):
     msg['Subject'] = MAIL_SUBJECT.format(**kwargs)
     msg['Precedence'] = "bulk"
     msg['Auto-Submitted'] = "auto-generated by mail-big-homedirs"
-    p = subprocess.Popen(SENDMAIL, stdin=subprocess.PIPE)
+    p = subprocess.Popen(SENDMAIL_COMMAND, stdin=subprocess.PIPE)
     p.communicate(msg.as_string())
     logging.debug(msg.as_string())
     if p.returncode != 0:
       raise SendmailError
 
+  def remove(self, username):
+    try:
+      pwinfo = pwd.getpwnam(username)
+    except KeyError:
+      return
+
+    command = RM_COMMAND + [pwinfo.pw_dir]
+    p = subprocess.check_call(command)
+
   def run(self):
     current_time = time.time()
+    explanation_template = EXPLANATIONS[random.randint(0,len(EXPLANATIONS)-1)]
+
     for username, homedir_size in self.homedir_sizes.iteritems():
       try:
-        name = pwd.getpwnam(username).pw_gecos.decode('utf-8').split(',', 1)[0].split(' ', 1)[0]
+        name = pwd.getpwnam(username).pw_gecos.decode('utf-8').split(',', 1)[0].split(',', 1)[0]
       except:
         name = username
       lastlog_time = self.lastlog_times[username]
       days_ago = int( (current_time - lastlog_time) / 3600 / 24 )
-      if [x for x in CRITERIA if days_ago >= x['days'] and homedir_size >= x['size']]:
-        explanation = EXPLANATIONS[random.randint(0,len(EXPLANATIONS)-1)].format(hostname=platform.node())
+
+      notify = False
+      remove = False
+      for x in CRITERIA:
+        if homedir_size > x['size'] and 'notifyafter' in x and days_ago >= x['notifyafter']: notify = True
+        if homedir_size > x['size'] and 'deleteafter' in x and days_ago >= x['deleteafter']: remove = True
+
+      if remove:
+        self.remove(username=username)
+      elif notify:
+        explanation = explanation_template.format(hostname=platform.node())
         self.send_mail(hostname=platform.node(), username=username, name=name, explanation=explanation, homedir_size=homedir_size, days_ago=days_ago)
 
 if __name__ == '__main__':