Ubuntu 16.04下部署Graylog日志服务器

运维 系统运维 Linux
Graylog 是一个开源的日志管理系统,集中式收集、索引、分析其它服务器发来的日志。它是由 Java 语言编写的,能够接收 TCP、UDP、AMQP 协议发送的日志信息,并且使用 Mongodb 做为后台数据库。它还有一个使用 Ruby 编写的 Web 管理接口,可以轻松管理 Graylog 和查询日志。

Graylog 是一个开源的日志管理系统,集中式收集、索引、分析其它服务器发来的日志。它是由 Java 语言编写的,能够接收 TCP、UDP、AMQP 协议发送的日志信息,并且使用 Mongodb 做为后台数据库。它还有一个使用 Ruby 编写的 Web 管理接口,可以轻松管理 Graylog 和查询日志。

Graylog 可以收集监控多种不同应用的日志。本文只是会为了示范说明,会把用到的组件全部安装到一个单独的服务器上。对于大型、生产系统你可以把组件分开安装在不同的服务器上,这样可以提高效率。

Graylog 2 的组件

Graylog 2 有 4 个基本组件:

  • Graylog Server:这个服务负责接收和处理日志/消息,并且和其他组件沟通。
  • Elasticsearch:存储所有的日志,它的性能依赖内存和硬盘IO。
  • MongoDB:存储数据。
  • Web接口:用户接口。

下面是 Graylog 2 各组件之间的关系图

 

安装和配置 Graylog 2

环境依赖

Graylog 2 需要以下环境依赖:

  • 一台 Ubuntu 16.04 服务器,至少有 2 GB 的 RAM。
  • Elasticsearch (>= 2.x,推荐使用***稳定版本。)
  • MongoDB (>= 2.4,推荐使用***稳定版本。)
  • Oracle Java SE 或 OpenJDK (>= 8,推荐使用***稳定版本。)

如果你的 Ubuntu Server 是最小化安装的,还需要提前安装以下软件包:

  1. $ sudo apt-get install apt-transport-https uuid-runtime pwgen 

安装 Java JDK

Elasticsearch 是基于 Java 的应用,我们首先需要安装 OpenJDK 或 Oracle JDK。这里我们选择安装 OpenJDK 8:

  1. $ sudo apt-get update && sudo apt-get install openjdk-8-jdk 

如果你的系统上存在多个 Java 版本,可使用以下指令设置默认使用版本。

  1. update-alternatives --config java 

安装 Elasticsearch

Elasticsearch 是 Graylog 一个主要的组件,负责分析和索引日志。Graylog 2.3.x 开始支持 Elasticsearch 5.x, 这里我们就安装 Elasticsearch 5.x 版本。

  1. # 添加 GPG 签名密钥 
  2. $ wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add - 
  3. # 添加 Eleasticsearch 源 
  4. $ echo "deb https://artifacts.elastic.co/packages/5.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-5.x.list 
  5. # 安装 Elasticsearch 
  6. $ sudo apt-get update && sudo apt-get install elasticsearch  

编辑 Elasticsearch 配置文件:

  1. $ sudo vim /etc/elasticsearch/elasticsearch.yml 
  2. # 把 cluster.name 设置为 graylog。 
  3. cluster.name: graylog  

修改配置后,你需要重启 Elasticsearch:

  1. $ sudo systemctl daemon-reload 
  2.  
  3. $ sudo systemctl restart elasticsearch.service  

如果要把 Elasticsearch 服务加入随系统启动,可以执行以下命令:

  1. $ sudo systemctl enable elasticsearch.service 

测试 Elastisearch 工作是否正常

Elastisearch 默认使用 9200 端口接收 http 请求,这里使用 curl 指令进行一个简单的请求测试。

  1. $ curl -X GET http://localhost:9200 
  2. "name" : "V8jWSvJ"
  3. "cluster_name" : "graylog"
  4. "cluster_uuid" : "8cnTgvEzRZ2U81LTYq5nEw"
  5. "version" : { 
  6. "number" : "5.6.3"
  7. "build_hash" : "1a2f265"
  8. "build_date" : "2017-10-06T20:33:39.012Z"
  9. "build_snapshot" : false
  10. "lucene_version" : "6.6.1" 
  11. }, 
  12. "tagline" : "You Know, for Search" 
  13.  

查看 Elasticsearch 的健康状态

  1. $ curl -XGET 'http://localhost:9200/_cluster/health?pretty=true' 
  2. "cluster_name" : "graylog"
  3. "status" : "green"
  4. "timed_out" : false
  5. "number_of_nodes" : 1, 
  6. "number_of_data_nodes" : 1, 
  7. "active_primary_shards" : 0, 
  8. "active_shards" : 0, 
  9. "relocating_shards" : 0, 
  10. "initializing_shards" : 0, 
  11. "unassigned_shards" : 0, 
  12. "delayed_unassigned_shards" : 0, 
  13. "number_of_pending_tasks" : 0, 
  14. "number_of_in_flight_fetch" : 0, 
  15. "task_max_waiting_in_queue_millis" : 0, 
  16. "active_shards_percent_as_number" : 100.0 
  17.  

安装 MongoDB

Ubuntu 16.04 默认安装源中包含的 MongoDB (目前版本是 2.6.10) 是支持 Graylog 2.3.x 或更高版本的。

  1. $ sudo apt-get install mongodb-server 

启动 MongoDB 服务

  1. $ sudo systemctl start mongodb 

如果要把 MongoDB 服务加入随系统启动,可以执行以下命令:

  1. $ sudo systemctl enable mongodb 

安装 Graylog Server

Graylog Server 负责接收和处理日志。

  1. # 下载安装 Graylog Server 仓库 
  2. $ wget https://packages.graylog2.org/repo/packages/graylog-2.3-repository_latest.deb 
  3. $ sudo dpkg -i graylog-2.3-repository_latest.deb 
  4.  
  5. # 安装 Graylog Server 
  6. $ sudo apt-get update && sudo apt-get install graylog-server  

编辑 Graylog Server 配置文件:

设置 password_secret,首先使用 pwgen 命令生成密码: 

  1. $ pwgen -N 1 -s 96 
  2. 1jfPjMRn5XRsCdVWArjy1nulgXbUJJ8khuW0xQGrqUvJ1iXefhqSh12xsp1dZgkKVsOwiOuDLArh6TYafQE8QFDjEzUIU1tS  

修改 password_secret 参数值:

  1. $ sudo vim /etc/graylog/server/server.conf 
  2. password_secret = 1jfPjMRn5XRsCdVWArjy1nulgXbUJJ8khuW0xQGrqUvJ1iXefhqSh12xsp1dZgkKVsOwiOuDLArh6TYafQE8QFDjEzUIU1tS  

设置 Graylog Server 管理员密码 root_password_sha2,这个密码用来登录 Web 管理页面。

假如你要把密码设置为 000000 ,可以使用 sha256sum 命令来生成:

  1. $ echo -n 000000 | sha256sum 
  2. 91b4d142823f7d20c5f08df69122de43f35f057a988d9619f6d3138485c9a203  

修改 root_password_sha2 参数值:

  1. $ sudo vim /etc/graylog/server/server.conf 
  2. root_password_sha2 = 91b4d142823f7d20c5f08df69122de43f35f057a988d9619f6d3138485c9a203  

注:password_secret 和 root_password_sha2 参数是必须设定的,否则 Graylog Server 将无法启动。

如果你要设置管理员邮箱和时区,可以使用以下参数值:

  1. $ sudo vim /etc/graylog/server/server.conf 
  2. root_email = "admin@hi-linux.com" 
  3. root_timezone = UTC  

设置 Elasticsearch 节点:

  1. $ sudo vim /etc/graylog/server/server.conf 
  2. Default: http://127.0.0.1:9200 
  3. elasticsearch_hosts = http://192.168.100.212:9200  

如果需要配置多个 Elasticsearch 节点或是需要认证的节点,可按以下格式配置:

  1. elasticsearch_hosts = http://node1:9200,http://user:password@node2:19200 

注:如不配置,默认是连接到本机的 Elasticsearch 节点。

其它一些和 Elasticsearch 的相关设置:

  1. elasticsearch_index_prefix = graylog 
  2. elasticsearch_connect_timeout = 10s 
  3. elasticsearch_max_docs_per_index = 20000000 
  4. elasticsearch_max_total_connections = 20 
  5. elasticsearch_max_number_of_indices = 20 
  6. elasticsearch_shards = 1 
  7. elasticsearch_replicas = 0  

安装 Graylog Web 接口

从 Graylog 2.x 版本开始,Graylog 已经默认集成了 Web 接口。

配置 Graylog Web 接口

  1. $ sudo vim /etc/graylog/server/server.conf 
  2. # 配置rest Api 的 URI 
  3. rest_listen_uri = http://your_ip_or_domain:9001/ 
  4. # 配置 Web 界面的 URI 
  5. web_listen_uri = http://your_ip_or_domain:9000/  

注: your_ip_or_domain 为你实际服务器 IP 或域名。

启动 Graylog Server 服务:

  1. $ sudo systemctl daemon-reload 
  2.  
  3. $ sudo systemctl start graylog-server.service  

如果要把 Graylog Server 服务加入随系统启动,可以执行以下命令:

  1. $ sudo systemctl enable graylog-server.service 

访问 Graylog Web

使用浏览器访问 http://your_ip_or_doamin:9000,能成功看到如下界面则表示安装成功。默认用户名为:admin,密码为:root_password_sha2 参数中设置的。

 

添加一个需收集日志的服务器

创建 Syslog UDP 输入

Ubuntu 系统自带 Rsyslog 服务无需安装,只需要配置一下即可。

打开 Graylog Web 页面,选择 System->Inputs->Syslog UDP->Launch new input,添加一个要接收 Syslog 日志的服务器。

在弹出的窗口上输入如下信息:

  • Node: 在列表中选择你的 Graylog Server 服务器
  • Title: Linux Server Logs
  • Port: 8514
  • Bind address: 0.0.0.0
  • 点击 Save

配置完成后就生成了一个监听在 8514 端口上的 Syslog 服务端,下面可以用它来收集其它服务器上的日志。 

 

本次测试采用同一台服务器做演示,所以绑定到所有网卡接口。如只在特定网络中访问,请按实际情况填写 Bind address 的 IP 地址。

现在,我们的 Graylog Server 服务器已经做好了接收其它服务器发来日志的准备。下面我们还需要配置需收集日志服务器,让这些服务器给 Graylog Server 服务器发送日志。

配置服务器发送日志到 Graylog

创建 rsyslog 配置文件 /etc/rsyslog.d/90-graylog.conf。

如果你的 rsyslog 版本 > 5.10,请按以下格式配置:

  1. *.* @graylog_server_ip:8514;RSYSLOG_SyslogProtocol23Format 

如果你的 rsyslog 版本 < 5.10,请按以下格式配置:

  1. $template GRAYLOGRFC5424,"<%PRI%>%PROTOCOL-VERSION% %TIMESTAMP:::date-rfc3339% %HOSTNAME% %APP-NAME% %PROCID% %MSGID% %STRUCTURED-DATA% %msg%\n" 
  2. *.* @graylog_server_ip:8514;GRAYLOGRFC5424  

注:把 graylog_server_ip 替换为 Graylog 服务器 IP 地址。

我这里使用的是 rsyslog 8.16 版本,修改后类似下面:

  1. $ sudo vim /etc/rsyslog.d/90-graylog.conf 
  2.  
  3. *.* @192.168.100.212:8514;RSYSLOG_SyslogProtocol23Format  

重启 rsyslog 服务使生效

  1. $ sudo systemctl restart rsyslog 

配置完成之后,回到 Graylog Web,点击 Sources,查看是否有新添加 Rsyslog 来源的图形。

 

搜素 Graylog

在 Graylog Web 上,点击 Search 可以访问 Graylog 搜索页面。 在这里可以根据条件查询指定的日志。

 

假如你要搜索 ssh 的活动日志,输入关键字 sshd,点搜索图标:

 

一些常用的搜索语法

  • 搜索包含关键字 ssh 的信息
  1. ssh 
  • 搜索包含关键字 ssh 或 login 的信息
  1. ssh login 
  • 搜索包含完整关键字 ssh login 的信息
  1. "ssh login" 
  • 搜索字段类型包含 ssh 的信息
  1. type:ssh 
  • 搜索字段类型包含 ssh 或 login 的信息
  1. type:(ssh login) 
  • 搜索字段类型包含完整关键字 ssh login 的信息
  1. type:"ssh login" 

更详细搜索语法可参考官方文档: http://docs.graylog.org/en/2.3/pages/queries.html

到此为止,就完成了一个基本的可以从其它服务器收集日志的 Graylog 服务器部署。当然 Graylog 还提供了其它一系列的丰富功能,比如仪表板、警报和流等功能,期待我们下次进一步的探索吧。

参考文档

  • http://www.google.com
  • http://blog.topspeedsnail.com/archives/6670
  • http://docs.graylog.org/en/2.3/pages/installation/os/ubuntu.html
  • https://marketplace.graylog.org/addons/a47beb3b-0bd9-4792-a56a-33b27b567856
  • https://www.digitalocean.com/community/tutorials/how-to-manage-logs-with-graylog-2-on-ubuntu-16-04 
责任编辑:庞桂玉 来源: 运维之美
相关推荐

2017-08-03 10:36:08

UbuntuCertbotNGINX

2017-10-17 11:10:45

UbuntuSQL Server

2013-10-22 10:59:37

Ubuntu 13.1Linux Linux服务器部署

2017-05-08 16:52:40

UbuntuApacheJenkins

2009-12-08 12:23:15

UbuntuFTP服务器

2014-04-03 16:36:46

Ubuntu ServCacti服务器监控

2021-09-16 07:52:18

FTP服务器Linux

2014-12-16 10:17:01

RHEL6.5samba

2011-02-25 15:38:58

2016-09-30 13:48:25

UbuntuPython报警系统

2021-06-10 09:48:48

Ubuntu 16.0FreeRADIUSDaloradius

2010-05-25 09:45:06

SVN服务器环境配置

2012-05-15 17:35:57

操作系统Ubuntu

2010-01-04 13:26:51

Ubuntu安装

2016-12-29 10:52:09

LinuxUbuntu邮件服务器

2017-01-09 08:59:17

Ubuntu邮件服务器

2011-08-29 14:20:00

Ubuntusamba

2013-07-16 14:44:43

Ubuntutftp服务器

2011-11-21 14:18:51

日志

2011-08-29 15:23:11

Ubuntuapache
点赞
收藏

51CTO技术栈公众号