當收集的數據出現在圖形之後,我慢慢感覺,整個ES的監控都沒什麼意義。其實,只要能保證ES的正常狀態,大部分參數(特別是圖形上的大多數監控值),對於小企業來說,並沒有什麼現實意義。既然搞出來了,那就索性整理好筆記吧。筆記完成後,我準備把大部分ES的監控全部禁用!
1. 獲取集羣健康狀態的api
ES提供了一個可以獲取集羣健康狀態的api,在瀏覽器訪問:http://10.253.40.87:9200/_cluster/health?pretty
和Elasticsearch裏其他API一樣,“cluster-health”會返回一個JSON響應。
響應的內容解釋:
"cluster_name" : "my-application", #集羣名
"status" : "green", #集羣健康狀態,正常的話是“green”,缺少副本分片爲“yellow”,缺少主分片爲“red”
"timed_out" : false,
"number_of_nodes" : 2, #集羣節點數
"number_of_data_nodes" : 2, #數據節點數
"active_primary_shards" : 138, #主分片數
"active_shards" : 274, #可用的分片數
"relocating_shards" : 0, #正在遷移的分片數
"initializing_shards" : 0, #正在初始化的分片數
"unassigned_shards" : 0, #未分配的分片,但在集羣中存在
"delayed_unassigned_shards" : 0, #延時待分配到具體節點上的分片數
"number_of_pending_tasks" : 0, #待處理的任務數,指主節點創建索引並分配shards等任務
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 100.0 #可用分片數佔總分片的比例
2. 編寫採集腳本獲取集羣狀態
[root@iZejm6lkdZ ~]# cat /etc/zabbix/scripts/monitor_es.sh #!/bin/bash case $1 in cluster_name) curl -s -XGET 'http://10.253.40.87:9200/_cluster/health?pretty' |awk -F\" '/cluster_name/ {print $4}' ;; status) curl -s -XGET 'http://10.253.40.87:9200/_cluster/health?pretty' |awk -F\" 'NR==3 {print $4}' ;; timed_out) curl -s -XGET 'http://10.253.40.87:9200/_cluster/health?pretty' |awk -F\, 'NR==4 {print $1}' |awk -F: '{print $2}' ;; number_nodes) curl -s -XGET 'http://10.253.40.87:9200/_cluster/health?pretty' |awk -F\, 'NR==5 {print $1}' |awk -F: '{print $2}' ;; data_nodes) curl -s -XGET 'http://10.253.40.87:9200/_cluster/health?pretty' |awk -F\, 'NR==6 {print $1}' |awk -F: '{print $2}' ;; active_primary_shards) curl -s -XGET 'http://10.253.40.87:9200/_cluster/health?pretty' |awk -F\, 'NR==7 {print $1}' |awk -F: '{print $2}' ;; active_shards) curl -s -XGET 'http://10.253.40.87:9200/_cluster/health?pretty' |awk -F\, 'NR==8 {print $1}' |awk -F: '{print $2}' ;; relocating_shards) curl -s -XGET 'http://10.253.40.87:9200/_cluster/health?pretty' |awk -F\, 'NR==9 {print $1}' |awk -F: '{print $2}' ;; initializing_shards) curl -s -XGET 'http://10.253.40.87:9200/_cluster/health?pretty' |awk -F\, 'NR==10 {print $1}' |awk -F: '{print $2}' ;; unassigned_shards) curl -s -XGET 'http://10.253.40.87:9200/_cluster/health?pretty' |awk -F\, 'NR==11 {print $1}' |awk -F: '{print $2}' ;; delayed_unassigned_shards) curl -s -XGET 'http://10.253.40.87:9200/_cluster/health?pretty' |awk -F\, 'NR==12 {print $1}' |awk -F: '{print $2}' ;; number_of_pending_tasks) curl -s -XGET 'http://10.253.40.87:9200/_cluster/health?pretty' |awk -F\, 'NR==13 {print $1}' |awk -F: '{print $2}' ;; active_shards_percent_as_number) curl -s -XGET 'http://10.253.40.87:9200/_cluster/health?pretty' |awk -F\, 'NR==16 {print $1}' |awk -F: '{print $2}' ;; *) echo "Usage: $0 { cluster_name | status | timed_out | number_nodes | data_nodes | active_primary_shards | active_shards | relocating_shards | initializing_shards | unassigned_shards|delayed_unassigned_shards|number_of_pending_tasks|active_shards_percent_as_number}" ;; esac
在shell腳本里,“curl -s -XGET 'http://10.253.40.87:9200/_cluster/health?pretty' |awk -F\, 'NR==16 {print $1}' |awk -F: '{print $2}'”這樣的命令,“NR==16”是指在瀏覽器訪問http://10.253.40.87:9200/_cluster/health?pretty,獲取頁面的第16行(從第1行的“{”開始計數)。
給腳本授予執行權限:
chmod +x monitor_es.sh
屬主、屬組可能也需要授權:
chown zabbix:zabbix monitor_es.sh
3. 增加zabbix-agent配置文件
[root@iZejm6lkdZ ~]# cat /etc/zabbix/zabbix_agentd.d/monitor_es.conf UserParameter=es_cluster_name,/etc/zabbix/scripts/monitor_es.sh cluster_name UserParameter=es_status,/etc/zabbix/scripts/monitor_es.sh status #UserParameter=timed_out,/etc/zabbix/scripts/monitor_es.sh timed_out UserParameter=es_number_nodes,/etc/zabbix/scripts/monitor_es.sh number_nodes UserParameter=es_data_nodes,/etc/zabbix/scripts/monitor_es.sh data_nodes UserParameter=es_active_primary_shards,/etc/zabbix/scripts/monitor_es.sh active_primary_shards UserParameter=es_active_shards,/etc/zabbix/scripts/monitor_es.sh active_shards UserParameter=es_relocating_shards,/etc/zabbix/scripts/monitor_es.sh relocating_shards UserParameter=es_initializing_shards,/etc/zabbix/scripts/monitor_es.sh initializing_shards UserParameter=es_unassigned_shards,/etc/zabbix/scripts/monitor_es.sh unassigned_shards UserParameter=es_delayed_unassigned_shards,/etc/zabbix/scripts/monitor_es.sh delayed_unassigned_shards UserParameter=es_number_of_pending_tasks,/etc/zabbix/scripts/monitor_es.sh number_of_pending_tasks UserParameter=es_active_shards_percent_as_number,/etc/zabbix/scripts/monitor_es.sh active_shards_percent_as_number
有行腳本被註釋,只是沒有加入監控,沒有其他意思。目前不確定這個監控項的含義,以後可以再添加。
4. 重啓zabbix-agent服務(zabbix-agent其實有多種重啓方式,好像有3種方法,這應該和安裝方法有關。這裏是我們生產環境唯一生效的方法)
[root@iZejm6lkdZ ~]# pkill -f /etc/zabbix/zabbix_agentd.conf [root@iZejm6lkdZ ~]# ps -ef|grep zabbix_agent root 26152 8156 0 16:06 pts/3 00:00:00 grep --color=auto zabbix_agent [root@iZejm6lkdZ ~]# zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf [root@iZejm6lkdZ ~]# ps -ef|grep zabbix_agent root 26155 1 0 16:06 ? 00:00:00 zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf root 26156 26155 0 16:06 ? 00:00:00 zabbix_agentd: collector [idle 1 sec] root 26157 26155 0 16:06 ? 00:00:00 zabbix_agentd: listener #1 [waiting for connection] root 26158 26155 0 16:06 ? 00:00:00 zabbix_agentd: listener #2 [waiting for connection] root 26159 26155 0 16:06 ? 00:00:00 zabbix_agentd: listener #3 [waiting for connection] root 26160 26155 0 16:06 ? 00:00:00 zabbix_agentd: active checks #1 [idle 1 sec] root 26162 8156 0 16:06 pts/3 00:00:00 grep --color=auto zabbix_agent
5. web頁面配置
item添加:
由於收集的信息只有“green”、“yellow”和“red”,所以,這裏的信息類型爲字符型。
只有“es集羣名稱”和“es集羣狀態”2個監控項的“Type of information”(字段類型)是字符型的,其他監控項必須是數值型的。例如:
由於“es集羣名稱”和“es集羣狀態”的字段類型是字符型的,所以“Trends”列這個監控項是空的。
這一點很重要。
如果所有監控項都是字符型,那麼,在後面添加“圖形”時,監控項是找不到的。
放大圖:
觸發器的創建:
點擊“Add”按紐,需要填寫的信息:
注意:“Function”和“N”是重點。
這個觸發器表達式的意思是,當字符串長度爲3,也就是狀態值爲“red”,觸發報警。
圖形創建:
點擊下面的“Add”按紐,在彈出的對話框裏能看到這次創建的監控項,就是因爲這些監控項的字段類型是數值型。當前,字符型的2個監控項這裏就不存在。
當前zabbix收集數據沒有問題:
參考文檔:
https://blog.51cto.com/766792592/1891112——zabbix監控elasticsearch集羣
https://mp.weixin.qq.com/s?__biz=MzIyMDY2MTE3Mw==&mid=2247484711&idx=1&sn=c011a564d2eec95e64ef01cc6410314e&chksm=97c9d1fda0be58ebd945bcf2805900a4d61f4cf57722bf3a7682f285fdf00c95491462d0808d&mpshare=1&scene=23&srcid=#rd——Zabbix監控es集羣狀態