Changeset - 707908d9cfa7
[Not reviewed]
default
0 0 1
Dennis Fink - 11 years ago 2013-11-20 01:10:02
dennis.fink@c3l.lu
added check_servers.py
1 file changed with 53 insertions and 0 deletions:
0 comments (0 inline, 0 general)
Scripts/check_servers.py
Show inline comments
 
new file 100644
 

	
 
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:
 
                    status = 'Unkown'
 
                else:
 
                    status = None
 

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

	
 
                json.dump(data, file_object)
 
        except:
 
            continue
 

	
 

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