How-to monitor Elasticsearch queries
This article explains how the active check check_elasticsearch_query runs a search against Elasticsearch and reports the result based on the number of matches found.
LAST TESTED ON CHECKMK 2.4.0P19
Table of Contents
Overview
The active check check_elasticsearch_query runs a Kibana Query Language (KQL) search against one or more Elasticsearch indices within a defined time range and counts the number of matching results. Based on the thresholds you configure, it returns OK, WARN, or CRIT.
Unlike the Elasticsearch special agent, which checks the health of the Elasticsearch cluster itself, this check simply looks at the result of a specific search query. It is useful when you want to monitor how many events or log entries match certain criteria.
Configuration
To gather data from Elasticsearch, Checkmk builds a query DSL object based on your check configuration and sends it directly to the Elasticsearch instance. It uses your search pattern as a query string, along with any defined field names and the selected time range. Checkmk then performs a count operation to determine how many matching results are returned.
To add a new rule, go to Setup → Services → HTTP, TCP, email, ... → Query elasticsearch logs
Provide the following:
Item suffix
DNS host name or IP address
Username
Password
Protocol or specific port (if used)
Search pattern
Indices to query
Time range
The results may differ from what you see in tools like Kibana. Checkmk queries Elasticsearch directly and does not apply additional filters such as Kibana data views. If you rely on those filters, you must define the correct indices and search terms directly in the Checkmk check configuration.
If you need help building or adjusting Elasticsearch queries, contact your Elasticsearch administrator or vendor for assistance.
Example
In the example below, check_elasticsearch_query searches an Elasticsearch database for logs in all indices matching filebeat*. It uses the search pattern container.id: syslog.log and checks how many matching results were created within the last three days.
Based on this configuration, Checkmk builds a query DSL object and sends a count operation to the configured Elasticsearch server. The search term is used as a query string, and the time range is added as a filter.
Example HTTP GET request sent by the active check to the Elasticsearch server
GET https://localhost/filebeat*/_count
Example JSON body API request:
Body:
{"query": {"bool": {"must": [{"query_string": {"query": "container.id : \"syslog.log\""}}, {"range": {"@timestamp": {"gte": "now-259200s", "lt": "now"}}}]}}}Elasticsearch returns the total number of matching results from the count operation.
Checkmk then compares this number against the configured warning and critical thresholds and sets the service state to OK, WARN, or CRIT depending on the result.
Related articles