blob: de3f99cc9487bbaeeeaa5c690ebfe8c799b2a6fb [file] [log] [blame]
Scott Zawalski20a9b582011-11-21 11:49:40 -08001#!/usr/bin/python
2
3import datetime
4import getpass
5import optparse
6import os
7import socket
8
9
10def read_loascertstatus():
11 # prodcertstatus --simple_output returns the #of seconds remaining before the
12 # cert is expired.
13 f = os.popen('prodcertstatus --simple_output | grep LOAS')
14 loas_expire = int(f.read().split(':')[1])
15 f.close()
16 return loas_expire
17
18
19def main():
20 parser = optparse.OptionParser()
21 parser.add_option('--expire_within', help='Send email if cert will expire '
22 'within this time window in seconds.',
23 type='int', dest='expire_within', default=24*3600)
24 parser.add_option('--to', help='Comma separated Email notification TO '
25 'recipients.', dest='to', type='string', default='')
26 parser.add_option('--cc', help='Comma separated Email notification CC '
27 'recipients.', dest='cc', type='string', default='')
28 options, _ = parser.parse_args()
29
30 loas_expire = read_loascertstatus()
31 host = socket.gethostname()
32 if loas_expire < options.expire_within:
33 tt = datetime.timedelta(seconds=loas_expire)
34 body_text = ('prod access cert (LOAS) for %s will expire within %s on %s.'
35 % (getpass.getuser(), tt, host))
36 if not options.to:
37 print body_text
38 else:
39 email_to = ['%s@google.com' % to.strip() for to in options.to.split(',')]
40
41 p = os.popen('/usr/sbin/sendmail -t', 'w')
42 p.write('To: %s\n' % ','.join(email_to))
43 if options.cc:
44 email_cc = ['%s@google.com' % cc.strip()
45 for cc in options.cc.split(',')]
46 p.write('Cc: %s\n' % ','.join(email_cc))
47
48 p.write('Subject: Prod access cert (LOAS) for %s will expire soon on %s.'
49 '\n' % (getpass.getuser(), host))
50 p.write('Content-Type: text/plain')
51 p.write('\n') # blank line separating headers from body
52 p.write(body_text)
53 p.write('\n')
54 return_code = p.close()
55 if return_code is not None:
56 print 'Sendmail exit status %s' % return_code
57
58
59if __name__ == '__main__':
60 main()