原文鏈接:http://www.ttlsa.com/elk/elk-packetbeat-deployment-guide/
Packetbeat 是一個實時網絡數據包分析工具,與elasticsearch一體來提供應用程序的監控和分析系統。
Packetbeat通過嗅探應用服務器之間的網絡通訊,來解碼應用層協議類型如HTTP、MySQL、redis等等,關聯請求與響應,並記錄每個事務有意義的字段。
Packetbeat可以幫助我們快速發現后端應用程序的問題,如bug或性能問題等等,修復排除故障也很快捷。
Packetbeat目前支持的協議有:
- HTTP
- MySQL
- PostgreSQL
- Redis
- Thrift-RPC
- MongoDB
- DNS
- Memcache
Packetbeat可以將相關事務直接插入到elasticsearch或redis(不推薦)或logstash。
Packetbeat可以運行在應用服務器上或者獨自的服務器。當運行在獨自服務器上時,需要從交換機的鏡像端口或者竊聽設備上獲取網絡流量。
對第七層信息解碼后,Packetbeat關聯與請求相關的響應,稱之為事務。每個事務,Packetbeat插入一個json格式文檔到elasticsearch。然后可通過kibana進行分析展示。
安裝
先配置beats yum 源,參見前文。
# yum install packetbeat
配置
選擇要從哪個網卡嗅探網絡通訊,默認是所有的網絡接口。
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))"。
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配置項,下文再說了。