Anomaly Detection with Elasticsearch and Kibana
An anomaly is any unusual change in behavior. Anomalies in your time-series data can lead to valuable insights. For example, for IT infrastructure data, an anomaly in the memory or CPU usage metric might help you uncover early signs of a system failure.
Discovering anomalies using conventional methods such as creating visualizations and dashboards can be challenging. You can set an alert based on a static threshold, but this requires prior domain knowledge and is not adaptive to data that exhibits organic growth or seasonal behavior.
The anomaly detection feature automatically detects anomalies in your Elasticsearch data in near real-time using the Random Cut Forest (RCF) algorithm. RCF is an unsupervised machine learning algorithm that models a sketch of your incoming data stream to compute an
anomaly grade
and confidence score
value for each incoming data point. These values are used to differentiate an anomaly from normal variations.
You can pair the anomaly detection plugin with the alerting plugin to notify you as soon as an anomaly is detected.
So, lets see how to use this Open Source Anomaly detection feature in Open Distro for Elasticsearch. Please visit my earlier post for installation of Open Distro Elasticsearch and related plugins.
Create a Detector
A detector is an individual anomaly detection task. You can create multiple detectors, and all the detectors can run simultaneously, with each analyzing data from different sources. Follow below steps to create a detector.- Login to Kibana http://localhost:5601/ and go to Anomaly Detector
- Click on Create Detector
- Enter the Name of the detector and a brief Description. Make sure the name that you enter is unique and descriptive enough to help you to identify the purpose of this detector.
- For Data source, choose the index that you want to use as the data source. You can optionally use index patterns to choose multiple indices.
- Choose the Timestamp field in your index.
- For Data filter, you can optionally filter the index that you chose as the data source. From the Filter type menu, choose Visual filter, and then design your filter query by selecting Fields, Operator, and Value, or choose Custom Expression and add in your own JSON filter query.
- For Detector operation settings, define the Detector interval to set the time interval at which the detector collects data.
- Click on Create
Add features to your detector
In this case, a feature is the field in your index that you want to check for anomalies. A detector can discover anomalies across one or more features. You must choose an aggregation method for each feature:average()
, count()
, sum()
, min()
, or max()
. The aggregation method determines what constitutes an anomaly.
For example, if you choose min()
, the detector focuses on finding anomalies based on the minimum values of your feature. If you choose average()
, the detector finds anomalies based on the average values of your feature.
Follow below steps to add features:
- On the Model configuration page, enter the Feature name.
- For Find anomalies based on, choose the method to find anomalies. For Field Value menu, choose the field and the aggregation method. Or choose Custom expression, and add in your own JSON aggregation query.
- Click on save And Start
Observe the results
Choose the Anomaly results tab. You will have to wait for some time to see the anomaly results. If the detector interval is 10 minutes, the detector might take more than an hour to start, as it’s waiting for sufficient data to generate anomalies. A shorter interval means the model passes the shingle process more quickly and starts to generate the anomaly results sooner. Use the profile detector operation to make sure you check you have sufficient data points.- The Live anomalies chart displays the live anomaly results for the last 60 intervals. For example, if the interval is set to 10, it shows the results for the last 600 minutes. This chart refreshes every 30 seconds.
- The Anomaly history chart plots the anomaly grade with the corresponding measure of confidence.
- The Feature breakdown graph plots the features based on the aggregation method. You can vary the date-time range of the detector.
- The Anomaly occurrence table shows the
Start time
,End time
,Data confidence
, andAnomaly grade
for each anomaly detected.
Set up alerts
To create a monitor to send you notifications when any anomalies are detected, choose Set up alerts. You’re redirected to the Alerting, Add monitor page. For steps to create a monitor and set notifications based on your anomaly detector, see my earlier post to create a Monitor. If you stop or delete a detector, make sure to delete any monitors associated with the detector.Adjust the model
To see all the configuration settings, choose the Detector configuration tab.- To make any changes to the detector configuration, or fine tune the time interval to minimize any false positives, in the Detector configuration section, choose Edit.
- You need to stop the detector to change the detector configuration. In the pop-up box, confirm that you want to stop the detector and proceed.
- To enable or disable features, in the Features section, choose Edit and adjust the feature settings as needed. After you make your changes, choose Save and start detector.
- Choose between automatically starting the detector (recommended) or manually starting the detector at a later time.
Analyze historical data
Analyzing historical data helps you get familiar with the anomaly detection plugin. You can also evaluate the performance of a detector with historical data to further fine-tune it. Follow below steps to analyze historical data- Choose Historical detectors and Create historical detector.
- Enter the Name of the detector and a brief Description.
- For Data source, choose the index that you want to use as the data source. You can optionally use index patterns to choose multiple indices.
- For Time range, select a time range for historical analysis.
- For Detector settings, choose to use settings of an existing detector. Or choose the Timestamp field in your index, add individual features to the detector, and set the detector interval.
- You can choose to run the historical detector automatically after creating.
- Choose Create.
Manage your detectors
Go to the Detector details page to change or delete your detectors.- To make changes to your detector, choose the detector name to open the detector details page.
- Choose Actions, and then choose Edit detector.
- You need to stop the detector to change the detector configuration. In the pop-up box, confirm that you want to stop the detector and proceed.
- After making your changes, choose Save changes.
- To delete your detector, choose Actions, and then choose Delete detector.
- In the pop-up box, type
delete
to confirm and choose Delete.
- In the pop-up box, type
Recommendation
If you want to go in depth and learn more about ELK stack I would recommend below books.- Learning Elastic Stack 7.0: Distributed search, analytics, and visualization using Elasticsearch, Logstash, Beats, and Kibana, 2nd Edition
- ELK Stack A Complete Guide – 2020 Edition
- Learning ELK Stack by Saurabh Chhajed