graylog--splunk的开源替代

目前公司用的日志管理工具splunk是免费试用版本,虽说能够无限试用,可是日志大小500M的限制仍是很讨厌,再精简二、3个月也要清理一次。这显然不能知足大部分的企业需求,有没有相似splunk的开源免费日志管理工具呢?网上找了一圈,还真有!node

graylog介绍linux

先看下官方的定义:https://www.graylog.org/nginx

Trusted full-featured log management.git

Open Source. Built for Security, Operations, and DevOps.github

一站式的日志管理工具,能够说splunk有的功能graylog一个不漏,经过api接口还能够跟其余IT系统整合,至关不错呢!web

安装

graylog最小化安装架构:正则表达式

architec_small_setup.png

官方提供的安装方式多种多样,源码和rpm方法安装的话,须要搭建webserver/MongoDB/Elasticsearch的基础环境,仍是很繁琐的。最方便的仍是虚拟化安装,官方提供的OVA、OpenStack、EC二、Docker四种安装方式,基本上均可以开箱即用。ubuntu

官方镜像是基于ubuntu系统制做,默认系统帐号为ubuntu/ubuntu,dhcp自动获取IP,若是在非dhcp环境启动,则须要手动配置IP地址:vim

Bashwindows

# vim /etc/network/interfaces
auto eth0
iface eth0 inet static
pre-up sleep 2
address 10.10.10.10
netmask 255.255.255.0
gateway 10.10.10.1
dns-nameservers 8.8.8.8

ifdown eth0
ifup eth0

再修改一些初始配置:

Bash

系统时区:	sudo dpkg-reconfigure tzdata
gray时区:	sudo graylog-ctl set-timezone Asia/Shanghai
同步当前时间:    sudo /etc/init.d/ntp stop && ntpdate 10.10.10.1
修改admin密码:    sudo graylog-ctl set-admin-password 123456

使用graylog-ctl修改的配置,须要用  sudo graylog-ctl reconfigure  命令生效,graylog会从新启动,能够看到监听了不少端口:

QQ截图20170116145755.png

日志采集

1)syslog日志采集

在server端开启一个UDP端口,接收syslog转发便可:

在client端,将须要采集的syslog日志转发至server端,例如:

Bash

# echo '*.* @10.10.10.10   # for graylog syslog collect' >> /etc/syslog.conf
# /etc/init.d/syslog restart

2)logfile采集

最新graylog推荐采用“beats”方式采集file log。工做流程以下:

sidecar_overview.png

hosts主机支持linux和windows,须要安装“sidecar”客户端软件,推荐下载最新稳定版本:

https://github.com/Graylog2/collector-sidecar/releases

client端在CentOS系统下安装配置:

Bash

# rpm ivh collector-sidecar-0.0.9-1.x86_64.rpm
# graylog-collector-sidecar -service install
# /etc/init.d/collector-sidecar start|stop|restart

配置文件示例:

Bash

# cat /etc/graylog/collector-sidecar/collector_sidecar.yml
server_url: http://10.10.10.10:9000/api/
update_interval: 60
tls_skip_verify: true
send_status: true
list_log_files:
    - /usr/local/nginx/logs
node_id: graylog-collector-sidecar
collector_id: file:/etc/graylog/collector-sidecar/collector-id
log_path: /var/log/graylog/collector-sidecar
log_rotation_time: 86400
log_max_age: 604800
tags:
    - webserver
    - nginx
backends:
    - name: nxlog
      enabled: false
      binary_path: /usr/bin/nxlog
      configuration_path: /etc/graylog/collector-sidecar/generated/nxlog.conf
    - name: filebeat
      enabled: true
      binary_path: /usr/bin/filebeat
      configuration_path: /etc/graylog/collector-sidecar/generated/filebeat.yml

对于默认配置文件,咱们只须要修改2个地方就能够正常采集了:

server_url: 指定server地址。

tags: client根据tags从server下载配置文件,配置文件包含了须要采集的file-log路径,能够包含多个tags。

list_log_files:  可选,支持多个路径,做用就是在graylog server的system--collectors点击collector能够看到指定路径下文件状态。

须要注意的一点是,logfile须要在server端指定而不是client端,client端根据tags向server端请求logfile的路径。

server端的配置:

菜单:system--inputs--launch new beat input

1484552115348.jpg

菜单:system--collectors--create new configration ,configration分三部分:

1)tag ,与client端配置文件中的tags对应,告诉client使用此配置。

2)beats-outputs , 告诉client将filelog发送到哪里。

3)beats-inputs  ,告诉client须要监控的filelog路径。

QQ截图20170116161102.png

QQ截图20170116154754.png

QQ截图20170116155637.png

input能够建立多个,也能够在一个input中使用正则表达式指定多个filelog。

注意“Type of input file”选项,至关于splunk client端配置的“sourcetype”选项,用于给日志分类,方便搜索及处理。

在graylog webconsole中的配置,都不须要任何重启,client端修改配置文件,须要重启下sidecar。

 

日志分析

 

怎么用好graylog的搜索框,只能说熟能生巧了。搜索结果还能够保存为视图,方便分析。

 

告警配置

 

跟splunk同样,graylog也能对特定日志进行实时监控告警。

一、配置告警发送邮箱SMTP

Bash

$ sudo graylog-ctl set-email-config smtp.mxhichina.com \
[--port 25 --user dmli@muzifei.com --password 123456 \
--from-email dmli@muzifei.com --web-url http://10.10.10.10 \
--no-tls --no-ssl ]
$ sudo graylog-ctl reconfigure

二、配置streams

graylog的告警是基于streams的。简单说就是按必定规则将采集的日志筛选到streams信息流,基于streams的内容再配置告警的规则。

新建一个streams:

QQ截图20170119171648.png

 

添加过滤规则:

QQ截图20170119172301.png

如上图,具体有哪些“Field”,能够回到搜索页面进行查看。

"manage outputs"能够配置streams转发到其余node,咱们这里不做配置。

 

下面建立邮件触发条件:

QQ截图20170120112852.png

如上图,对于filelog类型的日志,选择count condition比较合适,上图大意是1分钟内出现2次以上“hello world”则触发告警,且5分钟内只发送一次告警。

 

告警类型Callbacks咱们选择Email,邮件内容不熟悉格式的话先默认吧:

QQ截图20170119173258.png

 

Receivers 能够添加graylog用户,或者直接添加邮箱地址:

QQ截图20170119173755.png

”Send test alert“试试,能收到邮箱配置就没问题了。

 

启动streams:

QQ截图20170119174228.png

 

为模拟线上环境,咱们手动写几条error log到nginx日志里:

Bash

# echo "test log for hello world email alert" >> /usr/local/nginx/logs/error.log
# echo "test log for hello world email alert" >> /usr/local/nginx/logs/error.log
# echo "test log for hello world email alert" >> /usr/local/nginx/logs/error.log
# echo "test log for hello world email alert" >> /usr/local/nginx/logs/error.log

一会邮件就来了,没有问题:

QQ截图20170120112826.png

 

总结来讲,graylog是款很是棒的日志管理软件,上手简单,上面说的这些简单功能已经能知足小规模的日志管理。若是须要在大规模的环境中部署graylog集群,那就须要仔细地啃文档了。BTW,官方手册也很赞!

相关文章
相关标签/搜索