source: trunk/Monitoring/Watch/watch @ 937

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

Create AMQP queue on supervisor side if not exists already.

  • Property svn:executable set to *
File size: 7.8 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
15import pika
16import base64
17import json
18import sys
19import signal
20import traceback
21import smtplib
22from email.mime.text import MIMEText
23import datetime
24
25# line below is to include "smon" package in the search path
26sys.path.append("/opt/supervisor/Monitoring")
27
28import smon.repo_io as repo_io
29import smon.types
30
31CSTE_BROKER_HOST='cstest-broker.ipsl.jussieu.fr' # cstest
32#CSTE_BROKER_HOST='localhost' # vesg4
33
34class Mail():
35
36        @classmethod
37        def mail_example(cls):
38                me="jripsl@ipsl.jussieu.fr"
39                you="jripsl@ipsl.jussieu.fr"
40                body="Alarm"
41                object="Supervisor"
42
43                cls.mail(me,you,object,body)
44
45        @classmethod
46        def send_mail(cls,me,you,object,body):
47                msg = MIMEText(body)
48                msg['Subject'] = object
49                msg['From'] = me
50                msg['To'] = you
51
52                # Send the message via our own SMTP server, but don't include the # envelope header.
53                s = smtplib.SMTP('localhost')
54                s.sendmail(me,[you], msg.as_string())
55                s.quit()
56
57class Actions():
58
59        @classmethod
60        def store_msg(cls,message):
61
62                try:
63
64                        # the simu exists when we are here (see TAG0001 tag)
65                        s=repo_io.retrieve_simulation(message.simuid)
66
67                        if s is None:
68                                raise Exception("WATCH-ERR102","simulation not found")
69
70                        repo_io.create_message(message,s)
71
72                except:
73                        traceback.print_exc()
74                        raise
75
76        @classmethod
77        def cleanup(cls,message):
78                repo_io.cleanup() # truncate/delete everything
79
80        @classmethod
81        def set_sim_status_to_error(cls,message):
82
83                try:
84
85                        s=repo_io.retrieve_simulation(message.simuid)
86
87                        s.status="error"
88
89                        repo_io.update_simulation_status(s)
90
91                except:
92                        traceback.print_exc()
93                        raise
94
95        @classmethod
96        def set_sim_status_to_complete(cls,message):
97
98                s=repo_io.retrieve_simulation(message.simuid)
99
100                s.status="complete"
101
102                repo_io.update_simulation_status(s)
103
104        @classmethod
105        def crea_sim(cls,message):
106
107                s=repo_io.retrieve_simulation(message.simuid)
108
109                if s is not None:
110                        #repo_io.delete_simulation(name)
111
112                        s.status="running"
113                        repo_io.update_simulation_status(s)
114
115                else:
116                        simulation=smon.types.Simulation(name=message.simuid,status="running")
117
118
119                        repo_io.create_simulation(simulation)
120
121        @classmethod
122        def mail(cls):
123                cls.mail_example()
124
125        @classmethod
126        def print_stdout(cls,message):
127                # used for debug
128
129                """
130                if message.file is not None:
131                        print "%s %s %s %s %s\n"%(message.code,message.jobid,message.command,message.timestamp,message.file)
132                else:
133                        print "%s %s %s %s\n"%(message.code,message.jobid,message.command,message.timestamp)
134                """
135
136                print "%s %s %s %s\n"%(message.code,message.jobid,message.command,message.timestamp)
137                #pass
138
139        @classmethod
140        def log(cls,message):
141                with open("/opt/supervisor/log/supervisor.log", "a") as log_file:
142                        log_file.write("%s %s %s %s %s\n"%(datetime.datetime.now().strftime('%Y%m%d_%H%M%S'), message.code,message.jobid,message.timestamp,message.command))
143
144        @classmethod
145        def execActions(cls,message):
146
147                message_code=message.code
148
149                for action in MessageActionsMapping.mapping[message_code]:
150                        proc_name=action
151
152                        try:
153                                getattr(Actions, proc_name)(message)
154                        except Exception,e:
155                                traceback.print_exc()
156
157                                raise Exception("WATCH-ERR002","procedure error (%s,%s)"%(proc_name,str(e)))
158
159class MessageActionsMapping():
160
161        # debug
162        #
163        # TAG0001: note that crea_sim must be BEFORE store_msg in the list (because when we insert the msg, we need the simu_id)
164        #
165        mapping = { "0000":["crea_sim", "log", "store_msg", "print_stdout"],
166                                "0100":["log", "store_msg", "print_stdout", "set_sim_status_to_complete"],
167                                "1000":["log", "store_msg", "print_stdout"],
168                                "1100":["log", "store_msg", "print_stdout"],
169                                "2000":["log", "store_msg", "print_stdout"],
170                                "3000":["log", "store_msg", "print_stdout"],
171                                "8888":["cleanup"],
172                                "9000":["log", "store_msg", "print_stdout"],
173                                "9999":["log", "store_msg", "print_stdout", "set_sim_status_to_error"] }
174
175        # prod
176        #
177        # TAG0001: note that crea_sim must be BEFORE store_msg in the list (because when we insert the msg, we need the simu_id)
178        #
179        """
180        mapping = { "0000":["crea_sim", "log", "store_msg"],
181                                "0100":["log", "store_msg", "set_sim_status_to_complete"],
182                                "1000":["log", "store_msg"],
183                                "1100":["log", "store_msg"],
184                                "2000":["log", "store_msg"],
185                                "3000":["log", "store_msg"],
186                                "8888":["cleanup"],
187                                "9000":["log", "store_msg", "mail"],
188                                "9999":["log", "store_msg", "set_sim_status_to_error", "mail"] }
189        """
190
191class Watcher():
192
193        @classmethod
194        def start(cls):
195                repo_io.init() # open DB connection
196
197        @classmethod
198        def stop(cls):
199                repo_io.free() # close DB connection
200
201        @classmethod
202        def main(self):
203
204                """
205                # parse args
206                parser = argparse.ArgumentParser(prog='watcher')
207                parser.add_argument('-v', dest='verbose',required=False,action='store_true')
208                args = parser.parse_args()
209
210                # check
211                if not os.path.exists(SMON.smon_home):
212                        sys.exit(1)
213
214                SMON.init_singleton()
215                """
216
217                connection = pika.BlockingConnection(pika.ConnectionParameters(host=CSTE_BROKER_HOST))
218                self.channel = connection.channel()
219
220
221                print ' [*] Waiting for messages. To exit press CTRL+C'
222
223                def callback(ch, method, properties, raw_msg):
224
225                        # msg fmt: body:base64,file:base64 (no JSON here !!!)
226
227
228
229
230                        # first deserialization (no JSON here !!!)
231
232                        fields=raw_msg.split(",")
233
234                        l__tmp_dic={}
235
236                        for field in fields:
237
238                                # debug
239                                #print " [x] Received %s" % field
240
241                                splitted_field=field.split(":")
242
243                                key=splitted_field[0]
244                                val=splitted_field[1]
245
246                                l__tmp_dic[key]=val
247
248
249                        # debug
250                        #print " [x] Received %s (encoded)" % l__tmp_dic["body"]
251
252                       
253                        # base64 decode body
254                        base64_decoded_msg=base64.b64decode(l__tmp_dic["body"])
255
256
257                        # debug
258                        #print " [x] Received %s" % raw_msg
259                        #print " [x] Received %s (uudecoded)" % base64_decoded_msg
260                        #print " [x] Received %s (uudecoded)" % base64_decoded_msg
261
262
263                        # message deserialization
264                        message=None
265                        try:
266                                JSON_msg=json.loads(base64_decoded_msg)
267                                message=smon.types.Message(JSON_msg)      # all JSON object members will be available in smon.types.Message object
268
269                                # non working
270                                #print message.type
271
272                                # working
273                                #print message.code
274
275
276
277                        except Exception,e:
278                                print "ERR009 - exception occurs (exception=%s,msg=%s)"%(str(e),base64_decoded_msg)
279
280                                traceback.print_exc()
281                                raise
282
283
284
285                        # manage config-card file which is attached to the "0000" code message (this file is base64 encoded and need to be unencoded)
286                        #
287                        if "file" in l__tmp_dic:
288
289                                # base64 decode file
290                                base64_decoded_file=base64.b64decode(l__tmp_dic["file"])
291
292                                # add as msg attribute
293                                message.file=base64_decoded_file
294
295
296
297                        # execute actions
298                        try:
299                                # message code based action
300                                Actions.execActions(message)
301
302                        except Exception,e:
303                                print "ERR019 - exception occurs (exception=%s)"%(str(e))
304                                #print "ERR019 - exception occurs (exception=%s,msg=%s)"%(str(e),base64_decoded_msg)
305
306                                traceback.print_exc()
307
308                                raise
309
310
311                        # slow down consumer
312                        #time.sleep(0.5)
313
314                self.channel.queue_declare(queue='myqueue')
315
316                self.channel.basic_consume(callback, queue='myqueue', no_ack=True)
317
318                self.channel.start_consuming()
319
320
321                """
322                SMON.free_singleton()
323                """
324
325def signal_handler(signal, frame):
326                print 'You pressed Ctrl+C!'
327                Watcher.channel.stop_consuming()
328                Watcher.stop()
329                sys.exit(0)
330
331if __name__ == '__main__':
332
333        signal.signal(signal.SIGINT, signal_handler)
334
335        try:
336
337                Watcher.start()
338
339                Watcher.main()
340
341                Watcher.stop()
342
343                sys.exit(0)
344
345        except Exception, e:
346
347                traceback.print_exc()
348
349                sys.exit(1)
Note: See TracBrowser for help on using the repository browser.