eximconf: unfold Subject headers before processing in RT routers
[mirror/dsa-puppet.git] / modules / porterbox / files / mail-big-homedirs
index 43dfe91..f5225b5 100755 (executable)
@@ -35,7 +35,6 @@ from optparse import OptionParser
 import os.path
 import platform
 import pwd
-import socket
 import subprocess
 import struct
 import time
@@ -56,14 +55,17 @@ parser.add_option("-d", "--debug",
 (options, args) = parser.parse_args()
 options.section = 'dsa-homedirs'
 options.config = '/etc/dsa/pubsub.conf'
-config = Config(options)
-mq_conf  = {
-  'rabbit_userid': config.username,
-  'rabbit_password': config.password,
-  'rabbit_virtual_host': config.vhost,
-  'rabbit_hosts': ['pubsub02.debian.org', 'pubsub01.debian.org'],
-  'use_ssl': False
-}
+if os.access(options.config, os.R_OK):
+  mq_config = Config(options)
+  mq_conf  = {
+    'rabbit_userid': mq_config.username,
+    'rabbit_password': mq_config.password,
+    'rabbit_virtual_host': mq_config.vhost,
+    'rabbit_hosts': ['pubsub02.debian.org', 'pubsub01.debian.org'],
+    'use_ssl': False
+  }
+else:
+  mq_config = None
 
 if options.dryrun:
   SENDMAIL_COMMAND = ['/bin/cat']
@@ -79,7 +81,7 @@ CRITERIA = [
     { 'size':    20,  'notifyafter': 90, 'deleteafter': 150 },
     { 'size':     5,                     'deleteafter': 700 }
   ]
-EXCLUDED_USERNAMES = ['lost+found', 'debian']
+EXCLUDED_USERNAMES = ['lost+found', 'debian', 'buildd', 'd-i']
 MAIL_FROM = 'debian-admin (via Cron) <bulk@admin.debian.org>'
 MAIL_TO = '{username}@{hostname}.debian.org'
 MAIL_CC = 'debian-admin (bulk sink) <bulk@admin.debian.org>'
@@ -115,15 +117,31 @@ class SendmailError(Error):
   pass
 
 class LastlogTimes(dict):
-  LASTLOG_STRUCT = '=L32s256s'
+  LASTLOG_STRUCT_32 = '=L32s256s'
+  LASTLOG_STRUCT_64 = '=Q32s256s'
 
   def __init__(self):
-    record_size = struct.calcsize(self.LASTLOG_STRUCT)
+    record_size_32 = struct.calcsize(self.LASTLOG_STRUCT_32)
+    record_size_64 = struct.calcsize(self.LASTLOG_STRUCT_64)
+    # some 64bit arches have 32bit-compatible lastlog structures, others don't,
+    # in apparently incoherent ways, so hardcode a list...
+    if platform.machine() in ('aarch64', 's390x'):
+        self.LASTLOG_STRUCT = self.LASTLOG_STRUCT_64
+        record_size = record_size_64
+    else:
+        self.LASTLOG_STRUCT = self.LASTLOG_STRUCT_32
+        record_size = record_size_32
     with open('/var/log/lastlog', 'r') as fp:
       uid = -1 # there is one record per uid in lastlog
       for record in iter(lambda: fp.read(record_size), ''):
+        if len(record) != record_size:
+            raise RuntimeError('lastlog has unexpected size, read %d instead of %d'
+                               % (len(record), 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:
+            raise RuntimeError('unexpected last login time %d for user %s'
+                               % (lastlog_time, pwd.getpwuid(uid).pw_name))
         try:
           self[pwd.getpwuid(uid).pw_name] = lastlog_time
         except KeyError:
@@ -195,19 +213,29 @@ class HomedirReminder(object):
 
   def run(self):
     current_time = time.time()
+    conn = None
     try:
-      msg = {
-        'timestamp': current_time,
-        'data': self.homedir_sizes,
-        'host': socket.gethostname()
-      }
-      conn = Connection(conf=mq_conf)
-      conn.topic_send(config.topic,
-                      msg,
-                      exchange_name=config.exchange,
-                      timeout=5)
+      data = {}
+      for user in set(self.homedir_sizes.keys()) | \
+                  set(self.lastlog_times.keys()):
+        data[user] = {
+          'homedir': self.homedir_sizes.get(user, 0),
+          'lastlog': self.lastlog_times.get(user, 0),
+        }
+
+      if mq_config is not None:
+        msg = {
+          'timestamp': current_time,
+          'data': data,
+          'host': platform.node(),
+        }
+        conn = Connection(conf=mq_conf)
+        conn.topic_send(mq_config.topic,
+                        msg,
+                        exchange_name=mq_config.exchange,
+                        timeout=5)
     except Exception, e:
-      LOG.error("Error sending: %s" % e)
+      logging.error("Error sending: %s" % e)
     finally:
       if conn:
         conn.close()
@@ -217,7 +245,7 @@ class HomedirReminder(object):
         realname = pwd.getpwnam(username).pw_gecos.decode('utf-8').split(',', 1)[0]
       except:
         realname = username
-      lastlog_time = self.lastlog_times[username]
+      lastlog_time = self.lastlog_times.get(username, 0)
       days_ago = int( (current_time - lastlog_time) / 3600 / 24 )
       kwargs = {
           'hostname': platform.node(),