source: trunk/Monitoring/Analyze/analyze @ 879

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

Fix heartbeat test.

  • Property svn:executable set to *
File size: 3.4 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
21import 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=20 # unit => seconds
34
35        @classmethod
36        def msg_timeout(cls,message):
37
38                # get current epoch
39                current_epoch=time.time()
40
41                # get msg epoch
42                msg_time=time.strptime(str(message.crea_date), "%Y-%m-%d %H:%M:%S.%f")
43                msg_epoch=time.mktime(msg_time)
44
45                diff =  current_epoch - msg_epoch
46
47                # debug
48                #print "cur=%i,ms=%s"%(current_epoch,message.crea_date)
49
50                # debug
51                #print "diff=%i"%int(diff)
52
53                if diff>cls.max_time_between_msg:
54
55                        return True
56                else:
57                        return False
58
59        @classmethod
60        def C0001(cls):
61                """
62                description
63                  check heartbeat (currently, heartbeat is implemented using simulation progress messages).
64                  if progress messages suddently stops, it is likely that the simulation was Killed or
65                  a Segfault occurs. In such case, we inform the other components (failover, prodiguer GUI..) by
66                  changing the simulation status
67                """
68
69
70                for simulation in repo_io.get_running_simulations():
71
72                        print "hhh"
73
74                        print "checking heartbeat ('%s')"%simulation.name
75
76                        try:
77                                message=repo_io.retrieve_last_message(simulation)
78
79                                # debug
80                                #print "found"
81
82                        except types.MessageNotFoundException, e:
83                                # when we are here, it mean we are in the interval when a new simulation have just been inserted but the corresponding message have not been inserted yet
84
85                                print "no message found for simulation ('%s')"%simulation.name
86
87                                continue
88
89
90                        if cls.msg_timeout(message):
91
92                                simulation.status="error"
93
94                                repo_io.update_simulation_status(simulation)
95
96                                print "heartbeat NOK - simulation status set to 'error' (%s)"%simulation.name
97
98
99                        else:
100                                print "heartbeat OK (%s)"%simulation.name
101
102class Analyzer():
103
104        @classmethod
105        def start(cls):
106                repo_io.init() # open DB connection
107
108                Analyzer.main()
109
110        @classmethod
111        def stop(cls):
112                repo_io.free() # close DB connection
113
114        @classmethod
115        def main(self):
116
117                """
118                # parse args
119                parser = argparse.ArgumentParser(prog='analyzer')
120                parser.add_argument('-v', dest='verbose',required=False,action='store_true')
121                args = parser.parse_args()
122
123                # check
124                if not os.path.exists(SMON.smon_home):
125                        sys.exit(1)
126
127                SMON.init_singleton()
128                """
129
130                print ' [*] Analyzer running. To exit press CTRL+C'
131
132                while True:
133
134                        print "checking simulations heartbeats"
135
136                        CheckList.C0001()
137
138
139                        time.sleep(3)
140
141                """
142                SMON.free_singleton()
143                """
144
145def signal_handler(signal, frame):
146                print 'You pressed Ctrl+C!'
147
148                Analyzer.stop()
149
150                sys.exit(0)
151
152if __name__ == '__main__':
153
154        signal.signal(signal.SIGINT, signal_handler)
155
156        try:
157                Analyzer.start()
158
159                sys.exit(0)
160
161        except Exception, e:
162
163                traceback.print_exc()
164
165                sys.exit(1)
Note: See TracBrowser for help on using the repository browser.