2 ## vim:set et ts=2 sw=2 ai:
3 # Send email reminders to users having sizable homedirs.
5 # Copyright (c) 2013 Philipp Kern <phil@philkern.de>
6 # Copyright (c) 2013 Peter Palfrader <peter@palfrader.org>
7 # Copyright (c) 2013 Luca Filipozzi <lfilipoz@debian.org>
9 # Permission is hereby granted, free of charge, to any person obtaining a copy
10 # of this software and associated documentation files (the "Software"), to deal
11 # in the Software without restriction, including without limitation the rights
12 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 # copies of the Software, and to permit persons to whom the Software is
14 # furnished to do so, subject to the following conditions:
16 # The above copyright notice and this permission notice shall be included in
17 # all copies or substantial portions of the Software.
19 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 from __future__ import print_function
29 from collections import defaultdict
31 import email.mime.text
43 # avoid base64 encoding for utf-8
44 email.charset.add_charset('utf-8', email.charset.SHORTEST, email.charset.QP)
46 SENDMAIL = ['/usr/sbin/sendmail', '-t', '-oi']
47 #SENDMAIL = ['/bin/cat']
51 {hostname} /home is growing close to full. If you have anything in there that
52 you no longer need, please clean it up.""" # By Martin Zobel-Helas
54 Can you please look at your $HOME on {hostname} and remove files which you
55 no longer need (such as old sources).""" # By Martin Michlmayr
57 Thanks for your porting effort on {hostname}!
59 Please note that /home is running short of diskspace, so please remove files
60 you do not need anymore.""" # By Bill Allombert
61 # Please add more from your archive!
65 { 'days': 5, 'size': 10240 },
66 { 'days': 10, 'size': 1024 },
67 { 'days': 30, 'size': 100 },
68 { 'days': 60, 'size': 60 },
69 { 'days': 90, 'size': 30 }
71 USER_EXCLUSION_LIST = ['lost+found']
72 MAIL_FROM = 'debian-admin (via Cron) <bulk@admin.debian.org>'
73 MAIL_TO = '{username}@{hostname}.debian.org'
74 MAIL_CC = 'debian-admin (bulk sink) <bulk@admin.debian.org>'
75 MAIL_REPLYTO = 'debian-admin <dsa@debian.org>'
76 MAIL_SUBJECT = 'Please clean up ~{username} on {hostname}.debian.org'
82 For your information, you last logged into {hostname} {days_ago} days ago, and
83 your home directory there is {homedir_size} MB in size.
85 If you currently do not use {hostname}, please keep ~{username} under 30 MB,
86 if possible. Please assist us in freeing up space by deleting schroots, also.
90 Debian System Administration Team via Cron
92 PS: replies not required.
95 class Error(Exception):
98 class SendmailError(Error):
101 class LastLog(object):
102 LASTLOG_STRUCT = '=L32s256s'
104 def __init__(self, fname='/var/log/lastlog'):
105 record_size = struct.calcsize(self.LASTLOG_STRUCT)
107 with open(fname, 'r') as fp:
109 for record in iter(lambda: fp.read(record_size), ''):
111 last_login, _, _ = list(struct.unpack(self.LASTLOG_STRUCT, record))
115 self.records[pwd.getpwuid(uid).pw_name] = last_login
119 def last_login_for_user(self, username):
120 return self.records.get(username, 0)
122 class HomedirReminder(object):
124 self.lastlog = LastLog()
125 self.generate_homedir_list()
127 def parse_utmp(self):
128 self.utmp_records = defaultdict(list)
129 for wtmpfile in glob.glob('/var/log/wtmp*'):
130 for entry in utmp.UtmpRecord(wtmpfile):
131 # TODO: Login, does not account for non-idle sessions.
132 self.utmp_records[entry.ut_user].append(entry.ut_tv[0])
133 for username, timestamps in self.utmp_records.iteritems():
134 self.utmp_records[username] = sorted(timestamps)[-1]
136 def last_login_for_user(self, username):
137 return self.lastlog.last_login_for_user(username)
139 def generate_homedir_list(self):
140 self.homedir_sizes = {}
141 for direntry in glob.glob('/home/*'):
142 username = os.path.basename(direntry)
144 pwinfo = pwd.getpwnam(username)
146 if os.path.isdir(direntry):
147 logging.warning('Directory %s exists on %s but there is no %s user', direntry, platform.node(), username)
149 homedir = pwinfo.pw_dir
151 if username in USER_EXCLUSION_LIST:
153 # Ignore errors from du.
154 command = ['/usr/bin/du', '-ms', homedir]
155 p = subprocess.Popen(command,
156 stdout=subprocess.PIPE,
157 stderr=subprocess.PIPE)
158 (stdout, stderr) = p.communicate()
159 if p.returncode != 0:
160 logging.info('%s failed:', ' '.join(command))
163 size = int(stdout.split('\t')[0])
165 logging.error('Could not convert size output from %s: %s',
166 ' '.join(command), stdout)
168 self.homedir_sizes[username] = size
170 def send_mail(self, **kwargs):
171 msg = email.mime.text.MIMEText(MAIL_TEXT.format(**kwargs), _charset='UTF-8')
172 msg['From'] = MAIL_FROM.format(**kwargs)
173 msg['To'] = MAIL_TO.format(**kwargs)
174 if MAIL_CC != "": msg['Cc'] = MAIL_CC.format(**kwargs)
175 if MAIL_REPLYTO != "": msg['Reply-To'] = MAIL_REPLYTO.format(**kwargs)
176 msg['Subject'] = MAIL_SUBJECT.format(**kwargs)
177 msg['Precedence'] = "bulk"
178 msg['Auto-Submitted'] = "auto-generated by mail-big-homedirs"
179 p = subprocess.Popen(SENDMAIL, stdin=subprocess.PIPE)
180 p.communicate(msg.as_string())
181 logging.debug(msg.as_string())
182 if p.returncode != 0:
186 current_time = time.time()
187 for username, homedir_size in self.homedir_sizes.iteritems():
188 last_login = self.last_login_for_user(username)
189 logging.info('user %s: size %dMB, last login: %d', username, homedir_size, last_login)
190 days_ago = int( (current_time - last_login) / 3600 / 24 )
193 for e in REPORT_SIZES:
194 if days_ago >= e['days']: reportsize = e['size']
196 if reportsize is not None and homedir_size > reportsize:
197 logging.warning('Homedir of user %s is %d and did not login for a while', username, homedir_size)
199 name = pwd.getpwnam(username).pw_gecos.decode('utf-8')
200 name = name.split(',', 1)[0]
201 name = name.split(' ', 1)[0]
204 explanation = EXPLANATIONS[random.randint(0,len(EXPLANATIONS)-1)]
205 explanation = explanation.format(hostname=platform.node())
206 self.send_mail(hostname=platform.node(),
209 explanation=explanation,
210 homedir_size=homedir_size,
213 if __name__ == '__main__':
214 logging.basicConfig()
215 # DEBUG for debugging, ERROR for production.
216 logging.getLogger().setLevel(logging.ERROR)
217 HomedirReminder().run()