Exemplo n.º 1
0
def main():
    config = myCommon.MyConfig()

    ip_master = get_interface_ip(config.get_master_nic())
    ip_agent = get_interface_ip(config.get_agent_nic())
    print(myCommon.get_frame() +
          'My IP({0}) : {1}'.format(config.get_master_nic(), ip_master))
    if ip_master == config.get_master_ip():
        print(myCommon.get_frame() + 'This is master.')
        myMaster.master()
    elif ip_agent == config.get_agent_ip():
        print(myCommon.get_frame() + 'This is agent.')
        myAgent.agent(config)
Exemplo n.º 2
0
def agent(my_config):
    server_class = HTTPServer
    httpd = server_class(
        (my_config.get_agent_ip(), my_config.get_agent_port()), MyAgent)

    print(
        myCommon.get_frame() + time.asctime(), 'HTTPd starts - %s:%s' %
        (my_config.get_agent_ip(), my_config.get_agent_port()))
    try:
        httpd.serve_forever()
    except:
        pass
    httpd.server_close()

    print(
        myCommon.get_frame() + time.asctime(), 'HTTPd stops - %s:%s' %
        (my_config.get_agent_ip(), my_config.get_agent_port()))
Exemplo n.º 3
0
    def run(self):
        self.check_first()

        while self.is_end is False:
            time_start = datetime.datetime.now()

            if self.is_running(self.my_config.get_process_name()) is True:
                self.new_state.master = True
            else:
                self.new_state.master = False

            try:
                r = requests.get('http://{}:{}/?command=check'.format(self.my_config.get_agent_ip(),
                                                                      self.my_config.get_agent_port()))
                result = json.loads(r.text)
                if result['state'] == 'ok':
                    self.new_state.agent = True
                else:
                    self.new_state.agent = False
            except Exception as e:
                print(myCommon.get_frame() + e)

            if self.new_state.master is True:
                str_m = 'Master\'s \'{}\' is good.'.format(self.my_config.get_process_name())
            else:
                str_m = 'Master\'s \'{}\' is NOT good.'.format(self.my_config.get_process_name())

            if self.new_state.agent is True:
                str_a = 'Agent\'s \'{}\' is good.'.format(self.my_config.get_process_name())
            else:
                str_a = 'Agent\'s \'{}\' is NOT good.'.format(self.my_config.get_process_name())

            if self.new_state.master != self.old_state.master or self.new_state.agent != self.old_state.agent:
                if self.new_state.master is True and self.new_state.agent is True:
                    myCommon.vip_add(self.my_config.get_master_nic(), self.my_config.get_virtual_ip())
                    requests.get('http://{}:{}/?command=vip_del'.format(self.my_config.get_agent_ip(),
                                                                        self.my_config.get_agent_port()))
                elif self.new_state.master is True and self.new_state.agent is False:
                    myCommon.vip_add(self.my_config.get_master_nic(), self.my_config.get_virtual_ip())
                    requests.get('http://{}:{}/?command=vip_del'.format(self.my_config.get_agent_ip(),
                                                                        self.my_config.get_agent_port()))
                elif self.new_state.master is False and self.new_state.agent is True:
                    myCommon.vip_del(self.my_config.get_master_nic(), self.my_config.get_virtual_ip())
                    requests.get('http://{}:{}/?command=vip_add'.format(self.my_config.get_agent_ip(),
                                                                        self.my_config.get_agent_port()))
                else:
                    myCommon.vip_del(self.my_config.get_master_nic(), self.my_config.get_virtual_ip())
                    requests.get('http://{}:{}/?command=vip_del'.format(self.my_config.get_agent_ip(),
                                                                        self.my_config.get_agent_port()))
            self.old_state.master = self.new_state.master
            self.old_state.agent = self.new_state.agent

            self.logger.log(logging.INFO, str_m + ' ' + str_a)

            time_end = datetime.datetime.now()
            time.sleep(1.0 - (time_end - time_start).total_seconds())
Exemplo n.º 4
0
    def handle_http(self, path):
        path1 = urllib.parse.urlparse(path)
        path2 = urllib.parse.parse_qs(path1.query)
        json_data = {}
        status_code = 200
        if path2.get('command') == ['do_you_have_vip']:
            command_line = 'ip address show dev {0} | grep {1} | wc -l'.format(
                self.my_config.get_agent_nic(),
                self.my_config.get_virtual_ip())
            command_result = int(os.popen(command_line).read())
            if command_result == 1:
                json_data = {'do_you_have_vip': 'yes'}
            else:
                json_data = {'do_you_have_vip': 'no'}
        elif path2.get('command') == ['check']:
            if self.is_running() is True:
                json_data = {'mode': 'agent', 'state': 'ok'}
            else:
                json_data = {'mode': 'agent', 'state': 'not'}
        elif path2.get('command') == ['vip_add']:
            myCommon.vip_add(self.my_config.get_agent_nic(),
                             self.my_config.get_virtual_ip())
        elif path2.get('command') == ['vip_del']:
            myCommon.vip_del(self.my_config.get_agent_nic(),
                             self.my_config.get_virtual_ip())
        else:
            print(myCommon.get_frame() + 'Unknown : {}'.format(path2))
            status_code = 500

        self.send_response(status_code)
        self.send_header('Content-type', 'text/html')
        self.end_headers()

        json_string = json.dumps(json_data)
        content = '{}'.format(json_string)

        return bytes(content, 'UTF-8')
Exemplo n.º 5
0
 def __init__(self):
     print(myCommon.get_frame() + self.my_config.get_master_nic())
     print(myCommon.get_frame() + self.my_config.get_process_name())
     threading.Thread.__init__(self)
Exemplo n.º 6
0
            'enp0s31f6',
        ]
        for eth in interfaces:
            try:
                ip = eth + ' : ' + get_interface_ip(eth)
                break
            except IOError:
                pass
    return ip


if __name__ == '__main__':
    try:
        f = open('config.ini', 'r')
        f.close()
    except:
        print('Failed to read \'config.ini\'.')
        exit(-1)

    if platform.system() != 'Linux':
        print(myCommon.get_frame() + 'OS is NOT Linux.')
        exit(-1)

    if os.geteuid() != 0:
        print(myCommon.get_frame() +
              'You are NOT administrator. account=[ \'{0}({1})\' ].'.format(
                  os.environ.get('USER'), os.geteuid()))
        exit(-1)

    main()