Skip to content

Latest commit

 

History

History
55 lines (43 loc) · 2.62 KB

04_find_servers_with_high_memory_usage.md

File metadata and controls

55 lines (43 loc) · 2.62 KB

Find servers with high memory usage

from opensearchpy import OpenSearch

# OpenSearch configuration
opensearch_uri = 'YOUR OPENSEARCH URI GOES HERE'  # Replace with your OpenSearch server URI
index_name = 'server_logs'  # Replace with the desired index name
threshold = 80

# Establish connection to OpenSearch
opensearch = OpenSearch(opensearch_uri)

# Get servers with high memory usage
def get_servers_with_high_memory_usage(threshold):
    query = {
        "query": {
            "range": {
                "server_memory_used": {
                    "gt": threshold
                }
            }
        }
    }
    response = opensearch.search(index='server-logs', body=query, size=10)
    return response['hits']['hits']

# Usage
servers = get_servers_with_high_memory_usage(threshold)
for server in servers:
    print(server['_source'])

Let's understand what this program is doing:

  1. The program connects to the OpenSearch cluster using the client library.
  2. It specifies the index name where the server logs are stored and as well as the index name.
  3. The program defines a search query that searches for servers with memory usage greater than a specified threshold.
  4. The search query uses a "range" query to match documents where the "server_memory_used" field is greater than the threshold value.
  5. The program executes the search query against the OpenSearch cluster and retrieves the response.
  6. The response contains the matching documents (server logs) that satisfy the memory usage condition. In the given line of code:
response = opensearch.search(index='server-logs', body=query, size=10)

The size=10 parameter is used to specify the number of search results (documents) to be returned by the search query. It determines the maximum number of documents that will be included in the response.

In this case, size=10 means that the search query will return a maximum of 10 documents that match the search criteria. If there are more than 10 matching documents in the index, only the top 10 results will be returned in the response.

By adjusting the size parameter, you can control the number of search results you want to retrieve from the OpenSearch index. 7. The program processes the response and extracts the relevant information, such as the server ID, memory usage, and operating system. 8. It then prints the server details for each matching server log.

In summary, this program allows you to find servers with high memory usage by querying the OpenSearch cluster for server logs that meet the specified criteria. It helps you identify servers that may require attention or optimization due to their memory usage.