diff options
author | Martin Abente <martin.abente.lahaye@gmail.com> | 2011-03-23 13:25:45 (GMT) |
---|---|---|
committer | Martin Abente <martin.abente.lahaye@gmail.com> | 2011-03-23 17:35:33 (GMT) |
commit | 8c9f737f995e4d232e5c58d2f09f46f46aca1c01 (patch) | |
tree | 4ce2d31c30891c879a1283844cdb13b8e866eac1 | |
parent | fb1a5933a81871baf10cbe44e1273dedaad903f4 (diff) |
Add mailer script
As a first approach, feedback reports will be sent to
a special mailing list.
This will help us to check the reports more efficiently.
Signed-off-by: Martin Abente <martin.abente.lahaye@gmail.com>
-rw-r--r-- | config.ini.example | 9 | ||||
-rw-r--r-- | reports_cron | 1 | ||||
-rwxr-xr-x | reports_mailer.py | 163 | ||||
-rw-r--r-- | sugar-fbserver.spec | 11 |
4 files changed, 182 insertions, 2 deletions
diff --git a/config.ini.example b/config.ini.example index 517ba70..eaf929d 100644 --- a/config.ini.example +++ b/config.ini.example @@ -9,3 +9,12 @@ cert_file: /home/tch/Devel/feedback-server/pkey-cert.pem [feedback] reports_path: /home/tch/Devel/feedback-server/reports + +[mailer] +enabled: False +reporter_address: feedbackreporter@activitycentral.com +list_address: feedbackreports@activitycentral.com +smtp_user: feedbackreporter +smtp_password: 123456 +smtp_server: smtp.activitycentral.com +smtp_port: 25 diff --git a/reports_cron b/reports_cron new file mode 100644 index 0000000..184cece --- /dev/null +++ b/reports_cron @@ -0,0 +1 @@ +/opt/sugar-fbserver/reports_mailer.py diff --git a/reports_mailer.py b/reports_mailer.py new file mode 100755 index 0000000..a516d53 --- /dev/null +++ b/reports_mailer.py @@ -0,0 +1,163 @@ +#!/usr/bin/env python + +# Copyright (C) 2011, Martin Abente +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/> + +import os +import sys +import json +import time +import smtplib +from email.mime.text import MIMEText +from email.mime.multipart import MIMEMultipart +from ConfigParser import ConfigParser + +REPORT_INFO = 'report' +EMAILED_MARK = 'emailed' +REPORTS_PATH = '' + +REPORTER_ADDRESS = '' +LIST_ADDRESS = '' + +SMTP_USER = '' +SMTP_PASSWORD = '' +SMTP_SERVER = '' +SMTP_PORT = 0 + +CUSTOM_SUBJECT = 'custom.feedback.report' +UNKNOWN_SUBJECT = '???' + +def send_email(message): + mail_server = smtplib.SMTP(SMTP_SERVER, SMTP_PORT) + mail_server.ehlo() + mail_server.starttls() + mail_server.ehlo() + mail_server.login(SMTP_USER, SMTP_PASSWORD) + mail_server.sendmail(REPORTER_ADDRESS, [LIST_ADDRESS], message.as_string()) + mail_server.quit() + +def prepare_message(subject, body, report_path): + message = MIMEMultipart() + message['Subject'] = subject + message['From'] = REPORTER_ADDRESS + message['To'] = LIST_ADDRESS + message.preamble = 'Feedback Report' + + body_attachment = MIMEText(body.encode('utf-8'), 'plain', 'utf-8') + message.attach(body_attachment) + + for log_path in os.listdir(report_path): + abs_log_path = os.path.join(report_path, log_path) + + if not os.path.isfile(abs_log_path): + continue + + log_file = open(abs_log_path, 'r') + payload = log_file.read().encode('utf-8') + attachment = MIMEText(payload, 'plain', 'utf-8') + log_file.close() + + attachment.add_header('Content-Disposition', + 'attachment', + filename=log_path) + message.attach(attachment) + + return message + +def generate_body(info, report_info_path): + body = json.dumps(info) + report_time = time.ctime(os.path.getctime(report_info_path)) + body += '\n\nReported at %s.' % report_time + + return body + +def generate_subject(info): + if 'serial_number' in info: + subject = CUSTOM_SUBJECT + else: + subject = UNKNOWN_SUBJECT + if len(info.keys()) > 0: + subject = info.keys()[0] + + return ('[%s]' % subject) + +def check_reports(): + for report in os.listdir(REPORTS_PATH): + report_path = os.path.join(REPORTS_PATH, report) + + if os.path.isdir(report_path): + report_info_path = os.path.join(report_path, REPORT_INFO) + emailed_mark_path = os.path.join(report_path, EMAILED_MARK) + + if not os.path.exists(report_info_path) or \ + os.path.exists(emailed_mark_path): + continue + + report_info_file = open(report_info_path, 'r') + report_info = json.load(report_info_file) + report_info_file.close() + + body = generate_body(report_info, report_info_path) + subject = generate_subject(report_info) + message = prepare_message(subject, body, report_path) + + try: + send_email(message) + except Exception, e: + print str(e) + continue + + emailed_mark_file = open(emailed_mark_path, 'w') + emailed_mark_file.close() + print 'Marked %s.' % emailed_mark_path + +def load_configuration(config): + global REPORTS_PATH, REPORTER_ADDRESS, LIST_ADDRESS, \ + SMTP_USER, SMTP_PASSWORD, SMTP_SERVER, SMTP_PORT + + REPORTS_PATH = config.get('feedback', 'reports_path') + + if not os.path.exists(REPORTS_PATH): + print 'Reports directory (%s) does not exists.' % REPORTS_PATH + sys.exit(-1) + + REPORTER_ADDRESS = config.get('mailer', 'reporter_address') + LIST_ADDRESS = config.get('mailer', 'list_address') + SMTP_USER = config.get('mailer', 'smtp_user') + SMTP_PASSWORD = config.get('mailer', 'smtp_password') + SMTP_SERVER = config.get('mailer', 'smtp_server') + SMTP_PORT = config.get('mailer', 'smtp_port') + +def main(): + config = ConfigParser() + script_path = os.path.abspath(__file__) + config_path = os.path.join(os.path.dirname(script_path), 'config.ini') + + if len(config.read(config_path)) == 0: + print 'Can\'t load configuration file.' + sys.exit(-1) + + enabled = config.getboolean('mailer', 'enabled') + if not enabled: + print 'Mailer agent is not enabled.' + sys.exit(-1) + + load_configuration(config) + check_reports() + sys.exit(0) + +if __name__ == "__main__": + main() + diff --git a/sugar-fbserver.spec b/sugar-fbserver.spec index 4523ed8..4a168a8 100644 --- a/sugar-fbserver.spec +++ b/sugar-fbserver.spec @@ -1,6 +1,6 @@ Name: sugar-fbserver -Version: 0.1 -Release: 8 +Version: 0.2 +Release: 1 Vendor: Activity Central Summary: Sugar debugging feedback server Group: Applications/Internet @@ -27,6 +27,9 @@ cp -r * $RPM_BUILD_ROOT/opt/%{name} mkdir -p $RPM_BUILD_ROOT/etc/init.d/ cp fbserverd $RPM_BUILD_ROOT/etc/init.d/ +mkdir -p $RPM_BUILD_ROOT/etc/cron.hourly/ +cp reports_cron $RPM_BUILD_ROOT/etc/cron.hourly/ + # kill packaging rm $RPM_BUILD_ROOT/opt/%{name}/sugar-fbserver.spec rm $RPM_BUILD_ROOT/opt/%{name}/fbserverd @@ -52,9 +55,13 @@ chkconfig --level 345 fbserverd off %dir /opt/%{name} /opt/%{name}/ %attr(755,root,root) /etc/init.d/fbserverd +%attr(755,root,root) /etc/cron.hourly/reports_cron %changelog +* Wed Mar 23 2011 Martin Abente. <martin.abente.lahaye@gmail.com> +- Add feedback mailer script + * Mon Mar 7 2011 Martin Abente. <martin.abente.lahaye@gmail.com> - Add pyOpenSSL dependency |