<s> import xmlrpclib <EOL> import os <EOL> from subprocess import Popen , PIPE <EOL> from time import sleep <EOL> import smtplib <EOL> from email . mime . text import MIMEText <EOL> import logging <EOL> import logging . handlers <EOL> class VistrailWatcher ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , server , email_addresses , root_virtual_display = None ) : <EOL> self . server = server <EOL> self . root_virtual_display = root_virtual_display <EOL> self . email_addresses = email_addresses <EOL> self . sender = "<STR_LIT>" % server <EOL> self . proxies = { } <EOL> self . is_down = { } <EOL> self . logger = self . make_logger ( "<STR_LIT>" ) <EOL> proc1 = Popen ( [ "<STR_LIT>" , "<STR_LIT>" ] , stdout = PIPE ) <EOL> proc2 = Popen ( [ "<STR_LIT>" , "<STR_LIT>" ] , stdin = proc1 . stdout , stdout = PIPE ) <EOL> proc3 = Popen ( [ "<STR_LIT>" , "<STR_LIT>" ] , stdin = proc2 . stdout , stdout = PIPE ) <EOL> out , err = proc3 . communicate ( ) <EOL> out = out . strip ( ) . split ( '<STR_LIT:\n>' ) <EOL> self . ports = [ int ( x ) - <NUM_LIT:1> for x in out [ <NUM_LIT:1> : ] ] <EOL> for port in self . ports : <EOL> self . proxies [ port ] = xmlrpclib . ServerProxy ( "<STR_LIT>" % ( self . server , port ) ) <EOL> self . is_down [ port ] = False <EOL> def make_logger ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> logger = logging . getLogger ( "<STR_LIT>" ) <EOL> handler = logging . handlers . RotatingFileHandler ( filename , <EOL> maxBytes = <NUM_LIT> * <NUM_LIT> , <EOL> backupCount = <NUM_LIT:5> ) <EOL> handler . setFormatter ( logging . Formatter ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> handler . setLevel ( logging . DEBUG ) <EOL> logger . setLevel ( logging . DEBUG ) <EOL> logger . addHandler ( handler ) <EOL> return logger <EOL> def send_email ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> if not self . email_addresses : <EOL> return <EOL> msg = MIMEText ( msg ) <EOL> msg [ '<STR_LIT>' ] = '<STR_LIT>' % self . server <EOL> msg [ '<STR_LIT>' ] = self . sender <EOL> msg [ '<STR_LIT>' ] = '<STR_LIT:U+002CU+0020>' . join ( self . email_addresses ) <EOL> s = smtplib . SMTP ( '<STR_LIT:localhost>' ) <EOL> s . sendmail ( self . sender , self . email_addresses , msg . as_string ( ) ) <EOL> s . quit ( ) <EOL> def restart_instance ( self , port ) : <EOL> """<STR_LIT>""" <EOL> self . logger . info ( "<STR_LIT>" % port ) <EOL> script = os . path . join ( "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> instance_virtual_display = self . root_virtual_display + self . ports . index ( port ) + <NUM_LIT:1> <EOL> args = [ script , "<STR_LIT>" % instance_virtual_display , <EOL> self . server , str ( port + <NUM_LIT:1> ) , '<STR_LIT:0>' , '<STR_LIT:0>' ] <EOL> try : <EOL> Popen ( args ) <EOL> sleep ( <NUM_LIT:20> ) <EOL> except Exception , e : <EOL> self . logger . error ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % ( instance_virtual_display , port ) <EOL> self . logger . error ( str ( e ) ) <EOL> def watch ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . ports : <EOL> self . logger . info ( "<STR_LIT>" ) <EOL> print "<STR_LIT>" <EOL> return <EOL> self . logger . info ( "<STR_LIT>" % str ( self . ports ) ) <EOL> while <NUM_LIT:1> : <EOL> for port in self . ports : <EOL> try : <EOL> if not self . proxies [ port ] . try_ping ( ) : <EOL> if not self . is_down [ port ] : <EOL> self . send_email ( "<STR_LIT>" % ( self . server , port ) ) <EOL> self . logger . info ( "<STR_LIT>" % ( self . server , port ) ) <EOL> self . restart_instance ( port ) <EOL> self . is_down [ port ] = True <EOL> else : <EOL> if self . is_down [ port ] : <EOL> self . logger . info ( "<STR_LIT>" % ( self . server , port ) ) <EOL> self . is_down [ port ] = False <EOL> except Exception , e : <EOL> if not self . is_down [ port ] : <EOL> self . send_email ( "<STR_LIT>" % ( self . server , port ) ) <EOL> self . logger . info ( "<STR_LIT>" % ( self . server , port ) ) <EOL> self . restart_instance ( port ) <EOL> self . is_down [ port ] = True <EOL> sleep ( <NUM_LIT> * <NUM_LIT:5> ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> vt_watcher = VistrailWatcher ( "<STR_LIT>" , [ "<STR_LIT>" ] , root_virtual_display = <NUM_LIT:6> ) <EOL> vt_watcher . watch ( ) </s>
