<s> """<STR_LIT>""" <EOL> import json <EOL> import logging <EOL> import os . path <EOL> import re <EOL> import sys <EOL> import time <EOL> from datetime import datetime <EOL> from gzip import GzipFile <EOL> class BackupNinja ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , agent_config , checks_logger , raw_config ) : <EOL> self . agent_config = agent_config <EOL> self . checks_logger = checks_logger <EOL> self . raw_config = raw_config <EOL> self . log_parser = re . compile ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . config = self . raw_config . get ( '<STR_LIT>' , { } ) <EOL> self . main_log = self . config . get ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . rotated_log_type = self . config . get ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> def get_default_data ( self ) : <EOL> return { <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT:error>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : - <NUM_LIT:1> <EOL> } <EOL> def get_latest_status ( self , now , log_file ) : <EOL> data = self . get_default_data ( ) <EOL> for line in log_file . readlines ( ) : <EOL> match = self . log_parser . match ( line ) <EOL> if match : <EOL> entry_datestr = match . group ( <NUM_LIT:1> ) <EOL> entry_datetime = datetime . strptime ( <EOL> '<STR_LIT>' . format ( now . year , entry_datestr ) , <EOL> '<STR_LIT>' ) <EOL> diff_date = now - entry_datetime <EOL> total_seconds = ( <EOL> diff_date . seconds + diff_date . days * <NUM_LIT> * <NUM_LIT> ) <EOL> data [ '<STR_LIT>' ] = int ( round ( total_seconds / <NUM_LIT> ) ) <EOL> data [ '<STR_LIT>' ] = match . group ( <NUM_LIT:2> ) <EOL> data [ '<STR_LIT>' ] = match . group ( <NUM_LIT:3> ) <EOL> data [ '<STR_LIT:error>' ] = match . group ( <NUM_LIT:4> ) <EOL> data [ '<STR_LIT>' ] = match . group ( <NUM_LIT:5> ) <EOL> return data <EOL> def run ( self ) : <EOL> now = datetime . now ( ) <EOL> if not os . path . exists ( self . main_log ) : <EOL> return self . get_default_data ( ) <EOL> log_file = open ( self . main_log ) <EOL> data = self . get_latest_status ( now , log_file ) <EOL> if data [ '<STR_LIT>' ] == - <NUM_LIT:1> : <EOL> if self . rotated_log_type == '<STR_LIT:date>' : <EOL> log_file_path = '<STR_LIT>' . format ( <EOL> self . main_log , now . strftime ( '<STR_LIT>' ) ) <EOL> else : <EOL> log_file_path = '<STR_LIT>' . format ( self . main_log ) <EOL> if not os . path . exists ( log_file_path ) : <EOL> return data <EOL> log_file = GzipFile ( log_file_path ) <EOL> return self . get_latest_status ( now , log_file ) <EOL> return data <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> """<STR_LIT>""" <EOL> main_checks_logger = logging . getLogger ( '<STR_LIT>' ) <EOL> main_checks_logger . setLevel ( logging . DEBUG ) <EOL> main_checks_logger . addHandler ( logging . StreamHandler ( sys . stdout ) ) <EOL> backupninja_check = BackupNinja ( { } , main_checks_logger , { } ) <EOL> while True : <EOL> try : <EOL> print json . dumps ( <EOL> backupninja_check . run ( ) , indent = <NUM_LIT:4> , sort_keys = True ) <EOL> except : <EOL> main_checks_logger . exception ( "<STR_LIT>" ) <EOL> finally : <EOL> time . sleep ( <NUM_LIT> ) </s>
