X-Git-Url: https://git.adam-barratt.org.uk/?a=blobdiff_plain;f=modules%2Fporterbox%2Ffiles%2Fmail-big-homedirs;h=d5277aef41e66ca190c65f973d86120b447f2015;hb=ca7c18007c9f3f380dbf71841e97204eb90bb4ca;hp=4aec47f518484574ae1e3908a6ad0716cbde8852;hpb=c307247b825ff601685ddd7177d4f5a77d659379;p=mirror%2Fdsa-puppet.git diff --git a/modules/porterbox/files/mail-big-homedirs b/modules/porterbox/files/mail-big-homedirs index 4aec47f51..d5277aef4 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 @@ -24,17 +24,17 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. -from __future__ import print_function - 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 -import random import subprocess import struct import time @@ -43,45 +43,54 @@ 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'] -else: SENDMAIL_COMMAND = ['/usr/sbin/sendmail', '-t', '-oi'] - -if DRYRUN: RM_COMMAND = ['/bin/echo', 'Would remove'] -else: RM_COMMAND = ['/bin/rm', '-rf'] - -EXPLANATIONS = [ -u"""\ -{hostname}'s /home is, unfortunately, not infinite in size. If you have -anything in there that you no longer need, please clean it up.""" -,u"""\ -Can you please look at your $HOME on {hostname} and remove files which -you no longer need (such as old sources).""" -,u"""\ -Thanks for your porting effort on {hostname}! - -Please note that on most porterboxes /home is quite small, so please remove -files that you do not need anymore.""" - ] +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' +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 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'] MAIL_FROM = 'debian-admin (via Cron) ' MAIL_TO = '{username}@{hostname}.debian.org' MAIL_CC = 'debian-admin (bulk sink) ' MAIL_REPLYTO = 'debian-admin ' MAIL_SUBJECT = 'Please clean up ~{username} on {hostname}.debian.org' MAIL_MESSAGE = u"""\ -Hi {name}! +Hi {realname}! -{explanation} +Thanks for your porting effort on {hostname}! + +Please note that, on most porterboxes, /home is quite small, so please +remove files that you do not need anymore. For your information, you last logged into {hostname} {days_ago} days ago, and your home directory there is {homedir_size} MB in size. @@ -95,7 +104,7 @@ Thanks, Debian System Administration Team via Cron -PS: replies not required. +PS: A reply is not required. """ class Error(Exception): @@ -117,8 +126,7 @@ class LastlogTimes(dict): try: self[pwd.getpwuid(uid).pw_name] = lastlog_time except KeyError: - # this is a normal condition. - #logging.error('could not resolve username from uid %d', uid) + # this is a normal condition continue class HomedirSizes(dict): @@ -158,7 +166,7 @@ class HomedirReminder(object): self.lastlog_times = LastlogTimes() self.homedir_sizes = HomedirSizes() - def send_mail(self, **kwargs): + def notify(self, **kwargs): msg = email.mime.text.MIMEText(MAIL_MESSAGE.format(**kwargs), _charset='UTF-8') msg['From'] = MAIL_FROM.format(**kwargs) msg['To'] = MAIL_TO.format(**kwargs) @@ -175,9 +183,9 @@ class HomedirReminder(object): if p.returncode != 0: raise SendmailError - def remove(self, username): + def remove(self, **kwargs): try: - pwinfo = pwd.getpwnam(username) + pwinfo = pwd.getpwnam(kwargs.get('username')) except KeyError: return @@ -186,30 +194,62 @@ class HomedirReminder(object): def run(self): current_time = time.time() - explanation_template = EXPLANATIONS[random.randint(0,len(EXPLANATIONS)-1)] + 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': data, + 'host': platform.node(), + } + conn = Connection(conf=mq_conf) + conn.topic_send(config.topic, + msg, + exchange_name=config.exchange, + timeout=5) + except Exception, e: + LOG.error("Error sending: %s" % e) + finally: + if conn: + conn.close() 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] + realname = pwd.getpwnam(username).pw_gecos.decode('utf-8').split(',', 1)[0] except: - name = username + realname = username lastlog_time = self.lastlog_times[username] days_ago = int( (current_time - lastlog_time) / 3600 / 24 ) + kwargs = { + 'hostname': platform.node(), + 'username': username, + 'realname': realname, + 'homedir_size': homedir_size, + 'days_ago': days_ago + } 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 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) + self.remove(**kwargs) 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) + 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()