]> git.donarmstrong.com Git - dsa-puppet.git/blobdiff - modules/porterbox/files/mail-big-homedirs
Port LastlogTimes object to platforms with a 64-bit lastlog.ll_time
[dsa-puppet.git] / modules / porterbox / files / mail-big-homedirs
index 43dfe9167d51ceb95887f80a04f5863558b2b283..763afa5318e0980cde7b12714a1f13a0d0d1e165 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
@@ -115,10 +114,21 @@ 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)
+    lastlog_size = os.path.getsize('/var/log/lastlog')
+    if 0 == (lastlog_size % record_size_32):
+        self.LASTLOG_STRUCT = self.LASTLOG_STRUCT_32
+        record_size = record_size_32
+    elif 0 == (lastlog_size % record_size_64):
+        self.LASTLOG_STRUCT = self.LASTLOG_STRUCT_64
+        record_size = record_size_64
+    else:
+        raise RuntimeError('Unknown architecture, cannot interpret /var/log/lastlog file size (%d)' % lastlog_size)
     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), ''):
@@ -195,11 +205,20 @@ class HomedirReminder(object):
 
   def run(self):
     current_time = time.time()
+    conn = None
     try:
+      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),
+        }
+
       msg = {
         'timestamp': current_time,
-        'data': self.homedir_sizes,
-        'host': socket.gethostname()
+        'data': data,
+        'host': platform.node(),
       }
       conn = Connection(conf=mq_conf)
       conn.topic_send(config.topic,
@@ -207,7 +226,7 @@ class HomedirReminder(object):
                       exchange_name=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 +236,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(),