Monitor log keywords using Logstash

Updated at:

This document describes how to monitor log keywords using Logstash and the Event Hub standard integration.

Monitoring log keywords is a common Operations and Maintenance (O&M) task. It helps you quickly identify issues in your applications or business. For example, you can monitor for the `gc` keyword in Java Virtual Machine (JVM) logs or the `error` keyword in business logs. This document explains how to use Logstash to collect and push these abnormal logs.

Log construction

This guide uses the following `test.log` file for demonstration and validation.

2021-08-11T00:34:06+08:00 in shanghai,tradeplatform occur P1 ,112.11.12.11keywords error,message:Warning tradeplatform has some exception,content service exception;envirment type:online;group:aliyun;tags:xxxxx

Push logs to standard integration

  1. Download and install the Logstash 8.15.1 component.

  2. Modify the `logstash.conf` configuration file to push abnormal logs.

  • Find the path of the `logstash.conf` file:

    sudo find / -name logstash.conf
  • If this file does not exist, create it in the following path:

    sudo vi /etc/logstash/conf.d/logstash.conf
  • The `logstash.conf` configuration file is as follows:

    input {
            beats {
                    port => 5044
            }
      
            file {
                    path => "/home/test.log"
      type => "test"
            }
    }
    
    filter {
        if [type]=="test" {
            grok {
                    match => {
       "message" => "%{TIMESTAMP_ISO8601:timestamp} in %{DATA:region},%{DATA:application} occur %{DATA:level},%{IPV4:source}%{DATA:name},message:%{DATA:summary},content%{DATA:details};envirment type:%{DATA:class};group:%{DATA:group};tags:%{DATA:tag}"
                    }
            }
     mutate {
                    remove_field => ["host"]
                    remove_field => ["@version"]
                    remove_field => ["@timestamp"]
                    remove_field => ["path"]  
                    remove_field => ["message"]
            }
        }
    }
    
    output {
        if [type]=="test" {
            http {
                    url => "Replace this with the URL from the standard integration"
                    http_method => "post"
                    format => "json"
            }
        }
    
    }
    Note

    The URL is for the standard integration. To obtain this URL, go to the Event Hub console. In the navigation pane on the left, choose Integration Center > Integration Configuration. The URL is available in the standard integration settings. It looks like this: `https://public-alert.aliyuncs.com/event/standard/8cd0b46766dd4e2488a5cd5663xxxxxx`.

  1. Start Logstash to begin pushing abnormal logs to Event Hub.

  2. To view the data format in the standard integration, manually generate a log entry for this example.

    sudo echo '2021-08-11T00:34:06+08:00 in shanghai,tradeplatform occur P1 ,112.11.12.11keywords error,message:Warning tradeplatform has some exception,content service exception;envirment type:online;group:aliyun;tags:xxxxx' >> /home/test.log 
  3. On the Standard Integration page in the O&M Event Center, click Integrate.

  4. Verify the integration configuration:

    On the Integration Center > Integration Configuration page in the O&M Event Center, an integration is successful if it is listed under Integrated and displays Alerts Received. If it displays No Alerts Received, the integration failed.

Configure the integration and event routing

  1. In the Event Hub console, choose Integration Center > Integration Configuration in the navigation pane on the left. Click Standard Integration to view the 10 most recent data records that were pushed.

  2. Go to Policy Center > Event Routing Rules and add a new rule. Because an `error` log entry indicates a core issue, this example sets the priority to P1 and selects phone calls for notifications.

  3. For this demo, manually generate a log entry to create a corresponding event. Run the following command.

    sudo echo '2021-08-11T00:34:06+08:00 in shanghai,tradeplatform occur P1 ,112.11.12.11keywords error,message:Warning tradeplatform has some exception,content service exception;envirment type:online;group:aliyun;tags:xxxxx' >> /home/test.log 
  4. In the Event Hub console, go to Event Hub > Events. You can view the event generated from the latest alert push. The assigned contact will also receive phone, text message, and email notifications.

Miscellaneous

This guide described how to push logs using Logstash and configure keywords in Event Hub to process events. You can also use other methods. For example, you can use other open source log components, such as Filebeat, instead of Logstash. You can also use a Shell script to read and push application logs.