A Quick Guide to Analyzing Apache Logs on Alibaba Cloud Log Service

简介: This article describes how you can deploy Logstash and Kibana on Alibaba Cloud Log Service to monitor, analyze, and visualize Apache logs.

With Alibaba Cloud Log Service, there are several methods available for you to collect upstream data. You can use the built-in LogSearch and LogAnalytics functions, or you can deploy the more familiar ElasticSearch, Logstash, and Kibana (ELK) stack. In this article, we will discuss how you can build your own ELK stack on Alibaba Cloud Log Service to analyze and monitor Apache logs.

Installing Logstash Within the ECS

First, we need to install and deploy Logstash within the ECS. When you subscribe to the ECS service, be sure to prepare JDK version 1.8 or higher.

wget https://artifacts.elastic.co/downloads/logstash/logstash-5.5.3.tar.gz

Decompress and install

tar -xzvf logstash-5.5.3.tar.gz 

Establishing the Logstash Pipeline

In order to write data to ElasticSearch with Logstash, first we need to establish a Logstash pipeline, which has three parts:

input {   
}
# a note in this section indicates that this filter can be selected
filter {  
}
output {   
}

  • Set input to the data source
  • Set output to the target
  • A filter is optional, you can normally use it to set data filtering logic

Settings for this section are quite simple. Create a .conf file in the Logstash directory, then set input and output according to the following format:

input {
    file {
        path => "/usr/local/demoData/*.log"
        start_position => beginning
    }
}
output {
    ElasticSearch {
        hosts => ["http://*******************:9200"]
        user => "*******"
        password => "***********"
    }
}

Note: Because ElasticSearch is preset with the X-Pack plugin, you must verify all access. This will require you to set a username and password in the output.

Let us take a case where we need to send the Apache log indexing frequently generated by Alibaba Cloud ECS to ElasticSearch. We can deploy Logstash to the ECS on which the web server is running. If there are concerns about this affecting the application running on the web server, you can deploy Logstash to any accessible ECS over the network.

Note: Logstash input can handle different forms of input. If you have deployed a Logstash to a network-accessible ECS, you will need to configure an http template as an input as follows:

input {
 http {
      host => "**********"
   port => "**********"
 }
}

Because ElasticSearch is deployed in a VPC environment, if the ECS on which Logstash is deployed is on a classic network, then the VPC needs to be connected to via the Classiclink method.

Analyzing Apache Logs Using Logstash Filter

Let us now see how one can quickly analyze Apache logs using a Logstash filter. An Apache log typically contains the following information:

1

Table 1

To retrieve user distribution information from the log and make it more intuitive for non-technical users, we can use the Gork filter to analyze the Apache network logs.

filter {
    grok {
        match => { "message" => "%{COMBINEDAPACHELOG}"}
    }
}

We can take the original log information:

66.249.73.135 - - [04/Jan/2015:05:30:06 +0000] "GET /blog/web/firefox-scrolling-fix.html HTTP/1.1" 200 8956 "-" "Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"

Then filter it into standard JSON structure:

{
"clientip" : "66.249.73.135",
"ident" : ,
"auth" : ,
"timestamp" : "04/Jan/2015:05:30:06 +0000",
"verb" : "GET",
"request" : "/blog/web/firefox-scrolling-fix.html",
"httpversion" : "HTTP/1.1",
"response" : "200",
"bytes" : "8956",
"referrer" : "http://www.google.com/bot.html",
"agent" : "Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25"
}

We can then extract the IP to discern the user location using geoip.

filter {
    geoip {
        source => "clientip"
    }
}

Once we have the address information from the IP, we can enter a geoip field into the log information. We can receive the following information by checking an IP with geoip:

"geoip":{
        "timezone":"America/Los_Angeles",
        "ip":"66.249.73.135",
        "latitude":37.419200000000004,
        "continent_code":"NA",
        "city_name":"Mountain View",
        "country_name":"United States",
        "country_code2":"US",
        "dma_code":807,
        "country_code3":"US",
        "region_name":"California",
        "location":{
               "lon":-122.0574,
               "lat":37.419200000000004
        },
        "postal_code":"94043",
        "region_code":"CA",
        "longitude":-122.0574
},

Using Kibana, we can use the coordinate information stored in the location key from geoip. Subsequently, we can then create a visualization of the geographic distribution of users’ access locations.

With the above method, we can analyze ECS logs in batch and complete the configuration in Kibana.

You can get more information on Configuring Logstash here.

Conclusion

You can analyze and monitor logs with the LogSearch and LogAnalytics on Alibaba Cloud Log Service, or deploy your own ElasticSearch, Logstash, and Kibana (ELK) stack. Each option comes with its own set of benefits, and the effectiveness is highly dependent on your application.

I hope this blog helped you understand how you can install Logstash on Alibaba Cloud ECS and use it for analysis of Apache logs. To know more about Alibaba Cloud Log Service, visit the official product page or the official product documentation.

相关实践学习
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
目录
相关文章
|
存储 算法 C#
Leetcode算法系列| 8. 字符串转换整数 (atoi)
Leetcode算法系列| 8. 字符串转换整数 (atoi)
|
区块链
合约跟单量化系统开发规则详解 | 合约跟单量化系统开发源码示例
合约跟单量化系统是一种利用量化策略进行跟单交易的系统。它通过选择合适的交易员进行跟单,从而实现傻瓜式盈利。合约跟单系统需要一个交易员榜单界面,用户可以选择要跟单的交易员、交易币种和合约类型,进入跟单的入口,输入跟单金额、止盈止损比例,点击开始跟单。交易员可以获取额外的收益,而跟随者可以找到合适的交易员,实现傻瓜式盈利。
|
移动开发 小程序 Android开发
天猫精灵的初体验
新技能创建完整步骤
591 0
天猫精灵的初体验
|
JavaScript 前端开发 Java
Java和JavaSciprt比较
JavaScript 与Java是两个公司开发的不同的两个产品。Java 是原Sun Microsystems公司推出的面向对象的程序设计语言,特别适合于互联网应用程序开发;而JavaScript是Netscape公司的产品,为了扩展Netscape浏览器的功能而开发的一种可以嵌入Web页面中运行的基于对象和事件驱动的解释性语言。
1162 0
|
C语言
C语言如何跨文件调用函数定义中声明的变量
前几天向老师请教问题,结果被打击的体无完肤,评价为基础太差。于是敝人埋头研究了几日,颇有所得,之前困扰我许久的作用域问题也都不攻自破,不由得感叹,有个老师指导真好hhh
4698 0
|
17小时前
|
数据采集 人工智能 安全
|
9天前
|
云安全 监控 安全
|
1天前
|
自然语言处理 API
万相 Wan2.6 全新升级发布!人人都能当导演的时代来了
通义万相2.6全新升级,支持文生图、图生视频、文生视频,打造电影级创作体验。智能分镜、角色扮演、音画同步,让创意一键成片,大众也能轻松制作高质量短视频。
796 150