Changeset 854 for trunk/Monitoring/Watch


Ignore:
Timestamp:
04/26/13 16:57:16 (11 years ago)
Author:
jripsl
Message:

add JSON deserialization, mail notification, logging.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Monitoring/Watch/watch

    r841 r854  
    1 #!/usr/bin/python -u 
     1#!/usr/bin/env python 
    22# -*- coding: ISO-8859-1 -*- 
    33 
     
    1313################################## 
    1414 
    15 from smon import dao 
     15#from smon import dao 
     16import pika 
     17import base64 
     18import json 
     19import sys 
     20import traceback 
     21import smtplib 
     22from email.mime.text import MIMEText 
    1623 
    1724class Watcher(): 
     25        message_code_action_mapping = {"0000":["log","mail"],"1000":["log"],"2000":["log"],"3000":["log"],"9000":["log"],"9999":["log","mail"]} 
    1826 
    1927        @classmethod 
     
    2331        @classmethod 
    2432        def start(cls): 
    25                 dao.insert_progress_messages(cls.get_fake_progress_messages()) 
     33                #dao.insert_progress_messages(cls.get_fake_progress_messages()) 
     34                pass 
    2635 
    2736        @classmethod 
    2837        def stop(cls): 
    2938                pass 
    30   
     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 
    3190def main(): 
    3291 
     92        """ 
    3393        # parse args 
    3494        parser = argparse.ArgumentParser(prog='watcher') 
     
    41101 
    42102        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() 
    43133 
    44134 
    45  
     135        """ 
    46136        SMON.free_singleton() 
     137        """ 
    47138 
    48139if __name__ == '__main__': 
     
    54145        except Exception, e: 
    55146 
    56                 #traceback.print_exc() 
     147                traceback.print_exc() 
    57148 
    58149                sys.exit(1) 
Note: See TracChangeset for help on using the changeset viewer.