Esempio n. 1
0
    def get_status(self):

        version_str = ""
        try:
            fp = request.urlopen("http://www.elastichq.org/currversion.json",
                                 timeout=10)
            mbyte = fp.read()
            version_str = mbyte.decode("utf-8")
            fp.close()
        except Exception as ex:
            LOG.error("error retrieving version information", ex)

        stable_version = (json.loads(version_str)).get("version", None)

        clusters = ClusterService().get_clusters(create_if_missing=False)
        schema = ClusterDTO(many=True)
        result = schema.dump(clusters)

        status = {
            "name": "ElasticHQ",
            "installed_version": current_app.config.get('API_VERSION'),
            "current_stable_version": stable_version,
            "tagline": "You know, for Elasticsearch",
            "clusters": result.data
        }
        return status
Esempio n. 2
0
def init_connections():
    """
    Inits connections to all of the configured clusters.
    :return:
    """
    from elastichq.service import ClusterService
    ClusterService().get_clusters()
Esempio n. 3
0
    def get_indices_summary(self, cluster_name, indices_names=None):
        """
        Returns a formatted representation of one/many indices.
        :param cluster_name:
        :param indices_names:
        :return:
        """
        connection = ConnectionService().get_connection(cluster_name)
        indices_stats = connection.indices.stats(index=indices_names, request_timeout=REQUEST_TIMEOUT)

        # get shard info
        cluster_state = ClusterService().get_cluster_state(cluster_name, metric="metadata", indices=indices_names)
        state_indices = jmespath.search("metadata.indices", cluster_state)
        cat = connection.cat.indices(format='json')
        indices = []
        if state_indices:
            the_indices = indices_stats.get("indices", None)
            index_keys = list(the_indices.keys())
            for key in index_keys:
                one_index = the_indices.get(key)
                index = {"index_name": key}
                index['health'] = [x['health'] for x in cat if x['index'] == key][0]
                index['docs'] = jmespath.search("primaries.docs.count", one_index)
                index['docs_deleted'] = jmespath.search("primaries.docs.deleted", one_index)
                index['size_in_bytes'] = jmespath.search("primaries.store.size_in_bytes", one_index)
                index['fielddata'] = {
                    'memory_size_in_bytes': jmespath.search("total.fielddata.memory_size_in_bytes", one_index)}

                index_state = state_indices.get(key)
                index['settings'] = {
                    'number_of_shards': int(jmespath.search("settings.index.number_of_shards", index_state)),
                    "number_of_replicas": int(jmespath.search("settings.index.number_of_replicas", index_state))}
                index['state'] = index_state.get("state", None)
                indices.append(index)
        return indices
Esempio n. 4
0
    def get_deleted_indices(self, cluster_name):
        """
        Only supported in ES v5+
        :param cluster_name:
        :return:
        """
        cluster_state = ClusterService().get_cluster_state(cluster_name,
                                                           metric="metadata")

        state_indices = jmespath.search("metadata", cluster_state)
        graveyard = state_indices.get('index-graveyard', None)
        indices = []
        if graveyard is not None:
            if graveyard.get('tombstones', None) is not None:
                for tombstone in graveyard.get('tombstones'):
                    indices.append({
                        "index":
                        jmespath.search("index.index_name", tombstone),
                        "deleted":
                        tombstone.get("delete_date_in_millis")
                    })

        return indices
Esempio n. 5
0
 def do_task(self):
     from elastichq.service import ClusterService
     clusters = ClusterService().get_clusters(create_if_missing=False)
     return clusters
Esempio n. 6
0
    def get(self, cluster_name, command):
        """
        Endpoint for generic GET requests on a cluster. Simply does a pass-thru call to the actual cluster endpoint.

        :type cluster_name: string
        :param cluster_name:
        :type command: string
        :param command:
        :returns:

        :resheader Content-Type: application/json
        :status 200: OK
        :status 500: server error
        """

        if command is not None:
            if command == '_cluster_status':
                response = ClusterService().get_cluster_status(cluster_name)
            elif command == '_cluster_settings':
                response = ClusterService().get_cluster_settings(cluster_name)
            elif command == '_cluster_tasks':
                response = ClusterService().get_cluster_tasks(cluster_name)
            elif command == '_cluster_state':
                response = ClusterService().get_cluster_state(cluster_name)
            elif command == '_cluster_stats':
                response = ClusterService().get_cluster_stats(cluster_name)
            elif command == '_cluster_health':
                response = ClusterService().get_cluster_health(cluster_name)
            elif command == '_nodes':
                response = NodeService().get_node_info(cluster_name)
            elif command == '_nodes_stats':
                response = NodeService().get_node_stats(cluster_name)
            elif command == '_indices_info':
                response = IndicesService().get_indices(cluster_name)
            elif command == '_indices_mappings':
                response = IndicesService().get_mapping(cluster_name)
            elif command == '_indices_aliases':
                response = IndicesService().get_alias(cluster_name)
            elif command == '_indices_stats':
                response = IndicesService().get_indices_stats(cluster_name)
            elif command == '_indices_templates':
                response = IndicesService().get_indices_templates(cluster_name)
            elif command == '_indices_segments':
                response = IndicesService().get_indices_segments(cluster_name)
            elif command == '_indices_shard_stores':
                response = IndicesService().get_indices_shard_stores(
                    cluster_name)
            elif command == '_indices_recovery':
                response = IndicesService().get_indices_recovery(cluster_name)
            elif command == '_hq_status':
                response = HQService().get_status()
            elif command == '_hq_cluster_summary':
                response = ClusterService().get_cluster_summary(cluster_name)
            elif command == '_hq_cluster_list':
                res = ClusterService().get_clusters()
                schema = ClusterDTO(many=True)
                response = schema.dump(res)
            elif command.startswith(
                    '_cat'
            ):  # cat api is pretty safe and does not currently have a Service interface
                connection = ConnectionService().get_connection(cluster_name)
                format = 'json'
                if command == '_cat_aliases':
                    response = connection.cat.aliases(format=format, h="*")
                elif command == '_cat_allocation':
                    response = connection.cat.allocation(format=format, h="*")
                elif command == '_cat_count':
                    response = connection.cat.count(format=format, h="*")
                elif command == '_cat_fielddata':
                    response = connection.cat.fielddata(format=format, h="*")
                elif command == '_cat_health':
                    response = connection.cat.health(format=format, h="*")
                elif command == '_cat_indices':
                    response = connection.cat.indices(format=format, h="*")
                elif command == '_cat_master':
                    response = connection.cat.master(format=format, h="*")
                elif command == '_cat_nodeattrs':
                    response = connection.cat.nodeattrs(format=format, h="*")
                elif command == '_cat_nodes':
                    response = connection.cat.nodes(format=format,
                                                    full_id=True,
                                                    h="*")
                elif command == '_cat_pending_tasks':
                    response = connection.cat.pending_tasks(format=format,
                                                            h="*")
                elif command == '_cat_plugins':
                    response = connection.cat.plugins(format=format, h="*")
                elif command == '_cat_recovery':
                    response = connection.cat.recovery(format=format, h="*")
                elif command == '_cat_thread_pool':
                    response = connection.cat.thread_pool(format=format, h="*")
                elif command == '_cat_shards':
                    response = connection.cat.shards(format=format, h="*")
                elif command == '_cat_segments':
                    response = connection.cat.segments(format=format, h="*")

        # summary = DiagnosticsService().get_diagnostics_summary(cluster_name)
        return APIResponse(response, HTTP_Status.OK, None)