ELK Packetbeat 部署指南

原文連接:http://www.ttlsa.com/elk/elk-packetbeat-deployment-guide/html

Packetbeat 是一個實時網絡數據包分析工具,與elasticsearch一體來提供應用程序的監控和分析系統。mysql

Packetbeat經過嗅探應用服務器之間的網絡通信,來解碼應用層協議類型如HTTP、MySQL、redis等等,關聯請求與響應,並記錄每一個事務有意義的字段。redis

Packetbeat能夠幫助咱們快速發現後端應用程序的問題,如bug或性能問題等等,修復排除故障也很快捷。sql

Packetbeat目前支持的協議有:json

  • HTTP
  • MySQL
  • PostgreSQL
  • Redis
  • Thrift-RPC
  • MongoDB
  • DNS
  • Memcache

Packetbeat能夠將相關事務直接插入到elasticsearch或redis(不推薦)或logstash。後端

Packetbeat能夠運行在應用服務器上或者獨自的服務器。當運行在獨自服務器上時,須要從交換機的鏡像端口或者竊聽設備上獲取網絡流量。服務器

對第七層信息解碼後,Packetbeat關聯與請求相關的響應,稱之爲事務。每一個事務,Packetbeat插入一個json格式文檔到elasticsearch。而後可經過kibana進行分析展現。網絡

安裝

先配置beats yum 源,參見前文。app

# yum install packetbeat

配置

選擇要從哪一個網卡嗅探網絡通信,默認是全部的網絡接口。curl

interfaces:
  # Select on which network interfaces to sniff. You can use the "any"
  # keyword to sniff on all connected interfaces.
  device: any

在協議部分,配置端口以便Packetbeat找到每一個端口對應的協議。若是使用非標準端口,須要添加上。多個端口以逗號分隔。

protocols:
  # Configure which protocols to monitor and on which ports are they
  # running. You can disable a given protocol by commenting out its
  # configuration.
  http:
    ports: [80, 8080, 8081, 5000, 8002]
 
  memcache:
    ports: [11211]
 
  mysql:
    ports: [3306]
 
  redis:
    ports: [6379]
 
  pgsql:
    ports: [5432]
 
  thrift:
    ports: [9090]

定義elasticsearch服務

output:
 
  elasticsearch:
    # Uncomment out this option if you want to output to Elasticsearch. The
    # default is false.
    enabled: true
 
    # Set the host and port where to find Elasticsearch.
    host: 192.168.1.42
    port: 9200
 
    # Uncomment this option and set it to true if you want to store the topology in
    # Elasticsearch. Default behavior if this setting is left out of the
    # config file is equivalent to setting "save_topology" to "false"
    #save_topology: false

加載elasticsearch索引模板

加載索引模板,以便elasticsearch知道哪些字段該以何種方式進行分析。

# curl -XPUT 'http://10.1.19.18:9200/_template/packetbeat' -d@/etc/packetbeat/packetbeat.template.json

啓動服務

# /etc/init.d/packetbeat start

查看數據

加載kibana Packetbeat的儀表盤

這個在前面的文章中,有加載過。這裏再也不重複加載。

配置選項

beats公用的配置選前文有說的。下面說說Packetbeat自有的配置項:Interfaces、Protocols、Processes(可選)。

interfaces

interfaces 部分配置嗅探器

# Select the network interfaces to sniff the data. You can use the "any"
# keyword to sniff on all connected interfaces.
interfaces:
  # On which device to sniff
  device: any
 
  # The maximum capture size of a single packet.
  snaplen: 1514
 
  # The type of the sniffer to use
  type: af_packet
 
  # The size of the sniffing buffer
  buffer_size_mb: 100

device

從哪一個網絡接口捕獲通信。指定的設備自動設置爲混雜模式,這意味着Packetbeat能夠從同一個LAN捕獲其它主機的流量。

interfaces:
  device: eth0

在Linux上,能夠指定任何的設備。當指定爲any時,接口不會設置成混雜模式。

查看可用設備,可使用下面的命令:

# packetbeat -devices
0: eth0 (No description available)
1: eth1 (No description available)
2: usbmon1 (USB bus number 1)
3: any (Pseudo-device that captures on all interfaces)
4: lo (No description available)

device能夠指定爲上述返回列表的索引,如

interfaces:
  device: 0

表示是eth0。這對於設備名稱很長的狀況下很是有用咯。

snaplen

捕獲包的最大大小。默認65535。足夠應付全部網絡和接口類型。若是嗅探物理網絡接口,該值設置爲MTU大小。對於虛擬接口,仍是最好使用默認值。

interfaces:
  device: eth0
  snaplen: 1514

type

Packetbeat 支持下面的嗅探器類型:

  • pcap, 使用libpcap 庫,可工做在大多數平臺上,可是不是最快的選項。
  • af_packet, 使用 memory-mapped 嗅探。比 libpcap 快且不須要kernel模塊,Linux特定的。
  • pf_ring, 使用 ntop.org 項目。此設置提供了最好的嗅探速度,可是須要一個kernel模塊,Linux特定的。

默認的嗅探器類型是pcap。

interfaces:
  device: eth0
  type: af_packet

在Linux上,若是想優化Packetbeat耗CPU佔用率,建議使用 af_packet 和 pf_ring 選項。

若是使用 af_packet, 能夠經過下面選項調整行爲:

buffer_size_mb

內核和用戶空間之間使用的最大共享內存緩衝區大小。默認30MB。緩衝區越大,CPU使用率越低,可是會消耗更多內存。只對af_packet 有效。

interfaces:
  device: eth0
  type: af_packet
  buffer_size_mb: 100

with_vlans

Packetbeat 自動生成一個BPF來捕獲已知協議的端口流量。 例如,配置HTTP 80 和 MySQL 3306,  Packetbeat 生成 BPF 過濾器以下: "port 80 or port 3306"。

然而,若是通信包含VLAN標記,Packetbeat生成的過濾器將是無效的,由於offset經過四個字節移動的。爲了解決這個問題,啓用 with_vlans 選項,生成的 BPF 過濾器是這樣的: "port 80 or port 3306 or (vlan and (port 80 or port 3306))"。

bpf_filter

Packetbeat 自動生成一個BPF來捕獲已知協議的端口流量。 例如,配置HTTP 80 和 MySQL 3306,  Packetbeat 生成 BPF 過濾器以下: "port 80 or port 3306"。

可使用 bpf_filter 覆蓋生成的BPF 過濾器,如:

interfaces:
  device: eth0
  bpf_filter: "net 192.168.238.0/0 and port 80 and port 3306"

此設置是禁用自動生成的BPF過濾器。若是使用此設置,你須要保持BPF過濾器與協議部分定義的端口同步。

Protocols和Processes配置項,下文再說了。

相關文章
相關標籤/搜索