ELK之索引應用

創建kibana索引

若只需要收集顯示nginx的訪問日誌,則可以建立一個名爲nginx+時間的索引
若是需要收集一個服務器下的多個服務日誌,則可以在一個conf下添加多個input並根據type來區分和實現

環境
192.168.2.112 ES/kibana
192.168.2.118 logstash/nginx
192.168.2.117 logstash/mysql/nginx

  • 建立nginx索引

1)在118服的logstash/etc目錄下建立的nginxlog.conf,添加

input {
    file {
        path => "/usr/local/nginx/logs/access.log"
        type => "nginx"
    }
}
output {
    elasticsearch {
        hosts => "192.168.2.112:9200"
        index => "nginx-%{+YYYY.MM.dd}"
        }
}

其中,index即代表對應的索引名稱

2)然後啓動logstash

[root@localhost etc]# pwd
/usr/local/logstash/etc
[root@localhost etc]# ../bin/logstash  -f nginxlog.conf

3)登陸kibana設置索引

這裏寫圖片描述

這裏寫圖片描述

這裏寫圖片描述

這裏寫圖片描述

4)然後手動訪問nginx頁面後,可以在kibana的discover界面看到

這裏寫圖片描述

  • 收集nginx日誌和mysql日誌

1)把118服的logstash目錄複製到117服對應目錄下

scp -r logstash/*  root@192.168.2.117:/usr/local/logstash

2)在117服logstash/etc目錄下建立all.conf

input {
    file {
        path => "/usr/local/nginx/logs/access.log"
        type => "nginx"
    }
}

input {
    file {
        path => "/var/log/mysqld.log"
        type => "mysql"
    }
}

output {
  if [type] == "nginx"{
    elasticsearch {
        hosts => "192.168.2.112:9200"
        index => "nginx-%{+YYYY.MM.dd}"
        }
}
if [type] == "mysql"{
    elasticsearch {
        hosts => "192.168.2.112:9200"
        index => "mysql-%{+YYYY.MM.dd}"
        }
}
}

3)在kibana頁面建立mysql索引

這裏寫圖片描述

4)啓動logstash

[root@host107 etc]# pwd
/usr/local/logstash/etc
[root@host107 etc]# ../bin/logstash -f all.conf

5)然後啓動及關閉mysql服務,就可以看到日誌數據

這裏寫圖片描述

6)同樣的,訪問nginx頁面後,也會收到nginx日誌數據

這裏寫圖片描述

備註:
1)其中上面的host列顯示0.0.0.0,是因爲沒有設置主機名,在/etc/hosts下加上
127.0.0.1 hostmysqlnginx
然後hostname hostmysqlnginx
重啓下logstash,就可以看到host

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章