編輯:關於 Android 開發
上一篇完成了各元件的安裝,這一篇把每一個環節的設定檔逐一打開,看清楚資料是怎麼從 Nginx 日誌一路流到 Kibana 圖表上的。建議先讀完安裝篇再回到這裡。
原始文章:
https://blog.chinaunix.net/uid-25057421-id-5567766.html
與其事後用正則去切割傳統的 access log,不如讓 Nginx 一開始就吐 JSON。這樣 Logstash 端只需簡單的 codec 設定就能直接使用,省下大量規則維護成本:
log_format logstash_json '{ "@timestamp": "$time_iso8601", '
'"host": "$server_addr", '
'"clientip": "$remote_addr", '
'"size": $body_bytes_sent, '
'"responsetime": $request_time, '
'"upstreamtime": "$upstream_response_time", '
'"upstreamhost": "$upstream_addr", '
'"http_host": "$host", '
'"url": "$uri", '
'"referrer": "$http_referer", '
'"xff": "$http_x_forwarded_for", '
'"agent": "$http_user_agent", '
'"status": "$status"} ';
access_log /data/wwwlogs/access_jerrymin.test.com.log logstash_json;
Filebeat 只負責「把檔案往外送」,設定刻意保持極簡:
filebeat:
prospectors:
-
- /data/wwwlogs/access_jerrymin.test.com.log
documenttype: jerrymin.test.com
output:
logstash:
enabled: true
hosts: ["192.168.0.58:5044"]
shipper:
Topbeat 負責主機與行程層級的指標,預設每 10 秒採集一次,這裡直接輸出到 Elasticsearch:
input:
period: 10
procs: [".*"]
stats:
system: true
proc: true
filesystem: true
output:
elasticsearch:
hosts: ["192.168.0.58:9200"]
shipper:
logging:
files:
rotateeverybytes: 10485760 # = 10MB
input {
beats {
port => 5044
codec => json
}
}
filter {
mutate {
split => [ "upstreamtime", "," ]
}
mutate {
convert => [ "upstreamtime", "float" ]
}
}
output {
elasticsearch {
hosts => "192.168.0.58:9200"
sniffing => true
manage_template => false
index => "filebeat-%{type}-%{+YYYY.MM.dd}"
document_type => "%{[@metadata][type]}"
}
}
這裡有兩個值得注意的細節。第一,upstream_response_time 在有多層後端時會是逗號分隔的多個值,先用 split 切成陣列再 convert 成 float,後續才能做數值聚合。第二,關掉 manage_template 並自訂 index 名稱,可以避免 Beats 預設版型覆蓋你手工調校過的 mapping。
path.data: /data path.logs: /data/elklogs network.host: 192.168.0.58 http.port: 9200
server.port: 5601 server.host: "0.0.0.0" elasticsearch.url: "https://192.168.0.58:9200"
Kibana 本身沒有完整的存取控制,實務上通常掛在反向代理後面加上 Basic Auth,同時順便把 WebSocket 相關的標頭處理好:
server {
listen 8888;
server_name 192.168.0.58;
location / {
proxy_pass https://localhost:5601;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
auth_basic "Please input Username and Password";
auth_basic_user_file /usr/local/nginx/conf/.pass_file;
}
access_log /data/wwwlogs/access.kibana.log access;
}
開啟 https://192.168.0.58:8888 後,還有三步收尾。
建立索引:
建立檢視:
建立面板:
把流程串起來看其實很清楚:Nginx 產生結構化日誌 → Filebeat 讀檔並轉發 → Logstash 做正規化與欄位型別轉換 → Elasticsearch 建立索引 → Kibana 呈現。Beats 系列刻意做得很輕,把運算成本集中在 Logstash 與 Elasticsearch 端,這是它能大量部署在業務機器的關鍵設計。
PullToRefresh 使用疑難:XScrollView 內容布局處理
PullToRefresh 使用疑難:XScrollView 內容布局處理 使用 PullToRefresh 中的 XScrollView 時,若直接在 XML 中放入內
手機安全衛士實作:緩存清理功能解析
本文說明如何在 Android 中實作類似手機安全衛士的緩存清理功能,重點包含掃描應用程式快取、列出佔用空間,以及一鍵清除。核心思路實作時可先透過 PackageMana
自定義 View 案例:使用 ViewPager 實現廣告輪播
ViewPager 輪播的基本思路 ViewPager 常用於實現左右滑動頁面,例如引導頁、Tab 切換和廣告輪播。通過組合 PagerAdapter、定時器和頁面監聽,
Android 移除標題欄的三種做法
Android 移除標題欄的三種做法 開發全屏或自訂頂欄的應用時,第一步通常就是把系統預設的標題欄拿掉。以下整理三種做法,適用範圍各有不同。 做法一:在程式碼中實作 //