Prometheus安裝node
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安裝linux
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安裝git
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監控nodegithub
修改prometheus.yml 添加以下信息golang
- job_name: 'expoter' static_configs: - targets: ['10.10.10.12:9100'] labels: instance: expoter
修改完之後要重啓prometheusweb
設置prometheus鏈接grafanadocker
導入儀表盤json模板json
從官網獲取dashboards id或者json文件內容,在dasshboard 裏import菜單導入便可api
獲取id或者json文件內容瀏覽器
https://grafana.com/dashboards/
配置好之後如圖所示
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 模板(官網搜索)
導入之後若是沒法獲取數據,須要重新設置一下數據源,隨便點開一個監控項
畫框中選擇數據源返回便可
默認會顯示一週的數據,自定義設置時間便可
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 主要用於接收 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 #這個文件是本身自定義的,位置隨便放
修改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' #第三方企業應用的密鑰,能夠在本身建立的第三方企業應用詳情頁面查看個人企業