筆者看來, 監控不該該只是監控,除了及時有效的報警,更應該」好看」,由於視覺上的感覺更能給咱們直觀的感覺,更能從絢麗的走勢中發現異常, 若是你以爲監控就應該像老牌監控nagios,cacti同樣,我想也沒什麼不對的,由於也許那是大家最適合的,但,你仍是能夠瞧瞧這個監控能給你帶來什麼。node
爲了你能有更多的動力看下去,這裏放一部分經過Prometheus + grafana打造出來的監控平臺,效果圖以下。
python
若是你以爲不錯能夠繼續看下去,上面主要是kvm宿主機, ceph集羣, 物理機監控,以及ping, 最後一張的監控圖沒有展開是爲了讓你能夠瞥一眼所能監控的指標條目。mysql
Prometheus架構圖linux
參考:https://prometheus.io/docs/introduction/overview/
ios
若是你對Prometheus沒有接觸過,也許會看不懂上面說什麼,可是不要緊,若是你看完以後,在回過頭來瞧瞧,也許就瞭解這個架構了,也會對Prometheus有一個更深的認識。
這裏簡單說一下Prometheus的各個部分。
Prometheus Server: Prometheus服務端,因爲存儲及收集數據,提供相關api對外查詢用。
Exporter: 相似傳統意義上的被監控端的agent,有區別的是,它不會主動推送監控數據到server端,而是等待server端定時來手機數據,即所謂的主動監控。
Pushagateway: 用於網絡不可直達而居於exporter與server端的中轉站。
Alertmanager: 報警組件,將報警的功能單獨剝離出來放在alertmanager。
Web UI: Prometheus的web接口,可用於簡單可視化,及語句執行或者服務狀態監控。git
安裝
github
因爲Prometheus是go語言寫的,因此不須要編譯,安裝的過程很是簡單,僅須要解壓而後運行。
Prometheus官方下載地址:https://prometheus.io/download/golang
注:爲了演示方便,這裏node_exporter, Prometheus server, grafana都安裝再同一臺機器,系統環境Ubuntu14.04
web
安裝Prometheus serversql
解壓
tar xf prometheus-2.0.0-rc.2.linux-amd64.tar.gz
運行
cd prometheus-2.0.0-rc.2.linux-amd64 ./prometheus --config.file=prometheus.yml
而後咱們能夠訪問 http://<服務器IP地址>:9090,驗證Prometheus是否已安裝成功,web顯示應該以下
經過點擊下拉欄選取指標,點擊」Excute」 咱們可以看到Prometheus的性能指標。
點擊」status」能夠查看相關狀態。
可是光安裝Prometheus server意義不大,下面咱們再安裝node_exporter以及grafana
node_exporter安裝
解壓
tar xf node_exporter-0.15.0.linux-amd64.tar.gz
運行
cd node_exporter-0.15.0.linux-amd64 ./node_exporter
驗證node_exporter是否安裝成功
curl 127.0.0.1:9100
curl 127.0.0.1:9100/metrics
返回一大堆性能指標。
grafana安裝
下載deb安裝
wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana_4.5.2_amd64.deb dpkg -i grafana_4.5.2_amd64.deb
安裝依賴
sudo apt-get install -y adduser libfontconfig
啓動grafana
sudo service grafana-server start
加入自啓動
sudo update-rc.d grafana-server defaults
注:其餘系統安裝參考:http://docs.grafana.org/installation/
啓動grafana並查看狀態
systemctl daemon-reload systemctl start grafana-serversystemctl status grafana-server
訪問grafana, http://<服務器IP>:3000
默認用戶名密碼:admin/admin
爲grafana添加Prometheus數據源
至此全部安裝已完成
可是還存在如下問題
一:Prometheus server並無配置被監控端的IP地址,即沒有取指定的機器取數據
二:啓動的方式太不人性化了,沒有啓動腳本。
三:grafana沒有可用的dashboard用於展現
這些問題咱們放在下面的配置,可視化段落處理。
配置
關閉以前之間運行的node_exporter及prometheus
增長一個被監控端配置項
建立目錄/etc/prometheus/
mkdir /etc/prometheus/
建立配置文件
vi /etc/prometheus/prometheus.yml
修改以下(在有配置文件基礎上增長紅色區域)
# my global configglobal: 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. # scrape_timeout is set to the global default (10s). # Attach these labels to any time series or alerts when communicating with # external systems (federation, remote storage, Alertmanager). external_labels: monitor: 'codelab-monitor'# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.rule_files: # - "first.rules" # - "second.rules"# A scrape configuration containing exactly one endpoint to scrape:# Here it's Prometheus itself.scrape_configs: # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config. - job_name: 'prometheus' # metrics_path defaults to '/metrics' # scheme defaults to 'http'. static_configs: - targets: ['localhost:9090']
注意:縮進是必須的
添加啓動腳本
下載地址:https://github.com/youerning/blog/tree/master/prometheus
cp node-exporter.service /etc/init.d/node-exporter cp prometheus.service /etc/init.d/prometheuschmod +x /etc/init.d/node-exporterchmod +x /etc/init.d/prometheus
將上面的可執行二進制文件移到/usr/local/bin
cp prometheus-2.0.0-rc.2.linux-amd64/prometheus /usr/local/bin/prometheus mv node_exporter-0.15.0.linux-amd64/node_exporter /usr/local/bin/node_exporter
而後啓動Prometheus,node-exporter
建立工做目錄(Prometheus的數據會存在這,啓動腳本里面我寫的是/data)
mkdir /data service prometheus startservice node-exporter start
在Prometheus的web頁面能看到被監控端
而後grafana導入dashboard
下載地址:https://grafana.com/dashboards/1860
注:https://grafana.com/dashboards還有不少的dashboard能夠下載
按照如下步驟導入
點擊import之後grafana就會多一個dashboard
至此一個系統層面性能指標監控已經所有完成。
可視化自定義
因爲grafana的界面配置都是頁面點擊,須要截圖標註,若是截太多圖就文章太冗長了,這裏就不進一步說明了,相關配置參考
http://docs.grafana.org/features/panels/
經過上面的安裝配置發現,其實整個監控的流程還缺乏了報警的環節,若是不能及時通報異常狀況再好看也白搭。
報警
解壓
tar xf alertmanager-0.11.0.linux-amd64.tar.gz
規則配置
cat /etc/prometheus/alert.rules
groups:
name: uptime
rules:
- alert: InstanceDown
expr: up == 0
for: 1m
labels:
severity: page
annotations:
summary: "Instance {{ $labels.instance }} down"
description: "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 5 minutes."
prometheus.yml增長如下內容
rule_files: - "/etc/prometheus/alert.rules" alerting: alertmanagers: - scheme: http static_configs: - targets: - "localhost:9093"
詳細配置參考: https://github.com/youerning/blog/blob/master/prometheus/prometheus-alertmanager.yml
啓動alertmanager
./alertmanager --config.file=/etc/prometheus/alertmanager.yml
查看監控狀態
不過Prometheus的報警操做真的很扯淡。
支持的接受操做以下
<email_config>
<hipchat_config>
<pagerduty_config>
<pushover_config>
<slack_config>
<opsgenie_config>
<victorops_config>
<webhook_config>
而email報警有個扯淡的地方就是若是郵件服務器必須tls認證且ssl是自簽名的話就會starttls failed: x509: certificate signed by unknown authority
並且沒有一個in_secure:true的選項。
因此須要郵件報警的話有兩種方法,
一:再報警服務器裏面植入本身的證書,
參考:http://blog.amigapallo.org/2016/04/14/alertmanager-docker-container-self-signed-smtp-server-certificate/
二:容許smtp不使用tls
其實上面兩種方法都不太優雅,觀法推薦的是使用web_hook
可是又得保證web_hook的服務是運行的,這就很扯淡了,不過,若是是所有跑在docker管理平臺,如k8s,卻是不錯的。
下面是一個簡單的實現。
from __future__ import print_function import falcon from wsgiref import simple_server from email.mime.text import MIMEText import smtplib import json smtpServer = "mx.example.com" smtpUser = "sender@example.com" smtpPass = "password" sender = "sender@example.com" reciver = "reciver@example.com" tpl = """ status: {status} alerts: {alerts} """ def sendMail(reciver, subject, message): server = smtplib.SMTP(smtpServer, 587) server.ehlo() server.starttls() server.ehlo() server.login(smtpUser, smtpPass) server.set_debuglevel(1) msg = MIMEText(message, "plain", "utf8") msg["Subject"] = subject server.sendmail(sender, [reciver], msg.as_string()) server.quit() class WebHook(object): def on_post(self, req, resp): """Handles GET requests""" body = req.stream.read() postData = json.loads(body.decode('utf-8')) msg = tpl.format(**postData) print(msg) sendMail(reciver, "alert", msg) resp.status = falcon.HTTP_200 # This is the default status resp.body = "OK" app = falcon.API() app.add_route('/', WebHook()) if __name__ == '__main__': httpd = simple_server.make_server('0.0.0.0', 80, app) httpd.serve_forever()
源碼:https://github.com/youerning/blog/blob/master/prometheus/webhookmail.py
注意:有falcon的依賴,須要pip install falcon
效果以下
注:因爲我沒有進一步處理post過來的json數據,因此顯得不是很直觀,你們能夠根據本身的須要編排數據
其餘exporter
除了基本的node_exporter,Prometheus官方還提供其餘的exporter,如mysql, memcache,haproxy等
除了官方提供的,也還有不少第三方的expoter,參考:https://prometheus.io/docs/instrumenting/exporters/
自定義exporter
本文太長了, 直接看官方example吧。
參考:https://github.com/prometheus/client_golang/blob/master/examples/random/main.go
後記:我的認爲,其實你不必定知道你要監控什麼的,可是足夠多的監控數據,可以支撐你對異常的全面審查及追溯。其實Prometheus的不少細節沒有說,好比監控規則編寫,Prometheus的查詢語法,不過本文太長了,若是有機會在詳細說明吧。