在上一篇文章部署好了ELK環境,接下來進行nginx日誌的收集
環境
192.168.2.112 jdk/elasticsearch/kibana CentOS release 6.5
192.168.2.118 jdk/logstash/nginx CentOS release 6.3 (Final)
- nginx日誌收集
1)進入logstash的etc目錄下,創建文件nginxlog.conf並添加如下內容
input {
file {
path => "/usr/local/nginx/logs/access.log"
type => "nginx"
}
}
output {
elasticsearch {
hosts => "192.168.2.112:9200"
}
}
path指向日誌存儲路徑
2)然後啓動logstash
[root@log-nginx etc]# ../bin/logstash -f nginxlog.conf
# nohup /usr/local/logstash/bin/logstash -f nginxlog.conf &>/dev/null & 後臺啓動
可以看到如下圖顯示內容:
或者
ps -ef | grep java
3)此時可以先手動訪問nginx頁面,訪問日誌數據在增加,則logstash會把數據推送給ES,然後直接在kibana上查看如下圖:
- 加入redis消息隊列提高速度收集nginx日誌
1)118服上部署好redis服務並開啓
[root@log-nginx elktest]# ps -ef | grep redis
root 4841 1 0 22:12 pts/1 00:00:00 /usr/local/redis/bin/redis-server *:6379
root 4850 1499 0 22:13 pts/1 00:00:00 grep redis
[root@log-nginx elktest]# netstat -tunl | grep 6379
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN
tcp 0 0 :::6379 :::* LISTEN
2)在logstash的etc目錄下建立文件agentnginx.conf並添加如下內容
input {
file {
type => "nginx-access"
path => "/usr/local/nginx/logs/access.log"
}
}
output {
redis {
host => "localhost"
port => 6379
data_type => "list"
key => "logstash"
}
}
把nginx日誌數據推送到redis
3)在logstash的etc目錄下建立文件indexnginx.conf並添加如下內容
input {
redis {
host => "localhost"
port => "6379"
data_type => "list"
key => "logstash"
type => "redis-input"
batch_count => 1
}
}
output {
elasticsearch {
hosts => "192.168.2.112"
}
}
4)啓動logstash
nohup /usr/local/logstash/bin/logstash -f agentnginx.conf &>/dev/null &
nohup /usr/local/logstash/bin/logstash -f indexnginx.conf &>/dev/null &
ps -ef | grep java
5)此時查看web界面如下
至此nginx日誌的簡單收集功能完成。
若需要收集其他服務或系統的日誌,操作方法與上述類似
如果需要批量收集多臺服務器的日誌數據,可以通過shell腳本或藉助ansible/saltstack等工具把配置好的logstash目錄同步到其他的服務器
例如上述的nginx日誌收集,配置好了logstash裏的agent.conf和index.conf後,直接把logstash目錄同步到其他服務器對應的路徑,然後修改conf文件裏redis的ip即可(若本機有也可以不用修改)
備註:
1)可以點擊右上角的時間按鈕,然後點擊auto-refresh,可以設置自動刷新時間
2)在圖形左邊一列,可以選擇要顯示的內容
3)若顯示如下圖所示內容,則可能是本身沒有數據過來,或者是服務器之間的時間不同步。