目前公司用的日誌管理工具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最小化安裝架構:正則表達式
官方提供的安裝方式多種多樣,源碼和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會從新啓動,能夠看到監聽了不少端口:
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。工做流程以下:
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
菜單:system--collectors--create new configration ,configration分三部分:
1)tag ,與client端配置文件中的tags對應,告訴client使用此配置。
2)beats-outputs , 告訴client將filelog發送到哪裏。
3)beats-inputs ,告訴client須要監控的filelog路徑。
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:
添加過濾規則:
如上圖,具體有哪些「Field」,能夠回到搜索頁面進行查看。
"manage outputs"能夠配置streams轉發到其餘node,咱們這裏不做配置。
下面建立郵件觸發條件:
如上圖,對於filelog類型的日誌,選擇count condition比較合適,上圖大意是1分鐘內出現2次以上「hello world」則觸發告警,且5分鐘內只發送一次告警。
告警類型Callbacks咱們選擇Email,郵件內容不熟悉格式的話先默認吧:
Receivers 能夠添加graylog用戶,或者直接添加郵箱地址:
」Send test alert「試試,能收到郵箱配置就沒問題了。
啓動streams:
爲模擬線上環境,咱們手動寫幾條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
一會郵件就來了,沒有問題:
總結來講,graylog是款很是棒的日誌管理軟件,上手簡單,上面說的這些簡單功能已經能知足小規模的日誌管理。若是須要在大規模的環境中部署graylog集羣,那就須要仔細地啃文檔了。BTW,官方手冊也很贊!