<s> from mk2 . plugins import Plugin <EOL> from mk2 . events import ServerOutput , StatPlayerCount , ServerStop , ServerEvent , Event <EOL> class Check ( object ) : <EOL> alive = True <EOL> timeout = <NUM_LIT:0> <EOL> time = <NUM_LIT:0> <EOL> warn = <NUM_LIT:0> <EOL> def __init__ ( self , parent , ** kw ) : <EOL> self . dispatch = parent . dispatch <EOL> self . console = parent . console <EOL> for k , v in kw . items ( ) : <EOL> setattr ( self , k , v ) <EOL> def check ( self ) : <EOL> if self . alive : <EOL> self . alive = False <EOL> return True <EOL> return False <EOL> def step ( self ) : <EOL> if self . check ( ) : <EOL> return <EOL> self . time += <NUM_LIT:1> <EOL> if self . timeout and self . time == self . timeout : <EOL> timeout = "<STR_LIT>" . format ( self . timeout ) <EOL> self . console ( "<STR_LIT>" . format ( self . message . format ( timeout = timeout ) ) ) <EOL> self . dispatch ( ServerEvent ( cause = "<STR_LIT>" + self . event [ <NUM_LIT:0> ] , <EOL> data = "<STR_LIT>" + self . event [ <NUM_LIT:1> ] . format ( timeout = timeout ) , <EOL> priority = <NUM_LIT:1> ) ) <EOL> self . dispatch ( ServerStop ( reason = self . stop_reason , respawn = True ) ) <EOL> elif self . warn and self . time == self . warn : <EOL> if self . timeout : <EOL> self . console ( "<STR_LIT>" . format ( self . warning , self . timeout - self . time ) ) <EOL> else : <EOL> self . console ( self . warning ) <EOL> time = "<STR_LIT>" . format ( self . warn ) <EOL> self . dispatch ( ServerEvent ( cause = "<STR_LIT>" + self . event [ <NUM_LIT:0> ] , <EOL> data = "<STR_LIT>" + self . event [ <NUM_LIT:1> ] . format ( timeout = time ) , <EOL> priority = <NUM_LIT:1> ) ) <EOL> else : <EOL> if self . timeout : <EOL> self . console ( "<STR_LIT>" . format ( self . warning , self . timeout - self . time ) ) <EOL> else : <EOL> self . console ( self . warning ) <EOL> def reset ( self ) : <EOL> self . alive = True <EOL> self . time = <NUM_LIT:0> <EOL> class Monitor ( Plugin ) : <EOL> crash_enabled = Plugin . Property ( default = True ) <EOL> crash_timeout = Plugin . Property ( default = <NUM_LIT:3> ) <EOL> crash_warn = Plugin . Property ( default = <NUM_LIT:0> ) <EOL> crash_unknown_cmd_message = Plugin . Property ( default = "<STR_LIT>" ) <EOL> crash_check_command = Plugin . Property ( default = "<STR_LIT>" ) <EOL> oom_enabled = Plugin . Property ( default = True ) <EOL> ping_enabled = Plugin . Property ( default = True ) <EOL> ping_timeout = Plugin . Property ( default = <NUM_LIT:3> ) <EOL> ping_warn = Plugin . Property ( default = <NUM_LIT:0> ) <EOL> pcount_enabled = Plugin . Property ( default = False ) <EOL> pcount_timeout = Plugin . Property ( default = <NUM_LIT:3> ) <EOL> pcount_warn = Plugin . Property ( default = <NUM_LIT:0> ) <EOL> def setup ( self ) : <EOL> do_step = False <EOL> self . checks = { } <EOL> if self . oom_enabled : <EOL> self . register ( self . handle_oom , ServerOutput , level = '<STR_LIT>' , pattern = '<STR_LIT>' ) <EOL> if self . crash_enabled : <EOL> do_step = True <EOL> self . checks [ '<STR_LIT>' ] = Check ( self , name = "<STR_LIT>" , <EOL> timeout = self . crash_timeout , <EOL> warn = self . crash_warn , <EOL> message = "<STR_LIT>" , <EOL> warning = "<STR_LIT>" , <EOL> event = ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> stop_reason = "<STR_LIT>" ) <EOL> if self . ping_enabled : <EOL> self . register ( self . handle_ping , StatPlayerCount ) <EOL> do_step = True <EOL> self . checks [ '<STR_LIT>' ] = Check ( self , name = "<STR_LIT>" , <EOL> timeout = self . ping_timeout , <EOL> warn = self . ping_warn , <EOL> message = "<STR_LIT>" , <EOL> warning = "<STR_LIT>" , <EOL> event = ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> stop_reason = "<STR_LIT>" ) <EOL> if self . pcount_enabled : <EOL> self . register ( self . handle_pcount , StatPlayerCount ) <EOL> do_step = True <EOL> self . checks [ '<STR_LIT>' ] = Check ( self , name = "<STR_LIT>" , <EOL> timeout = self . pcount_timeout , <EOL> warn = self . pcount_warn , <EOL> message = "<STR_LIT>" , <EOL> warning = "<STR_LIT>" , <EOL> event = ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> stop_reason = "<STR_LIT>" ) <EOL> self . do_step = do_step <EOL> def server_started ( self , event ) : <EOL> self . reset_counts ( ) <EOL> if self . do_step : <EOL> self . repeating_task ( self . step , <NUM_LIT> ) <EOL> def load_state ( self , state ) : <EOL> self . server_started ( None ) <EOL> def step ( self , * a ) : <EOL> for c in self . checks . values ( ) : <EOL> c . step ( ) <EOL> if self . crash_enabled : <EOL> self . register ( self . handle_crash_ok , ServerOutput , <EOL> pattern = self . crash_unknown_cmd_message , <EOL> track = False ) <EOL> self . send ( self . crash_check_command ) <EOL> def reset_counts ( self ) : <EOL> for c in self . checks . values ( ) : <EOL> c . reset ( ) <EOL> def handle_crash_ok ( self , event ) : <EOL> self . checks [ "<STR_LIT>" ] . reset ( ) <EOL> return Event . EAT | Event . UNREGISTER <EOL> def handle_oom ( self , event ) : <EOL> self . console ( '<STR_LIT>' ) <EOL> self . dispatch ( ServerEvent ( cause = '<STR_LIT>' , <EOL> data = "<STR_LIT>" , <EOL> priority = <NUM_LIT:1> ) ) <EOL> self . dispatch ( ServerStop ( reason = '<STR_LIT>' , respawn = True ) ) <EOL> def handle_ping ( self , event ) : <EOL> if event . source == '<STR_LIT>' : <EOL> self . checks [ "<STR_LIT>" ] . reset ( ) <EOL> def handle_pcount ( self , event ) : <EOL> if event . players_current > <NUM_LIT:0> : <EOL> self . checks [ "<STR_LIT>" ] . reset ( ) <EOL> else : <EOL> self . checks [ "<STR_LIT>" ] . alive = False </s>
