|
| 1 | +#encoding: utf-8 |
| 2 | + |
| 3 | +import os |
| 4 | +import argparse |
| 5 | +import logging |
| 6 | +import time |
| 7 | + |
| 8 | +import gconf |
| 9 | +from utils import fileutils, sysutils |
| 10 | +from utils import mqueue |
| 11 | + |
| 12 | +from plugins.ens import ENS |
| 13 | +from plugins.client import Client |
| 14 | +from plugins.heartbeat import Heartbeat |
| 15 | +from plugins.resource import Resource |
| 16 | + |
| 17 | +logger = logging.getLogger(__name__) |
| 18 | + |
| 19 | +parser = argparse.ArgumentParser(prog='agentd', add_help=False) |
| 20 | +parser.add_argument('-H', '--host', dest='host', help='', nargs='+') |
| 21 | +parser.add_argument('-P', '--port', dest='port', help='', nargs='+') |
| 22 | +parser.add_argument('-V', '--verbose', dest='detail', help='', action='store_true') |
| 23 | +args = parser.parse_args() |
| 24 | + |
| 25 | +def main(config): |
| 26 | + ths = {} |
| 27 | + |
| 28 | + ths['ens'] = {'class' : ENS, 'threading': None} |
| 29 | + ths['client'] = {'class' : Client, 'threading': None} |
| 30 | + ths['heartbeat'] = {'class' : Heartbeat, 'threading': None} |
| 31 | + ths['resource'] = {'class' : Resource, 'threading': None} |
| 32 | + |
| 33 | + while True: |
| 34 | + for key, value in ths.items(): |
| 35 | + if value['threading'] is None or not value['threading'].is_alive(): |
| 36 | + logger.error('threading[%s] is dead and restart', key) |
| 37 | + value['threading'] = value['class'](config) |
| 38 | + value['threading'].start() |
| 39 | + |
| 40 | + time.sleep(3) |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | +if __name__ == '__main__': |
| 45 | + |
| 46 | + config = gconf.Config |
| 47 | + |
| 48 | + HOST = args.host[0] |
| 49 | + PORT = args.port[0] |
| 50 | + PROJECT_PATH = os.path.dirname(os.path.abspath(__file__)) |
| 51 | + PID = os.getpid() |
| 52 | + |
| 53 | + if args.detail: |
| 54 | + logging.basicConfig( |
| 55 | + level=logging.DEBUG, |
| 56 | + format='%(asctime)s - %(name)s - %(levelname)s:%(message)s', |
| 57 | + filename=os.path.join(PROJECT_PATH, 'logs', 'agentd.log'), |
| 58 | + filemode='w', |
| 59 | + ) |
| 60 | + else: |
| 61 | + logging.basicConfig( |
| 62 | + level=logging.INFO, |
| 63 | + format='%(asctime)s - %(name)s - %(levelname)s:%(message)s', |
| 64 | + filename=os.path.join(PROJECT_PATH, 'logs', 'agentd.log'), |
| 65 | + filemode='w', |
| 66 | + ) |
| 67 | + |
| 68 | + setattr(config, 'HOST', HOST) |
| 69 | + setattr(config, 'PORT', PORT) |
| 70 | + setattr(config, 'PROJECT_PATH', PROJECT_PATH) |
| 71 | + setattr(config, 'PID', PID) |
| 72 | + |
| 73 | + setattr(config, 'QUEUE', mqueue.Queue()) |
| 74 | + |
| 75 | + PATH_UUID = os.path.join(PROJECT_PATH, 'UUID') |
| 76 | + UUID = fileutils.read_file(PATH_UUID) |
| 77 | + if not UUID: |
| 78 | + UUID = sysutils.get_uuid() |
| 79 | + fileutils.write_file(PATH_UUID, UUID) |
| 80 | + |
| 81 | + setattr(config, 'UUID', UUID) |
| 82 | + |
| 83 | + logger.info('agentd is starting...') |
| 84 | + logger.info('PID: %s', PID) |
| 85 | + logger.info('UUID: %s', UUID) |
| 86 | + |
| 87 | + main(config) |
0 commit comments