source: trunk/Monitoring/Analyze/analyze @ 877

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

Fix bug (incorrect simulation_id was used in message table).

  • 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: analyzer 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 sys
16import signal
17import traceback
18import smtplib
19from email.mime.text import MIMEText
20import time;
21from datetime import datetime
22
23
24# line below is to include "smon" package in the search path
25sys.path.append("/home/jripsl/snapshot/Monitoring")
26
27import smon.repo_io as repo_io
28import smon.types as types
29
30
31
32class CheckList():
33        max_time_between_msg=10 # unit => seconds
34
35        @classmethod
36        def datetime_to_epoch(cls,datetime):
37                epoch = time.mktime(time.strptime(datetime, "%d.%m.%Y %H:%M:%S")).time(); # assuming datetime format is "29.08.2011 11:05:02"
38                return epoch
39
40        @classmethod
41        def msg_timeout(cls,message):
42
43                msg_time=cls.datetime_to_epoch(message.timestamp)
44                current_time=time.time()
45
46                diff=current_time-msg_time
47
48                # debug
49                print "diff=%s"%diff
50
51                if diff>cls.max_time_between_msg:
52
53                        return True
54                else:
55                        return False
56
57        @classmethod
58        def C0001(cls):
59                """
60                description
61                  check heartbeat (currently, heartbeat is implemented using simulation progress messages).
62                  if progress messages suddently stops, it is likely that the simulation was Killed or
63                  a Segfault occurs. In such case, we inform the other components (failover, prodiguer GUI..) by
64                  changing the simulation status
65                """
66
67
68                for simulation in repo_io.get_running_simulations():
69
70                        print "checking heartbeat for '%s'"%simulation.name
71
72                        try:
73
74                                message=repo_io.retrieve_last_message(simulation)
75
76                        except types.MessageNotFoundException, e:
77
78                                continue
79
80
81                        if msg_timeout(message):
82
83                                simulation.status="error"
84
85                                repo_io.update_simulation_status(simulation)
86
87                                print "heartbeat NOK (simulation status set to 'error')"%simulation.name
88
89
90                        else:
91                                print "heartbeat OK"%simulation.name
92
93class Analyzer():
94
95        @classmethod
96        def start(cls):
97                repo_io.init() # open DB connection
98
99                Analyzer.main()
100
101        @classmethod
102        def stop(cls):
103                repo_io.free() # close DB connection
104
105        @classmethod
106        def main(self):
107
108                """
109                # parse args
110                parser = argparse.ArgumentParser(prog='analyzer')
111                parser.add_argument('-v', dest='verbose',required=False,action='store_true')
112                args = parser.parse_args()
113
114                # check
115                if not os.path.exists(SMON.smon_home):
116                        sys.exit(1)
117
118                SMON.init_singleton()
119                """
120
121                print ' [*] Analyzer running. To exit press CTRL+C'
122
123                while True:
124
125                        print "checking simulations heartbeats"
126
127                        CheckList.C0001()
128
129
130                        time.sleep(1)
131
132                """
133                SMON.free_singleton()
134                """
135
136def signal_handler(signal, frame):
137                print 'You pressed Ctrl+C!'
138
139                Analyzer.stop()
140
141                sys.exit(0)
142
143if __name__ == '__main__':
144
145        signal.signal(signal.SIGINT, signal_handler)
146
147        try:
148                Analyzer.start()
149
150                sys.exit(0)
151
152        except Exception, e:
153
154                traceback.print_exc()
155
156                sys.exit(1)
Note: See TracBrowser for help on using the repository browser.