[854] | 1 | #!/usr/bin/env python |
---|
[840] | 2 | # -*- coding: ISO-8859-1 -*- |
---|
| 3 | |
---|
| 4 | ################################## |
---|
| 5 | # @program smon |
---|
| 6 | # @description simulation monitor |
---|
| 7 | # @copyright Copyright â(c)2009 Centre National de la Recherche Scientifique CNRS. |
---|
| 8 | # All Rights Reservedâ |
---|
| 9 | # @svn_file $Id: watcher 2545 2013-02-01 09:58:10Z jripsl $ |
---|
| 10 | # @version $Rev: 2545 $ |
---|
| 11 | # @lastrevision $Date: 2013-02-01 10:58:10 +0100 (Fri, 01 Feb 2013) $ |
---|
| 12 | # @license CeCILL (http://dods.ipsl.jussieu.fr/jripsl/smon/LICENSE) |
---|
| 13 | ################################## |
---|
| 14 | |
---|
[854] | 15 | #from smon import dao |
---|
| 16 | import pika |
---|
| 17 | import base64 |
---|
| 18 | import json |
---|
| 19 | import sys |
---|
| 20 | import traceback |
---|
| 21 | import smtplib |
---|
| 22 | from email.mime.text import MIMEText |
---|
[840] | 23 | |
---|
| 24 | class Watcher(): |
---|
[854] | 25 | message_code_action_mapping = {"0000":["log","mail"],"1000":["log"],"2000":["log"],"3000":["log"],"9000":["log"],"9999":["log","mail"]} |
---|
[840] | 26 | |
---|
| 27 | @classmethod |
---|
| 28 | def get_fake_progress_messages(cls): |
---|
| 29 | pass |
---|
| 30 | |
---|
| 31 | @classmethod |
---|
| 32 | def start(cls): |
---|
[854] | 33 | #dao.insert_progress_messages(cls.get_fake_progress_messages()) |
---|
| 34 | pass |
---|
[840] | 35 | |
---|
| 36 | @classmethod |
---|
| 37 | def stop(cls): |
---|
| 38 | pass |
---|
[854] | 39 | |
---|
| 40 | @classmethod |
---|
| 41 | def add(cls,message): |
---|
| 42 | pass |
---|
| 43 | |
---|
| 44 | @classmethod |
---|
| 45 | def mail_example(cls): |
---|
| 46 | me="jripsl@ipsl.jussieu.fr" |
---|
| 47 | you="jripsl@ipsl.jussieu.fr" |
---|
| 48 | body="Alarm" |
---|
| 49 | object="Supervisor" |
---|
| 50 | |
---|
| 51 | cls.mail(me,you,object,body) |
---|
| 52 | |
---|
| 53 | @classmethod |
---|
| 54 | def mail(cls): |
---|
| 55 | cls.mail_example() |
---|
| 56 | |
---|
| 57 | @classmethod |
---|
| 58 | def send_mail(cls,me,you,object,body): |
---|
| 59 | msg = MIMEText(body) |
---|
| 60 | msg['Subject'] = object |
---|
| 61 | msg['From'] = me |
---|
| 62 | msg['To'] = you |
---|
| 63 | |
---|
| 64 | # Send the message via our own SMTP server, but don't include the # envelope header. |
---|
| 65 | s = smtplib.SMTP('localhost') |
---|
| 66 | s.sendmail(me,[you], msg.as_string()) |
---|
| 67 | s.quit() |
---|
| 68 | |
---|
| 69 | @classmethod |
---|
| 70 | def log(cls,message): |
---|
| 71 | |
---|
| 72 | with open("/home/jripsl/supervisor/log/supervisor.log", "a") as log_file: |
---|
| 73 | log_file.write("%s %s\n"%(message["code"],message["jobid"])) |
---|
| 74 | |
---|
| 75 | @classmethod |
---|
| 76 | def execActions(cls,message): |
---|
| 77 | |
---|
| 78 | message_code=message["code"] |
---|
| 79 | |
---|
| 80 | for action in cls.message_code_action_mapping[message_code]: |
---|
| 81 | proc_name=action |
---|
| 82 | |
---|
| 83 | try: |
---|
| 84 | getattr(cls, proc_name)(message) |
---|
| 85 | except Exception,e: |
---|
| 86 | traceback.print_exc() |
---|
| 87 | |
---|
| 88 | raise Exception("WATCH-ERR002","procedure error (%s,%s)"%(proc_name,str(e))) |
---|
| 89 | |
---|
[840] | 90 | def main(): |
---|
| 91 | |
---|
[854] | 92 | """ |
---|
[840] | 93 | # parse args |
---|
| 94 | parser = argparse.ArgumentParser(prog='watcher') |
---|
| 95 | parser.add_argument('-v', dest='verbose',required=False,action='store_true') |
---|
| 96 | args = parser.parse_args() |
---|
| 97 | |
---|
| 98 | # check |
---|
| 99 | if not os.path.exists(SMON.smon_home): |
---|
| 100 | sys.exit(1) |
---|
| 101 | |
---|
[841] | 102 | SMON.init_singleton() |
---|
[854] | 103 | """ |
---|
[840] | 104 | |
---|
[854] | 105 | connection = pika.BlockingConnection(pika.ConnectionParameters( host='localhost')) |
---|
| 106 | channel = connection.channel() |
---|
[840] | 107 | |
---|
[854] | 108 | #channel.queue_declare(queue='myqueue') |
---|
[840] | 109 | |
---|
[854] | 110 | print ' [*] Waiting for messages. To exit press CTRL+C' |
---|
| 111 | |
---|
| 112 | def callback(ch, method, properties, raw_msg): |
---|
| 113 | # |
---|
| 114 | #return |
---|
| 115 | |
---|
| 116 | base64_decoded_msg=base64.b64decode(raw_msg) |
---|
| 117 | |
---|
| 118 | # debug |
---|
| 119 | #print " [x] Received %s" % raw_msg |
---|
| 120 | #print " [x] Received %s (uudecoded)" % base64_decoded_msg |
---|
| 121 | |
---|
| 122 | try: |
---|
| 123 | message=json.loads(base64_decoded_msg) |
---|
| 124 | except Exception,e: |
---|
| 125 | print base64_decoded_msg |
---|
| 126 | |
---|
| 127 | # message code based action |
---|
| 128 | Watcher.execActions(message) |
---|
| 129 | |
---|
| 130 | channel.basic_consume(callback, queue='myqueue', no_ack=True) |
---|
| 131 | |
---|
| 132 | channel.start_consuming() |
---|
| 133 | |
---|
| 134 | |
---|
| 135 | """ |
---|
[841] | 136 | SMON.free_singleton() |
---|
[854] | 137 | """ |
---|
[840] | 138 | |
---|
| 139 | if __name__ == '__main__': |
---|
| 140 | try: |
---|
| 141 | main() |
---|
| 142 | |
---|
| 143 | sys.exit(0) |
---|
| 144 | |
---|
| 145 | except Exception, e: |
---|
| 146 | |
---|
[854] | 147 | traceback.print_exc() |
---|
[840] | 148 | |
---|
| 149 | sys.exit(1) |
---|