安裝配置prometheus和grafana
Prometheus安裝
wget https://github.com/prometheus/prometheus/releases/download/v1.6.2/prometheus-2.3.2.linux-amd64.tar.gz tar zxvf prometheus-2.3.2.linux-amd64.tar.gz cd prometheus-2.3.2.linux-amd64 ./prometheus --version #查看版本 ./prometheus #啟動
node-exporter安裝
wget https://github.com/prometheus/node_exporter/releases/download/v0.14.0/node_exporter-0.16.0.linux-amd64.tar.gz tar -xvf node_exporter-0.16.0.linux-amd64.tar.gz 后台運行 ./node_exporter &
grafana安裝
wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-5.2.2.linux-amd64.tar.gz tar -zxvf grafana-5.2.2.linux-amd64.tar.gz
//啟動
${GRAFANA_HOME}/bin/grafana-server start
端口3000 默認密碼 admin/admin
配置prometheus監控node
修改prometheus.yml 添加如下信息
- job_name: 'expoter' static_configs: - targets: ['10.10.10.12:9100'] labels: instance: expoter
修改完以后要重啟prometheus
設置prometheus連接grafana
導入儀表盤json模板
從官網獲取dashboards id或者json文件內容,在dasshboard 里import菜單導入即可
獲取id或者json文件內容
https://grafana.com/dashboards/
配置好以后如圖所示
實例之監控docker
Prometheus支持深度監控Docker容器的資源和運行特性,多維度查詢,聚合Docker監控數據
在 Prometheus 中負責數據匯報的程序統一叫做 Exporter, 而不同的 Exporter 負責不同的業務。 它們具有統一命名格式,即 xx_exporter, 例如負責主機信息收集的 node_exporter。Prometheus 社區已經提供了很多 exporter,地址為:https://prometheus.io/docs/instrumenting/exporters/,並且通過瀏覽器可以獲取 目標機器的指標(mertics)信息(http://x.x.x.x:port/mertics)
同樣,監控docker contianer的exporter也可以從這里查找,通常使用cAdvisor,並以容器的方式啟動。
從官方提供的exporter中設置exporter並啟動(注意端口別沖突)
//地址(從官網跳轉得到) https://github.com/google/cadvisor sudo docker run \ --volume=/:/rootfs:ro \ --volume=/var/run:/var/run:ro \ --volume=/sys:/sys:ro \ --volume=/var/lib/docker/:/var/lib/docker:ro \ --volume=/dev/disk/:/dev/disk:ro \ --publish=8080:8080 \ --detach=true \ --name=cadvisor \ google/cadvisor:latest
注:由於最新版的cAdvisor存在bug,啟動的時候會報如下錯誤
1 cadvisor.go:156] Failed to start container manager: inotify_add_watch /sys/fs/cgroup/cpuacct,cpu: no such file or directory
解決辦法
ount -o remount,rw /sys/fs/cgroup/;ln -sf /sys/fs/cgroup/cpu,cpuacct /sys/fs/cgroup/cpuacct,cpu
啟動以后,訪問宿主機的8080端口
獲取容器的指標信息(http://ip:8080/mertics)
配置prometheus監控容器信息
修改prometheus.yml,添加如下信息
- job_name: 'cadvisor-container' static_configs: - targets: ['ip:8080'] #這里就寫上面cAdvisor的地址 labels: instance: cadvisor-container
配置完以后重啟prometheus,打開prometheus web界面
設置grafana顯示圖像信息
首先導入Docker Container dashboard 模板(官網搜索)
導入以后如果無法獲取數據,需要從新設置一下數據源,隨便點開一個監控項
畫框中選擇數據源返回即可
默認會顯示一周的數據,自定義設置時間即可
實例之監控elasticsearch並定義報警規則
prometheus監控es,需要使用對應版本的監控插件(exporter),我使用的是elasticsearch版本5.5.2,github下載對應版本的插件
https://github.com/vvanholl/elasticsearch-prometheus-exporter/releases
安裝監控插件
#安裝
sh ${elasticsearch_HOME}/bin/elasticsearch-plugin install file:///opt/elasticsearch-prometheus-exporter-5.5.2.0.zip
#安裝完成重啟es(非root用戶)后訪問下面地址:
http://es_ip:9200/_prometheus/metrics,看到監控指標如下,說明安裝成功
通過prometheus監控es,配置文件如下
[root@master conf]# cat conf/prometheus.yml |grep -v "^#\|^$" global: scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute. evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute. alerting: alertmanagers: - static_configs: - targets: # - alertmanager:9093 #alertmansger的ip及端口 rule_files: #如果沒有規則時可以不用指定 - "rules/*" #自定義conf目錄下的rules目錄存放報警規則,與prometheus同級目錄 scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['xxxx:9090'] - job_name: elasticsearch scrape_interval: 10s metrics_path: "/_prometheus/metrics" file_sd_configs: - files: - es.yml #這里 表示引用文件 [root@master conf]# cat conf/es.yml |grep -v "^#\|^$" - targets: - 192.168.177.142:9200 - 192.168.177.134:9200 - 192.168.177.206:9200 labels: server: dev-es - targets: - 192.168.177.11:9200 - 192.168.177.208:9200 - 192.168.177.226:9200 - 192.168.177.236:9200 - 192.168.177.237:9200 labels: server: test-es - targets: - 172.21.0.22:9200 - 172.21.0.42:9200 - 172.21.0.19:9200 labels: server: pre-es
啟動時要指定配置文件
#啟動之前先檢查一下配置文件是否正確
./promtool check config conf/prometheus.yml
#啟動 ./prometheus --config.file=/opt/prometheus-2.5.0.linux-amd64/conf/prometheus.yml
查看prometheus
然后去grafana配置,首先去grafana官網去找es的dashboard模板,266,導入模板,完成后監控如下所示:
配置報警規則
根據prometheus監控ElasticSearch指標進行相應的監控
報警規則編寫
打開prometheus,ip:9090, Esecute下拉框里會顯示es監控相關的查詢語句的索引,如下圖所示
選中一個進行查詢,例如,查詢cpu的使用率,查看grafana里的查詢語句
然后在prometheus進行查詢
此時可寫報警規則如下
- alert: EsCpuUsed expr: es_os_cpu_percent > 80 for: 5m labels: severity: warning annotations: description: "elasticsearch in {{$labels.server}}-{{$labels.node}} cpu used is above 80% current {{$value}}%" #這里為自定義報警信息,{{$labels.server}}server 為lables里的鍵
例如查詢JVM Heap堆棧使用率,Element可以理解為查詢語句,value為查詢到的值,percent代表百分比
然后看一下grafan里JVM HEAP監控里如何定義的,
這里只是記錄了jvm Heap使用的內存大小,es_jvm_mem_heap_max_bytes為設定的jvm的大小,進而可以手動查出堆棧使用率,如下所示
所以rules里定義規則就可以寫成
- alert: EsJvmHeapMemUsed expr: es_jvm_mem_heap_used_percent > 75 #大於百分之75就報警 for: 10m labels: severity: warnning annotations: description: "elasticsearch node {{$labels.node}} Jvm Heap mem used is above 75% current {{$value}}%" #描述,自定義,{{$label.node}}表示在element里出的標簽(label)和相應的鍵(node)
在例如下所示,監控cluster狀態有三個顏色,三個值:
根據監控指標可知集群狀態,green( 所有的主分片和副本分片都正常運行)、yellow(所有的主分片都正常運行,但不是所有的副本分片都正常運行)red(有主分片沒能正常運行)
所以規則可寫成如下(值不為0的都視為有問題):
- alert: esclusterwrong expr: es_cluster_status != 0 for: 5m labels: severity: critical annotations: description: "elasticsearch cluster {{$labels.server}} had primary shared not normal runningworking"
整體規則如下
[root@master rules]# cat conf/rules/es.yml groups: - name: es.rules rules: - alert: esclusterwrong expr: es_cluster_status != 0 for: 5m labels: severity: critical annotations: description: "elasticsearch cluster {{$labels.server}} had primary shared not normal runningworking" - alert: esDown expr: up{job="elasticsearch"} == 0 for: 5m labels: severity: critical annotations: description: "elasticsearch service {{$labels.instance}} down" - alert: EsUnassignedTotal expr: es_cluster_shards_number{type="unassigned"} > 0 for: 5m labels: severity: critical annotations: description: "elasticsearch cluster {{$labels.server}} had shares lost" - alert: EsCpuUsed expr: es_os_cpu_percent > 80 for: 5m labels: severity: warning annotations: description: "elasticsearch in {{$labels.server}}-{{$labels.node}} cpu used is above 80% current {{$value}}%" - alert: EsJvmHeapMemUsed expr: es_jvm_mem_heap_used_percent > 75 for: 10m labels: severity: warnning annotations: description: "elasticsearch node {{$labels.node}} Jvm Heap mem used is above 75% current {{$value}}%" - alert: EsDiskUsed expr: ceil((1 - es_fs_path_available_bytes / es_fs_path_total_bytes) * 100 ) > 90 for: 5m labels: severity: warnning annotations: description: "{{$labels.node}} elasticsearch DiskUsage Used is above 90% current {{$value}}%"
此時可以再prometheus界面查看報警和rules
AlertManager報警
-
安裝alertmanager
Alertmanager 主要用於接收 Prometheus 發送的告警信息,它支持豐富的告警通知渠道,而且很容易做到告警信息進行去重,降噪,分組,策略路由,是一款前衛的告警通知系統。
#安裝go 1.11 $ wget https://studygolang.com/dl/golang/go1.11.linux-amd64.tar.gz $ tar zxvf go1.11.linux-amd64.tar.gz && mv go1.11 /opt/go $ vi /etc/profile 添加 export GOROOT=/opt/go export PATH=$GOROOT/bin:$PATH export GOPATH=/opt/go-project export PATH=$PATH:$GOPATH/bin $ source /etc/profile $ go version #安裝alertmanager $ git clone https://github.com/prometheus/alertmanager.git $ cd alertmanager/ $ make build 安裝成功以后,便可編輯報警配置文件了
基本配置模板:
global: resolve_timeout: 2h route: group_by: ['alertname'] group_wait: 5s group_interval: 10s repeat_interval: 1h receiver: 'webhook' receivers: - name: 'webhook' webhook_configs: #通過webhook報警 - url: 'http://example.com/xxxx' send_resolved: true
啟動alertmanager時需要指定配置文件
./alertmanager --config.file=/opt/prometheus-2.5.0.linux-amd64/conf/alertmanager.yml #這個文件是自己自定義的,位置隨便放
-
通過email報警
修改alertmanager配置文件
global: smtp_smarthost: 'smtp.qq.com:587' smtp_from: 'xxx@qq.com' smtp_auth_username: 'xxx@qq.com' smtp_auth_password: 'your_email_password' route: # 重復報警時間,默認10秒 repeat_interval: 10s # 接收者 receiver: team-X-mails receivers: - name: 'team-X-mails' #與上面receiver對應 email_configs: - to: 'team-X+alerts@example.org'
在prometheus指定好報警規則后啟動alertmanger即可
-
企業微信告警
②、訪問app創建應用(自建應用)
③、alertmanager.yml配置
route:
group_by: ['alertname']
receiver: 'wechat'
receivers:
- name: 'wechat'
wechat_configs:
- corp_id: 'xxx' #企業微信賬號唯一 ID, 可以在我的企業
中查看。
to_party: '1' #需要發送的組。
agent_id: '1000002' #第三方企業應用的 ID,可以在自己創建的第三方企業應用詳情頁面查看。
api_secret: 'xxxx' #第三方企業應用的密鑰,可以在自己創建的第三方企業應用詳情頁面查看