Ejemplo n.º 1
0
 def _init_ipc_sockets(self):
     self.__num_messages = 0
     sock_list = [
         ("receiver", zmq.PULL, 2),  # @UndefinedVariable
         ("sender", zmq.PUB, 1024),  # @UndefinedVariable
     ]
     [setattr(self, "{}_socket".format(short_sock_name), None) for short_sock_name, _a0, _b0 in sock_list]
     for short_sock_name, sock_type, hwm_size in sock_list:
         sock_name = process_tools.get_zmq_ipc_name(short_sock_name, s_name="snmp-relay")
         file_name = sock_name[5:]
         self.log(
             "init {} ipc_socket '{}' (HWM: {:d})".format(
                 short_sock_name,
                 sock_name,
                 hwm_size
             )
         )
         if os.path.exists(file_name):
             self.log("removing previous file")
             try:
                 os.unlink(file_name)
             except:
                 self.log("... %s" % (process_tools.get_except_info()), logging_tools.LOG_LEVEL_ERROR)
         wait_iter = 0
         while os.path.exists(file_name) and wait_iter < 100:
             self.log("socket %s still exists, waiting" % (sock_name))
             time.sleep(0.1)
             wait_iter += 1
         cur_socket = self.zmq_context.socket(sock_type)
         try:
             process_tools.bind_zmq_socket(cur_socket, sock_name)
             # client.bind("tcp://*:8888")
         except zmq.ZMQError:
             self.log(
                 "error binding %s: %s" % (
                     short_sock_name,
                     process_tools.get_except_info()
                 ),
                 logging_tools.LOG_LEVEL_CRITICAL)
             raise
         else:
             setattr(self, "%s_socket" % (short_sock_name), cur_socket)
             os.chmod(file_name, 0777)
             self.receiver_socket.setsockopt(zmq.LINGER, 0)  # @UndefinedVariable
             self.receiver_socket.setsockopt(zmq.RCVHWM, hwm_size)  # @UndefinedVariable
             self.receiver_socket.setsockopt(zmq.SNDHWM, hwm_size)  # @UndefinedVariable
             if sock_type == zmq.PULL:  # @UndefinedVariable
                 self.register_poller(cur_socket, zmq.POLLIN, self._recv_command)  # @UndefinedVariable
Ejemplo n.º 2
0
    def _init_network_sockets(self):
        self.network_bind(
            need_all_binds=False,
            bind_to_localhost=True,
            service_type_enum=icswServiceEnum.monitor_slave,
            client_type=icswServiceEnum.monitor_slave,
            simple_server_bind=True,
            pollin=self.remote_call,
        )

        self.__slaves = {}

        conn_str = process_tools.get_zmq_ipc_name(
            "vector", s_name="collserver", connect_to_root_instance=True)
        vector_socket = self.zmq_context.socket(zmq.PUSH)
        vector_socket.setsockopt(zmq.LINGER, 0)
        vector_socket.connect(conn_str)
        self.vector_socket = vector_socket

        # copy from relay, Refactor
        self.__nhm_connections = set()
        sock_list = [
            ("ipc", "receiver", zmq.PULL, 2),
        ]
        [
            setattr(self, "{}_socket".format(short_sock_name), None)
            for _sock_proto, short_sock_name, _a0, _b0 in sock_list
        ]
        for _sock_proto, short_sock_name, sock_type, hwm_size in sock_list:
            sock_name = process_tools.get_zmq_ipc_name(
                short_sock_name,
                s_name="md-sync-server",
                connect_to_root_instance=True)
            file_name = sock_name[5:]
            self.log("init {} ipc_socket '{}' (HWM: {:d})".format(
                short_sock_name, sock_name, hwm_size))
            if os.path.exists(file_name):
                self.log("removing previous file")
                try:
                    os.unlink(file_name)
                except:
                    self.log("... {}".format(process_tools.get_except_info()),
                             logging_tools.LOG_LEVEL_ERROR)
            wait_iter = 0
            while os.path.exists(file_name) and wait_iter < 100:
                self.log("socket {} still exists, waiting".format(sock_name))
                time.sleep(0.1)
                wait_iter += 1
            cur_socket = self.zmq_context.socket(sock_type)
            try:
                process_tools.bind_zmq_socket(cur_socket, sock_name)
                # client.bind("tcp://*:8888")
            except zmq.ZMQError:
                self.log(
                    "error binding {}: {}".format(
                        short_sock_name, process_tools.get_except_info()),
                    logging_tools.LOG_LEVEL_CRITICAL)
                raise
            else:
                setattr(self, "{}_socket".format(short_sock_name), cur_socket)
                os.chmod(file_name, 0o777)
                cur_socket.setsockopt(zmq.LINGER, 0)
                cur_socket.setsockopt(zmq.SNDHWM, hwm_size)
                cur_socket.setsockopt(zmq.RCVHWM, hwm_size)
                if sock_type == zmq.PULL:
                    self.register_poller(cur_socket, zmq.POLLIN,
                                         self._recv_command_ipc)
Ejemplo n.º 3
0
    def _init_network_sockets(self):
        zmq_id_name = "/etc/sysconfig/host-monitoring.d/0mq_id"
        my_0mq_id = uuid_tools.get_uuid().get_urn()
        if not config_store.ConfigStore.exists(ZMQ_ID_MAP_STORE):
            create_0mq_cs = True
            if os.path.exists(zmq_id_name):
                try:
                    zmq_id_dict = {
                        cur_el.attrib["bind_address"]:
                        (cur_el.text,
                         True if "virtual" in cur_el.attrib else False)
                        for cur_el in
                        etree.fromstring(file(zmq_id_name, "r").read()).xpath(
                            ".//zmq_id[@bind_address]", smart_strings=False)
                    }
                except:
                    self.log(
                        "error reading from  {}: {}".format(
                            zmq_id_name, process_tools.get_except_info()),
                        logging_tools.LOG_LEVEL_ERROR)
                    zmq_id_dict = {}
            else:
                zmq_id_dict = {}
            if "*" not in zmq_id_dict:
                zmq_id_dict["*"] = (my_0mq_id, False)
            _cs = config_store.ConfigStore(
                ZMQ_ID_MAP_STORE,
                log_com=self.log,
                read=False,
                prefix="bind",
                access_mode=config_store.AccessModeEnum.LOCAL,
                fix_access_mode=True,
            )
            for _idx, _key in enumerate(["*"] + sorted(
                [_key for _key in zmq_id_dict.keys() if _key not in ["*"]])):
                _cs["{:d}".format(_idx)] = {
                    "address": _key,
                    "uuid": zmq_id_dict[_key][0],
                    "virtual": zmq_id_dict[_key][1]
                }
        else:
            # read from cs
            _cs = config_store.ConfigStore(
                ZMQ_ID_MAP_STORE,
                log_com=self.log,
                prefix="bind",
                access_mode=config_store.AccessModeEnum.LOCAL,
                fix_access_mode=True,
            )
            create_0mq_cs = False

        if "0" not in _cs:
            _cs["0"] = {
                "address": "*",
                "virtual": False,
                "uuid": my_0mq_id,
            }
        if _cs["0"]["uuid"] != my_0mq_id:
            self.log(
                "0MQ id from cluster ({}) differs from host-monitoring 0MQ id ({})"
                .format(
                    my_0mq_id,
                    _cs["bind_0_uuid"],
                ))
            # code snippet to update value
            _cur = _cs["0"]
            _cur["uuid"] = my_0mq_id
            _cs["0"] = _cur
            create_0mq_cs = True
        if create_0mq_cs:
            _cs.write()
        # get all ipv4 interfaces with their ip addresses, dict: interfacename -> IPv4
        zmq_id_dict = {}
        for _idx in _cs.keys():
            _bind = _cs[_idx]
            zmq_id_dict[_bind["address"]] = (
                _bind["uuid"],
                _bind["virtual"],
            )
        ipv4_dict = {
            cur_if_name: [ip_tuple["addr"] for ip_tuple in value[2]][0]
            for cur_if_name, value in [(if_name,
                                        netifaces.ifaddresses(if_name))
                                       for if_name in netifaces.interfaces()]
            if 2 in value
        }
        # ipv4_lut = dict([(value, key) for key, value in ipv4_dict.iteritems()])
        ipv4_addresses = ipv4_dict.values()
        if zmq_id_dict.keys() == ["*"]:
            # wildcard bind
            pass
        else:
            if "*" in zmq_id_dict:
                wc_urn, wc_virtual = zmq_id_dict.pop("*")
                for target_ip in ipv4_addresses:
                    if target_ip not in zmq_id_dict:
                        zmq_id_dict[target_ip] = (wc_urn, wc_virtual)
        ref_id = "*" if "*" in zmq_id_dict else "127.0.0.1"
        self.zeromq_id = zmq_id_dict[ref_id][0].split(":")[-1]
        self.log("0MQ bind info (global 0MQ id is {})".format(self.zeromq_id))
        for key in sorted(zmq_id_dict.iterkeys()):
            self.log("bind address {:<15s}: {}{}".format(
                key, zmq_id_dict[key][0],
                " is virtual" if zmq_id_dict[key][1] else ""))
        self.zmq_id_dict = zmq_id_dict
        self._bind_external()
        sock_list = [
            ("ipc", "vector", zmq.PULL, 512, None, ""),  # @UndefinedVariable
            ("ipc", "command", zmq.PULL, 512, self._recv_ext_command,
             ""),  # @UndefinedVariable
            ("ipc", "result", zmq.ROUTER, 512, None,
             process_tools.zmq_identity_str("host_monitor")
             )  # @UndefinedVariable
        ]
        for _sock_proto, short_sock_name, sock_type, hwm_size, dst_func, zmq_id in sock_list:
            sock_name = process_tools.get_zmq_ipc_name(short_sock_name,
                                                       s_name="collserver")
            file_name = sock_name[5:]
            self.log("init {} ipc_socket '{}' (HWM: {:d})".format(
                short_sock_name, sock_name, hwm_size))
            if os.path.exists(file_name):
                self.log("removing previous file")
                try:
                    os.unlink(file_name)
                except:
                    self.log("... {}".format(process_tools.get_except_info()),
                             logging_tools.LOG_LEVEL_ERROR)
            wait_iter = 0
            while os.path.exists(file_name) and wait_iter < 100:
                self.log("socket {} still exists, waiting".format(sock_name))
                time.sleep(0.1)
                wait_iter += 1
            cur_socket = self.zmq_context.socket(sock_type)
            if zmq_id:
                cur_socket.setsockopt_string(zmq.IDENTITY,
                                             zmq_id)  # @UndefinedVariable
            try:
                process_tools.bind_zmq_socket(cur_socket, sock_name)
                # client.bind("tcp://*:8888")
            except zmq.ZMQError:
                self.log(
                    "error binding {}: {}".format(
                        short_sock_name, process_tools.get_except_info()),
                    logging_tools.LOG_LEVEL_CRITICAL)
                raise
            else:
                setattr(self, "{}_socket".format(short_sock_name), cur_socket)
                _backlog_size = self.CC.CS["hm.socket.backlog.size"]
                os.chmod(file_name, 0777)
                cur_socket.setsockopt(zmq.LINGER, 0)  # @UndefinedVariable
                cur_socket.setsockopt(zmq.SNDHWM,
                                      hwm_size)  # @UndefinedVariable
                cur_socket.setsockopt(zmq.RCVHWM,
                                      hwm_size)  # @UndefinedVariable
                if dst_func:
                    self.register_poller(cur_socket, zmq.POLLIN,
                                         dst_func)  # @UndefinedVariable
Ejemplo n.º 4
0
 def _init_ipc_sockets(self):
     # init IP lookup table
     # if self.__force_resolve:
     #    self.__ip_lut = {}
     #    self.__forward_lut = {}
     self.__num_messages = 0
     # nhm (not host monitoring) dictionary for timeout
     self.__nhm_dict = {}
     # raw_nhm (not host monitoring) dictionary for timeout, raw connections (no XML)
     self.__raw_nhm_dict = {}
     self.__nhm_connections = set()
     # also used in md-sync-server/server, ToDo: Refactor
     sock_list = [
         {
             "proto": "ipc",
             "name": "receiver",
             "type": zmq.PULL,
             "hwm_size": 2,
         },
         {
             "proto": "ipc",
             "name": "sender",
             "type": zmq.PUB,
             "hwm_size": 1024,
         },
     ]
     [
         setattr(self, "{}_socket".format(_sock["name"]), None) for _sock in sock_list
     ]
     for _sock in sock_list:
         sock_name = process_tools.get_zmq_ipc_name(_sock["name"], s_name="collrelay")
         file_name = sock_name[5:]
         self.log(
             "init {} ipc_socket '{}' (HWM: {:d})".format(
                 _sock["name"],
                 sock_name,
                 _sock["hwm_size"],
             )
         )
         if os.path.exists(file_name):
             self.log("removing previous file")
             try:
                 os.unlink(file_name)
             except:
                 self.log(
                     "... {}".format(process_tools.get_except_info()),
                     logging_tools.LOG_LEVEL_ERROR
                 )
         wait_iter = 0
         while os.path.exists(file_name) and wait_iter < 100:
             self.log("socket {} still exists, waiting".format(sock_name))
             time.sleep(0.1)
             wait_iter += 1
         cur_socket = self.zmq_context.socket(_sock["type"])
         try:
             process_tools.bind_zmq_socket(cur_socket, sock_name)
             # client.bind("tcp://*:8888")
         except zmq.ZMQError:
             self.log(
                 "error binding {}: {}" .format(
                     _sock["name"],
                     process_tools.get_except_info()
                 ),
                 logging_tools.LOG_LEVEL_CRITICAL
             )
             raise
         else:
             setattr(self, "{}_socket".format(_sock["name"]), cur_socket)
             os.chmod(file_name, 0o777)
             cur_socket.setsockopt(zmq.LINGER, 0)
             cur_socket.setsockopt(zmq.SNDHWM, _sock["hwm_size"])
             cur_socket.setsockopt(zmq.RCVHWM, _sock["hwm_size"])
             if _sock["type"] == zmq.PULL:
                 self.register_poller(cur_socket, zmq.POLLIN, self._recv_command_ipc)
     self.client_socket = process_tools.get_socket(
         self.zmq_context,
         "ROUTER",
         identity="ccollclient:{}".format(process_tools.get_machine_name()),
         linger=0,
         sndhwm=2,
         rcvhwm=2,
         immediate=True,
     )
     self.register_poller(self.client_socket, zmq.POLLIN, self._recv_nhm_result)