玫瑰心语儿 發表於 2023-9-26 00:00:00

ELK Packetbeat 部署指南(15th)

<p>Packetbeat 是一个实时网络数据包分析工具,与elasticsearch一体来提供应用程序的监控和分析系统。</p>
<p>Packetbeat通过嗅探应用服务器之间的网络通讯,来解码应用层协议类型如HTTP、MySQL、redis等等,关联请求与响应,并记录每个事务有意义的字段。</p>
<p>Packetbeat可以帮助我们快速发现后端应用程序的问题,如bug或性能问题等等,修复排除故障也很快捷。</p>
<p>Packetbeat目前支持的协议有:</p>
<ul class="itemizedlist" type="disc">
<li class="listitem">HTTP</li>
<li class="listitem">MySQL</li>
<li class="listitem">PostgreSQL</li>
<li class="listitem">Redis</li>
<li class="listitem">Thrift-RPC</li>
<li class="listitem">MongoDB</li>
<li class="listitem">DNS</li>
<li class="listitem">Memcache</li>
</ul>
<p>Packetbeat可以将相关事务直接插入到elasticsearch或redis(不推荐)或logstash。</p>
<p>Packetbeat可以运行在应用服务器上或者独自的服务器。当运行在独自服务器上时,需要从交换机的镜像端口或者窃听设备上获取网络流量。</p>
<p>对第七层信息解码后,Packetbeat关联与请求相关的响应,称之为事务。每个事务,Packetbeat插入一个json格式文档到elasticsearch。然后可通过kibana进行分析展示。</p>
<h3>安装</h3>
<p>先配置beats yum 源,参见前文。</p><pre class="brush:bash;toolbar:false"># yum install packetbeat</pre><p></p>
<h3>配置</h3>
<p>选择要从哪个网卡嗅探网络通讯,默认是所有的网络接口。</p><pre class="brush:bash;toolbar:false">interfaces:
# Select on which network interfaces to sniff. You can use the "any"
# keyword to sniff on all connected interfaces.
device: any</pre><p>在协议部分,配置端口以便Packetbeat找到每个端口对应的协议。如果使用非标准端口,需要添加上。多个端口以逗号分隔。</p><pre class="brush:bash;toolbar:false">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:

memcache:
    ports:

mysql:
    ports:

redis:
    ports:

pgsql:
    ports:

thrift:
    ports: </pre><p>定义elasticsearch服务</p><pre class="brush:bash;toolbar:false">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</pre><p></p>
<h3>加载elasticsearch索引模板</h3>
<p>加载索引模板,以便elasticsearch知道哪些字段该以何种方式进行分析。</p><pre class="brush:bash;toolbar:false"># curl -XPUT 'http://10.1.19.18:9200/_template/packetbeat' -d@/etc/packetbeat/packetbeat.template.json</pre><p></p>
<h3>启动服务</h3>
<p></p><pre class="brush:bash;toolbar:false"># /etc/init.d/packetbeat start</pre><p>查看数据</p>
<p><img title="ELK Packetbeat 部署指南(15th)" class="alignnone wp-image-10757" src="https://zhuji.jb51.net/uploads/img/20230519/c55dd772aed2a537977e4f8a88819d9c.jpg" width="670" height="460"></p>
<h3>加载kibana Packetbeat的仪表盘</h3>
<p>这个在前面的文章中,有加载过。这里不再重复加载。</p>
<h3>配置选项</h3>
<p>beats公用的配置选前文有说的。下面说说Packetbeat自有的配置项:Interfaces、Protocols、Processes(可选)。</p>
<h4>interfaces</h4>
<p>interfaces 部分配置嗅探器</p><pre class="brush:bash;toolbar:false"># 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</pre><p></p>
<div class="titlepage">
<div>
<div>
<p class="title"><strong>device</strong></p>
</div>
</div>
</div>
<p>从哪个网络接口捕获通讯。指定的设备自动设置为混杂模式,这意味着Packetbeat可以从同一个LAN捕获其它主机的流量。</p><pre class="brush:bash;toolbar:false">interfaces:
device: eth0</pre><p>在Linux上,可以指定任何的设备。当指定为any时,接口不会设置成混杂模式。</p>
<p>查看可用设备,可以使用下面的命令:</p><pre class="brush:bash;toolbar:false"># 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)</pre><p>device可以指定为上述返回列表的索引,如</p><pre class="brush:bash;toolbar:false">interfaces:
device: 0</pre><p>表示是eth0。这对于设备名称很长的情况下非常有用咯。</p>
<div class="titlepage">
<div>
<div>
<p class="title"><strong>snaplen</strong></p>
</div>
</div>
</div>
<p>捕获包的最大大小。默认65535。足够应付所有网络和接口类型。如果嗅探物理网络接口,该值设置为MTU大小。对于虚拟接口,还是最好使用默认值。</p><pre class="brush:bash;toolbar:false">interfaces:
device: eth0
snaplen: 1514</pre><p></p>
<div class="titlepage">
<div>
<div>
<p class="title"><strong>type</strong></p>
</div>
</div>
</div>
<p>Packetbeat 支持下面的嗅探器类型:</p>
<div class="itemizedlist">
<ul class="itemizedlist" type="disc">
<li class="listitem">
<code class="literal">pcap</code>, 使用libpcap 库,可工作在大多数平台上,但是不是最快的选项。</li>
<li class="listitem">
<code class="literal">af_packet</code>, 使用 memory-mapped 嗅探。比 libpcap 快且不需要kernel模块,Linux特定的。</li>
<li class="listitem">
<code class="literal">pf_ring</code>, 使用 ntop.org 项目。此设置提供了最好的嗅探速度,但是需要一个kernel模块,Linux特定的。</li>
</ul>
</div>
<p>默认的嗅探器类型是<code class="literal">pcap。</code></p><pre class="brush:bash;toolbar:false">interfaces:
device: eth0
type: af_packet</pre><p>在Linux上,如果想优化Packetbeat耗CPU占用率,建议使用 <code class="literal">af_packet 和</code> <code class="literal">pf_ring</code> 选项。</p>
<p>如果使用 <code class="literal">af_packet</code>, 可以通过下面选项调整行为:</p>
<div class="section">
<div class="titlepage">
<div>
<div>
<p class="title"><strong>buffer_size_mb</strong></p>
</div>
</div>
</div>
<p>内核和用户空间之间使用的最大共享内存缓冲区大小。默认30MB。缓冲区越大,CPU使用率越低,但是会消耗更多内存。只对<code class="literal">af_packet</code> 有效。</p><pre class="brush:bash;toolbar:false">interfaces:
device: eth0
type: af_packet
buffer_size_mb: 100</pre><p>
</p>
</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<p class="title"><strong>with_vlans</strong></p>
</div>
</div>
</div>
<p>Packetbeat 自动生成一个BPF来捕获已知协议的端口流量。 例如,配置HTTP 80 和 MySQL 3306,  Packetbeat 生成 BPF 过滤器如下: <code class="literal">"port 80 or port 3306"。</code></p>
<p>然而,如果通讯包含VLAN标记,Packetbeat生成的过滤器将是无效的,因为offset通过四个字节移动的。为了解决这个问题,启用 <code class="literal">with_vlans</code> 选项,生成的 BPF 过滤器是这样的: <code class="literal">"port 80 or port 3306 or (vlan and (port 80 or port 3306))"。</code></p>
</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<p class="title"><strong>bpf_filter</strong></p>
</div>
</div>
</div>
<p>Packetbeat 自动生成一个BPF来捕获已知协议的端口流量。 例如,配置HTTP 80 和 MySQL 3306,  Packetbeat 生成 BPF 过滤器如下: <code class="literal">"port 80 or port 3306"。</code></p>
<p><code class="literal">可以使用 bpf_filter</code> 覆盖生成的BPF 过滤器,如:</p><pre class="brush:bash;toolbar:false">interfaces:
device: eth0
bpf_filter: "net 192.168.238.0/0 and port 80 and port 3306"</pre><p>
</p>
</div>
<p>此设置是禁用自动生成的BPF过滤器。如果使用此设置,你需要保持BPF过滤器与协议部分定义的端口同步。</p>
<p>Protocols和Processes配置项,下文再说了。</p>
頁: [1]
查看完整版本: ELK Packetbeat 部署指南(15th)