Changeset - 6632069b797d
[Not reviewed]
default
0 1 0
Dennis Fink - 11 years ago 2014-01-21 19:00:40
dennis.fink@c3l.lu
updated check_server.py
1 file changed with 3 insertions and 2 deletions:
0 comments (0 inline, 0 general)
Scripts/check_servers.py
Show inline comments
 

	
 
import os
 
import os.path
 
import json
 

	
 
from datetime import datetime
 

	
 
DATE_FORMAT = '%d-%m-%Y %H:%M:%S'
 

	
 
DATADIR = './data'
 

	
 

	
 
def get_json_files():
 

	
 
    for root, _, files in os.walk(DATADIR):
 
        for f in files:
 
            if f.endswith('.json'):
 
                yield os.path.join(root, f)
 

	
 

	
 
def check_all_servers():
 

	
 
    for f in get_json_files():
 
        try:
 
            with open(f, encoding='utf-8') as file_object:
 
                data = json.load(file_object)
 

	
 
            with open(f, mode='w', encoding='utf-8') as file_object:
 

	
 
                date = datetime.strptime(data['last_updated'],
 
                                         DATE_FORMAT)
 
                now = datetime.utcnow()
 

	
 
                delta = now - date
 

	
 
                if delta.seconds >= 3600:
 
                    status = 'Offline'
 
                elif delta.second >= 1200:
 
                elif delta.seconds >= 1200:
 
                    status = 'Unkown'
 
                else:
 
                    status = None
 

	
 
                if status:
 
                    for key in ('server_status', 'tor_status'):
 
                        data[key] = status
 

	
 
                json.dump(data, file_object)
 
        except:
 
        except Exception as e:
 
            print(e)
 
            continue
 

	
 

	
 
if __name__ == '__main__':
 
    check_all_servers()
0 comments (0 inline, 0 general)