1. 程式人生 > >Beats數據采集

Beats數據采集

clas 安裝 所有權 所在 sage star 接收 protocol json

Beats數據采集

Beats是elastic公司的一款輕量級數據采集產品,它包含了幾個子產品:

packetbeat(用於監控網絡流量)、

filebeat(用於監聽日誌數據,可以替代logstash-input-file)、

topbeat(用於搜集進程的信息、負載、內存、磁盤等數據)、

winlogbeat(用於搜集windows事件日誌)

另外社區還提供了dockerbeat等工具。由於他們都是基於libbeat寫出來的,因此配置上基本相同,只是input輸入的地方各有差異。

本文按照如下的內容依次進行介紹:

背景知識:關於Powershell的使用
packetbeat的下載、部署、使用、結果樣例

filebeat的下載、部署、使用、樣例
topbeat的樣例
winlogbeat的樣例

關於Powershell
如果你是想在linux下使用,那麽可以跳過本節。

elastic中的Beats在windows環境中基本都是使用Powershell的腳本,因此用戶必須對Powershell有一定的了解。Powershell可以理解成windows對命令行的高級封裝,加了個殼,從而支持更多高級的用法。在windows7開始,系統就內置了Powershell工具。因此如果你的系統是xp這種比較老的版本,就需要自己安裝Powershell了。

啟動Powershell
在windows下,有兩種方式打開Powershell(要以管理員的身份打開)。

通過圖標打開
在windows下開啟搜索,輸入powershell,右鍵以管理員身份運行。

通過命令行啟動
在系統路徑C:\Windows\System32下,以管理員身份啟動cmd.exe(右鍵選擇 以管理員身份運行)。

輸入命令Powershell,進入Powershell命令窗口。

C:\Windows\system32>Powershell
Windows PowerShell
版權所有 (C) 2009 Microsoft Corporation。保留所有權利。

PS C:\Windows\system32>
開啟腳本限制
默認的情況下,系統會禁止運行腳本,返回下面的錯誤提示:

PS E:\packetbeat> .\install-service-packetbeat.ps1
無法加載文件 E:\packetbeat\install-service-packetbeat.ps1,因為在此系統中禁止執
行腳本。有關詳細信息,請參閱 "get-help about_signing"。
所在位置 行:1 字符: 33

  • .\install-service-packetbeat.ps1 <<<<
    • CategoryInfo : NotSpecified: (:) [], PSSecurityException
    • FullyQualifiedErrorId : RuntimeException
      需要修改該參數執行下面的命令,開啟Powershell腳本功能:

PS E:\packetbeat> set-ExecutionPolicy RemoteSigned
Packetbeat 網絡流量監控
Packetbeat屬於beats產品的一部分,專門負責網絡數據包分析,可以:

針對特定的網卡監聽流量;
可以設置相關的監聽對象和端口號,支持dns,http,memcahce,mysql,pgsql,redis,thrift,mongodb等;
可以輸出到特定的目的地,如elasticsearch、logstash、file、console等。
下載
https://www.elastic.co/downloads/beats

部署
linux環境
第一步,解壓縮
下載.tar.gz的安裝包後,解壓:

tar -zxvf packetbeat-1.2.3-x86_64.tar.gz
進入解壓後的文件夾,裏面有3個文件:

--- packetbeat #啟動文件
--- packetbeat.template.json #Elasticsearch中的映射配置
--- packetbeat.yml #Packetbeat的配置文件
第二步,修改配置文件
配置文件包括了幾大部分:

配置網絡監聽的顯卡

interfaces:
device:any

配置協議

protocols:
http:
ports:[80,8080,9000]
redis:
ports:[6379]

配置輸出

output:
elasticsearch:
hosts:["localhost:9200"]
inex:"packetbeat"
template:
name:"packetbeat"
path:"packetbeat.template.json"
overwrite:false
logstah:
hosts:["localhost:5044"]
file:
path:"/tmp/packetbeat"
filename:packetbeat
console:
shipper:
logging:
第三步,運行
正常的運行:

./packetbeat
如果想要後臺運行,則可以像下面這樣:

nohup ./packetbeat &
默認日誌都會輸出到nohup.out中。

windows環境
第一步,解壓
相比linux,多了兩個powershell的腳本。

--- install-service-packetbeat.ps1 # 註冊腳本
--- uninstall-service-packetbeat.ps1 # 註銷腳本
--- packetbeat.exe #啟動文件
--- packetbeat.template.json #Elasticsearch中的映射配置
--- packetbeat.yml #Packetbeat的配置文件
第二步,以管理員身份進入命令行,運行註冊腳本
進入指定的目錄,運行註冊腳本。

.\install-service-winlogbeat.ps1
第三步,啟動服務
Start-Service packetbeat.exe
對接Elasticsearch
Packetbeat配置如下:

elasticsearch:
hosts: ["localhost:9200"]
index: "packetbeat"
template:
name: "packetbeat"
path: "packetbeat.template.json"
對接logstash
Packetbeat配置如下:

logstash:
# The Logstash hosts
hosts: ["localhost:5044"]
logstash采用logstash-input-beats接收,配置可以參考如下:

input{
beats{
port => 5044
}
stdin{}
}
output{
stdout{
codec => rubydebug
}
file{
path => "E:\server.log"
}
}
存儲到file
PacketBeat配置:

file:
path: "E:/packetbeat"
filename: packetbeat
默認是按照文件大小輪詢。

日誌管理
日誌可以設置輸出的位置,以及級別。跟平常使用的log4j差不多:

logging:
files:
path: E:/mybeat
name: mybeat
level: debug
Packetbeat監聽到的內容
{
"_index": "packetbeat-2016.08.01",
"_type": "dns",
"_id": "AVZELeQzbZnlZq0jh6Vk",
"_version": 1,
"_score": 1,
"_source": {
"@timestamp": "2016-08-01T03:37:53.106Z",
"beat": {
"hostname": "XINGHL",
"name": "XINGHL"
},
"bytes_in": 31,
"bytes_out": 260,
"client_ip": "10.4.45.44",
"client_port": 51599,
"client_proc": "",
"client_server": "",
"count": 1,
"direction": "out",
"dns": {
"additionals": [
{
"class": "IN",
"data": "115.239.210.176",
"name": "ns4.a.shifen.com",
"ttl": 281,
"type": "A"
},
{
"class": "IN",
"data": "119.75.222.17",
"name": "ns5.a.shifen.com",
"ttl": 281,
"type": "A"
},
{
"class": "IN",
"data": "61.135.165.224",
"name": "ns1.a.shifen.com",
"ttl": 281,
"type": "A"
},
{
"class": "IN",
"data": "180.149.133.241",
"name": "ns2.a.shifen.com",
"ttl": 281,
"type": "A"
},
{
"class": "IN",
"data": "61.135.162.215",
"name": "ns3.a.shifen.com",
"ttl": 281,
"type": "A"
}
],
"additionals_count": 5,
"answers": [
{
"class": "IN",
"data": "www.a.shifen.com",
"name": "sp1.baidu.com",
"ttl": 33,
"type": "CNAME"
},
{
"class": "IN",
"data": "61.135.169.125",
"name": "www.a.shifen.com",
"ttl": 282,
"type": "A"
},
{
"class": "IN",
"data": "61.135.169.121",
"name": "www.a.shifen.com",
"ttl": 282,
"type": "A"
}
],
"answers_count": 3,
"authorities": [
{
"class": "IN",
"data": "ns5.a.shifen.com",
"name": "a.shifen.com",
"ttl": 1182,
"type": "NS"
},
{
"class": "IN",
"data": "ns1.a.shifen.com",
"name": "a.shifen.com",
"ttl": 1182,
"type": "NS"
},
{
"class": "IN",
"data": "ns3.a.shifen.com",
"name": "a.shifen.com",
"ttl": 1182,
"type": "NS"
},
{
"class": "IN",
"data": "ns2.a.shifen.com",
"name": "a.shifen.com",
"ttl": 1182,
"type": "NS"
},
{
"class": "IN",
"data": "ns4.a.shifen.com",
"name": "a.shifen.com",
"ttl": 1182,
"type": "NS"
}
],
"authorities_count": 5,
"flags": {
"authoritative": false,
"recursion_allowed": true,
"recursion_desired": true,
"truncated_response": false
},
"id": 32509,
"op_code": "QUERY",
"question": {
"class": "IN",
"name": "sp1.baidu.com",
"type": "A"
},
"response_code": "NOERROR"
},
"ip": "210.83.210.155",
"method": "QUERY",
"port": 53,
"proc": "",
"query": "class IN, type A, sp1.baidu.com",
"resource": "sp1.baidu.com",
"responsetime": 1,
"server": "",
"status": "OK",
"transport": "udp",
"type": "dns"
}
}
filebeat 日誌監聽
filebeat是Beats的重要組成部分,它可以作為輕量級的數據采集引擎,替代之前的logstash-forward。

下載
https://www.elastic.co/downloads/beats

說明
filebeat.yml為filebeat的配置文件,包括下面幾個部分:

-- filebeat # 配置filebeat監聽的對象,即文件路徑或者目錄的路徑
-- output # 輸出配置,支持es,logstash,file,console等
-- shipper
-- logging # 配置日誌
filebeat.template.json 為默認提供的elasticsearch映射模板
filebeat為主要的執行程序

運行
linux環境
運行命令解壓安裝包——filebeat.tar.gz

tar -zxvf filebeat.tar.gz
編輯filebeat.yml

vim filebeat.yml
啟動filebeat

nohup ./filebeat &
windows環境
以管理員身份運行cmd, 並執行Powershell命令,進入PS模式.啟動filebeat註冊腳本:

C:\Windows\system32>Powershell
Windows PowerShell
版權所有 (C) 2009 Microsoft Corporation。保留所有權利。

PS C:\Windows\system32> e:
PS E:> cd .\filebeat-1.2.3-windows
PS E:\filebeat-1.2.3-windows> dir

目錄: E:\filebeat-1.2.3-windows

Mode LastWriteTime Length Name
---- ------------- ------ ----
----- 2016/5/18 4:33 10361856 filebeat.exe
----- 2016/5/18 4:33 814 filebeat.template.json
----- 2016/5/18 4:33 17533 filebeat.yml
----- 2016/5/18 4:33 442 install-service-filebeat.ps1
----- 2016/5/18 4:33 184 uninstall-service-filebeat.ps1

PS E:\filebeat-1.2.3-windows> .\install-service-filebeat.ps1

Status Name DisplayName
------ ---- -----------
Stopped filebeat filebeat

編輯配置文件,filebeat.yml

啟動filebeat文件

PS E:\filebeat-1.2.3-windows> Start-Service filebeat
樣例
{
"_index": "filebeat-2016.08.01",
"_type": "log",
"_id": "AVZE1AMfbZnlZq0jh6cF",
"_version": 1,
"_score": 1,
"_source": {
"@timestamp": "2016-08-01T06:39:15.193Z",
"beat": {
"hostname": "XINGHL",
"name": "XINGHL"
},
"count": 1,
"fields": null,
"input_type": "log",
"message": "hello filebeat",
"offset": 22988,
"source": "e:\logs\test.log",
"type": "log"
}
}
topbeat 監聽進程資源信息
啟動方式與前面幾種類似,這裏就不過多贅述了。

topbeat - windows版
{
"_index": "topbeat-windows-2016.08.01",
"_type": "process",
"_id": "AVZE7zC6bZnlZq0jh8QD",
"_version": 1,
"_score": 1,
"_source": {
"@timestamp": "2016-08-01T07:09:01.206Z",
"beat": {
"hostname": "XINGHL",
"name": "XINGHL"
},
"count": 1,
"proc": {
"cmdline": "%SystemRoot%\system32\csrss.exe ObjectDirectory=\Windows SharedSection=1024,20480,768 Windows=On SubSystemType=Windows ServerDll=basesrv,1 ServerDll=winsrv:UserServerDllInitialization,3 ServerDll=winsrv:ConServerDllInitialization,2 ServerDll=sxssrv,4 ProfileControl=Off MaxRequestThreads=16",
"cpu": {
"user": 5538,
"user_p": 0,
"system": 7753,
"total": 13291,
"start_time": "Jan01"
},
"mem": {
"size": 3391488,
"rss": 3366912,
"rss_p": 0,
"share": 0
},
"name": "csrss.exe",
"pid": 544,
"ppid": 0,
"state": "running",
"username": "NT AUTHORITY\SYSTEM"
},
"type": "process"
}
}
topbeat - linux版本
{
"_index": "topbeat-2016.08.01",
"_type": "process",
"_id": "AVZE6Mh4bZnlZq0jh6jT",
"_version": 1,
"_score": 1,
"_source": {
"@timestamp": "2016-08-01T07:01:09.641Z",
"beat": {
"hostname": "10.0.67.101",
"name": "10.0.67.101"
},
"count": 1,
"proc": {
"cpu": {
"user": 0,
"user_p": 0,
"system": 0,
"total": 0,
"start_time": "Jul06"
},
"mem": {
"size": 0,
"rss": 0,
"rss_p": 0,
"share": 0
},
"name": "migration/0",
"pid": 5,
"ppid": 2,
"state": "sleeping",
"username": "root"
},
"type": "process"
}
}
winlogbeat windows事件監聽
啟動方式與前面幾種類似,這裏就不過多贅述了。

{
"_index": "winlogbeat-2015.11.09",
"_type": "wineventlog",
"_id": "AVZE_J7FbZnlZq0jh_sL",
"_version": 1,
"_score": 1,
"_source": {
"@timestamp": "2015-11-09T00:28:50.953Z",
"beat": {
"hostname": "XINGHL",
"name": "XINGHL"
},
"computer_name": "xinghailong",
"count": 1,
"event_id": 35,
"level": "信息",
"log_name": "System",
"message": "時間服務現在用時間源 time.neusoft.com,0x9 (ntp.m|0x9|0.0.0.0:123->202.118.6.8:123) 同步系統時間。",
"record_number": "25479",
"source_name": "Microsoft-Windows-Time-Service",
"type": "wineventlog",
"user": {
"domain": "NT AUTHORITY",
"identifier": "S-1-5-19",
"name": "LOCAL SERVICE",
"type": "Well Known Group"
}
}
}
參考
1 官方文檔

2 ELK Beats文檔

博客不再維護,請關註新的博客:https://xinghalo.github.io/

http://www.cnblogs.com/xing901022/category/677227.html
http://www.cnblogs.com/xing901022/category/642865.html

Beats數據采集