JobPlus知识库 互联网 网络安全 文章
ELK+Beats实现Windows服务器系统日志监控

ELK在运维监控领域使用非常广泛,日志采集通常依靠Logstash,但是通常来讲Logstash架构比较重载,一个安装包由几百MB,相比之下Elastic还提供另一种更轻量的采集工具Beats。Beats 平台集合了多种单一用途数据采集器。这些采集器安装后可用作轻量型代理,从成百上千或成千上万台机器向 Logstash 或 Elasticsearch 发送数据。本文简要介绍一下使用Winlogbeat收集Windows日志,并用ES + Kibana检索的配置方法。

步骤一:安装Winlogbeat

  1. 下载Winlogbeat安装包downloads page.
  2. 解压到C:\Program Files.
  3. 重名名解压文件winlogbeat-<version>目录为Winlogbeat
  4. 打开PowerShell,以管理员身份运行(右键点击Powershell并选择以管理员身份运行)。如果你使用XP,可能需要自行下载安装PowerShell
  5. 在PowerShell中运行如下命令安装服务

在运行Winlogbeat前,还需要检查一下配置文件中的选项,比如C:\Program Files\Winlogbeat\winlogbeat.yml,安装包里提供一个完整的配置文件样例winlogbeat.full.yml,配置Winlogbeat

编辑winlogbeat.yml来修改配置,熟悉yaml语法的同学一定不会陌生,不熟悉的请自行熟悉。
举例如下:

winlogbeat.event_logs:  - name: Application  - name: Security  - name: System output.elasticsearch:  hosts:    - localhost:9200 logging.to_files: truelogging.files:  path: C:/ProgramData/winlogbeat/Logs logging.level: info步骤二:配置Winlogbeat

  1. 在event_logs章节,指定你要监控的event log类型,Winlogbeat默认监控application, security, and system这三种

winlogbeat.event_logs:  - name: Application  - name: Security  - name: System

要查看全部类型,在PowerShell中运行Get-EventLog *,更多信息请参考event_logs.name.

  1. 如果需要把日志发送到Elasticsearch,这里把IP端口写进去:

output.elasticsearch:  hosts:    - localhost:9200

如果要把日志写到Logstash,请继续往下看

  1. 修改完配置文件以后运行下面的命令测试一下:

PS C:\Program Files\Winlogbeat> .\winlogbeat.exe -c .\winlogbeat.yml -configtest -e步骤三:配置Winlogbeat使用Logstash

如果你想把日志直接写到Elasticsearch那么这一步可以跳过。

要将日志通过Logstash写到ES,需要先配置Logstash的转发规则,举例如下:

input{    beats{        port=>5044type=>"wineventlog"    } } output{    stdout{            }if[        type    ]=="wineventlog"{        elasticsearch{            hosts=>"100.100.16.231"index=>"winlogbeat-%{+yyyy.MM.dd}"        }    }else{        elasticsearch{            hosts=>"100.100.16.231"        }    } }

配置好以后确保Logstash已经启动且运行正常。接下来要配置Winlogbeat将日志发送到Logstash的5044端口

#----------------------------- Logstash output --------------------------------output.logstash:  hosts: ["127.0.0.1:5044"]

这里还需要额外做一个步骤,让Elasticsearch加载索引模板,因为这里我们是用Logstash往ES上转日志,所以必须手动加载索引模板,如果直接往ES发日志,这一步可以省略

步骤四:加载索引模板

在Elasticsearch里面,索引模板(Index templates)用来定义字段的设置和映射关系,推荐的Winlogbeat索引模板配置文件在Winlogbeat的安装包里winlogbeat.yml,下面我们通过ES的API接口把模板推送过去,其实就是构造一个HTTP的PUT请求把数据发出去,工具和方法很多这里介绍两种,

使用PowerShell

PS C:\Program Files\Winlogbeat> Invoke-WebRequest -Method Put -InFile winlogbeat.template.json -Uri  http://localhost:9200/_template/winlogbeat?pretty -ContentType application/json

使用Postman

第五步:启动Winlogbeat

用PowerShell启动:

PS C:\Program Files\Winlogbeat> Start-Service winlogbeat

执行以后Winlogbeat就开始执行了,如果使用上面的配置文件,日志会输出到C:\ProgramData\winlogbeat\Logs\winlogbeat。
我们可以通过Services management控制这个任务的状态,启动控制台

PS C:\Program Files\Winlogbeat> services.msc

停止任务:

PS C:\Program Files\Winlogbeat> Stop-Service winlogbeat步骤六:加载Kibana仪表盘样例

Winlogbeat安装包里有个scripts/import_dashboards程序,操作步骤非常简单,打开PowerShell命名行,执行该程序:

PS > scripts\import_dashboards.exe

脚本默认假设你的Elasticsearch在127.0.0.1:9200,如果你的环境不在本地,可以执行下面的命令指定服务器位置:

PS > scripts\import_dashboards.exe -es http://192.168.33.60:9200

如果ES开启了账号密码认证:

PS > scripts\import_dashboards.exe -es https://xyz.found.io -user user -pass password

查看仪表板

导入完成以后,用浏览器访问Kibana页面,如:http://127.0.0.1:5601.

在Discover 页面,可以看到之前定义好的索引winlogbeat-*

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

¥ 打赏支持
260人赞 举报
分享到
用户评价(0)

暂无评价,你也可以发布评价哦:)

扫码APP

扫描使用APP

扫码使用

扫描使用小程序