source: people/peter.buschman/backup_monitoring/backmon/commands/mail/_mail.py@ 1084

Last change on this file since 1084 was 976, checked in by peter, on Dec 6, 2011 at 10:19:33 AM

Raw checkin of current NetBackup / TSM parsing code.

File size: 6.3 KB
Line 
1#!/usr/bin/python26
2###
3### backmon.commands.mail
4###
5
6import sys
7import os
8import os.path
9import glob
10import re
11import datetime
12import pytz
13import dateutil
14import MySQLdb
15import mimetypes
16
17from optparse import OptionParser
18
19from email import encoders
20from email.message import Message
21from email.mime.audio import MIMEAudio
22from email.mime.base import MIMEBase
23from email.mime.image import MIMEImage
24from email.mime.multipart import MIMEMultipart
25from email.mime.text import MIMEText
26
27from ...lib import *
28from ....dal.builtins import *
29from ....datetime import *
30
31import subcommands
32
33SUBCOMMAND_LIST = [
34 'test',
35 'morning_report',
36]
37
38SUBCOMMAND_HELP = {
39 'test' : 'send test email',
40 'morning_report' : 'send morning report',
41}
42
43##
44## OPTIONS
45##
46usage = """Usage: %prog [options] mail [subcommand] [options] [arguments]'
47
48 available subcommands:
49
50""" + '\n'.join([' %-12s %s' % (cmd, SUBCOMMAND_HELP[cmd]) for cmd in SUBCOMMAND_LIST])
51
52parser = OptionParser(usage=usage)
53parser.add_option('-n', '--now', action='store', default='now', help='override now for updates')
54
55def run(args, kwargs):
56
57 #
58 # add kwargs to local namespace
59 #
60 for key in kwargs.keys():
61
62 if re.compile('^[A-Z][A-Z_]+$').match(key):
63 exec(key + ' = kwargs[\'' + key + '\']')
64
65 #
66 # email settings
67 #
68 TO = ', '.join(CONFIG['EMAIL']['DEFAULT']['to']) if type(CONFIG['EMAIL']['DEFAULT']['to']) is list else CONFIG['EMAIL']['DEFAULT']['to']
69 FROM = CONFIG['EMAIL']['DEFAULT']['from']
70 CC = ', '.join(CONFIG['EMAIL']['DEFAULT']['cc']) if type(CONFIG['EMAIL']['DEFAULT']['cc']) is list else CONFIG['EMAIL']['DEFAULT']['cc']
71 BCC = ', '.join(CONFIG['EMAIL']['DEFAULT']['bcc']) if type(CONFIG['EMAIL']['DEFAULT']['bcc']) is list else CONFIG['EMAIL']['DEFAULT']
72
73 DEBUG('')
74 DEBUG('TO.................: %s' % (TO))
75 DEBUG('FROM...............: %s' % (FROM))
76 DEBUG('CC.................: %s' % (CC))
77 DEBUG('BCC................: %s' % (BCC))
78 DEBUG('')
79
80 #
81 # connect to database
82 #
83 DEBUG('DATABASE_ENGINE....: %s' % (DATABASE_ENGINE))
84 DEBUG('DATABASE_NAME......: %s' % (DATABASE_NAME))
85 DEBUG('DATABASE_USER......: %s' % (DATABASE_USER))
86 DEBUG('DATABASE_PASSWORD..: %s' % (DATABASE_PASSWORD))
87 DEBUG('DATABASE_HOST......: %s' % (DATABASE_HOST))
88 DEBUG('DATABASE_PORT......: %s' % (DATABASE_PORT))
89
90 try:
91
92 CONN = MySQLdb.connect(host=DATABASE_HOST, user=DATABASE_USER, passwd=DATABASE_PASSWORD, db=DATABASE_NAME)
93 DEBUG('Connected to %s database.' % (DATABASE_NAME))
94
95 except Exception, e:
96
97 ERROR('Could not connect to %s database! %s' % (DATABASE_NAME, e))
98 sys.exit(1)
99
100
101 SUBCOMMAND=None
102 subcommand_pos = 0
103
104 i = 0
105
106 for arg in args:
107
108 i += 1
109
110 if arg in SUBCOMMAND_LIST:
111 SUBCOMMAND = arg
112 subcommand_pos = i
113 break
114
115 subcommand_args = args[subcommand_pos:]
116
117 if SUBCOMMAND:
118
119 (options, args) = parser.parse_args(args[:subcommand_pos])
120
121 #
122 # timestamps
123 #
124 TZ = pytz.timezone(one_value(CONN.cursor(), "SELECT value FROM settings WHERE name = 'timezone'"))
125 UTC = pytz.timezone('UTC')
126
127 BOT = dateutil.parser.parse(one_value(CONN.cursor(), "SELECT value FROM settings WHERE name = 'bot'")).replace(tzinfo=TZ)
128 DEBUG('BOT = %s' % (BOT))
129
130 BOT_ISO = BOT.strftime('%Y-%m-%d %H:%M:%S')
131 DEBUG('BOT_ISO = %s' % (BOT_ISO))
132
133 if options.now == 'now':
134 NOW = datetime.datetime.now(TZ)
135 else:
136 NOW = dateutil.parser.parse(options.now).replace(tzinfo=TZ)
137
138 DEBUG('NOW = %s' % (NOW))
139
140 UTCNOW = NOW.astimezone(UTC)
141 DEBUG('UTCNOW = %s' % (UTCNOW))
142
143 UTCNOW_ISO = UTCNOW.strftime('%Y-%m-%d %H:%M:%S')
144 DEBUG('UTCNOW_ISO = %s' % (UTCNOW_ISO))
145
146 #TODAY = NOW.replace(hour=0, minute=0, second=0, microsecond=0, tzinfo=None)
147 TODAY = NOW.replace(hour=0, minute=0, second=0, microsecond=0)
148 DEBUG('TODAY = %s' % (TODAY))
149
150 TODAY_ISO = TODAY.strftime('%Y-%m-%d %H:%M:%S')
151 DEBUG('TODAY_ISO = %s' % (TODAY_ISO))
152
153 TOMORROW = TODAY + datetime.timedelta(days=1)
154 DEBUG('TOMORROW = %s' % (TOMORROW))
155
156 TOMORROW_ISO = TOMORROW.strftime('%Y-%m-%d %H:%M:%S')
157 DEBUG('TOMORROW_ISO = %s' % (TOMORROW_ISO))
158
159 YESTERDAY = TODAY - datetime.timedelta(days=1)
160 DEBUG('YESTERDAY = %s' % (YESTERDAY))
161
162 YESTERDAY_ISO = YESTERDAY.strftime('%Y-%m-%d %H:%M:%S')
163 DEBUG('YESTERDAY_ISO = %s' % (YESTERDAY_ISO))
164
165 CURR_BACKUP_DAY = backup_day(NOW)
166 DEBUG('CURR_BACKUP_DAY = %s' % (CURR_BACKUP_DAY))
167
168 CURR_BACKUP_DAY_ISO = CURR_BACKUP_DAY.strftime('%Y-%m-%d %H:%M:%S')
169 DEBUG('CURR_BACKUP_DAY_ISO = %s' % (CURR_BACKUP_DAY_ISO))
170
171 NEXT_BACKUP_DAY = CURR_BACKUP_DAY + datetime.timedelta(days=1)
172 DEBUG('NEXT_BACKUP_DAY = %s' % (NEXT_BACKUP_DAY))
173
174 NEXT_BACKUP_DAY_ISO = NEXT_BACKUP_DAY.strftime('%Y-%m-%d %H:%M:%S')
175 DEBUG('NEXT_BACKUP_DAY_ISO = %s' % (NEXT_BACKUP_DAY_ISO))
176
177 PREV_BACKUP_DAY = CURR_BACKUP_DAY - datetime.timedelta(days=1)
178 DEBUG('PREV_BACKUP_DAY = %s' % (PREV_BACKUP_DAY))
179
180 PREV_BACKUP_DAY_ISO = PREV_BACKUP_DAY.strftime('%Y-%m-%d %H:%M:%S')
181 DEBUG('PREV_BACKUP_DAY_ISO = %s' % (PREV_BACKUP_DAY_ISO))
182
183 TIMESTAMP = NOW.replace(second=0, microsecond=0)
184 DEBUG('TIMESTAMP = %s' % (TIMESTAMP))
185
186 TIMESTAMP_ISO = TIMESTAMP.strftime('%Y-%m-%d %H:%M:%S')
187 DEBUG('TIMESTAMP_ISO = %s' % (TIMESTAMP_ISO))
188
189 UTCTIMESTAMP = UTCNOW.replace(second=0, microsecond=0)
190 DEBUG('UTCTIMESTAMP = %s' % (UTCTIMESTAMP))
191
192 UTCTIMESTAMP_ISO = UTCTIMESTAMP.strftime('%Y-%m-%d %H:%M:%S')
193 DEBUG('UTCTIMESTAMP_ISO = %s' % (UTCTIMESTAMP_ISO))
194
195
196 #
197 # SUBCOMMAND
198 #
199 if( not SUBCOMMAND ):
200 parser.print_help()
201 sys.exit(1)
202
203 DEBUG('SUBCOMMAND=%s' % (SUBCOMMAND))
204
205 if hasattr(subcommands, SUBCOMMAND):
206 exec 'subcommands.' + SUBCOMMAND + '.run(subcommand_args, locals())'
207 else:
208 ERROR('Invalid subcommand \'%s\'.' % (SUBCOMMAND))
209
210
Note: See TracBrowser for help on using the repository browser.