X-Git-Url: https://git.adam-barratt.org.uk/?a=blobdiff_plain;f=modules%2Fporterbox%2Ffiles%2Fmail-big-homedirs;h=f5225b5c8982fc61666aa01d1fb68a50b77ff752;hb=d824f9b2ae6fe566ad3c47c9d4f0c6aaa1057242;hp=f94e836fbd6e43e1b80248091b15f81a49e51271;hpb=62206f25cd0c5f5513d67655330427771863c629;p=mirror%2Fdsa-puppet.git diff --git a/modules/porterbox/files/mail-big-homedirs b/modules/porterbox/files/mail-big-homedirs index f94e836fb..f5225b5c8 100755 --- a/modules/porterbox/files/mail-big-homedirs +++ b/modules/porterbox/files/mail-big-homedirs @@ -3,7 +3,7 @@ # Send email reminders to users having sizable homedirs. ## # Copyright (c) 2013 Philipp Kern -# Copyright (c) 2013 Peter Palfrader +# Copyright (c) 2013, 2014 Peter Palfrader # Copyright (c) 2013 Luca Filipozzi # # Permission is hereby granted, free of charge, to any person obtaining a copy @@ -25,10 +25,13 @@ # THE SOFTWARE. from collections import defaultdict +from dsa_mq.connection import Connection +from dsa_mq.config import Config import email import email.mime.text import glob import logging +from optparse import OptionParser import os.path import platform import pwd @@ -40,26 +43,45 @@ import StringIO # avoid base64 encoding for utf-8 email.charset.add_charset('utf-8', email.charset.SHORTEST, email.charset.QP) -DRYRUN = False - -if DRYRUN: - SENDMAIL_COMMAND = ['/bin/cat'] +parser = OptionParser() +parser.add_option("-D", "--dryrun", + action="store_true", default=False, + help="Dry run mode") + +parser.add_option("-d", "--debug", + action="store_true", default=False, + help="Enable debug output") + +(options, args) = parser.parse_args() +options.section = 'dsa-homedirs' +options.config = '/etc/dsa/pubsub.conf' +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: - SENDMAIL_COMMAND = ['/usr/sbin/sendmail', '-t', '-oi'] + mq_config = None -if DRYRUN: +if options.dryrun: + SENDMAIL_COMMAND = ['/bin/cat'] RM_COMMAND = ['/bin/echo', 'Would remove'] else: + SENDMAIL_COMMAND = ['/usr/sbin/sendmail', '-t', '-oi'] RM_COMMAND = ['/bin/rm', '-rf'] CRITERIA = [ - { '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 } + { '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': 700 } ] -EXCLUDED_USERNAMES = ['lost+found'] +EXCLUDED_USERNAMES = ['lost+found', 'debian', 'buildd', 'd-i'] MAIL_FROM = 'debian-admin (via Cron) ' MAIL_TO = '{username}@{hostname}.debian.org' MAIL_CC = 'debian-admin (bulk sink) ' @@ -95,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: @@ -167,7 +205,6 @@ class HomedirReminder(object): def remove(self, **kwargs): try: pwinfo = pwd.getpwnam(kwargs.get('username')) - return except KeyError: return @@ -176,13 +213,39 @@ 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), + } + + 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: + logging.error("Error sending: %s" % e) + finally: + if conn: + conn.close() for username, homedir_size in self.homedir_sizes.iteritems(): try: 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(), @@ -206,7 +269,8 @@ class HomedirReminder(object): self.notify(**kwargs) if __name__ == '__main__': - logging.basicConfig() - # DEBUG for debugging, ERROR for production. - logging.getLogger().setLevel(logging.ERROR) + lvl = logging.ERROR + if options.debug: + lvl = logging.DEBUG + logging.basicConfig(level=lvl) HomedirReminder().run()