Пример #1
0
    def send_json_response(self, sliver_tools, query):
        """Sends the response to the lookup request in json format.

        Args:
            sliver_tools: A list of SliverTool instances,
                representing the best sliver
                tool selected for this lookup request.
            query: A LookupQuery instance representing the user lookup request.
        """
        array_response = False
        if len(sliver_tools) > 1:
            array_response = True

        if type(sliver_tools) is not list:
            logging.error("Problem: sliver_tools is not a list.")
            return

        tool = None
        json_data = "";
        for sliver_tool in sliver_tools:
            data = {}

            ip = []

            if tool == None:
                tool = model.get_tool_from_tool_id(sliver_tool.tool_id)

            logging.info('user_defined_af = %s', query.user_defined_af)
            if query.user_defined_af == message.ADDRESS_FAMILY_IPv4:
                ip = [sliver_tool.sliver_ipv4]
            elif query.user_defined_af == message.ADDRESS_FAMILY_IPv6:
                ip = [sliver_tool.sliver_ipv6]
            else:
                # If 'address_family' is not specified, the default is to
                # return both valid IP addresses (if both 'status_ipv4' and
                # 'status_ipv6' are 'online').
                # Although the update will only set the sliver as online if it
                # has a valid IP address, the resolver still returns it as
                # a candidate.
                if (sliver_tool.sliver_ipv4 != message.NO_IP_ADDRESS and
                    sliver_tool.status_ipv4 == message.STATUS_ONLINE):
                    ip.append(sliver_tool.sliver_ipv4)
                if (sliver_tool.sliver_ipv6 != message.NO_IP_ADDRESS and
                    sliver_tool.status_ipv6 == message.STATUS_ONLINE):
                    ip.append(sliver_tool.sliver_ipv6)

            fqdn = self._add_fqdn_annotation(query, sliver_tool.fqdn)
            if sliver_tool.http_port:
                data['url'] = ''.join([ 'http://', fqdn, ':', sliver_tool.http_port])
            if sliver_tool.server_port:
                data['port'] = sliver_tool.server_port

            data['fqdn'] = fqdn
            data['ip'] = ip
            data['site'] = sliver_tool.site_id
            data['city'] = sliver_tool.city
            data['country'] = sliver_tool.country

            if sliver_tool.tool_extra and tool.show_tool_extra:
                data['tool_extra'] = sliver_tool.tool_extra

            if json_data != "":
                json_data += ","
            json_data += json.dumps(data)

        if array_response:
            json_data = "[" + json_data + "]"
        self.response.headers['Access-Control-Allow-Origin'] = '*'
        self.response.headers['Content-Type'] = 'application/json'
        self.response.out.write(json_data)
Пример #2
0
    def send_json_response(self, sliver_tools, query):
        """Sends the response to the lookup request in json format.

        Args:
            sliver_tools: A list of SliverTool instances,
                representing the best sliver
                tool selected for this lookup request.
            query: A LookupQuery instance representing the user lookup request.
        """
        if type(sliver_tools) is not list:
            logging.error("Problem: sliver_tools is not a list.")
            return

        # We will respond with HTTP status 204 if len(sliver_tools) == 0
        array_response = False
        if len(sliver_tools) > 1:
            array_response = True

        tool = None
        json_data = ""
        for sliver_tool in sliver_tools:
            data = {}

            if tool == None:
                tool = model.get_tool_from_tool_id(sliver_tool.tool_id)

            if query.tool_address_family == message.ADDRESS_FAMILY_IPv4:
                ips = [sliver_tool.sliver_ipv4]
            elif query.tool_address_family == message.ADDRESS_FAMILY_IPv6:
                ips = [sliver_tool.sliver_ipv6]
            else:
                ips = []
                if sliver_tool.status_ipv4 == message.STATUS_ONLINE:
                    ips.append(sliver_tool.sliver_ipv4)
                if sliver_tool.status_ipv6 == message.STATUS_ONLINE:
                    ips.append(sliver_tool.sliver_ipv6)

            fqdn = fqdn_rewrite.rewrite(sliver_tool.fqdn,
                                        query.tool_address_family,
                                        sliver_tool.tool_id)
            if sliver_tool.http_port:
                data['url'] = _create_tool_url(fqdn, sliver_tool.http_port)
            if sliver_tool.server_port:
                data['port'] = sliver_tool.server_port

            data['fqdn'] = fqdn
            data['ip'] = ips
            data['site'] = sliver_tool.site_id
            data['city'] = sliver_tool.city
            data['country'] = sliver_tool.country

            if sliver_tool.tool_extra and tool.show_tool_extra:
                data['tool_extra'] = sliver_tool.tool_extra

            if json_data != "":
                json_data += ","
            json_data += json.dumps(data)

        if array_response:
            json_data = "[" + json_data + "]"
        if json_data:
            self.response.headers['Access-Control-Allow-Origin'] = '*'
            self.response.headers['Content-Type'] = 'application/json'
            self.response.out.write(json_data)
        else:
            util.send_no_content(self)
Пример #3
0
    def get(self):
        """Triggers the update handler.

        Updates sliver status with information from either Nagios or Prometheus.
        The base URLs for accessing status information are stored in the
        datastore along with the credentials necessary to access the data.
        """
        # Determine if there are any dependencies on Prometheus.
        prometheus_deps = model.get_status_source_deps('prometheus')
        # Get Prometheus configs, and authenticate.
        prometheus_config = prometheus_config_wrapper.get_prometheus_config()
        if prometheus_config is None:
            logging.error('Datastore does not have the Prometheus configs.')
        else:
            prometheus_opener = prometheus_status.authenticate_prometheus(
                prometheus_config)

        # Determine if there are any dependencies on Nagios.
        nagios_deps = model.get_status_source_deps('nagios')
        # Get Nagios configs, and authenticate.
        nagios_config = nagios_config_wrapper.get_nagios_config()
        if nagios_config is None:
            logging.error('Datastore does not have the Nagios configs.')
        else:
            nagios_opener = nagios_status.authenticate_nagios(nagios_config)

        # If we have dependencies on both Prometheus and Nagios, and neither one
        # of the configs is available, then abort, because we can't fetch status
        # from either. However, if we have one or the other, then continue,
        # because it may be preferable to update _some_ statuses than none.
        if (prometheus_deps and not prometheus_config) and (nagios_deps and
                                                            not nagios_config):
            logging.error(
                'Neither Nagios nor Prometheus configs are available.')
            return util.send_not_found(self)

        for tool_id in model.get_all_tool_ids():
            tool = model.get_tool_from_tool_id(tool_id)
            for address_family in ['', '_ipv6']:
                if tool.status_source == 'prometheus':
                    logging.info('Status source for %s%s is: prometheus',
                                 tool_id, address_family)
                    # Only proceed if prometheus_config exists, and hence
                    # prometheus_opener should also exist.
                    if prometheus_config:
                        slice_info = prometheus_status.get_slice_info(
                            prometheus_config.url, tool_id, address_family)
                        if not slice_info:
                            continue
                        slice_status = prometheus_status.get_slice_status(
                            slice_info.slice_url, prometheus_opener)
                    else:
                        logging.error(
                            'Prometheus config unavailable. Skipping %s%s',
                            tool_id, address_family)
                        continue
                elif tool.status_source == 'nagios':
                    logging.info('Status source for %s%s is: nagios', tool_id,
                                 address_family)
                    # Only proceed if nagios_config exists, and hence
                    # nagios_opener should also exist.
                    if nagios_config:
                        slice_info = nagios_status.get_slice_info(
                            nagios_config.url, tool_id, address_family)
                        slice_status = nagios_status.get_slice_status(
                            slice_info.slice_url, nagios_opener)
                    else:
                        logging.error(
                            'Nagios config unavailable. Skipping %s%s', tool_id,
                            address_family)
                        continue
                else:
                    logging.error('Unknown tool status_source: %s.',
                                  tool.status_source)
                    continue

                if slice_status:
                    self.update_sliver_tools_status(slice_status,
                                                    slice_info.tool_id,
                                                    slice_info.address_family)

        return util.send_success(self)
Пример #4
0
    def send_json_response(self, sliver_tools, query):
        """Sends the response to the lookup request in json format.

        Args:
            sliver_tools: A list of SliverTool instances,
                representing the best sliver
                tool selected for this lookup request.
            query: A LookupQuery instance representing the user lookup request.
        """
        array_response = False
        if len(sliver_tools) > 1:
            array_response = True

        if type(sliver_tools) is not list:
            logging.error("Problem: sliver_tools is not a list.")
            return

        tool = None
        json_data = "";
        for sliver_tool in sliver_tools:
            data = {}

            if tool == None:
                tool = model.get_tool_from_tool_id(sliver_tool.tool_id)

            if query.tool_address_family == message.ADDRESS_FAMILY_IPv4:
                ips = [sliver_tool.sliver_ipv4]
            elif query.tool_address_family == message.ADDRESS_FAMILY_IPv6:
                ips = [sliver_tool.sliver_ipv6]
            else:
                ips = []
                if sliver_tool.status_ipv4 == message.STATUS_ONLINE:
                    ips.append(sliver_tool.sliver_ipv4)
                if sliver_tool.status_ipv6 == message.STATUS_ONLINE:
                    ips.append(sliver_tool.sliver_ipv6)

            fqdn = fqdn_rewrite.rewrite(sliver_tool.fqdn,
                                        query.tool_address_family,
                                        sliver_tool.tool_id)
            if sliver_tool.http_port:
                data['url'] = _create_tool_url(fqdn, sliver_tool.http_port)
            if sliver_tool.server_port:
                data['port'] = sliver_tool.server_port

            data['fqdn'] = fqdn
            data['ip'] = ips
            data['site'] = sliver_tool.site_id
            data['city'] = sliver_tool.city
            data['country'] = sliver_tool.country

            if sliver_tool.tool_extra and tool.show_tool_extra:
                data['tool_extra'] = sliver_tool.tool_extra

            if json_data != "":
                json_data += ","
            json_data += json.dumps(data)

        if array_response:
            json_data = "[" + json_data + "]"
        self.response.headers['Access-Control-Allow-Origin'] = '*'
        self.response.headers['Content-Type'] = 'application/json'
        self.response.out.write(json_data)
Пример #5
0
    def send_json_response(self, sliver_tools, query):
        """Sends the response to the lookup request in json format.

        Args:
            sliver_tools: A list of SliverTool instances,
                representing the best sliver
                tool selected for this lookup request.
            query: A LookupQuery instance representing the user lookup request.
        """
        array_response = False
        if len(sliver_tools) > 1:
            array_response = True

        if type(sliver_tools) is not list:
            logging.error("Problem: sliver_tools is not a list.")
            return

        tool = None
        json_data = ""
        for sliver_tool in sliver_tools:
            data = {}

            ip = []

            if tool == None:
                tool = model.get_tool_from_tool_id(sliver_tool.tool_id)

            logging.info('user_defined_af = %s', query.user_defined_af)
            if query.user_defined_af == message.ADDRESS_FAMILY_IPv4:
                ip = [sliver_tool.sliver_ipv4]
            elif query.user_defined_af == message.ADDRESS_FAMILY_IPv6:
                ip = [sliver_tool.sliver_ipv6]
            else:
                # If 'address_family' is not specified, the default is to
                # return both valid IP addresses (if both 'status_ipv4' and
                # 'status_ipv6' are 'online').
                # Although the update will only set the sliver as online if it
                # has a valid IP address, the resolver still returns it as
                # a candidate.
                if (sliver_tool.sliver_ipv4 != message.NO_IP_ADDRESS
                        and sliver_tool.status_ipv4 == message.STATUS_ONLINE):
                    ip.append(sliver_tool.sliver_ipv4)
                if (sliver_tool.sliver_ipv6 != message.NO_IP_ADDRESS
                        and sliver_tool.status_ipv6 == message.STATUS_ONLINE):
                    ip.append(sliver_tool.sliver_ipv6)

            fqdn = self._add_fqdn_annotation(query, sliver_tool.fqdn)
            if sliver_tool.http_port:
                data['url'] = ''.join(
                    ['http://', fqdn, ':', sliver_tool.http_port])
            if sliver_tool.server_port:
                data['port'] = sliver_tool.server_port

            data['fqdn'] = fqdn
            data['ip'] = ip
            data['site'] = sliver_tool.site_id
            data['city'] = sliver_tool.city
            data['country'] = sliver_tool.country

            if sliver_tool.tool_extra and tool.show_tool_extra:
                data['tool_extra'] = sliver_tool.tool_extra

            if json_data != "":
                json_data += ","
            json_data += json.dumps(data)

        if array_response:
            json_data = "[" + json_data + "]"
        self.response.headers['Access-Control-Allow-Origin'] = '*'
        self.response.headers['Content-Type'] = 'application/json'
        self.response.out.write(json_data)
Пример #6
0
    def get(self):
        """Triggers the update handler.

        Updates sliver status with information from either Nagios or Prometheus.
        The base URLs for accessing status information are stored in the
        datastore along with the credentials necessary to access the data.
        """
        # Determine if there are any dependencies on Prometheus.
        prometheus_deps = model.get_status_source_deps('prometheus')
        # Get Prometheus configs, and authenticate.
        prometheus_config = prometheus_config_wrapper.get_prometheus_config()
        if prometheus_config is None:
            logging.error('Datastore does not have the Prometheus configs.')
        else:
            prometheus_opener = prometheus_status.authenticate_prometheus(
                prometheus_config)

        # Determine if there are any dependencies on Nagios.
        nagios_deps = model.get_status_source_deps('nagios')
        # Get Nagios configs, and authenticate.
        nagios_config = nagios_config_wrapper.get_nagios_config()
        if nagios_config is None:
            logging.error('Datastore does not have the Nagios configs.')
        else:
            nagios_opener = nagios_status.authenticate_nagios(nagios_config)

        # If we have dependencies on both Prometheus and Nagios, and neither one
        # of the configs is available, then abort, because we can't fetch status
        # from either. However, if we have one or the other, then continue,
        # because it may be preferable to update _some_ statuses than none.
        if (prometheus_deps and not prometheus_config) and (nagios_deps and
                                                            not nagios_config):
            logging.error(
                'Neither Nagios nor Prometheus configs are available.')
            return util.send_not_found(self)

        for tool_id in model.get_all_tool_ids():
            tool = model.get_tool_from_tool_id(tool_id)
            for address_family in ['', '_ipv6']:
                if tool.status_source == 'prometheus':
                    logging.info('Status source for %s%s is: prometheus',
                                 tool_id, address_family)
                    # Only proceed if prometheus_config exists, and hence
                    # prometheus_opener should also exist.
                    if prometheus_config:
                        slice_info = prometheus_status.get_slice_info(
                            prometheus_config.url, tool_id, address_family)
                        if not slice_info:
                            continue
                        slice_status = prometheus_status.get_slice_status(
                            slice_info.slice_url, prometheus_opener)
                    else:
                        logging.error(
                            'Prometheus config unavailable. Skipping %s%s',
                            tool_id, address_family)
                        continue
                elif tool.status_source == 'nagios':
                    logging.info('Status source for %s%s is: nagios', tool_id,
                                 address_family)
                    # Only proceed if nagios_config exists, and hence
                    # nagios_opener should also exist.
                    if nagios_config:
                        slice_info = nagios_status.get_slice_info(
                            nagios_config.url, tool_id, address_family)
                        slice_status = nagios_status.get_slice_status(
                            slice_info.slice_url, nagios_opener)
                    else:
                        logging.error(
                            'Nagios config unavailable. Skipping %s%s', tool_id,
                            address_family)
                        continue
                else:
                    logging.error('Unknown tool status_source: %s.',
                                  tool.status_source)
                    continue

                if slice_status:
                    self.update_sliver_tools_status(slice_status,
                                                    slice_info.tool_id,
                                                    slice_info.address_family)

        return util.send_success(self)