天天看點

Ubuntu 16.04下部署Graylog日志伺服器

Graylog 是一個開源的日志管理系統,集中式收集、索引、分析其它伺服器發來的日志。它是由 Java 語言編寫的,能夠接收 TCP、UDP、AMQP 協定發送的日志資訊,并且使用 Mongodb 做為背景資料庫。它還有一個使用 Ruby 編寫的 Web 管理接口,可以輕松管理 Graylog 和查詢日志。

Graylog 可以收集監控多種不同應用的日志。本文隻是會為了示範說明,會把用到的元件全部安裝到一個單獨的伺服器上。對于大型、生産系統你可以把元件分開安裝在不同的伺服器上,這樣可以提高效率。

Graylog 2 的元件

Graylog 2 有 4 個基本元件:

Graylog Server:這個服務負責接收和處理日志/消息,并且和其他元件溝通。

Elasticsearch:存儲所有的日志,它的性能依賴記憶體和硬碟IO。

MongoDB:存儲資料。

Web接口:使用者接口。

下面是 Graylog 2 各元件之間的關系圖

Ubuntu 16.04下部署Graylog日志伺服器

安裝和配置 Graylog 2

環境依賴

Graylog 2 需要以下環境依賴:

一台 Ubuntu 16.04 伺服器,至少有 2 GB 的 RAM。

Elasticsearch (>= 2.x,推薦使用最新穩定版本。)

MongoDB (>= 2.4,推薦使用最新穩定版本。)

Oracle Java SE 或 OpenJDK (>= 8,推薦使用最新穩定版本。)

如果你的 Ubuntu Server 是最小化安裝的,還需要提前安裝以下軟體包:

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

安裝 Java JDK

Elasticsearch 是基于 Java 的應用,我們首先需要安裝 OpenJDK 或 Oracle JDK。這裡我們選擇安裝 OpenJDK 8:

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

如果你的系統上存在多個 Java 版本,可使用以下指令設定預設使用版本。

$ update-alternatives --config java 

安裝 Elasticsearch

Elasticsearch 是 Graylog 一個主要的元件,負責分析和索引日志。Graylog 2.3.x 開始支援 Elasticsearch 5.x, 這裡我們就安裝 Elasticsearch 5.x 版本。

# 添加 GPG 簽名密鑰 

$ wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add - 

# 添加 Eleasticsearch 源 

$ echo "deb https://artifacts.elastic.co/packages/5.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-5.x.list 

# 安裝 Elasticsearch 

$ sudo apt-get update && sudo apt-get install elasticsearch  

編輯 Elasticsearch 配置檔案:

$ sudo vim /etc/elasticsearch/elasticsearch.yml 

# 把 cluster.name 設定為 graylog。 

cluster.name: graylog  

修改配置後,你需要重新開機 Elasticsearch:

$ sudo systemctl daemon-reload 

$ sudo systemctl restart elasticsearch.service  

如果要把 Elasticsearch 服務加入随系統啟動,可以執行以下指令:

$ sudo systemctl enable elasticsearch.service 

測試 Elastisearch 工作是否正常

Elastisearch 預設使用 9200 端口接收 http 請求,這裡使用 curl 指令進行一個簡單的請求測試。

$ curl -X GET http://localhost:9200 

"name" : "V8jWSvJ", 

"cluster_name" : "graylog", 

"cluster_uuid" : "8cnTgvEzRZ2U81LTYq5nEw", 

"version" : { 

"number" : "5.6.3", 

"build_hash" : "1a2f265", 

"build_date" : "2017-10-06T20:33:39.012Z", 

"build_snapshot" : false, 

"lucene_version" : "6.6.1" 

}, 

"tagline" : "You Know, for Search" 

}  

檢視 Elasticsearch 的健康狀态

$ curl -XGET 'http://localhost:9200/_cluster/health?pretty=true' 

"status" : "green", 

"timed_out" : false, 

"number_of_nodes" : 1, 

"number_of_data_nodes" : 1, 

"active_primary_shards" : 0, 

"active_shards" : 0, 

"relocating_shards" : 0, 

"initializing_shards" : 0, 

"unassigned_shards" : 0, 

"delayed_unassigned_shards" : 0, 

"number_of_pending_tasks" : 0, 

"number_of_in_flight_fetch" : 0, 

"task_max_waiting_in_queue_millis" : 0, 

"active_shards_percent_as_number" : 100.0 

安裝 MongoDB

Ubuntu 16.04 預設安裝源中包含的 MongoDB (目前版本是 2.6.10) 是支援 Graylog 2.3.x 或更高版本的。

$ sudo apt-get install mongodb-server 

啟動 MongoDB 服務

$ sudo systemctl start mongodb 

如果要把 MongoDB 服務加入随系統啟動,可以執行以下指令:

$ sudo systemctl enable mongodb 

安裝 Graylog Server

Graylog Server 負責接收和處理日志。

# 下載下傳安裝 Graylog Server 倉庫 

$ wget https://packages.graylog2.org/repo/packages/graylog-2.3-repository_latest.deb 

$ sudo dpkg -i graylog-2.3-repository_latest.deb 

# 安裝 Graylog Server 

$ sudo apt-get update && sudo apt-get install graylog-server  

編輯 Graylog Server 配置檔案:

設定 password_secret,首先使用 pwgen 指令生成密碼:

$ pwgen -N 1 -s 96 

1jfPjMRn5XRsCdVWArjy1nulgXbUJJ8khuW0xQGrqUvJ1iXefhqSh12xsp1dZgkKVsOwiOuDLArh6TYafQE8QFDjEzUIU1tS  

修改 password_secret 參數值:

$ sudo vim /etc/graylog/server/server.conf 

password_secret = 1jfPjMRn5XRsCdVWArjy1nulgXbUJJ8khuW0xQGrqUvJ1iXefhqSh12xsp1dZgkKVsOwiOuDLArh6TYafQE8QFDjEzUIU1tS  

設定 Graylog Server 管理者密碼 root_password_sha2,這個密碼用來登入 Web 管理頁面。

假如你要把密碼設定為 000000 ,可以使用 sha256sum 指令來生成:

$ echo -n 000000 | sha256sum 

91b4d142823f7d20c5f08df69122de43f35f057a988d9619f6d3138485c9a203  

修改 root_password_sha2 參數值:

root_password_sha2 = 91b4d142823f7d20c5f08df69122de43f35f057a988d9619f6d3138485c9a203  

注:password_secret 和 root_password_sha2 參數是必須設定的,否則 Graylog Server 将無法啟動。

如果你要設定管理者郵箱和時區,可以使用以下參數值:

root_email = "[email protected]

root_timezone = UTC  

設定 Elasticsearch 節點:

# Default: http://127.0.0.1:9200 

elasticsearch_hosts = http://192.168.100.212:9200  

如果需要配置多個 Elasticsearch 節點或是需要認證的節點,可按以下格式配置:

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

注:如不配置,預設是連接配接到本機的 Elasticsearch 節點。

其它一些和 Elasticsearch 的相關設定:

elasticsearch_index_prefix = graylog 

elasticsearch_connect_timeout = 10s 

elasticsearch_max_docs_per_index = 20000000 

elasticsearch_max_total_connections = 20 

elasticsearch_max_number_of_indices = 20 

elasticsearch_shards = 1 

elasticsearch_replicas = 0  

安裝 Graylog Web 接口

從 Graylog 2.x 版本開始,Graylog 已經預設內建了 Web 接口。

配置 Graylog Web 接口

# 配置rest Api 的 URI 

rest_listen_uri = http://your_ip_or_domain:9001/ 

# 配置 Web 界面的 URI 

web_listen_uri = http://your_ip_or_domain:9000/  

注: your_ip_or_domain 為你實際伺服器 IP 或域名。

啟動 Graylog Server 服務:

$ sudo systemctl start graylog-server.service  

如果要把 Graylog Server 服務加入随系統啟動,可以執行以下指令:

$ sudo systemctl enable graylog-server.service 

通路 Graylog Web

使用浏覽器通路 http://your_ip_or_doamin:9000,能成功看到如下界面則表示安裝成功。預設使用者名為:admin,密碼為:root_password_sha2 參數中設定的。

Ubuntu 16.04下部署Graylog日志伺服器

添加一個需收集日志的伺服器

建立 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 服務端,下面可以用它來收集其它伺服器上的日志。

Ubuntu 16.04下部署Graylog日志伺服器

本次測試采用同一台伺服器做示範,是以綁定到所有網卡接口。如隻在特定網絡中通路,請按實際情況填寫 Bind address 的 IP 位址。

現在,我們的 Graylog Server 伺服器已經做好了接收其它伺服器發來日志的準備。下面我們還需要配置需收集日志伺服器,讓這些伺服器給 Graylog Server 伺服器發送日志。

配置伺服器發送日志到 Graylog

建立 rsyslog 配置檔案 /etc/rsyslog.d/90-graylog.conf。

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

*.* @graylog_server_ip:8514;RSYSLOG_SyslogProtocol23Format 

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

$template GRAYLOGRFC5424,"<%PRI%>%PROTOCOL-VERSION% %TIMESTAMP:::date-rfc3339% %HOSTNAME% %APP-NAME% %PROCID% %MSGID% %STRUCTURED-DATA% %msg%\n" 

*.* @graylog_server_ip:8514;GRAYLOGRFC5424  

注:把 graylog_server_ip 替換為 Graylog 伺服器 IP 位址。

我這裡使用的是 rsyslog 8.16 版本,修改後類似下面:

$ sudo vim /etc/rsyslog.d/90-graylog.conf 

*.* @192.168.100.212:8514;RSYSLOG_SyslogProtocol23Format  

重新開機 rsyslog 服務使生效

$ sudo systemctl restart rsyslog 

配置完成之後,回到 Graylog Web,點選 Sources,檢視是否有新添加 Rsyslog 來源的圖形。

Ubuntu 16.04下部署Graylog日志伺服器

搜素 Graylog

在 Graylog Web 上,點選 Search 可以通路 Graylog 搜尋頁面。 在這裡可以根據條件查詢指定的日志。

Ubuntu 16.04下部署Graylog日志伺服器

假如你要搜尋 ssh 的活動日志,輸入關鍵字 sshd,點搜尋圖示:

Ubuntu 16.04下部署Graylog日志伺服器

一些常用的搜尋文法

搜尋包含關鍵字 ssh 的資訊

ssh 

搜尋包含關鍵字 ssh 或 login 的資訊

ssh login 

搜尋包含完整關鍵字 ssh login 的資訊

"ssh login" 

搜尋字段類型包含 ssh 的資訊

type:ssh 

搜尋字段類型包含 ssh 或 login 的資訊

type:(ssh login) 

搜尋字段類型包含完整關鍵字 ssh login 的資訊

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-10-27 

本文作者:Mike

本文來自雲栖社群合作夥伴“51CTO”,了解相關資訊可以關注。