注:
2017年10月16日:
使用中發現 es 查詢時序數據的性能較差,且 watch 腳本的編寫比較麻煩,因此已將監控系統切換到了 influxdb+grafana平台。新監控系統各方面情況比較滿意。
---------------
在企業監控領域,nagios 和 zabbix 一直是使用率比較高的工具。最近幾年,業界又出現了新的工具和架構,比如:telegraf(數據抓取工具,還有 collectd, logstash,heapster) + influxdb(數據存儲和搜索工具,還有 elasticsearch、opentsd) + kapacitor(數據處理和報警工具,還有 elastAlert,watch) + grafana(數據展示工具)。新的監控工具和架構具有分布式架構、組件獨立、松耦合、易於擴展、插件豐富、適用范圍廣等優點,從容器監控到傳統的服務器、虛擬機監控都適用。
在數據存儲和搜索方面,influxdb 正在快速發展中,目前免費版沒有集群功能;而elasticsearch 是一種健壯、高效、使用廣泛的大數據全文搜索引擎,將監控數據存儲在 es里,能與大數據平台結合,發揮更大作用。
(轉載請注明出處:http://www.cnblogs.com/hahp)
1. 系統結構圖
注:我的測試環境數據不多,因此源數據由 heapster 和 telegraf 抓取后直接存到 elasticsearch。如果數據量很大、elasticsearch 的寫操作出現瓶頸,可以在 elasticsearch前加 kafka 和 logstash。
2. 數據抓取
heapster:用於抓取 kubernetes 容器監控數據,直接存到elasticsearch(也支持存到kafka等其它地方);
telegraf:用於抓取非容器的其它監控數據,它的插件很多,幾乎涵蓋了各種數據源。
3. 數據存儲和搜索
elasticsearch 集群,我采用的是兩台虛擬機;
4. 告警
我采用的是 elastic x-pack中的 watch:
https://www.elastic.co/guide/en/x-pack/current/how-watcher-works.html
目前 watch的action只支持 email、webhook、index、loggin、hipchat、slack、pagerduty、jira。如果想執行一個外部腳本,比如:shell、python、perl腳本,可以將這些腳本集成到一台 restful web service服務器中,watch 便能夠通過 webhook 方式調用。
watch 的搜索、狀態判斷、數據轉換部分都支持一種 plainless script 語言,比 一般的方式更靈活、功能更強:
https://www.elastic.co/guide/en/elasticsearch/painless/master/painless-specification.html
5. 展示
grafana 的效果非常好,支持 elasticsearch。
附:x-pack watch 例子
下面的 watch 用於監控 k8s nodes 的可用磁盤容量,如果任意一台node的可用磁盤容量低於5G,或者2分鍾內獲取不到監控數據,watch就會發送短信和郵件報警。
我是把上述功能寫到兩個watch里,你也可以研究更好的方法精簡這兩個watch。
PUT _xpack/watcher/watch/k8s_node_filesystem_available_evaluate { "trigger" : { "schedule" : { "interval" : "10s" }}, "input" : { "search" : { "request" : { "indices" : [ "<heapster-{now}>", "<heapster-{now-1h}>" ], "body" : { "query" : { "bool" : { "must" : [ { "term": { "_type": "filesystem" }}, { "term": { "MetricsTags.resource_id": "/" }}, { "term": { "MetricsTags.type": "node" }} ], "filter" : [ { "range": { "Metrics.filesystem/available.value": {"lt": 5000000000}} }, { "range": {"FilesystemMetricsTimestamp": {"gte": "now-70s"}} } ] } }, "aggs": { "group_by_host_id": { "terms": { "size": 20, "field": "MetricsTags.host_id" }, "aggs" : { "group_by_available_value" : { "terms": { "script": "params['_source']['Metrics']['filesystem/available']['value']>0?params['_source']['Metrics']['filesystem/available']['value']/(1024*1024*1024):params['_source']['Metrics']['filesystem/available']['value']" } } } } } } } } }, "condition" : { "compare" : { "ctx.payload.hits.total" : { "gt": 0 }} }, "throttle_period" : "60m", "actions" : { "send_sms" : { "webhook" : { "method" : "POST", "host" : "sms.xxx.com", "port" : 80, "path" : "/actions/sendsms", "params" : { "phone": "1580000000", "message": "【XXX】報警:k8s nodes filesytem available:{{#ctx.payload.aggregations.group_by_host_id.buckets}}{{key}} {{group_by_available_value.buckets.0.key}}GB, {{/ctx.payload.aggregations.group_by_host_id.buckets}}" } } }, "send_email" : { "email" : { "to" : "AAAAAAA@xxx.com", "subject" : "【XXX】報警:k8s nodes filesytem available", "body" : "{{#ctx.payload.aggregations.group_by_host_id.buckets}}{{key}} {{group_by_available_value.buckets.0.key}}GB, {{/ctx.payload.aggregations.group_by_host_id.buckets}}" } } } }
PUT _xpack/watcher/watch/k8s_node_filesystem_hava_data { "trigger" : { "schedule" : { "interval" : "10s" }}, "input" : { "search" : { "request" : { "indices" : [ "<heapster-{now}>", "<heapster-{now-1h}>" ], "body" : { "query" : { "bool" : { "must" : [ { "term": { "_type": "filesystem" }}, { "term": { "MetricsTags.resource_id": "/" }}, { "term": { "MetricsTags.type": "node" }}, { "exists": { "field": "Metrics.filesystem/available.value" }} ], "filter" : [ { "range": {"FilesystemMetricsTimestamp": {"gte": "now-130s"}} } ] } }, "aggs": { "group_by_host_id": { "terms": { "size": 20, "field": "MetricsTags.host_id" } } } } } } }, "condition" : { "script" : "if(ctx.payload.aggregations.group_by_host_id.buckets.length<12){ return true; } else{ return false;}" }, "transform" : { "script" : "List host_all = ['172.31.17.31','172.31.17.32','172.31.17.33','172.31.17.34','172.31.17.35','172.31.17.36','172.31.17.37','172.31.17.38','172.31.17.39','172.31.17.71','172.31.17.72','172.31.17.73']; List host_ids = []; for (int i = 0; i < ctx.payload.aggregations.group_by_host_id.buckets.length; ++i ){ host_ids.add(ctx.payload.aggregations.group_by_host_id.buckets[i].key); } List host_no_data = []; for(item in host_all){ if(!host_ids.contains(item)){ host_no_data.add(['key':item]); } } return ['host_no_data':host_no_data];" }, "throttle_period" : "60m", "actions" : { "send_sms" : { "webhook" : { "method" : "POST", "host" : "sms.xxx.com", "port" : 80, "path" : "/actions/sendsms", "params" : { "phone": "1580000000", "message": "【XXX】報警:k8s nodes filesytem no data:{{#ctx.payload.host_no_data}}{{key}},{{/ctx.payload.host_no_data}}" } } }, "send_email" : { "email" : { "to" : "AAAAAAA@xxx.com", "subject" : "【XXX】報警:k8s nodes filesytem no data", "body" : "{{#ctx.payload.host_no_data}}{{key}},{{/ctx.payload.host_no_data}}" } } } }