source: trunk/Monitoring/Watch/watch @ 854

Last change on this file since 854 was 854, checked in by jripsl, 11 years ago

add JSON deserialization, mail notification, logging.

  • Property svn:executable set to *
File size: 3.2 KB
Line 
1#!/usr/bin/env python
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
15#from smon import dao
16import pika
17import base64
18import json
19import sys
20import traceback
21import smtplib
22from email.mime.text import MIMEText
23
24class Watcher():
25        message_code_action_mapping = {"0000":["log","mail"],"1000":["log"],"2000":["log"],"3000":["log"],"9000":["log"],"9999":["log","mail"]}
26
27        @classmethod
28        def get_fake_progress_messages(cls):
29                pass
30
31        @classmethod
32        def start(cls):
33                #dao.insert_progress_messages(cls.get_fake_progress_messages())
34                pass
35
36        @classmethod
37        def stop(cls):
38                pass
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
90def main():
91
92        """
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
102        SMON.init_singleton()
103        """
104
105        connection = pika.BlockingConnection(pika.ConnectionParameters( host='localhost'))
106        channel = connection.channel()
107
108        #channel.queue_declare(queue='myqueue')
109
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        """
136        SMON.free_singleton()
137        """
138
139if __name__ == '__main__':
140        try:
141                main()
142
143                sys.exit(0)
144
145        except Exception, e:
146
147                traceback.print_exc()
148
149                sys.exit(1)
Note: See TracBrowser for help on using the repository browser.